refactor(ivy): move ngcc into a higher level folder (#29092)
PR Close #29092
This commit is contained in:

committed by
Matias Niemelä

parent
cf4718c366
commit
a770aa231d
@ -0,0 +1,231 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
import * as ts from 'typescript';
|
||||
|
||||
import {AbsoluteFsPath} from '../../../src/ngtsc/path';
|
||||
import {Decorator} from '../../../src/ngtsc/reflection';
|
||||
import {DecoratorHandler, DetectResult} from '../../../src/ngtsc/transform';
|
||||
import {CompiledClass, DecorationAnalyses, DecorationAnalyzer} from '../../src/analysis/decoration_analyzer';
|
||||
import {NgccReferencesRegistry} from '../../src/analysis/ngcc_references_registry';
|
||||
import {Esm2015ReflectionHost} from '../../src/host/esm2015_host';
|
||||
import {makeTestBundleProgram} from '../helpers/utils';
|
||||
|
||||
const TEST_PROGRAM = [
|
||||
{
|
||||
name: 'test.js',
|
||||
contents: `
|
||||
import {Component, Directive, Injectable} from '@angular/core';
|
||||
|
||||
export class MyComponent {}
|
||||
MyComponent.decorators = [{type: Component}];
|
||||
|
||||
export class MyDirective {}
|
||||
MyDirective.decorators = [{type: Directive}];
|
||||
|
||||
export class MyService {}
|
||||
MyService.decorators = [{type: Injectable}];
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: 'other.js',
|
||||
contents: `
|
||||
import {Component} from '@angular/core';
|
||||
|
||||
export class MyOtherComponent {}
|
||||
MyOtherComponent.decorators = [{type: Component}];
|
||||
`,
|
||||
},
|
||||
];
|
||||
|
||||
const INTERNAL_COMPONENT_PROGRAM = [
|
||||
{
|
||||
name: 'entrypoint.js',
|
||||
contents: `
|
||||
import {Component, NgModule} from '@angular/core';
|
||||
import {ImportedComponent} from './component';
|
||||
|
||||
export class LocalComponent {}
|
||||
LocalComponent.decorators = [{type: Component}];
|
||||
|
||||
export class MyModule {}
|
||||
MyModule.decorators = [{type: NgModule, args: [{
|
||||
declarations: [ImportedComponent, LocalComponent],
|
||||
exports: [ImportedComponent, LocalComponent],
|
||||
},] }];
|
||||
`
|
||||
},
|
||||
{
|
||||
name: 'component.js',
|
||||
contents: `
|
||||
import {Component} from '@angular/core';
|
||||
export class ImportedComponent {}
|
||||
ImportedComponent.decorators = [{type: Component}];
|
||||
`,
|
||||
isRoot: false,
|
||||
}
|
||||
];
|
||||
|
||||
type DecoratorHandlerWithResolve = DecoratorHandler<any, any>& {
|
||||
resolve: NonNullable<DecoratorHandler<any, any>['resolve']>;
|
||||
};
|
||||
|
||||
describe('DecorationAnalyzer', () => {
|
||||
describe('analyzeProgram()', () => {
|
||||
let logs: string[];
|
||||
let program: ts.Program;
|
||||
let testHandler: jasmine.SpyObj<DecoratorHandlerWithResolve>;
|
||||
let result: DecorationAnalyses;
|
||||
|
||||
// Helpers
|
||||
const createTestHandler = () => {
|
||||
const handler = jasmine.createSpyObj<DecoratorHandlerWithResolve>('TestDecoratorHandler', [
|
||||
'detect',
|
||||
'analyze',
|
||||
'resolve',
|
||||
'compile',
|
||||
]);
|
||||
// Only detect the Component and Directive decorators
|
||||
handler.detect.and.callFake(
|
||||
(node: ts.Declaration, decorators: Decorator[]): DetectResult<any>| undefined => {
|
||||
logs.push(`detect: ${(node as any).name.text}@${decorators.map(d => d.name)}`);
|
||||
if (!decorators) {
|
||||
return undefined;
|
||||
}
|
||||
const metadata = decorators.find(d => d.name === 'Component' || d.name === 'Directive');
|
||||
if (metadata === undefined) {
|
||||
return undefined;
|
||||
} else {
|
||||
return {
|
||||
metadata,
|
||||
trigger: metadata.node,
|
||||
};
|
||||
}
|
||||
});
|
||||
// The "test" analysis is an object with the name of the decorator being analyzed
|
||||
handler.analyze.and.callFake((decl: ts.Declaration, dec: Decorator) => {
|
||||
logs.push(`analyze: ${(decl as any).name.text}@${dec.name}`);
|
||||
return {analysis: {decoratorName: dec.name}, diagnostics: undefined};
|
||||
});
|
||||
// The "test" resolution is just setting `resolved: true` on the analysis
|
||||
handler.resolve.and.callFake((decl: ts.Declaration, analysis: any) => {
|
||||
logs.push(`resolve: ${(decl as any).name.text}@${analysis.decoratorName}`);
|
||||
analysis.resolved = true;
|
||||
});
|
||||
// The "test" compilation result is just the name of the decorator being compiled
|
||||
// (suffixed with `(compiled)`)
|
||||
handler.compile.and.callFake((decl: ts.Declaration, analysis: any) => {
|
||||
logs.push(
|
||||
`compile: ${(decl as any).name.text}@${analysis.decoratorName} (resolved: ${analysis.resolved})`);
|
||||
return `@${analysis.decoratorName} (compiled)`;
|
||||
});
|
||||
return handler;
|
||||
};
|
||||
|
||||
const setUpAndAnalyzeProgram = (...progArgs: Parameters<typeof makeTestBundleProgram>) => {
|
||||
logs = [];
|
||||
|
||||
const {options, host, ...bundle} = makeTestBundleProgram(...progArgs);
|
||||
program = bundle.program;
|
||||
|
||||
const reflectionHost = new Esm2015ReflectionHost(false, program.getTypeChecker());
|
||||
const referencesRegistry = new NgccReferencesRegistry(reflectionHost);
|
||||
const analyzer = new DecorationAnalyzer(
|
||||
program, options, host, program.getTypeChecker(), reflectionHost, referencesRegistry,
|
||||
[AbsoluteFsPath.fromUnchecked('/')], false);
|
||||
testHandler = createTestHandler();
|
||||
analyzer.handlers = [testHandler];
|
||||
result = analyzer.analyzeProgram();
|
||||
};
|
||||
|
||||
describe('basic usage', () => {
|
||||
beforeEach(() => setUpAndAnalyzeProgram(TEST_PROGRAM));
|
||||
|
||||
it('should return an object containing a reference to the original source file', () => {
|
||||
TEST_PROGRAM.forEach(({name}) => {
|
||||
const file = program.getSourceFile(name) !;
|
||||
expect(result.get(file) !.sourceFile).toBe(file);
|
||||
});
|
||||
});
|
||||
|
||||
it('should call detect on the decorator handlers with each class from the parsed file',
|
||||
() => {
|
||||
expect(testHandler.detect).toHaveBeenCalledTimes(4);
|
||||
expect(testHandler.detect.calls.allArgs().map(args => args[1][0])).toEqual([
|
||||
jasmine.objectContaining({name: 'Component'}),
|
||||
jasmine.objectContaining({name: 'Directive'}),
|
||||
jasmine.objectContaining({name: 'Injectable'}),
|
||||
jasmine.objectContaining({name: 'Component'}),
|
||||
]);
|
||||
});
|
||||
|
||||
it('should return an object containing the classes that were analyzed', () => {
|
||||
const file1 = program.getSourceFile(TEST_PROGRAM[0].name) !;
|
||||
const compiledFile1 = result.get(file1) !;
|
||||
expect(compiledFile1.compiledClasses.length).toEqual(2);
|
||||
expect(compiledFile1.compiledClasses[0]).toEqual(jasmine.objectContaining({
|
||||
name: 'MyComponent', compilation: ['@Component (compiled)'],
|
||||
} as unknown as CompiledClass));
|
||||
expect(compiledFile1.compiledClasses[1]).toEqual(jasmine.objectContaining({
|
||||
name: 'MyDirective', compilation: ['@Directive (compiled)'],
|
||||
} as unknown as CompiledClass));
|
||||
|
||||
const file2 = program.getSourceFile(TEST_PROGRAM[1].name) !;
|
||||
const compiledFile2 = result.get(file2) !;
|
||||
expect(compiledFile2.compiledClasses.length).toEqual(1);
|
||||
expect(compiledFile2.compiledClasses[0]).toEqual(jasmine.objectContaining({
|
||||
name: 'MyOtherComponent', compilation: ['@Component (compiled)'],
|
||||
} as unknown as CompiledClass));
|
||||
});
|
||||
|
||||
it('should analyze, resolve and compile the classes that are detected', () => {
|
||||
expect(logs).toEqual([
|
||||
// First detect and (potentially) analyze.
|
||||
'detect: MyComponent@Component',
|
||||
'analyze: MyComponent@Component',
|
||||
'detect: MyDirective@Directive',
|
||||
'analyze: MyDirective@Directive',
|
||||
'detect: MyService@Injectable',
|
||||
'detect: MyOtherComponent@Component',
|
||||
'analyze: MyOtherComponent@Component',
|
||||
// The resolve.
|
||||
'resolve: MyComponent@Component',
|
||||
'resolve: MyDirective@Directive',
|
||||
'resolve: MyOtherComponent@Component',
|
||||
// Finally compile.
|
||||
'compile: MyComponent@Component (resolved: true)',
|
||||
'compile: MyDirective@Directive (resolved: true)',
|
||||
'compile: MyOtherComponent@Component (resolved: true)',
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('internal components', () => {
|
||||
beforeEach(() => setUpAndAnalyzeProgram(INTERNAL_COMPONENT_PROGRAM));
|
||||
|
||||
// The problem of exposing the type of these internal components in the .d.ts typing files
|
||||
// is not yet solved.
|
||||
it('should analyze an internally imported component, which is not publicly exported from the entry-point',
|
||||
() => {
|
||||
const file = program.getSourceFile('component.js') !;
|
||||
const analysis = result.get(file) !;
|
||||
expect(analysis).toBeDefined();
|
||||
const ImportedComponent =
|
||||
analysis.compiledClasses.find(f => f.name === 'ImportedComponent') !;
|
||||
expect(ImportedComponent).toBeDefined();
|
||||
});
|
||||
|
||||
it('should analyze an internally defined component, which is not exported at all', () => {
|
||||
const file = program.getSourceFile('entrypoint.js') !;
|
||||
const analysis = result.get(file) !;
|
||||
expect(analysis).toBeDefined();
|
||||
const LocalComponent = analysis.compiledClasses.find(f => f.name === 'LocalComponent') !;
|
||||
expect(LocalComponent).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
@ -0,0 +1,401 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
import * as ts from 'typescript';
|
||||
|
||||
import {ModuleWithProvidersAnalyses, ModuleWithProvidersAnalyzer} from '../../src/analysis/module_with_providers_analyzer';
|
||||
import {NgccReferencesRegistry} from '../../src/analysis/ngcc_references_registry';
|
||||
import {Esm2015ReflectionHost} from '../../src/host/esm2015_host';
|
||||
import {BundleProgram} from '../../src/packages/bundle_program';
|
||||
import {getDeclaration, makeTestBundleProgram, makeTestProgram} from '../helpers/utils';
|
||||
|
||||
const TEST_PROGRAM = [
|
||||
{
|
||||
name: '/src/entry-point.js',
|
||||
contents: `
|
||||
export * from './explicit';
|
||||
export * from './any';
|
||||
export * from './implicit';
|
||||
export * from './no-providers';
|
||||
export * from './module';
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/src/explicit.js',
|
||||
contents: `
|
||||
import {ExternalModule} from './module';
|
||||
import {LibraryModule} from 'some-library';
|
||||
export class ExplicitInternalModule {}
|
||||
export function explicitInternalFunction() {
|
||||
return {
|
||||
ngModule: ExplicitInternalModule,
|
||||
providers: []
|
||||
};
|
||||
}
|
||||
export function explicitExternalFunction() {
|
||||
return {
|
||||
ngModule: ExternalModule,
|
||||
providers: []
|
||||
};
|
||||
}
|
||||
export function explicitLibraryFunction() {
|
||||
return {
|
||||
ngModule: LibraryModule,
|
||||
providers: []
|
||||
};
|
||||
}
|
||||
export class ExplicitClass {
|
||||
static explicitInternalMethod() {
|
||||
return {
|
||||
ngModule: ExplicitInternalModule,
|
||||
providers: []
|
||||
};
|
||||
}
|
||||
static explicitExternalMethod() {
|
||||
return {
|
||||
ngModule: ExternalModule,
|
||||
providers: []
|
||||
};
|
||||
}
|
||||
static explicitLibraryMethod() {
|
||||
return {
|
||||
ngModule: LibraryModule,
|
||||
providers: []
|
||||
};
|
||||
}
|
||||
}
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/src/any.js',
|
||||
contents: `
|
||||
import {ExternalModule} from './module';
|
||||
import {LibraryModule} from 'some-library';
|
||||
export class AnyInternalModule {}
|
||||
export function anyInternalFunction() {
|
||||
return {
|
||||
ngModule: AnyInternalModule,
|
||||
providers: []
|
||||
};
|
||||
}
|
||||
export function anyExternalFunction() {
|
||||
return {
|
||||
ngModule: ExternalModule,
|
||||
providers: []
|
||||
};
|
||||
}
|
||||
export function anyLibraryFunction() {
|
||||
return {
|
||||
ngModule: LibraryModule,
|
||||
providers: []
|
||||
};
|
||||
}
|
||||
export class AnyClass {
|
||||
static anyInternalMethod() {
|
||||
return {
|
||||
ngModule: AnyInternalModule,
|
||||
providers: []
|
||||
};
|
||||
}
|
||||
static anyExternalMethod() {
|
||||
return {
|
||||
ngModule: ExternalModule,
|
||||
providers: []
|
||||
};
|
||||
}
|
||||
static anyLibraryMethod() {
|
||||
return {
|
||||
ngModule: LibraryModule,
|
||||
providers: []
|
||||
};
|
||||
}
|
||||
}
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/src/implicit.js',
|
||||
contents: `
|
||||
import {ExternalModule} from './module';
|
||||
import {LibraryModule} from 'some-library';
|
||||
export class ImplicitInternalModule {}
|
||||
export function implicitInternalFunction() {
|
||||
return {
|
||||
ngModule: ImplicitInternalModule,
|
||||
providers: [],
|
||||
};
|
||||
}
|
||||
export function implicitExternalFunction() {
|
||||
return {
|
||||
ngModule: ExternalModule,
|
||||
providers: [],
|
||||
};
|
||||
}
|
||||
export function implicitLibraryFunction() {
|
||||
return {
|
||||
ngModule: LibraryModule,
|
||||
providers: [],
|
||||
};
|
||||
}
|
||||
export class ImplicitClass {
|
||||
static implicitInternalMethod() {
|
||||
return {
|
||||
ngModule: ImplicitInternalModule,
|
||||
providers: [],
|
||||
};
|
||||
}
|
||||
static implicitExternalMethod() {
|
||||
return {
|
||||
ngModule: ExternalModule,
|
||||
providers: [],
|
||||
};
|
||||
}
|
||||
static implicitLibraryMethod() {
|
||||
return {
|
||||
ngModule: LibraryModule,
|
||||
providers: [],
|
||||
};
|
||||
}
|
||||
}
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/src/no-providers.js',
|
||||
contents: `
|
||||
import {ExternalModule} from './module';
|
||||
import {LibraryModule} from 'some-library';
|
||||
export class NoProvidersInternalModule {}
|
||||
export function noProvExplicitInternalFunction() {
|
||||
return {ngModule: NoProvidersInternalModule};
|
||||
}
|
||||
export function noProvExplicitExternalFunction() {
|
||||
return {ngModule: ExternalModule};
|
||||
}
|
||||
export function noProvExplicitLibraryFunction() {
|
||||
return {ngModule: LibraryModule};
|
||||
}
|
||||
export function noProvAnyInternalFunction() {
|
||||
return {ngModule: NoProvidersInternalModule};
|
||||
}
|
||||
export function noProvAnyExternalFunction() {
|
||||
return {ngModule: ExternalModule};
|
||||
}
|
||||
export function noProvAnyLibraryFunction() {
|
||||
return {ngModule: LibraryModule};
|
||||
}
|
||||
export function noProvImplicitInternalFunction() {
|
||||
return {ngModule: NoProvidersInternalModule};
|
||||
}
|
||||
export function noProvImplicitExternalFunction() {
|
||||
return {ngModule: ExternalModule};
|
||||
}
|
||||
export function noProvImplicitLibraryFunction() {
|
||||
return {ngModule: LibraryModule};
|
||||
}
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/src/module.js',
|
||||
contents: `
|
||||
export class ExternalModule {}
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/node_modules/some-library/index.d.ts',
|
||||
contents: 'export declare class LibraryModule {}'
|
||||
},
|
||||
];
|
||||
const TEST_DTS_PROGRAM = [
|
||||
{
|
||||
name: '/typings/entry-point.d.ts',
|
||||
contents: `
|
||||
export * from './explicit';
|
||||
export * from './any';
|
||||
export * from './implicit';
|
||||
export * from './no-providers';
|
||||
export * from './module';
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/typings/explicit.d.ts',
|
||||
contents: `
|
||||
import {ModuleWithProviders} from './core';
|
||||
import {ExternalModule} from './module';
|
||||
import {LibraryModule} from 'some-library';
|
||||
export declare class ExplicitInternalModule {}
|
||||
export declare function explicitInternalFunction(): ModuleWithProviders<ExplicitInternalModule>;
|
||||
export declare function explicitExternalFunction(): ModuleWithProviders<ExternalModule>;
|
||||
export declare function explicitLibraryFunction(): ModuleWithProviders<LibraryModule>;
|
||||
export declare class ExplicitClass {
|
||||
static explicitInternalMethod(): ModuleWithProviders<ExplicitInternalModule>;
|
||||
static explicitExternalMethod(): ModuleWithProviders<ExternalModule>;
|
||||
static explicitLibraryMethod(): ModuleWithProviders<LibraryModule>;
|
||||
}
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/typings/any.d.ts',
|
||||
contents: `
|
||||
import {ModuleWithProviders} from './core';
|
||||
export declare class AnyInternalModule {}
|
||||
export declare function anyInternalFunction(): ModuleWithProviders<any>;
|
||||
export declare function anyExternalFunction(): ModuleWithProviders<any>;
|
||||
export declare function anyLibraryFunction(): ModuleWithProviders<any>;
|
||||
export declare class AnyClass {
|
||||
static anyInternalMethod(): ModuleWithProviders<any>;
|
||||
static anyExternalMethod(): ModuleWithProviders<any>;
|
||||
static anyLibraryMethod(): ModuleWithProviders<any>;
|
||||
}
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/typings/implicit.d.ts',
|
||||
contents: `
|
||||
import {ExternalModule} from './module';
|
||||
import {LibraryModule} from 'some-library';
|
||||
export declare class ImplicitInternalModule {}
|
||||
export declare function implicitInternalFunction(): { ngModule: typeof ImplicitInternalModule; providers: never[]; };
|
||||
export declare function implicitExternalFunction(): { ngModule: typeof ExternalModule; providers: never[]; };
|
||||
export declare function implicitLibraryFunction(): { ngModule: typeof LibraryModule; providers: never[]; };
|
||||
export declare class ImplicitClass {
|
||||
static implicitInternalMethod(): { ngModule: typeof ImplicitInternalModule; providers: never[]; };
|
||||
static implicitExternalMethod(): { ngModule: typeof ExternalModule; providers: never[]; };
|
||||
static implicitLibraryMethod(): { ngModule: typeof LibraryModule; providers: never[]; };
|
||||
}
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/typings/no-providers.d.ts',
|
||||
contents: `
|
||||
import {ModuleWithProviders} from './core';
|
||||
import {ExternalModule} from './module';
|
||||
import {LibraryModule} from 'some-library';
|
||||
export declare class NoProvidersInternalModule {}
|
||||
export declare function noProvExplicitInternalFunction(): ModuleWithProviders<NoProvidersInternalModule>;
|
||||
export declare function noProvExplicitExternalFunction(): ModuleWithProviders<ExternalModule>;
|
||||
export declare function noProvExplicitLibraryFunction(): ModuleWithProviders<LibraryModule>;
|
||||
export declare function noProvAnyInternalFunction(): ModuleWithProviders<any>;
|
||||
export declare function noProvAnyExternalFunction(): ModuleWithProviders<any>;
|
||||
export declare function noProvAnyLibraryFunction(): ModuleWithProviders<any>;
|
||||
export declare function noProvImplicitInternalFunction(): { ngModule: typeof NoProvidersInternalModule; };
|
||||
export declare function noProvImplicitExternalFunction(): { ngModule: typeof ExternalModule; };
|
||||
export declare function noProvImplicitLibraryFunction(): { ngModule: typeof LibraryModule; };
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/typings/module.d.ts',
|
||||
contents: `
|
||||
export declare class ExternalModule {}
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/typings/core.d.ts',
|
||||
contents: `
|
||||
|
||||
export declare interface Type<T> {
|
||||
new (...args: any[]): T
|
||||
}
|
||||
export declare type Provider = any;
|
||||
export declare interface ModuleWithProviders<T> {
|
||||
ngModule: Type<T>
|
||||
providers?: Provider[]
|
||||
}
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/node_modules/some-library/index.d.ts',
|
||||
contents: 'export declare class LibraryModule {}'
|
||||
},
|
||||
];
|
||||
|
||||
describe('ModuleWithProvidersAnalyzer', () => {
|
||||
describe('analyzeProgram()', () => {
|
||||
let analyses: ModuleWithProvidersAnalyses;
|
||||
let program: ts.Program;
|
||||
let dtsProgram: BundleProgram;
|
||||
let referencesRegistry: NgccReferencesRegistry;
|
||||
|
||||
beforeAll(() => {
|
||||
program = makeTestProgram(...TEST_PROGRAM);
|
||||
dtsProgram = makeTestBundleProgram(TEST_DTS_PROGRAM);
|
||||
const host = new Esm2015ReflectionHost(false, program.getTypeChecker(), dtsProgram);
|
||||
referencesRegistry = new NgccReferencesRegistry(host);
|
||||
|
||||
const analyzer = new ModuleWithProvidersAnalyzer(host, referencesRegistry);
|
||||
analyses = analyzer.analyzeProgram(program);
|
||||
});
|
||||
|
||||
it('should ignore declarations that already have explicit NgModule type params',
|
||||
() => { expect(getAnalysisDescription(analyses, '/typings/explicit.d.ts')).toEqual([]); });
|
||||
|
||||
it('should find declarations that use `any` for the NgModule type param', () => {
|
||||
const anyAnalysis = getAnalysisDescription(analyses, '/typings/any.d.ts');
|
||||
expect(anyAnalysis).toContain(['anyInternalFunction', 'AnyInternalModule', null]);
|
||||
expect(anyAnalysis).toContain(['anyExternalFunction', 'ExternalModule', null]);
|
||||
expect(anyAnalysis).toContain(['anyLibraryFunction', 'LibraryModule', 'some-library']);
|
||||
expect(anyAnalysis).toContain(['anyInternalMethod', 'AnyInternalModule', null]);
|
||||
expect(anyAnalysis).toContain(['anyExternalMethod', 'ExternalModule', null]);
|
||||
expect(anyAnalysis).toContain(['anyLibraryMethod', 'LibraryModule', 'some-library']);
|
||||
});
|
||||
|
||||
it('should track internal module references in the references registry', () => {
|
||||
const declarations = referencesRegistry.getDeclarationMap();
|
||||
const externalModuleDeclaration =
|
||||
getDeclaration(program, '/src/module.js', 'ExternalModule', ts.isClassDeclaration);
|
||||
const libraryModuleDeclaration = getDeclaration(
|
||||
program, '/node_modules/some-library/index.d.ts', 'LibraryModule', ts.isClassDeclaration);
|
||||
expect(declarations.has(externalModuleDeclaration.name !)).toBe(true);
|
||||
expect(declarations.has(libraryModuleDeclaration.name !)).toBe(false);
|
||||
});
|
||||
|
||||
it('should find declarations that have implicit return types', () => {
|
||||
const anyAnalysis = getAnalysisDescription(analyses, '/typings/implicit.d.ts');
|
||||
expect(anyAnalysis).toContain(['implicitInternalFunction', 'ImplicitInternalModule', null]);
|
||||
expect(anyAnalysis).toContain(['implicitExternalFunction', 'ExternalModule', null]);
|
||||
expect(anyAnalysis).toContain(['implicitLibraryFunction', 'LibraryModule', 'some-library']);
|
||||
expect(anyAnalysis).toContain(['implicitInternalMethod', 'ImplicitInternalModule', null]);
|
||||
expect(anyAnalysis).toContain(['implicitExternalMethod', 'ExternalModule', null]);
|
||||
expect(anyAnalysis).toContain(['implicitLibraryMethod', 'LibraryModule', 'some-library']);
|
||||
});
|
||||
|
||||
it('should find declarations that do not specify a `providers` property in the return type',
|
||||
() => {
|
||||
const anyAnalysis = getAnalysisDescription(analyses, '/typings/no-providers.d.ts');
|
||||
expect(anyAnalysis).not.toContain([
|
||||
'noProvExplicitInternalFunction', 'NoProvidersInternalModule'
|
||||
]);
|
||||
expect(anyAnalysis).not.toContain([
|
||||
'noProvExplicitExternalFunction', 'ExternalModule', null
|
||||
]);
|
||||
expect(anyAnalysis).toContain([
|
||||
'noProvAnyInternalFunction', 'NoProvidersInternalModule', null
|
||||
]);
|
||||
expect(anyAnalysis).toContain(['noProvAnyExternalFunction', 'ExternalModule', null]);
|
||||
expect(anyAnalysis).toContain([
|
||||
'noProvAnyLibraryFunction', 'LibraryModule', 'some-library'
|
||||
]);
|
||||
expect(anyAnalysis).toContain([
|
||||
'noProvImplicitInternalFunction', 'NoProvidersInternalModule', null
|
||||
]);
|
||||
expect(anyAnalysis).toContain(['noProvImplicitExternalFunction', 'ExternalModule', null]);
|
||||
expect(anyAnalysis).toContain([
|
||||
'noProvImplicitLibraryFunction', 'LibraryModule', 'some-library'
|
||||
]);
|
||||
});
|
||||
|
||||
function getAnalysisDescription(analyses: ModuleWithProvidersAnalyses, fileName: string) {
|
||||
const file = dtsProgram.program.getSourceFile(fileName) !;
|
||||
const analysis = analyses.get(file);
|
||||
return analysis ?
|
||||
analysis.map(
|
||||
info =>
|
||||
[info.declaration.name !.getText(),
|
||||
(info.ngModule.node as ts.ClassDeclaration).name !.getText(),
|
||||
info.ngModule.viaModule]) :
|
||||
[];
|
||||
}
|
||||
});
|
||||
});
|
@ -0,0 +1,241 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
import * as ts from 'typescript';
|
||||
|
||||
import {Reference} from '../../../src/ngtsc/imports';
|
||||
import {NgccReferencesRegistry} from '../../src/analysis/ngcc_references_registry';
|
||||
import {PrivateDeclarationsAnalyzer} from '../../src/analysis/private_declarations_analyzer';
|
||||
import {Esm2015ReflectionHost} from '../../src/host/esm2015_host';
|
||||
import {getDeclaration, makeTestBundleProgram, makeTestProgram} from '../helpers/utils';
|
||||
|
||||
describe('PrivateDeclarationsAnalyzer', () => {
|
||||
describe('analyzeProgram()', () => {
|
||||
|
||||
const TEST_PROGRAM = [
|
||||
{
|
||||
name: '/src/entry_point.js',
|
||||
isRoot: true,
|
||||
contents: `
|
||||
export {PublicComponent} from './a';
|
||||
export {ModuleA} from './mod';
|
||||
export {ModuleB} from './b';
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/src/a.js',
|
||||
isRoot: false,
|
||||
contents: `
|
||||
import {Component} from '@angular/core';
|
||||
export class PublicComponent {}
|
||||
PublicComponent.decorators = [
|
||||
{type: Component, args: [{selectors: 'a', template: ''}]}
|
||||
];
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/src/b.js',
|
||||
isRoot: false,
|
||||
contents: `
|
||||
import {Component, NgModule} from '@angular/core';
|
||||
class PrivateComponent1 {}
|
||||
PrivateComponent1.decorators = [
|
||||
{type: Component, args: [{selectors: 'b', template: ''}]}
|
||||
];
|
||||
class PrivateComponent2 {}
|
||||
PrivateComponent2.decorators = [
|
||||
{type: Component, args: [{selectors: 'c', template: ''}]}
|
||||
];
|
||||
export class ModuleB {}
|
||||
ModuleB.decorators = [
|
||||
{type: NgModule, args: [{declarations: [PrivateComponent1]}]}
|
||||
];
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/src/c.js',
|
||||
isRoot: false,
|
||||
contents: `
|
||||
import {Component} from '@angular/core';
|
||||
export class InternalComponent1 {}
|
||||
InternalComponent1.decorators = [
|
||||
{type: Component, args: [{selectors: 'd', template: ''}]}
|
||||
];
|
||||
export class InternalComponent2 {}
|
||||
InternalComponent2.decorators = [
|
||||
{type: Component, args: [{selectors: 'e', template: ''}]}
|
||||
];
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/src/mod.js',
|
||||
isRoot: false,
|
||||
contents: `
|
||||
import {Component, NgModule} from '@angular/core';
|
||||
import {PublicComponent} from './a';
|
||||
import {ModuleB} from './b';
|
||||
import {InternalComponent1} from './c';
|
||||
export class ModuleA {}
|
||||
ModuleA.decorators = [
|
||||
{type: NgModule, args: [{
|
||||
declarations: [PublicComponent, InternalComponent1],
|
||||
imports: [ModuleB]
|
||||
}]}
|
||||
];
|
||||
`
|
||||
}
|
||||
];
|
||||
const TEST_DTS_PROGRAM = [
|
||||
{
|
||||
name: '/typings/entry_point.d.ts',
|
||||
isRoot: true,
|
||||
contents: `
|
||||
export {PublicComponent} from './a';
|
||||
export {ModuleA} from './mod';
|
||||
export {ModuleB} from './b';
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/typings/a.d.ts',
|
||||
isRoot: false,
|
||||
contents: `
|
||||
export declare class PublicComponent {}
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/typings/b.d.ts',
|
||||
isRoot: false,
|
||||
contents: `
|
||||
export declare class ModuleB {}
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/typings/c.d.ts',
|
||||
isRoot: false,
|
||||
contents: `
|
||||
export declare class InternalComponent1 {}
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/typings/mod.d.ts',
|
||||
isRoot: false,
|
||||
contents: `
|
||||
import {PublicComponent} from './a';
|
||||
import {ModuleB} from './b';
|
||||
import {InternalComponent1} from './c';
|
||||
export declare class ModuleA {}
|
||||
`
|
||||
},
|
||||
];
|
||||
|
||||
it('should find all NgModule declarations that were not publicly exported from the entry-point',
|
||||
() => {
|
||||
const {program, referencesRegistry, analyzer} = setup(TEST_PROGRAM, TEST_DTS_PROGRAM);
|
||||
|
||||
addToReferencesRegistry(program, referencesRegistry, '/src/a.js', 'PublicComponent');
|
||||
addToReferencesRegistry(program, referencesRegistry, '/src/b.js', 'PrivateComponent1');
|
||||
addToReferencesRegistry(program, referencesRegistry, '/src/c.js', 'InternalComponent1');
|
||||
|
||||
const analyses = analyzer.analyzeProgram(program);
|
||||
// Note that `PrivateComponent2` and `InternalComponent2` are not found because they are
|
||||
// not added to the ReferencesRegistry (i.e. they were not declared in an NgModule).
|
||||
expect(analyses.length).toEqual(2);
|
||||
expect(analyses).toEqual([
|
||||
{identifier: 'PrivateComponent1', from: '/src/b.js', dtsFrom: null, alias: null},
|
||||
{
|
||||
identifier: 'InternalComponent1',
|
||||
from: '/src/c.js',
|
||||
dtsFrom: '/typings/c.d.ts',
|
||||
alias: null
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
const ALIASED_EXPORTS_PROGRAM = [
|
||||
{
|
||||
name: '/src/entry_point.js',
|
||||
isRoot: true,
|
||||
contents: `
|
||||
// This component is only exported as an alias.
|
||||
export {ComponentOne as aliasedComponentOne} from './a';
|
||||
// This component is exported both as itself and an alias.
|
||||
export {ComponentTwo as aliasedComponentTwo, ComponentTwo} from './a';
|
||||
`
|
||||
},
|
||||
{
|
||||
name: '/src/a.js',
|
||||
isRoot: false,
|
||||
contents: `
|
||||
import {Component} from '@angular/core';
|
||||
export class ComponentOne {}
|
||||
ComponentOne.decorators = [
|
||||
{type: Component, args: [{selectors: 'a', template: ''}]}
|
||||
];
|
||||
|
||||
export class ComponentTwo {}
|
||||
Component.decorators = [
|
||||
{type: Component, args: [{selectors: 'a', template: ''}]}
|
||||
];
|
||||
`
|
||||
}
|
||||
];
|
||||
const ALIASED_EXPORTS_DTS_PROGRAM = [
|
||||
{
|
||||
name: '/typings/entry_point.d.ts',
|
||||
isRoot: true,
|
||||
contents: `
|
||||
export declare class aliasedComponentOne {}
|
||||
export declare class ComponentTwo {}
|
||||
export {ComponentTwo as aliasedComponentTwo}
|
||||
`
|
||||
},
|
||||
];
|
||||
|
||||
it('should find all non-public declarations that were aliased', () => {
|
||||
const {program, referencesRegistry, analyzer} =
|
||||
setup(ALIASED_EXPORTS_PROGRAM, ALIASED_EXPORTS_DTS_PROGRAM);
|
||||
|
||||
addToReferencesRegistry(program, referencesRegistry, '/src/a.js', 'ComponentOne');
|
||||
addToReferencesRegistry(program, referencesRegistry, '/src/a.js', 'ComponentTwo');
|
||||
|
||||
const analyses = analyzer.analyzeProgram(program);
|
||||
expect(analyses).toEqual([{
|
||||
identifier: 'ComponentOne',
|
||||
from: '/src/a.js',
|
||||
dtsFrom: null,
|
||||
alias: 'aliasedComponentOne',
|
||||
}]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
type Files = {
|
||||
name: string,
|
||||
contents: string, isRoot?: boolean | undefined
|
||||
}[];
|
||||
|
||||
function setup(jsProgram: Files, dtsProgram: Files) {
|
||||
const program = makeTestProgram(...jsProgram);
|
||||
const dts = makeTestBundleProgram(dtsProgram);
|
||||
const host = new Esm2015ReflectionHost(false, program.getTypeChecker(), dts);
|
||||
const referencesRegistry = new NgccReferencesRegistry(host);
|
||||
const analyzer = new PrivateDeclarationsAnalyzer(host, referencesRegistry);
|
||||
return {program, referencesRegistry, analyzer};
|
||||
}
|
||||
|
||||
/**
|
||||
* Add up the named component to the references registry.
|
||||
*
|
||||
* This would normally be done by the decoration handlers in the `DecorationAnalyzer`.
|
||||
*/
|
||||
function addToReferencesRegistry(
|
||||
program: ts.Program, registry: NgccReferencesRegistry, fileName: string,
|
||||
componentName: string) {
|
||||
const declaration = getDeclaration(program, fileName, componentName, ts.isClassDeclaration);
|
||||
registry.add(null !, new Reference(declaration));
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
import * as ts from 'typescript';
|
||||
|
||||
import {Reference} from '../../../src/ngtsc/imports';
|
||||
import {PartialEvaluator} from '../../../src/ngtsc/partial_evaluator';
|
||||
import {TypeScriptReflectionHost} from '../../../src/ngtsc/reflection';
|
||||
import {getDeclaration, makeProgram} from '../../../src/ngtsc/testing/in_memory_typescript';
|
||||
import {NgccReferencesRegistry} from '../../src/analysis/ngcc_references_registry';
|
||||
|
||||
describe('NgccReferencesRegistry', () => {
|
||||
it('should return a mapping from resolved reference identifiers to their declarations', () => {
|
||||
const {program, options, host} = makeProgram([{
|
||||
name: 'index.ts',
|
||||
contents: `
|
||||
export class SomeClass {}
|
||||
export function someFunction() {}
|
||||
export const someVariable = 42;
|
||||
|
||||
export const testArray = [SomeClass, someFunction, someVariable];
|
||||
`
|
||||
}]);
|
||||
|
||||
const checker = program.getTypeChecker();
|
||||
|
||||
const testArrayDeclaration =
|
||||
getDeclaration(program, 'index.ts', 'testArray', ts.isVariableDeclaration);
|
||||
const someClassDecl = getDeclaration(program, 'index.ts', 'SomeClass', ts.isClassDeclaration);
|
||||
const someFunctionDecl =
|
||||
getDeclaration(program, 'index.ts', 'someFunction', ts.isFunctionDeclaration);
|
||||
const someVariableDecl =
|
||||
getDeclaration(program, 'index.ts', 'someVariable', ts.isVariableDeclaration);
|
||||
const testArrayExpression = testArrayDeclaration.initializer !;
|
||||
|
||||
const reflectionHost = new TypeScriptReflectionHost(checker);
|
||||
const evaluator = new PartialEvaluator(reflectionHost, checker);
|
||||
const registry = new NgccReferencesRegistry(reflectionHost);
|
||||
|
||||
const references = (evaluator.evaluate(testArrayExpression) as any[])
|
||||
.filter(ref => ref instanceof Reference) as Reference<ts.Declaration>[];
|
||||
registry.add(null !, ...references);
|
||||
|
||||
const map = registry.getDeclarationMap();
|
||||
expect(map.size).toEqual(2);
|
||||
expect(map.get(someClassDecl.name !) !.node).toBe(someClassDecl);
|
||||
expect(map.get(someFunctionDecl.name !) !.node).toBe(someFunctionDecl);
|
||||
expect(map.has(someVariableDecl.name as ts.Identifier)).toBe(false);
|
||||
});
|
||||
});
|
@ -0,0 +1,76 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
import * as ts from 'typescript';
|
||||
|
||||
import {SwitchMarkerAnalyzer} from '../../src/analysis/switch_marker_analyzer';
|
||||
import {Esm2015ReflectionHost} from '../../src/host/esm2015_host';
|
||||
import {makeTestProgram} from '../helpers/utils';
|
||||
|
||||
const TEST_PROGRAM = [
|
||||
{
|
||||
name: 'entrypoint.js',
|
||||
contents: `
|
||||
import {a} from './a';
|
||||
import {b} from './b';
|
||||
`
|
||||
},
|
||||
{
|
||||
name: 'a.js',
|
||||
contents: `
|
||||
import {c} from './c';
|
||||
export const a = 1;
|
||||
`
|
||||
},
|
||||
{
|
||||
name: 'b.js',
|
||||
contents: `
|
||||
export const b = 42;
|
||||
var factoryB = factory__PRE_R3__;
|
||||
`
|
||||
},
|
||||
{
|
||||
name: 'c.js',
|
||||
contents: `
|
||||
export const c = 'So long, and thanks for all the fish!';
|
||||
var factoryC = factory__PRE_R3__;
|
||||
var factoryD = factory__PRE_R3__;
|
||||
`
|
||||
},
|
||||
];
|
||||
|
||||
describe('SwitchMarkerAnalyzer', () => {
|
||||
describe('analyzeProgram()', () => {
|
||||
it('should check for switchable markers in all the files of the program', () => {
|
||||
const program = makeTestProgram(...TEST_PROGRAM);
|
||||
const host = new Esm2015ReflectionHost(false, program.getTypeChecker());
|
||||
const analyzer = new SwitchMarkerAnalyzer(host);
|
||||
const analysis = analyzer.analyzeProgram(program);
|
||||
|
||||
const entrypoint = program.getSourceFile('entrypoint.js') !;
|
||||
const a = program.getSourceFile('a.js') !;
|
||||
const b = program.getSourceFile('b.js') !;
|
||||
const c = program.getSourceFile('c.js') !;
|
||||
|
||||
expect(analysis.size).toEqual(2);
|
||||
expect(analysis.has(entrypoint)).toBe(false);
|
||||
expect(analysis.has(a)).toBe(false);
|
||||
expect(analysis.has(b)).toBe(true);
|
||||
expect(analysis.get(b) !.sourceFile).toBe(b);
|
||||
expect(analysis.get(b) !.declarations.map(decl => decl.getText())).toEqual([
|
||||
'factoryB = factory__PRE_R3__'
|
||||
]);
|
||||
|
||||
expect(analysis.has(c)).toBe(true);
|
||||
expect(analysis.get(c) !.sourceFile).toBe(c);
|
||||
expect(analysis.get(c) !.declarations.map(decl => decl.getText())).toEqual([
|
||||
'factoryC = factory__PRE_R3__',
|
||||
'factoryD = factory__PRE_R3__',
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user