refactor(ivy): remove ngcc Parser
and use NgccReflectionHost
instead (#26082)
PR Close #26082
This commit is contained in:

committed by
Miško Hevery

parent
7f03528dbc
commit
26209fca49
@ -124,16 +124,20 @@ describe('Esm2015ReflectionHost', () => {
|
||||
const program = makeProgram(...DECORATED_FILES);
|
||||
const dtsMapper = new DtsMapper('/src', '/typings');
|
||||
const host = new Esm2015ReflectionHost(false, program.getTypeChecker(), dtsMapper);
|
||||
const decoratedFiles =
|
||||
host.findDecoratedFiles(program.getSourceFile(DECORATED_FILES[0].name) !);
|
||||
expect(decoratedFiles.length).toEqual(2);
|
||||
const primary = decoratedFiles[0];
|
||||
const primaryFile = program.getSourceFile(DECORATED_FILES[0].name) !;
|
||||
const secondaryFile = program.getSourceFile(DECORATED_FILES[1].name) !;
|
||||
const decoratedFiles = host.findDecoratedFiles(primaryFile);
|
||||
|
||||
expect(decoratedFiles.size).toEqual(2);
|
||||
|
||||
const primary = decoratedFiles.get(primaryFile) !;
|
||||
expect(primary.decoratedClasses.length).toEqual(1);
|
||||
const classA = primary.decoratedClasses.find(c => c.name === 'A') !;
|
||||
expect(classA.name).toEqual('A');
|
||||
expect(ts.isClassDeclaration(classA.declaration)).toBeTruthy();
|
||||
expect(classA.decorators.map(decorator => decorator.name)).toEqual(['Directive']);
|
||||
const secondary = decoratedFiles[1];
|
||||
|
||||
const secondary = decoratedFiles.get(secondaryFile) !;
|
||||
expect(secondary.decoratedClasses.length).toEqual(1);
|
||||
const classD = secondary.decoratedClasses.find(c => c.name === 'D') !;
|
||||
expect(classD.name).toEqual('D');
|
||||
|
@ -452,7 +452,7 @@ const DECORATED_FILES = [
|
||||
function C() {}
|
||||
return C;
|
||||
});
|
||||
export { A, x, C };
|
||||
export { A, x, C };
|
||||
export { D } from '/secondary';
|
||||
`
|
||||
},
|
||||
@ -467,7 +467,7 @@ const DECORATED_FILES = [
|
||||
];
|
||||
return D;
|
||||
}());
|
||||
export { D };
|
||||
export { D };
|
||||
`
|
||||
}
|
||||
];
|
||||
@ -1257,17 +1257,19 @@ describe('Esm5ReflectionHost', () => {
|
||||
() => {
|
||||
const program = makeProgram(...DECORATED_FILES);
|
||||
const host = new Esm5ReflectionHost(false, program.getTypeChecker());
|
||||
const decoratedFiles =
|
||||
host.findDecoratedFiles(program.getSourceFile(DECORATED_FILES[0].name) !);
|
||||
expect(decoratedFiles.length).toEqual(2);
|
||||
const primary = decoratedFiles[0];
|
||||
expect(primary.decoratedClasses.length).toEqual(1);
|
||||
const classA = primary.decoratedClasses[0];
|
||||
const primary = program.getSourceFile(DECORATED_FILES[0].name) !;
|
||||
const decoratedFiles = host.findDecoratedFiles(primary);
|
||||
expect(decoratedFiles.size).toEqual(2);
|
||||
const primaryClasses = decoratedFiles.get(primary) !.decoratedClasses;
|
||||
expect(primaryClasses.length).toEqual(1);
|
||||
const classA = primaryClasses.find(c => c.name === 'A') !;
|
||||
expect(classA.name).toEqual('A');
|
||||
expect(classA.decorators.map(decorator => decorator.name)).toEqual(['Directive']);
|
||||
const secondary = decoratedFiles[1];
|
||||
expect(secondary.decoratedClasses.length).toEqual(1);
|
||||
const classD = secondary.decoratedClasses[0];
|
||||
|
||||
const secondary = program.getSourceFile(DECORATED_FILES[1].name) !;
|
||||
const secondaryClasses = decoratedFiles.get(secondary) !.decoratedClasses;
|
||||
expect(secondaryClasses.length).toEqual(1);
|
||||
const classD = secondaryClasses.find(c => c.name === 'D') !;
|
||||
expect(classD.name).toEqual('D');
|
||||
expect(classD.decorators.map(decorator => decorator.name)).toEqual(['Directive']);
|
||||
});
|
||||
|
@ -1189,10 +1189,11 @@ describe('Fesm2015ReflectionHost', () => {
|
||||
() => {
|
||||
const program = makeProgram(DECORATED_FILE);
|
||||
const host = new Fesm2015ReflectionHost(false, program.getTypeChecker());
|
||||
const decoratedFiles =
|
||||
host.findDecoratedFiles(program.getSourceFile(DECORATED_FILE.name) !);
|
||||
expect(decoratedFiles.length).toEqual(1);
|
||||
const decoratedClasses = decoratedFiles[0].decoratedClasses;
|
||||
const primaryFile = program.getSourceFile(DECORATED_FILE.name) !;
|
||||
const decoratedFiles = host.findDecoratedFiles(primaryFile);
|
||||
|
||||
expect(decoratedFiles.size).toEqual(1);
|
||||
const decoratedClasses = decoratedFiles.get(primaryFile) !.decoratedClasses;
|
||||
expect(decoratedClasses.length).toEqual(2);
|
||||
|
||||
const decoratedClassA = decoratedClasses.find(c => c.name === 'A') !;
|
||||
|
@ -1,72 +0,0 @@
|
||||
/**
|
||||
* @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 {Fesm2015ReflectionHost} from '../../src/host/fesm2015_host';
|
||||
import {Esm2015FileParser} from '../../src/parsing/esm2015_parser';
|
||||
import {makeProgram} from '../helpers/utils';
|
||||
|
||||
const BASIC_FILE = {
|
||||
name: '/primary.js',
|
||||
contents: `
|
||||
import {Directive} from '@angular/core';
|
||||
class A {}
|
||||
A.decorators = [
|
||||
{ type: Directive, args: [{ selector: '[a]' }] }
|
||||
];
|
||||
|
||||
class B {}
|
||||
B.decorators = [
|
||||
{ type: Directive, args: [{ selector: '[b]' }] }
|
||||
];
|
||||
|
||||
function x() {}
|
||||
|
||||
function y() {}
|
||||
|
||||
class C {}
|
||||
|
||||
let D = class D {}
|
||||
D = tslib_1.__decorate([
|
||||
Directive({ selector: '[d]' }),
|
||||
OtherD()
|
||||
], D);
|
||||
export {D};
|
||||
|
||||
export { A, x, C };
|
||||
`
|
||||
};
|
||||
|
||||
describe('Esm2015FileParser', () => {
|
||||
describe('parseFile()', () => {
|
||||
it('should return an array of object for each class that is exported and decorated', () => {
|
||||
const program = makeProgram(BASIC_FILE);
|
||||
const host = new Fesm2015ReflectionHost(false, program.getTypeChecker());
|
||||
const parser = new Esm2015FileParser(program, host);
|
||||
|
||||
const parsedFiles = parser.parseFile(program.getSourceFile(BASIC_FILE.name) !);
|
||||
|
||||
expect(parsedFiles.length).toEqual(1);
|
||||
const decoratedClasses = parsedFiles[0].decoratedClasses;
|
||||
expect(decoratedClasses.length).toEqual(2);
|
||||
|
||||
const decoratedClassA = decoratedClasses.find(c => c.name === 'A') !;
|
||||
expect(decoratedClassA.decorators.map(decorator => decorator.name)).toEqual(['Directive']);
|
||||
expect(decoratedClassA.decorators.map(
|
||||
decorator => decorator.args && decorator.args.map(arg => arg.getText())))
|
||||
.toEqual([[`{ selector: '[a]' }`]]);
|
||||
|
||||
const decoratedClassD = decoratedClasses.find(c => c.name === 'D') !;
|
||||
expect(decoratedClassD.decorators.map(decorator => decorator.name)).toEqual(['Directive']);
|
||||
expect(decoratedClassD.decorators.map(
|
||||
decorator => decorator.args && decorator.args.map(arg => arg.getText())))
|
||||
.toEqual([[`{ selector: '[d]' }`]]);
|
||||
});
|
||||
});
|
||||
});
|
@ -1,65 +0,0 @@
|
||||
/**
|
||||
* @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 {Esm5ReflectionHost} from '../../src/host/esm5_host';
|
||||
import {Esm5FileParser} from '../../src/parsing/esm5_parser';
|
||||
import {makeProgram} from '../helpers/utils';
|
||||
|
||||
const BASIC_FILE = {
|
||||
name: '/primary.js',
|
||||
contents: `
|
||||
import {Directive} from '@angular/core';
|
||||
var A = (function() {
|
||||
function A() {}
|
||||
A.decorators = [
|
||||
{ type: Directive, args: [{ selector: '[a]' }] }
|
||||
];
|
||||
return A;
|
||||
}());
|
||||
|
||||
var B = (function() {
|
||||
function B() {}
|
||||
B.decorators = [
|
||||
{ type: Directive, args: [{ selector: '[b]' }] }
|
||||
];
|
||||
return B;
|
||||
}());
|
||||
|
||||
function x() {}
|
||||
|
||||
function y() {}
|
||||
|
||||
var C = (function() {
|
||||
function C() {}
|
||||
return C;
|
||||
});
|
||||
|
||||
export { A, x, C };
|
||||
`
|
||||
};
|
||||
|
||||
describe('Esm5FileParser', () => {
|
||||
describe('getDecoratedClasses()', () => {
|
||||
it('should return an array of object for each class that is exported and decorated', () => {
|
||||
const program = makeProgram(BASIC_FILE);
|
||||
const host = new Esm5ReflectionHost(false, program.getTypeChecker());
|
||||
const parser = new Esm5FileParser(program, host);
|
||||
|
||||
const parsedFiles = parser.parseFile(program.getSourceFile(BASIC_FILE.name) !);
|
||||
|
||||
expect(parsedFiles.length).toEqual(1);
|
||||
const decoratedClasses = parsedFiles[0].decoratedClasses;
|
||||
expect(decoratedClasses.length).toEqual(1);
|
||||
const decoratedClass = decoratedClasses[0];
|
||||
expect(decoratedClass.name).toEqual('A');
|
||||
expect(decoratedClass.decorators.map(decorator => decorator.name)).toEqual(['Directive']);
|
||||
});
|
||||
});
|
||||
});
|
@ -10,21 +10,19 @@ import MagicString from 'magic-string';
|
||||
import {makeProgram} from '../helpers/utils';
|
||||
import {Analyzer} from '../../src/analyzer';
|
||||
import {Fesm2015ReflectionHost} from '../../src/host/fesm2015_host';
|
||||
import {Esm2015FileParser} from '../../src/parsing/esm2015_parser';
|
||||
import {Esm2015Renderer} from '../../src/rendering/esm2015_renderer';
|
||||
|
||||
function setup(file: {name: string, contents: string}) {
|
||||
const program = makeProgram(file);
|
||||
const host = new Fesm2015ReflectionHost(false, program.getTypeChecker());
|
||||
const parser = new Esm2015FileParser(program, host);
|
||||
const analyzer = new Analyzer(program.getTypeChecker(), host, [''], false);
|
||||
const renderer = new Esm2015Renderer(host, false, null);
|
||||
return {analyzer, host, parser, program, renderer};
|
||||
return {analyzer, host, program, renderer};
|
||||
}
|
||||
|
||||
function analyze(parser: Esm2015FileParser, analyzer: Analyzer, file: ts.SourceFile) {
|
||||
const parsedFiles = parser.parseFile(file);
|
||||
return parsedFiles.map(file => analyzer.analyzeFile(file))[0];
|
||||
function analyze(host: Fesm2015ReflectionHost, analyzer: Analyzer, file: ts.SourceFile) {
|
||||
const decoratedFiles = host.findDecoratedFiles(file);
|
||||
return Array.from(decoratedFiles.values()).map(file => analyzer.analyzeFile(file))[0];
|
||||
}
|
||||
|
||||
const PROGRAM = {
|
||||
@ -159,8 +157,8 @@ export class A {}`);
|
||||
|
||||
describe('addDefinitions', () => {
|
||||
it('should insert the definitions directly after the class declaration', () => {
|
||||
const {analyzer, parser, program, renderer} = setup(PROGRAM);
|
||||
const analyzedFile = analyze(parser, analyzer, program.getSourceFile(PROGRAM.name) !);
|
||||
const {analyzer, host, program, renderer} = setup(PROGRAM);
|
||||
const analyzedFile = analyze(host, analyzer, program.getSourceFile(PROGRAM.name) !);
|
||||
const output = new MagicString(PROGRAM.contents);
|
||||
renderer.addDefinitions(output, analyzedFile.analyzedClasses[0], 'SOME DEFINITION TEXT');
|
||||
expect(output.toString()).toContain(`
|
||||
@ -177,8 +175,8 @@ A.decorators = [
|
||||
describe('[static property declaration]', () => {
|
||||
it('should delete the decorator (and following comma) that was matched in the analysis',
|
||||
() => {
|
||||
const {analyzer, parser, program, renderer} = setup(PROGRAM);
|
||||
const analyzedFile = analyze(parser, analyzer, program.getSourceFile(PROGRAM.name) !);
|
||||
const {analyzer, host, program, renderer} = setup(PROGRAM);
|
||||
const analyzedFile = analyze(host, analyzer, program.getSourceFile(PROGRAM.name) !);
|
||||
const output = new MagicString(PROGRAM.contents);
|
||||
const analyzedClass = analyzedFile.analyzedClasses[0];
|
||||
const decorator = analyzedClass.decorators[0];
|
||||
@ -196,8 +194,8 @@ A.decorators = [
|
||||
|
||||
it('should delete the decorator (but cope with no trailing comma) that was matched in the analysis',
|
||||
() => {
|
||||
const {analyzer, parser, program, renderer} = setup(PROGRAM);
|
||||
const analyzedFile = analyze(parser, analyzer, program.getSourceFile(PROGRAM.name) !);
|
||||
const {analyzer, host, program, renderer} = setup(PROGRAM);
|
||||
const analyzedFile = analyze(host, analyzer, program.getSourceFile(PROGRAM.name) !);
|
||||
const output = new MagicString(PROGRAM.contents);
|
||||
const analyzedClass = analyzedFile.analyzedClasses[1];
|
||||
const decorator = analyzedClass.decorators[0];
|
||||
@ -215,8 +213,8 @@ A.decorators = [
|
||||
|
||||
it('should delete the decorator (and its container if there are no other decorators left) that was matched in the analysis',
|
||||
() => {
|
||||
const {analyzer, parser, program, renderer} = setup(PROGRAM);
|
||||
const analyzedFile = analyze(parser, analyzer, program.getSourceFile(PROGRAM.name) !);
|
||||
const {analyzer, host, program, renderer} = setup(PROGRAM);
|
||||
const analyzedFile = analyze(host, analyzer, program.getSourceFile(PROGRAM.name) !);
|
||||
const output = new MagicString(PROGRAM.contents);
|
||||
const analyzedClass = analyzedFile.analyzedClasses[2];
|
||||
const decorator = analyzedClass.decorators[0];
|
||||
@ -236,9 +234,9 @@ A.decorators = [
|
||||
|
||||
describe('[__decorate declarations]', () => {
|
||||
it('should delete the decorator (and following comma) that was matched in the analysis', () => {
|
||||
const {analyzer, parser, program, renderer} = setup(PROGRAM_DECORATE_HELPER);
|
||||
const {analyzer, host, program, renderer} = setup(PROGRAM_DECORATE_HELPER);
|
||||
const analyzedFile =
|
||||
analyze(parser, analyzer, program.getSourceFile(PROGRAM_DECORATE_HELPER.name) !);
|
||||
analyze(host, analyzer, program.getSourceFile(PROGRAM_DECORATE_HELPER.name) !);
|
||||
const output = new MagicString(PROGRAM_DECORATE_HELPER.contents);
|
||||
const analyzedClass = analyzedFile.analyzedClasses.find(c => c.name === 'A') !;
|
||||
const decorator = analyzedClass.decorators.find(d => d.name === 'Directive') !;
|
||||
@ -254,9 +252,9 @@ A.decorators = [
|
||||
|
||||
it('should delete the decorator (but cope with no trailing comma) that was matched in the analysis',
|
||||
() => {
|
||||
const {analyzer, parser, program, renderer} = setup(PROGRAM_DECORATE_HELPER);
|
||||
const {analyzer, host, program, renderer} = setup(PROGRAM_DECORATE_HELPER);
|
||||
const analyzedFile =
|
||||
analyze(parser, analyzer, program.getSourceFile(PROGRAM_DECORATE_HELPER.name) !);
|
||||
analyze(host, analyzer, program.getSourceFile(PROGRAM_DECORATE_HELPER.name) !);
|
||||
const output = new MagicString(PROGRAM_DECORATE_HELPER.contents);
|
||||
const analyzedClass = analyzedFile.analyzedClasses.find(c => c.name === 'B') !;
|
||||
const decorator = analyzedClass.decorators.find(d => d.name === 'Directive') !;
|
||||
@ -273,9 +271,9 @@ A.decorators = [
|
||||
|
||||
it('should delete the decorator (and its container if there are not other decorators left) that was matched in the analysis',
|
||||
() => {
|
||||
const {analyzer, parser, program, renderer} = setup(PROGRAM_DECORATE_HELPER);
|
||||
const {analyzer, host, program, renderer} = setup(PROGRAM_DECORATE_HELPER);
|
||||
const analyzedFile =
|
||||
analyze(parser, analyzer, program.getSourceFile(PROGRAM_DECORATE_HELPER.name) !);
|
||||
analyze(host, analyzer, program.getSourceFile(PROGRAM_DECORATE_HELPER.name) !);
|
||||
const output = new MagicString(PROGRAM_DECORATE_HELPER.contents);
|
||||
const analyzedClass = analyzedFile.analyzedClasses.find(c => c.name === 'C') !;
|
||||
const decorator = analyzedClass.decorators.find(d => d.name === 'Directive') !;
|
||||
|
@ -10,21 +10,19 @@ import MagicString from 'magic-string';
|
||||
import {makeProgram} from '../helpers/utils';
|
||||
import {Analyzer} from '../../src/analyzer';
|
||||
import {Esm5ReflectionHost} from '../../src/host/esm5_host';
|
||||
import {Esm5FileParser} from '../../src/parsing/esm5_parser';
|
||||
import {Esm5Renderer} from '../../src/rendering/esm5_renderer';
|
||||
|
||||
function setup(file: {name: string, contents: string}) {
|
||||
const program = makeProgram(file);
|
||||
const host = new Esm5ReflectionHost(false, program.getTypeChecker());
|
||||
const parser = new Esm5FileParser(program, host);
|
||||
const analyzer = new Analyzer(program.getTypeChecker(), host, [''], false);
|
||||
const renderer = new Esm5Renderer(host, false, null);
|
||||
return {analyzer, host, parser, program, renderer};
|
||||
return {analyzer, host, program, renderer};
|
||||
}
|
||||
|
||||
function analyze(parser: Esm5FileParser, analyzer: Analyzer, file: ts.SourceFile) {
|
||||
const parsedFiles = parser.parseFile(file);
|
||||
return parsedFiles.map(file => analyzer.analyzeFile(file))[0];
|
||||
function analyze(host: Esm5ReflectionHost, analyzer: Analyzer, file: ts.SourceFile) {
|
||||
const decoratedFiles = host.findDecoratedFiles(file);
|
||||
return Array.from(decoratedFiles.values()).map(file => analyzer.analyzeFile(file))[0];
|
||||
}
|
||||
|
||||
const PROGRAM = {
|
||||
@ -184,8 +182,8 @@ var A = (function() {`);
|
||||
|
||||
describe('addDefinitions', () => {
|
||||
it('should insert the definitions directly after the class declaration', () => {
|
||||
const {analyzer, parser, program, renderer} = setup(PROGRAM);
|
||||
const analyzedFile = analyze(parser, analyzer, program.getSourceFile(PROGRAM.name) !);
|
||||
const {analyzer, host, program, renderer} = setup(PROGRAM);
|
||||
const analyzedFile = analyze(host, analyzer, program.getSourceFile(PROGRAM.name) !);
|
||||
const output = new MagicString(PROGRAM.contents);
|
||||
renderer.addDefinitions(output, analyzedFile.analyzedClasses[0], 'SOME DEFINITION TEXT');
|
||||
expect(output.toString()).toContain(`
|
||||
@ -201,8 +199,8 @@ SOME DEFINITION TEXT
|
||||
describe('removeDecorators', () => {
|
||||
|
||||
it('should delete the decorator (and following comma) that was matched in the analysis', () => {
|
||||
const {analyzer, parser, program, renderer} = setup(PROGRAM);
|
||||
const analyzedFile = analyze(parser, analyzer, program.getSourceFile(PROGRAM.name) !);
|
||||
const {analyzer, host, program, renderer} = setup(PROGRAM);
|
||||
const analyzedFile = analyze(host, analyzer, program.getSourceFile(PROGRAM.name) !);
|
||||
const output = new MagicString(PROGRAM.contents);
|
||||
const analyzedClass = analyzedFile.analyzedClasses[0];
|
||||
const decorator = analyzedClass.decorators[0];
|
||||
@ -219,8 +217,8 @@ SOME DEFINITION TEXT
|
||||
|
||||
it('should delete the decorator (but cope with no trailing comma) that was matched in the analysis',
|
||||
() => {
|
||||
const {analyzer, parser, program, renderer} = setup(PROGRAM);
|
||||
const analyzedFile = analyze(parser, analyzer, program.getSourceFile(PROGRAM.name) !);
|
||||
const {analyzer, host, program, renderer} = setup(PROGRAM);
|
||||
const analyzedFile = analyze(host, analyzer, program.getSourceFile(PROGRAM.name) !);
|
||||
const output = new MagicString(PROGRAM.contents);
|
||||
const analyzedClass = analyzedFile.analyzedClasses[1];
|
||||
const decorator = analyzedClass.decorators[0];
|
||||
@ -238,8 +236,8 @@ SOME DEFINITION TEXT
|
||||
|
||||
it('should delete the decorator (and its container if there are not other decorators left) that was matched in the analysis',
|
||||
() => {
|
||||
const {analyzer, parser, program, renderer} = setup(PROGRAM);
|
||||
const analyzedFile = analyze(parser, analyzer, program.getSourceFile(PROGRAM.name) !);
|
||||
const {analyzer, host, program, renderer} = setup(PROGRAM);
|
||||
const analyzedFile = analyze(host, analyzer, program.getSourceFile(PROGRAM.name) !);
|
||||
const output = new MagicString(PROGRAM.contents);
|
||||
const analyzedClass = analyzedFile.analyzedClasses[2];
|
||||
const decorator = analyzedClass.decorators[0];
|
||||
@ -259,9 +257,9 @@ SOME DEFINITION TEXT
|
||||
|
||||
describe('[__decorate declarations]', () => {
|
||||
it('should delete the decorator (and following comma) that was matched in the analysis', () => {
|
||||
const {analyzer, parser, program, renderer} = setup(PROGRAM_DECORATE_HELPER);
|
||||
const {analyzer, host, program, renderer} = setup(PROGRAM_DECORATE_HELPER);
|
||||
const analyzedFile =
|
||||
analyze(parser, analyzer, program.getSourceFile(PROGRAM_DECORATE_HELPER.name) !);
|
||||
analyze(host, analyzer, program.getSourceFile(PROGRAM_DECORATE_HELPER.name) !);
|
||||
const output = new MagicString(PROGRAM_DECORATE_HELPER.contents);
|
||||
const analyzedClass = analyzedFile.analyzedClasses.find(c => c.name === 'A') !;
|
||||
const decorator = analyzedClass.decorators.find(d => d.name === 'Directive') !;
|
||||
@ -277,9 +275,9 @@ SOME DEFINITION TEXT
|
||||
|
||||
it('should delete the decorator (but cope with no trailing comma) that was matched in the analysis',
|
||||
() => {
|
||||
const {analyzer, parser, program, renderer} = setup(PROGRAM_DECORATE_HELPER);
|
||||
const {analyzer, host, program, renderer} = setup(PROGRAM_DECORATE_HELPER);
|
||||
const analyzedFile =
|
||||
analyze(parser, analyzer, program.getSourceFile(PROGRAM_DECORATE_HELPER.name) !);
|
||||
analyze(host, analyzer, program.getSourceFile(PROGRAM_DECORATE_HELPER.name) !);
|
||||
const output = new MagicString(PROGRAM_DECORATE_HELPER.contents);
|
||||
const analyzedClass = analyzedFile.analyzedClasses.find(c => c.name === 'B') !;
|
||||
const decorator = analyzedClass.decorators.find(d => d.name === 'Directive') !;
|
||||
@ -296,9 +294,9 @@ SOME DEFINITION TEXT
|
||||
|
||||
it('should delete the decorator (and its container if there are no other decorators left) that was matched in the analysis',
|
||||
() => {
|
||||
const {analyzer, parser, program, renderer} = setup(PROGRAM_DECORATE_HELPER);
|
||||
const {analyzer, host, program, renderer} = setup(PROGRAM_DECORATE_HELPER);
|
||||
const analyzedFile =
|
||||
analyze(parser, analyzer, program.getSourceFile(PROGRAM_DECORATE_HELPER.name) !);
|
||||
analyze(host, analyzer, program.getSourceFile(PROGRAM_DECORATE_HELPER.name) !);
|
||||
const output = new MagicString(PROGRAM_DECORATE_HELPER.contents);
|
||||
const analyzedClass = analyzedFile.analyzedClasses.find(c => c.name === 'C') !;
|
||||
const decorator = analyzedClass.decorators.find(d => d.name === 'Directive') !;
|
||||
|
@ -13,7 +13,6 @@ import {fromObject, generateMapFileComment} from 'convert-source-map';
|
||||
import {makeProgram} from '../helpers/utils';
|
||||
import {AnalyzedClass, Analyzer} from '../../src/analyzer';
|
||||
import {Fesm2015ReflectionHost} from '../../src/host/fesm2015_host';
|
||||
import {Esm2015FileParser} from '../../src/parsing/esm2015_parser';
|
||||
import {Renderer} from '../../src/rendering/renderer';
|
||||
|
||||
class TestRenderer extends Renderer {
|
||||
@ -45,13 +44,15 @@ function createTestRenderer() {
|
||||
function analyze(file: {name: string, contents: string}) {
|
||||
const program = makeProgram(file);
|
||||
const host = new Fesm2015ReflectionHost(false, program.getTypeChecker());
|
||||
const parser = new Esm2015FileParser(program, host);
|
||||
const analyzer = new Analyzer(program.getTypeChecker(), host, [''], false);
|
||||
|
||||
const parsedFiles = parser.parseFile(program.getSourceFile(file.name) !);
|
||||
return parsedFiles.map(file => analyzer.analyzeFile(file));
|
||||
const decoratedFiles = host.findDecoratedFiles(program.getSourceFile(file.name) !);
|
||||
const analyzedFiles = Array.from(decoratedFiles.values()).map(file => analyzer.analyzeFile(file));
|
||||
|
||||
return {program, host, analyzer, decoratedFiles, analyzedFiles};
|
||||
}
|
||||
|
||||
|
||||
describe('Renderer', () => {
|
||||
const INPUT_PROGRAM = {
|
||||
name: '/file.js',
|
||||
@ -99,7 +100,7 @@ describe('Renderer', () => {
|
||||
it('should render the modified contents; and a new map file, if the original provided no map file.',
|
||||
() => {
|
||||
const renderer = createTestRenderer();
|
||||
const analyzedFiles = analyze(INPUT_PROGRAM);
|
||||
const {analyzedFiles} = analyze(INPUT_PROGRAM);
|
||||
const result = renderer.renderFile(analyzedFiles[0], '/output_file.js');
|
||||
expect(result.source.path).toEqual('/output_file.js');
|
||||
expect(result.source.contents)
|
||||
@ -111,7 +112,7 @@ describe('Renderer', () => {
|
||||
it('should call addImports with the source code and info about the core Angular library.',
|
||||
() => {
|
||||
const renderer = createTestRenderer();
|
||||
const analyzedFiles = analyze(INPUT_PROGRAM);
|
||||
const {analyzedFiles} = analyze(INPUT_PROGRAM);
|
||||
renderer.renderFile(analyzedFiles[0], '/output_file.js');
|
||||
expect(renderer.addImports.calls.first().args[0].toString()).toEqual(RENDERED_CONTENTS);
|
||||
expect(renderer.addImports.calls.first().args[1]).toEqual([
|
||||
@ -122,12 +123,12 @@ describe('Renderer', () => {
|
||||
it('should call addDefinitions with the source code, the analyzed class and the renderered definitions.',
|
||||
() => {
|
||||
const renderer = createTestRenderer();
|
||||
const analyzedFile = analyze(INPUT_PROGRAM)[0];
|
||||
renderer.renderFile(analyzedFile, '/output_file.js');
|
||||
const {analyzedFiles} = analyze(INPUT_PROGRAM);
|
||||
renderer.renderFile(analyzedFiles[0], '/output_file.js');
|
||||
expect(renderer.addDefinitions.calls.first().args[0].toString())
|
||||
.toEqual(RENDERED_CONTENTS);
|
||||
expect(renderer.addDefinitions.calls.first().args[1])
|
||||
.toBe(analyzedFile.analyzedClasses[0]);
|
||||
.toBe(analyzedFiles[0].analyzedClasses[0]);
|
||||
expect(renderer.addDefinitions.calls.first().args[2])
|
||||
.toEqual(
|
||||
`A.ngDirectiveDef = ɵngcc0.ɵdefineDirective({ type: A, selectors: [["", "a", ""]], factory: function A_Factory(t) { return new (t || A)(); }, features: [ɵngcc0.ɵPublicFeature] });`);
|
||||
@ -136,8 +137,8 @@ describe('Renderer', () => {
|
||||
it('should call removeDecorators with the source code, a map of class decorators that have been analyzed',
|
||||
() => {
|
||||
const renderer = createTestRenderer();
|
||||
const analyzedFile = analyze(INPUT_PROGRAM)[0];
|
||||
renderer.renderFile(analyzedFile, '/output_file.js');
|
||||
const {analyzedFiles} = analyze(INPUT_PROGRAM);
|
||||
renderer.renderFile(analyzedFiles[0], '/output_file.js');
|
||||
expect(renderer.removeDecorators.calls.first().args[0].toString())
|
||||
.toEqual(RENDERED_CONTENTS);
|
||||
|
||||
@ -157,7 +158,7 @@ describe('Renderer', () => {
|
||||
it('should merge any inline source map from the original file and write the output as an inline source map',
|
||||
() => {
|
||||
const renderer = createTestRenderer();
|
||||
const analyzedFiles = analyze({
|
||||
const {analyzedFiles} = analyze({
|
||||
...INPUT_PROGRAM,
|
||||
contents: INPUT_PROGRAM.contents + '\n' + INPUT_PROGRAM_MAP.toComment()
|
||||
});
|
||||
@ -174,7 +175,7 @@ describe('Renderer', () => {
|
||||
const readFileSyncSpy =
|
||||
spyOn(fs, 'readFileSync').and.returnValue(INPUT_PROGRAM_MAP.toJSON());
|
||||
const renderer = createTestRenderer();
|
||||
const analyzedFiles = analyze({
|
||||
const {analyzedFiles} = analyze({
|
||||
...INPUT_PROGRAM,
|
||||
contents: INPUT_PROGRAM.contents + '\n//# sourceMappingURL=file.js.map'
|
||||
});
|
||||
|
Reference in New Issue
Block a user