refactor(ivy): ngcc - DecorationAnalyzer
acts on whole program (#26082)
PR Close #26082
This commit is contained in:

committed by
Miško Hevery

parent
9562324ea4
commit
f7b17a4784
@ -9,23 +9,21 @@ import * as ts from 'typescript';
|
||||
|
||||
import {Decorator} from '../../../ngtsc/host';
|
||||
import {DecoratorHandler} from '../../../ngtsc/transform';
|
||||
import {DecorationAnalysis, DecorationAnalyzer} from '../../src/analysis/decoration_analyzer';
|
||||
import {DecoratedClass} from '../../src/host/decorated_class';
|
||||
import {DecoratedFile} from '../../src/host/decorated_file';
|
||||
import {DecorationAnalyses, DecorationAnalyzer} from '../../src/analysis/decoration_analyzer';
|
||||
import {Fesm2015ReflectionHost} from '../../src/host/fesm2015_host';
|
||||
|
||||
import {getDeclaration, makeProgram} from '../helpers/utils';
|
||||
import {makeProgram} from '../helpers/utils';
|
||||
|
||||
const TEST_PROGRAM = {
|
||||
name: 'test.js',
|
||||
contents: `
|
||||
import {Component, Injectable} from '@angular/core';
|
||||
|
||||
@Component()
|
||||
export class MyComponent {}
|
||||
MyComponent.decorators = [{type: Component}];
|
||||
|
||||
@Injectable()
|
||||
export class MyService {}
|
||||
MyService.decorators = [{type: Injectable}];
|
||||
`
|
||||
};
|
||||
|
||||
@ -50,49 +48,26 @@ function createTestHandler() {
|
||||
return handler;
|
||||
}
|
||||
|
||||
function createParsedFile(program: ts.Program) {
|
||||
const file = new DecoratedFile(program.getSourceFile('test.js') !);
|
||||
|
||||
const componentClass = getDeclaration(program, 'test.js', 'MyComponent', ts.isClassDeclaration);
|
||||
file.decoratedClasses.push(
|
||||
new DecoratedClass('MyComponent', {} as any, [{
|
||||
name: 'Component',
|
||||
import: {from: '@angular/core', name: 'Component'},
|
||||
node: null as any,
|
||||
args: null
|
||||
}]));
|
||||
|
||||
const serviceClass = getDeclaration(program, 'test.js', 'MyService', ts.isClassDeclaration);
|
||||
file.decoratedClasses.push(
|
||||
new DecoratedClass('MyService', {} as any, [{
|
||||
name: 'Injectable',
|
||||
import: {from: '@angular/core', name: 'Injectable'},
|
||||
node: null as any,
|
||||
args: null
|
||||
}]));
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
describe('DecorationAnalyzer', () => {
|
||||
describe('analyzeFile()', () => {
|
||||
describe('analyzeProgram()', () => {
|
||||
let program: ts.Program;
|
||||
let testHandler: jasmine.SpyObj<DecoratorHandler<any, any>>;
|
||||
let result: DecorationAnalysis;
|
||||
let result: DecorationAnalyses;
|
||||
|
||||
beforeEach(() => {
|
||||
program = makeProgram(TEST_PROGRAM);
|
||||
const file = createParsedFile(program);
|
||||
const analyzer = new DecorationAnalyzer(
|
||||
program.getTypeChecker(), new Fesm2015ReflectionHost(false, program.getTypeChecker()),
|
||||
[''], false);
|
||||
testHandler = createTestHandler();
|
||||
analyzer.handlers = [testHandler];
|
||||
result = analyzer.analyzeFile(file);
|
||||
result = analyzer.analyzeProgram(program);
|
||||
});
|
||||
|
||||
it('should return an object containing a reference to the original source file',
|
||||
() => { expect(result.sourceFile).toBe(program.getSourceFile('test.js') !); });
|
||||
it('should return an object containing a reference to the original source file', () => {
|
||||
const file = program.getSourceFile(TEST_PROGRAM.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(2);
|
||||
@ -103,8 +78,10 @@ describe('DecorationAnalyzer', () => {
|
||||
});
|
||||
|
||||
it('should return an object containing the classes that were analyzed', () => {
|
||||
expect(result.analyzedClasses.length).toEqual(1);
|
||||
expect(result.analyzedClasses[0].name).toEqual('MyComponent');
|
||||
const file = program.getSourceFile(TEST_PROGRAM.name) !;
|
||||
const analysis = result.get(file) !;
|
||||
expect(analysis.analyzedClasses.length).toEqual(1);
|
||||
expect(analysis.analyzedClasses[0].name).toEqual('MyComponent');
|
||||
});
|
||||
|
||||
it('should analyze and compile the classes that are detected', () => {
|
||||
|
Reference in New Issue
Block a user