2019-07-18 16:05:32 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
2019-11-23 12:54:07 -05:00
|
|
|
import * as ts from 'typescript';
|
|
|
|
|
|
|
|
import {ErrorCode, makeDiagnostic} from '../../../src/ngtsc/diagnostics';
|
2019-10-20 14:40:48 -04:00
|
|
|
import {AbsoluteFsPath, absoluteFrom} from '../../../src/ngtsc/file_system';
|
|
|
|
import {runInEachFileSystem} from '../../../src/ngtsc/file_system/testing';
|
2019-09-01 14:54:41 -04:00
|
|
|
import {ClassDeclaration, Decorator} from '../../../src/ngtsc/reflection';
|
2019-07-18 16:05:32 -04:00
|
|
|
import {AnalysisOutput, CompileResult, DecoratorHandler, DetectResult, HandlerPrecedence} from '../../../src/ngtsc/transform';
|
|
|
|
import {DefaultMigrationHost} from '../../src/analysis/migration_host';
|
|
|
|
import {AnalyzedClass, AnalyzedFile} from '../../src/analysis/types';
|
2019-09-01 14:54:41 -04:00
|
|
|
import {NgccClassSymbol} from '../../src/host/ngcc_host';
|
2019-11-23 12:54:07 -05:00
|
|
|
import {createComponentDecorator} from '../../src/migrations/utils';
|
2019-07-18 16:05:32 -04:00
|
|
|
|
2019-10-20 14:40:48 -04:00
|
|
|
runInEachFileSystem(() => {
|
|
|
|
describe('DefaultMigrationHost', () => {
|
|
|
|
let _: typeof absoluteFrom;
|
|
|
|
let entryPointPath: AbsoluteFsPath;
|
|
|
|
let mockHost: any;
|
|
|
|
let mockMetadata: any = {};
|
|
|
|
let mockEvaluator: any = {};
|
|
|
|
let mockClazz: any;
|
|
|
|
let mockDecorator: any = {name: 'MockDecorator'};
|
2019-11-23 12:54:07 -05:00
|
|
|
let diagnosticHandler = () => {};
|
2019-10-20 14:40:48 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
_ = absoluteFrom;
|
|
|
|
entryPointPath = _('/node_modules/some-package/entry-point');
|
|
|
|
mockHost = {
|
|
|
|
getClassSymbol: (node: any): NgccClassSymbol | undefined => {
|
|
|
|
const symbol = { valueDeclaration: node, name: node.name.text } as any;
|
|
|
|
return {
|
|
|
|
name: node.name.text,
|
|
|
|
declaration: symbol,
|
|
|
|
implementation: symbol,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const mockSourceFile: any = {
|
|
|
|
fileName: _('/node_modules/some-package/entry-point/test-file.js'),
|
|
|
|
};
|
|
|
|
mockClazz = {
|
|
|
|
name: {text: 'MockClazz'},
|
|
|
|
getSourceFile: () => mockSourceFile,
|
|
|
|
};
|
2019-07-18 16:05:32 -04:00
|
|
|
});
|
|
|
|
|
2019-10-20 14:40:48 -04:00
|
|
|
describe('injectSyntheticDecorator()', () => {
|
|
|
|
it('should call `detect()` on each of the provided handlers', () => {
|
|
|
|
const log: string[] = [];
|
|
|
|
const handler1 = new TestHandler('handler1', log);
|
|
|
|
const handler2 = new TestHandler('handler2', log);
|
|
|
|
const host = new DefaultMigrationHost(
|
2019-11-23 12:54:07 -05:00
|
|
|
mockHost, mockMetadata, mockEvaluator, [handler1, handler2], entryPointPath, [],
|
|
|
|
diagnosticHandler);
|
2019-10-20 14:40:48 -04:00
|
|
|
host.injectSyntheticDecorator(mockClazz, mockDecorator);
|
|
|
|
expect(log).toEqual([
|
|
|
|
`handler1:detect:MockClazz:MockDecorator`,
|
|
|
|
`handler2:detect:MockClazz:MockDecorator`,
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call `analyze()` on each of the provided handlers whose `detect()` call returns a result',
|
|
|
|
() => {
|
|
|
|
const log: string[] = [];
|
|
|
|
const handler1 = new TestHandler('handler1', log);
|
|
|
|
const handler2 = new AlwaysDetectHandler('handler2', log);
|
|
|
|
const handler3 = new TestHandler('handler3', log);
|
|
|
|
const host = new DefaultMigrationHost(
|
|
|
|
mockHost, mockMetadata, mockEvaluator, [handler1, handler2, handler3],
|
2019-11-23 12:54:07 -05:00
|
|
|
entryPointPath, [], diagnosticHandler);
|
2019-10-20 14:40:48 -04:00
|
|
|
host.injectSyntheticDecorator(mockClazz, mockDecorator);
|
|
|
|
expect(log).toEqual([
|
|
|
|
`handler1:detect:MockClazz:MockDecorator`,
|
|
|
|
`handler2:detect:MockClazz:MockDecorator`,
|
|
|
|
`handler3:detect:MockClazz:MockDecorator`,
|
|
|
|
'handler2:analyze:MockClazz',
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should add a newly `AnalyzedFile` to the `analyzedFiles` object', () => {
|
|
|
|
const log: string[] = [];
|
|
|
|
const handler = new AlwaysDetectHandler('handler', log);
|
|
|
|
const analyzedFiles: AnalyzedFile[] = [];
|
|
|
|
const host = new DefaultMigrationHost(
|
2019-11-23 12:54:07 -05:00
|
|
|
mockHost, mockMetadata, mockEvaluator, [handler], entryPointPath, analyzedFiles,
|
|
|
|
diagnosticHandler);
|
2019-10-20 14:40:48 -04:00
|
|
|
host.injectSyntheticDecorator(mockClazz, mockDecorator);
|
|
|
|
expect(analyzedFiles.length).toEqual(1);
|
|
|
|
expect(analyzedFiles[0].analyzedClasses.length).toEqual(1);
|
|
|
|
expect(analyzedFiles[0].analyzedClasses[0].name).toEqual('MockClazz');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should add a newly `AnalyzedClass` to an existing `AnalyzedFile` object', () => {
|
|
|
|
const DUMMY_CLASS_1: any = {};
|
|
|
|
const DUMMY_CLASS_2: any = {};
|
|
|
|
const log: string[] = [];
|
|
|
|
const handler = new AlwaysDetectHandler('handler', log);
|
|
|
|
const analyzedFiles: AnalyzedFile[] = [{
|
|
|
|
sourceFile: mockClazz.getSourceFile(),
|
|
|
|
analyzedClasses: [DUMMY_CLASS_1, DUMMY_CLASS_2],
|
|
|
|
}];
|
|
|
|
const host = new DefaultMigrationHost(
|
2019-11-23 12:54:07 -05:00
|
|
|
mockHost, mockMetadata, mockEvaluator, [handler], entryPointPath, analyzedFiles,
|
|
|
|
diagnosticHandler);
|
2019-10-20 14:40:48 -04:00
|
|
|
host.injectSyntheticDecorator(mockClazz, mockDecorator);
|
|
|
|
expect(analyzedFiles.length).toEqual(1);
|
|
|
|
expect(analyzedFiles[0].analyzedClasses.length).toEqual(3);
|
|
|
|
expect(analyzedFiles[0].analyzedClasses[2].name).toEqual('MockClazz');
|
|
|
|
});
|
2019-07-18 16:05:32 -04:00
|
|
|
|
2019-10-20 14:40:48 -04:00
|
|
|
it('should add a new decorator into an already existing `AnalyzedClass`', () => {
|
|
|
|
const analyzedClass: AnalyzedClass = {
|
|
|
|
name: 'MockClazz',
|
|
|
|
declaration: mockClazz,
|
|
|
|
matches: [],
|
|
|
|
decorators: null,
|
|
|
|
};
|
|
|
|
const log: string[] = [];
|
|
|
|
const handler = new AlwaysDetectHandler('handler', log);
|
|
|
|
const analyzedFiles: AnalyzedFile[] = [{
|
|
|
|
sourceFile: mockClazz.getSourceFile(),
|
|
|
|
analyzedClasses: [analyzedClass],
|
|
|
|
}];
|
|
|
|
const host = new DefaultMigrationHost(
|
2019-11-23 12:54:07 -05:00
|
|
|
mockHost, mockMetadata, mockEvaluator, [handler], entryPointPath, analyzedFiles,
|
|
|
|
diagnosticHandler);
|
2019-10-20 14:40:48 -04:00
|
|
|
host.injectSyntheticDecorator(mockClazz, mockDecorator);
|
|
|
|
expect(analyzedFiles.length).toEqual(1);
|
|
|
|
expect(analyzedFiles[0].analyzedClasses.length).toEqual(1);
|
|
|
|
expect(analyzedFiles[0].analyzedClasses[0]).toBe(analyzedClass);
|
|
|
|
expect(analyzedClass.decorators !.length).toEqual(1);
|
|
|
|
expect(analyzedClass.decorators ![0].name).toEqual('MockDecorator');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should merge a new decorator into pre-existing decorators an already existing `AnalyzedClass`',
|
|
|
|
() => {
|
|
|
|
const analyzedClass: AnalyzedClass = {
|
|
|
|
name: 'MockClazz',
|
|
|
|
declaration: mockClazz,
|
|
|
|
matches: [],
|
|
|
|
decorators: [{name: 'OtherDecorator'} as Decorator],
|
|
|
|
};
|
|
|
|
const log: string[] = [];
|
|
|
|
const handler = new AlwaysDetectHandler('handler', log);
|
|
|
|
const analyzedFiles: AnalyzedFile[] = [{
|
|
|
|
sourceFile: mockClazz.getSourceFile(),
|
|
|
|
analyzedClasses: [analyzedClass],
|
|
|
|
}];
|
|
|
|
const host = new DefaultMigrationHost(
|
2019-11-23 12:54:07 -05:00
|
|
|
mockHost, mockMetadata, mockEvaluator, [handler], entryPointPath, analyzedFiles,
|
|
|
|
diagnosticHandler);
|
2019-10-20 14:40:48 -04:00
|
|
|
host.injectSyntheticDecorator(mockClazz, mockDecorator);
|
|
|
|
expect(analyzedFiles.length).toEqual(1);
|
|
|
|
expect(analyzedFiles[0].analyzedClasses.length).toEqual(1);
|
|
|
|
expect(analyzedFiles[0].analyzedClasses[0]).toBe(analyzedClass);
|
|
|
|
expect(analyzedClass.decorators !.length).toEqual(2);
|
|
|
|
expect(analyzedClass.decorators ![1].name).toEqual('MockDecorator');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error if the injected decorator already exists', () => {
|
|
|
|
const analyzedClass: AnalyzedClass = {
|
|
|
|
name: 'MockClazz',
|
|
|
|
declaration: mockClazz,
|
|
|
|
matches: [],
|
|
|
|
decorators: [{name: 'MockDecorator'} as Decorator],
|
|
|
|
};
|
|
|
|
const log: string[] = [];
|
|
|
|
const handler = new AlwaysDetectHandler('handler', log);
|
|
|
|
const analyzedFiles: AnalyzedFile[] = [{
|
|
|
|
sourceFile: mockClazz.getSourceFile(),
|
|
|
|
analyzedClasses: [analyzedClass],
|
|
|
|
}];
|
|
|
|
const host = new DefaultMigrationHost(
|
2019-11-23 12:54:07 -05:00
|
|
|
mockHost, mockMetadata, mockEvaluator, [handler], entryPointPath, analyzedFiles,
|
|
|
|
diagnosticHandler);
|
2019-10-20 14:40:48 -04:00
|
|
|
expect(() => host.injectSyntheticDecorator(mockClazz, mockDecorator))
|
|
|
|
.toThrow(jasmine.objectContaining(
|
|
|
|
{code: ErrorCode.NGCC_MIGRATION_DECORATOR_INJECTION_ERROR}));
|
|
|
|
});
|
2019-11-23 12:54:07 -05:00
|
|
|
|
|
|
|
it('should report diagnostics from handlers', () => {
|
|
|
|
const log: string[] = [];
|
|
|
|
const handler = new DiagnosticProducingHandler('handler', log);
|
|
|
|
const analyzedFiles: AnalyzedFile[] = [];
|
|
|
|
const diagnostics: ts.Diagnostic[] = [];
|
|
|
|
const host = new DefaultMigrationHost(
|
|
|
|
mockHost, mockMetadata, mockEvaluator, [handler], entryPointPath, analyzedFiles,
|
|
|
|
diagnostic => diagnostics.push(diagnostic));
|
|
|
|
mockClazz.getStart = () => 0;
|
|
|
|
mockClazz.getWidth = () => 0;
|
|
|
|
|
|
|
|
const decorator = createComponentDecorator(mockClazz, {selector: 'comp', exportAs: null});
|
|
|
|
host.injectSyntheticDecorator(mockClazz, decorator);
|
|
|
|
|
|
|
|
expect(diagnostics.length).toBe(1);
|
|
|
|
expect(ts.flattenDiagnosticMessageText(diagnostics[0].messageText, '\n'))
|
|
|
|
.toEqual(
|
|
|
|
`test diagnostic\n` +
|
|
|
|
` Occurs for @Component decorator inserted by an automatic migration\n` +
|
|
|
|
` @Component({ template: "", selector: "comp" })`);
|
|
|
|
});
|
2019-07-18 16:05:32 -04:00
|
|
|
});
|
|
|
|
|
2019-10-20 14:40:48 -04:00
|
|
|
describe('getAllDecorators', () => {
|
|
|
|
it('should be null for unknown source files', () => {
|
|
|
|
const log: string[] = [];
|
|
|
|
const handler = new AlwaysDetectHandler('handler', log);
|
|
|
|
const analyzedFiles: AnalyzedFile[] = [];
|
|
|
|
const host = new DefaultMigrationHost(
|
2019-11-23 12:54:07 -05:00
|
|
|
mockHost, mockMetadata, mockEvaluator, [handler], entryPointPath, analyzedFiles,
|
|
|
|
diagnosticHandler);
|
2019-10-20 14:40:48 -04:00
|
|
|
|
|
|
|
const decorators = host.getAllDecorators(mockClazz);
|
|
|
|
expect(decorators).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be null for unknown classes', () => {
|
|
|
|
const log: string[] = [];
|
|
|
|
const handler = new AlwaysDetectHandler('handler', log);
|
|
|
|
const analyzedFiles: AnalyzedFile[] = [];
|
|
|
|
const host = new DefaultMigrationHost(
|
2019-11-23 12:54:07 -05:00
|
|
|
mockHost, mockMetadata, mockEvaluator, [handler], entryPointPath, analyzedFiles,
|
|
|
|
diagnosticHandler);
|
2019-10-20 14:40:48 -04:00
|
|
|
|
|
|
|
const sourceFile: any = {};
|
|
|
|
const unrelatedClass: any = {
|
|
|
|
getSourceFile: () => sourceFile,
|
|
|
|
};
|
|
|
|
analyzedFiles.push({sourceFile, analyzedClasses: [unrelatedClass]});
|
|
|
|
|
|
|
|
const decorators = host.getAllDecorators(mockClazz);
|
|
|
|
expect(decorators).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should include injected decorators', () => {
|
|
|
|
const log: string[] = [];
|
|
|
|
const handler = new AlwaysDetectHandler('handler', log);
|
|
|
|
const existingDecorator = { name: 'ExistingDecorator' } as Decorator;
|
|
|
|
const analyzedClass: AnalyzedClass = {
|
|
|
|
name: 'MockClazz',
|
|
|
|
declaration: mockClazz,
|
|
|
|
matches: [],
|
|
|
|
decorators: [existingDecorator],
|
|
|
|
};
|
|
|
|
const analyzedFiles: AnalyzedFile[] = [{
|
|
|
|
sourceFile: mockClazz.getSourceFile(),
|
|
|
|
analyzedClasses: [analyzedClass],
|
|
|
|
}];
|
|
|
|
const host = new DefaultMigrationHost(
|
2019-11-23 12:54:07 -05:00
|
|
|
mockHost, mockMetadata, mockEvaluator, [handler], entryPointPath, analyzedFiles,
|
|
|
|
diagnosticHandler);
|
2019-10-20 14:40:48 -04:00
|
|
|
host.injectSyntheticDecorator(mockClazz, mockDecorator);
|
|
|
|
|
|
|
|
const decorators = host.getAllDecorators(mockClazz) !;
|
|
|
|
expect(decorators.length).toBe(2);
|
|
|
|
expect(decorators[0]).toBe(existingDecorator);
|
|
|
|
expect(decorators[1]).toBe(mockDecorator);
|
|
|
|
});
|
2019-07-18 16:05:32 -04:00
|
|
|
});
|
|
|
|
|
2019-10-20 14:40:48 -04:00
|
|
|
describe('isInScope', () => {
|
|
|
|
it('should be true for nodes within the entry-point', () => {
|
|
|
|
const analyzedFiles: AnalyzedFile[] = [];
|
|
|
|
const host = new DefaultMigrationHost(
|
2019-11-23 12:54:07 -05:00
|
|
|
mockHost, mockMetadata, mockEvaluator, [], entryPointPath, analyzedFiles,
|
|
|
|
diagnosticHandler);
|
2019-10-20 14:40:48 -04:00
|
|
|
|
|
|
|
const sourceFile: any = {
|
|
|
|
fileName: _('/node_modules/some-package/entry-point/relative.js'),
|
|
|
|
};
|
|
|
|
const clazz: any = {
|
|
|
|
getSourceFile: () => sourceFile,
|
|
|
|
};
|
|
|
|
expect(host.isInScope(clazz)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be false for nodes outside the entry-point', () => {
|
|
|
|
const analyzedFiles: AnalyzedFile[] = [];
|
|
|
|
const host = new DefaultMigrationHost(
|
2019-11-23 12:54:07 -05:00
|
|
|
mockHost, mockMetadata, mockEvaluator, [], entryPointPath, analyzedFiles,
|
|
|
|
diagnosticHandler);
|
2019-10-20 14:40:48 -04:00
|
|
|
|
|
|
|
const sourceFile: any = {
|
|
|
|
fileName: _('/node_modules/some-package/other-entry/index.js'),
|
|
|
|
};
|
|
|
|
const clazz: any = {
|
|
|
|
getSourceFile: () => sourceFile,
|
|
|
|
};
|
|
|
|
expect(host.isInScope(clazz)).toBe(false);
|
|
|
|
});
|
2019-07-18 16:05:32 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-12-09 18:22:59 -05:00
|
|
|
class TestHandler implements DecoratorHandler<unknown, unknown, unknown> {
|
perf(ivy): reuse prior analysis work during incremental builds (#34288)
Previously, the compiler performed an incremental build by analyzing and
resolving all classes in the program (even unchanged ones) and then using
the dependency graph information to determine which .js files were stale and
needed to be re-emitted. This algorithm produced "correct" rebuilds, but the
cost of re-analyzing the entire program turned out to be higher than
anticipated, especially for component-heavy compilations.
To achieve performant rebuilds, it is necessary to reuse previous analysis
results if possible. Doing this safely requires knowing when prior work is
viable and when it is stale and needs to be re-done.
The new algorithm implemented by this commit is such:
1) Each incremental build starts with knowledge of the last known good
dependency graph and analysis results from the last successful build,
plus of course information about the set of files changed.
2) The previous dependency graph's information is used to determine the
set of source files which have "logically" changed. A source file is
considered logically changed if it or any of its dependencies have
physically changed (on disk) since the last successful compilation. Any
logically unchanged dependencies have their dependency information copied
over to the new dependency graph.
3) During the `TraitCompiler`'s loop to consider all source files in the
program, if a source file is logically unchanged then its previous
analyses are "adopted" (and their 'register' steps are run). If the file
is logically changed, then it is re-analyzed as usual.
4) Then, incremental build proceeds as before, with the new dependency graph
being used to determine the set of files which require re-emitting.
This analysis reuse avoids template parsing operations in many circumstances
and significantly reduces the time it takes ngtsc to rebuild a large
application.
Future work will increase performance even more, by tackling a variety of
other opportunities to reuse or avoid work.
PR Close #34288
2019-12-05 19:03:17 -05:00
|
|
|
constructor(readonly name: string, protected log: string[]) {}
|
2019-07-18 16:05:32 -04:00
|
|
|
|
|
|
|
precedence = HandlerPrecedence.PRIMARY;
|
2019-12-09 18:22:59 -05:00
|
|
|
detect(node: ClassDeclaration, decorators: Decorator[]|null): DetectResult<unknown>|undefined {
|
2019-07-18 16:05:32 -04:00
|
|
|
this.log.push(`${this.name}:detect:${node.name.text}:${decorators !.map(d => d.name)}`);
|
|
|
|
return undefined;
|
|
|
|
}
|
2019-12-09 18:22:59 -05:00
|
|
|
analyze(node: ClassDeclaration): AnalysisOutput<unknown> {
|
2019-07-18 16:05:32 -04:00
|
|
|
this.log.push(this.name + ':analyze:' + node.name.text);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
compile(node: ClassDeclaration): CompileResult|CompileResult[] {
|
|
|
|
this.log.push(this.name + ':compile:' + node.name.text);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class AlwaysDetectHandler extends TestHandler {
|
2019-12-09 18:22:59 -05:00
|
|
|
detect(node: ClassDeclaration, decorators: Decorator[]|null): DetectResult<unknown>|undefined {
|
2019-07-18 16:05:32 -04:00
|
|
|
super.detect(node, decorators);
|
|
|
|
return {trigger: node, metadata: {}};
|
|
|
|
}
|
|
|
|
}
|
2019-11-23 12:54:07 -05:00
|
|
|
|
|
|
|
class DiagnosticProducingHandler extends AlwaysDetectHandler {
|
|
|
|
analyze(node: ClassDeclaration): AnalysisOutput<any> {
|
|
|
|
super.analyze(node);
|
|
|
|
return {diagnostics: [makeDiagnostic(9999, node, 'test diagnostic')]};
|
|
|
|
}
|
|
|
|
}
|