fix(ngcc): report errors from analyze
and resolve
processing (#33964)
Previously, these errors were being swallowed, which made it hard to debug problems with packages. See https://github.com/angular/angular/issues/33685#issuecomment-557091719 PR Close #33964
This commit is contained in:
parent
e53375b7b6
commit
715d02aa14
@ -212,9 +212,12 @@ export class DecorationAnalyzer {
|
|||||||
analyzedFile.analyzedClasses.forEach(({declaration, matches}) => {
|
analyzedFile.analyzedClasses.forEach(({declaration, matches}) => {
|
||||||
matches.forEach(({handler, analysis}) => {
|
matches.forEach(({handler, analysis}) => {
|
||||||
if ((handler.resolve !== undefined) && analysis) {
|
if ((handler.resolve !== undefined) && analysis) {
|
||||||
const res = handler.resolve(declaration, analysis);
|
const {reexports, diagnostics} = handler.resolve(declaration, analysis);
|
||||||
if (res.reexports !== undefined) {
|
if (reexports !== undefined) {
|
||||||
this.addReexports(res.reexports, declaration);
|
this.addReexports(reexports, declaration);
|
||||||
|
}
|
||||||
|
if (diagnostics !== undefined) {
|
||||||
|
diagnostics.forEach(error => this.diagnosticHandler(error));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -40,7 +40,7 @@ runInEachFileSystem(() => {
|
|||||||
let result: DecorationAnalyses;
|
let result: DecorationAnalyses;
|
||||||
|
|
||||||
// Helpers
|
// Helpers
|
||||||
const createTestHandler = () => {
|
const createTestHandler = (options: {analyzeError: boolean, resolveError: boolean}) => {
|
||||||
const handler = jasmine.createSpyObj<DecoratorHandlerWithResolve>('TestDecoratorHandler', [
|
const handler = jasmine.createSpyObj<DecoratorHandlerWithResolve>('TestDecoratorHandler', [
|
||||||
'detect',
|
'detect',
|
||||||
'analyze',
|
'analyze',
|
||||||
@ -74,13 +74,20 @@ runInEachFileSystem(() => {
|
|||||||
// The "test" analysis is an object with the name of the decorator being analyzed
|
// The "test" analysis is an object with the name of the decorator being analyzed
|
||||||
handler.analyze.and.callFake((decl: ts.Declaration, dec: Decorator) => {
|
handler.analyze.and.callFake((decl: ts.Declaration, dec: Decorator) => {
|
||||||
logs.push(`analyze: ${(decl as any).name.text}@${dec.name}`);
|
logs.push(`analyze: ${(decl as any).name.text}@${dec.name}`);
|
||||||
return {analysis: {decoratorName: dec.name}, diagnostics: undefined};
|
return {
|
||||||
|
analysis: {decoratorName: dec.name},
|
||||||
|
diagnostics: options.analyzeError ? [makeDiagnostic(9999, decl, 'analyze diagnostic')] :
|
||||||
|
undefined
|
||||||
|
};
|
||||||
});
|
});
|
||||||
// The "test" resolution is just setting `resolved: true` on the analysis
|
// The "test" resolution is just setting `resolved: true` on the analysis
|
||||||
handler.resolve.and.callFake((decl: ts.Declaration, analysis: any) => {
|
handler.resolve.and.callFake((decl: ts.Declaration, analysis: any) => {
|
||||||
logs.push(`resolve: ${(decl as any).name.text}@${analysis.decoratorName}`);
|
logs.push(`resolve: ${(decl as any).name.text}@${analysis.decoratorName}`);
|
||||||
analysis.resolved = true;
|
analysis.resolved = true;
|
||||||
return {};
|
return {
|
||||||
|
diagnostics: options.resolveError ? [makeDiagnostic(9998, decl, 'resolve diagnostic')] :
|
||||||
|
undefined
|
||||||
|
};
|
||||||
});
|
});
|
||||||
// The "test" compilation result is just the name of the decorator being compiled
|
// The "test" compilation result is just the name of the decorator being compiled
|
||||||
// (suffixed with `(compiled)`)
|
// (suffixed with `(compiled)`)
|
||||||
@ -92,7 +99,10 @@ runInEachFileSystem(() => {
|
|||||||
return handler;
|
return handler;
|
||||||
};
|
};
|
||||||
|
|
||||||
function setUpAnalyzer(testFiles: TestFile[]) {
|
function setUpAnalyzer(
|
||||||
|
testFiles: TestFile[],
|
||||||
|
options: {analyzeError: boolean,
|
||||||
|
resolveError: boolean} = {analyzeError: false, resolveError: false}) {
|
||||||
logs = [];
|
logs = [];
|
||||||
loadTestFiles(testFiles);
|
loadTestFiles(testFiles);
|
||||||
loadFakeCore(getFileSystem());
|
loadFakeCore(getFileSystem());
|
||||||
@ -107,7 +117,7 @@ runInEachFileSystem(() => {
|
|||||||
const analyzer = new DecorationAnalyzer(
|
const analyzer = new DecorationAnalyzer(
|
||||||
getFileSystem(), bundle, reflectionHost, referencesRegistry,
|
getFileSystem(), bundle, reflectionHost, referencesRegistry,
|
||||||
(error) => diagnosticLogs.push(error));
|
(error) => diagnosticLogs.push(error));
|
||||||
testHandler = createTestHandler();
|
testHandler = createTestHandler(options);
|
||||||
analyzer.handlers = [testHandler];
|
analyzer.handlers = [testHandler];
|
||||||
migrationLogs = [];
|
migrationLogs = [];
|
||||||
const migration1 = new MockMigration('migration1', migrationLogs);
|
const migration1 = new MockMigration('migration1', migrationLogs);
|
||||||
@ -362,6 +372,26 @@ runInEachFileSystem(() => {
|
|||||||
expect(diagnosticLogs[0]).toEqual(jasmine.objectContaining({code: -999999}));
|
expect(diagnosticLogs[0]).toEqual(jasmine.objectContaining({code: -999999}));
|
||||||
expect(diagnosticLogs[1]).toEqual(jasmine.objectContaining({code: -996666}));
|
expect(diagnosticLogs[1]).toEqual(jasmine.objectContaining({code: -996666}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should report analyze and resolve diagnostics to the `diagnosticHandler` callback',
|
||||||
|
() => {
|
||||||
|
const analyzer = setUpAnalyzer(
|
||||||
|
[
|
||||||
|
{
|
||||||
|
name: _('/node_modules/test-package/index.js'),
|
||||||
|
contents: `
|
||||||
|
import {Component, Directive, Injectable} from '@angular/core';
|
||||||
|
export class MyComponent {}
|
||||||
|
MyComponent.decorators = [{type: Component}];
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
{analyzeError: true, resolveError: true});
|
||||||
|
analyzer.analyzeProgram();
|
||||||
|
expect(diagnosticLogs.length).toEqual(2);
|
||||||
|
expect(diagnosticLogs[0]).toEqual(jasmine.objectContaining({code: -999999}));
|
||||||
|
expect(diagnosticLogs[1]).toEqual(jasmine.objectContaining({code: -999998}));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user