refactor(ivy): move and rename Analyzer
to DecorationAnalyzer
(#26082)
This is in preparation for adding in other kinds of Analyzer. PR Close #26082
This commit is contained in:
parent
64c96186da
commit
880c0add56
@ -9,13 +9,13 @@ import {ConstantPool} from '@angular/compiler';
|
|||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as ts from 'typescript';
|
import * as ts from 'typescript';
|
||||||
|
|
||||||
import {BaseDefDecoratorHandler, ComponentDecoratorHandler, DirectiveDecoratorHandler, InjectableDecoratorHandler, NgModuleDecoratorHandler, PipeDecoratorHandler, ResourceLoader, SelectorScopeRegistry} from '../../ngtsc/annotations';
|
import {BaseDefDecoratorHandler, ComponentDecoratorHandler, DirectiveDecoratorHandler, InjectableDecoratorHandler, NgModuleDecoratorHandler, PipeDecoratorHandler, ResourceLoader, SelectorScopeRegistry} from '../../../ngtsc/annotations';
|
||||||
import {CompileResult, DecoratorHandler} from '../../ngtsc/transform';
|
import {CompileResult, DecoratorHandler} from '../../../ngtsc/transform';
|
||||||
|
|
||||||
import {DecoratedClass} from './host/decorated_class';
|
import {DecoratedClass} from '../host/decorated_class';
|
||||||
import {DecoratedFile} from './host/decorated_file';
|
import {DecoratedFile} from '../host/decorated_file';
|
||||||
import {NgccReflectionHost} from './host/ngcc_host';
|
import {NgccReflectionHost} from '../host/ngcc_host';
|
||||||
import {isDefined} from './utils';
|
import {isDefined} from '../utils';
|
||||||
|
|
||||||
export interface AnalyzedClass<A = any, M = any> extends DecoratedClass {
|
export interface AnalyzedClass<A = any, M = any> extends DecoratedClass {
|
||||||
handler: DecoratorHandler<A, M>;
|
handler: DecoratorHandler<A, M>;
|
||||||
@ -24,7 +24,7 @@ export interface AnalyzedClass<A = any, M = any> extends DecoratedClass {
|
|||||||
compilation: CompileResult[];
|
compilation: CompileResult[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AnalyzedFile {
|
export interface DecorationAnalysis {
|
||||||
analyzedClasses: AnalyzedClass[];
|
analyzedClasses: AnalyzedClass[];
|
||||||
sourceFile: ts.SourceFile;
|
sourceFile: ts.SourceFile;
|
||||||
constantPool: ConstantPool;
|
constantPool: ConstantPool;
|
||||||
@ -42,7 +42,10 @@ export class FileResourceLoader implements ResourceLoader {
|
|||||||
load(url: string): string { return fs.readFileSync(url, 'utf8'); }
|
load(url: string): string { return fs.readFileSync(url, 'utf8'); }
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Analyzer {
|
/**
|
||||||
|
* This Analyzer will analyze the files that have decorated classes that need to be transformed.
|
||||||
|
*/
|
||||||
|
export class DecorationAnalyzer {
|
||||||
resourceLoader = new FileResourceLoader();
|
resourceLoader = new FileResourceLoader();
|
||||||
scopeRegistry = new SelectorScopeRegistry(this.typeChecker, this.host);
|
scopeRegistry = new SelectorScopeRegistry(this.typeChecker, this.host);
|
||||||
handlers: DecoratorHandler<any, any>[] = [
|
handlers: DecoratorHandler<any, any>[] = [
|
||||||
@ -65,7 +68,7 @@ export class Analyzer {
|
|||||||
* should be converted to use ivy definitions.
|
* should be converted to use ivy definitions.
|
||||||
* @param file The file to be analysed for decorated classes.
|
* @param file The file to be analysed for decorated classes.
|
||||||
*/
|
*/
|
||||||
analyzeFile(file: DecoratedFile): AnalyzedFile {
|
analyzeFile(file: DecoratedFile): DecorationAnalysis {
|
||||||
const constantPool = new ConstantPool();
|
const constantPool = new ConstantPool();
|
||||||
const analyzedClasses =
|
const analyzedClasses =
|
||||||
file.decoratedClasses.map(clazz => this.analyzeClass(constantPool, clazz))
|
file.decoratedClasses.map(clazz => this.analyzeClass(constantPool, clazz))
|
@ -11,7 +11,7 @@ import {mkdir, mv} from 'shelljs';
|
|||||||
import * as ts from 'typescript';
|
import * as ts from 'typescript';
|
||||||
|
|
||||||
import {DtsFileTransformer} from '../../../ngtsc/transform';
|
import {DtsFileTransformer} from '../../../ngtsc/transform';
|
||||||
import {AnalyzedFile, Analyzer} from '../analyzer';
|
import {DecorationAnalysis, DecorationAnalyzer} from '../analysis/decoration_analyzer';
|
||||||
import {IMPORT_PREFIX} from '../constants';
|
import {IMPORT_PREFIX} from '../constants';
|
||||||
import {DtsMapper} from '../host/dts_mapper';
|
import {DtsMapper} from '../host/dts_mapper';
|
||||||
import {Esm2015ReflectionHost} from '../host/esm2015_host';
|
import {Esm2015ReflectionHost} from '../host/esm2015_host';
|
||||||
@ -77,7 +77,7 @@ export class Transformer {
|
|||||||
const reflectionHost = this.getHost(isCore, format, packageProgram, dtsMapper);
|
const reflectionHost = this.getHost(isCore, format, packageProgram, dtsMapper);
|
||||||
const r3SymbolsFile = r3SymbolsPath && packageProgram.getSourceFile(r3SymbolsPath) || null;
|
const r3SymbolsFile = r3SymbolsPath && packageProgram.getSourceFile(r3SymbolsPath) || null;
|
||||||
|
|
||||||
const analyzer = new Analyzer(typeChecker, reflectionHost, rootDirs, isCore);
|
const analyzer = new DecorationAnalyzer(typeChecker, reflectionHost, rootDirs, isCore);
|
||||||
const renderer =
|
const renderer =
|
||||||
this.getRenderer(format, packageProgram, reflectionHost, isCore, r3SymbolsFile);
|
this.getRenderer(format, packageProgram, reflectionHost, isCore, r3SymbolsFile);
|
||||||
|
|
||||||
@ -147,7 +147,7 @@ export class Transformer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
transformDtsFiles(
|
transformDtsFiles(
|
||||||
analyzedFiles: AnalyzedFile[], sourceNodeModules: string, targetNodeModules: string,
|
analyzedFiles: DecorationAnalysis[], sourceNodeModules: string, targetNodeModules: string,
|
||||||
dtsMapper: DtsMapper): FileInfo[] {
|
dtsMapper: DtsMapper): FileInfo[] {
|
||||||
const outputFiles: FileInfo[] = [];
|
const outputFiles: FileInfo[] = [];
|
||||||
|
|
||||||
@ -177,7 +177,7 @@ export class Transformer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
transformSourceFiles(
|
transformSourceFiles(
|
||||||
analyzedFiles: AnalyzedFile[], sourceNodeModules: string, targetNodeModules: string,
|
analyzedFiles: DecorationAnalysis[], sourceNodeModules: string, targetNodeModules: string,
|
||||||
renderer: Renderer): FileInfo[] {
|
renderer: Renderer): FileInfo[] {
|
||||||
const outputFiles: FileInfo[] = [];
|
const outputFiles: FileInfo[] = [];
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
import * as ts from 'typescript';
|
import * as ts from 'typescript';
|
||||||
import MagicString from 'magic-string';
|
import MagicString from 'magic-string';
|
||||||
import {POST_NGCC_MARKER, PRE_NGCC_MARKER} from '../host/ngcc_host';
|
import {POST_NGCC_MARKER, PRE_NGCC_MARKER} from '../host/ngcc_host';
|
||||||
import {AnalyzedClass} from '../analyzer';
|
import {AnalyzedClass} from '../analysis/decoration_analyzer';
|
||||||
import {Renderer} from './renderer';
|
import {Renderer} from './renderer';
|
||||||
|
|
||||||
export class Esm2015Renderer extends Renderer {
|
export class Esm2015Renderer extends Renderer {
|
||||||
|
@ -15,10 +15,10 @@ import * as ts from 'typescript';
|
|||||||
|
|
||||||
import {Decorator} from '../../../ngtsc/host';
|
import {Decorator} from '../../../ngtsc/host';
|
||||||
import {translateStatement} from '../../../ngtsc/translator';
|
import {translateStatement} from '../../../ngtsc/translator';
|
||||||
import {NgccImportManager} from './ngcc_import_manager';
|
import {AnalyzedClass, DecorationAnalysis} from '../analysis/decoration_analyzer';
|
||||||
import {AnalyzedClass, AnalyzedFile} from '../analyzer';
|
|
||||||
import {IMPORT_PREFIX} from '../constants';
|
import {IMPORT_PREFIX} from '../constants';
|
||||||
import {NgccReflectionHost} from '../host/ngcc_host';
|
import {NgccReflectionHost} from '../host/ngcc_host';
|
||||||
|
import {NgccImportManager} from './ngcc_import_manager';
|
||||||
|
|
||||||
interface SourceMapInfo {
|
interface SourceMapInfo {
|
||||||
source: string;
|
source: string;
|
||||||
@ -33,7 +33,7 @@ export interface RenderResult {
|
|||||||
/**
|
/**
|
||||||
* The file that has been rendered.
|
* The file that has been rendered.
|
||||||
*/
|
*/
|
||||||
file: AnalyzedFile;
|
file: DecorationAnalysis;
|
||||||
/**
|
/**
|
||||||
* The rendered source file.
|
* The rendered source file.
|
||||||
*/
|
*/
|
||||||
@ -74,7 +74,7 @@ export abstract class Renderer {
|
|||||||
* @param file The analyzed file to render.
|
* @param file The analyzed file to render.
|
||||||
* @param targetPath The absolute path where the rendered file will be written.
|
* @param targetPath The absolute path where the rendered file will be written.
|
||||||
*/
|
*/
|
||||||
renderFile(file: AnalyzedFile, targetPath: string): RenderResult {
|
renderFile(file: DecorationAnalysis, targetPath: string): RenderResult {
|
||||||
const importManager =
|
const importManager =
|
||||||
new NgccImportManager(!this.rewriteCoreImportsTo, this.isCore, IMPORT_PREFIX);
|
new NgccImportManager(!this.rewriteCoreImportsTo, this.isCore, IMPORT_PREFIX);
|
||||||
const input = this.extractSourceMap(file.sourceFile);
|
const input = this.extractSourceMap(file.sourceFile);
|
||||||
@ -180,7 +180,7 @@ export abstract class Renderer {
|
|||||||
* with an appropriate source-map comment pointing to the merged source-map.
|
* with an appropriate source-map comment pointing to the merged source-map.
|
||||||
*/
|
*/
|
||||||
protected renderSourceAndMap(
|
protected renderSourceAndMap(
|
||||||
file: AnalyzedFile, input: SourceMapInfo, output: MagicString,
|
file: DecorationAnalysis, input: SourceMapInfo, output: MagicString,
|
||||||
outputPath: string): RenderResult {
|
outputPath: string): RenderResult {
|
||||||
const outputMapPath = `${outputPath}.map`;
|
const outputMapPath = `${outputPath}.map`;
|
||||||
const outputMap = output.generateMap({
|
const outputMap = output.generateMap({
|
||||||
|
@ -7,14 +7,14 @@
|
|||||||
*/
|
*/
|
||||||
import * as ts from 'typescript';
|
import * as ts from 'typescript';
|
||||||
|
|
||||||
import {Decorator} from '../../ngtsc/host';
|
import {Decorator} from '../../../ngtsc/host';
|
||||||
import {DecoratorHandler} from '../../ngtsc/transform';
|
import {DecoratorHandler} from '../../../ngtsc/transform';
|
||||||
import {AnalyzedFile, Analyzer} from '../src/analyzer';
|
import {DecorationAnalysis, DecorationAnalyzer} from '../../src/analysis/decoration_analyzer';
|
||||||
import {DecoratedClass} from '../src/host/decorated_class';
|
import {DecoratedClass} from '../../src/host/decorated_class';
|
||||||
import {DecoratedFile} from '../src/host/decorated_file';
|
import {DecoratedFile} from '../../src/host/decorated_file';
|
||||||
import {Fesm2015ReflectionHost} from '../src/host/fesm2015_host';
|
import {Fesm2015ReflectionHost} from '../../src/host/fesm2015_host';
|
||||||
|
|
||||||
import {getDeclaration, makeProgram} from './helpers/utils';
|
import {getDeclaration, makeProgram} from '../helpers/utils';
|
||||||
|
|
||||||
const TEST_PROGRAM = {
|
const TEST_PROGRAM = {
|
||||||
name: 'test.js',
|
name: 'test.js',
|
||||||
@ -74,16 +74,16 @@ function createParsedFile(program: ts.Program) {
|
|||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('Analyzer', () => {
|
describe('DecorationAnalyzer', () => {
|
||||||
describe('analyzeFile()', () => {
|
describe('analyzeFile()', () => {
|
||||||
let program: ts.Program;
|
let program: ts.Program;
|
||||||
let testHandler: jasmine.SpyObj<DecoratorHandler<any, any>>;
|
let testHandler: jasmine.SpyObj<DecoratorHandler<any, any>>;
|
||||||
let result: AnalyzedFile;
|
let result: DecorationAnalysis;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
program = makeProgram(TEST_PROGRAM);
|
program = makeProgram(TEST_PROGRAM);
|
||||||
const file = createParsedFile(program);
|
const file = createParsedFile(program);
|
||||||
const analyzer = new Analyzer(
|
const analyzer = new DecorationAnalyzer(
|
||||||
program.getTypeChecker(), new Fesm2015ReflectionHost(false, program.getTypeChecker()),
|
program.getTypeChecker(), new Fesm2015ReflectionHost(false, program.getTypeChecker()),
|
||||||
[''], false);
|
[''], false);
|
||||||
testHandler = createTestHandler();
|
testHandler = createTestHandler();
|
@ -8,19 +8,19 @@
|
|||||||
import * as ts from 'typescript';
|
import * as ts from 'typescript';
|
||||||
import MagicString from 'magic-string';
|
import MagicString from 'magic-string';
|
||||||
import {makeProgram} from '../helpers/utils';
|
import {makeProgram} from '../helpers/utils';
|
||||||
import {Analyzer} from '../../src/analyzer';
|
import {DecorationAnalyzer} from '../../src/analysis/decoration_analyzer';
|
||||||
import {Fesm2015ReflectionHost} from '../../src/host/fesm2015_host';
|
import {Fesm2015ReflectionHost} from '../../src/host/fesm2015_host';
|
||||||
import {Esm2015Renderer} from '../../src/rendering/esm2015_renderer';
|
import {Esm2015Renderer} from '../../src/rendering/esm2015_renderer';
|
||||||
|
|
||||||
function setup(file: {name: string, contents: string}) {
|
function setup(file: {name: string, contents: string}) {
|
||||||
const program = makeProgram(file);
|
const program = makeProgram(file);
|
||||||
const host = new Fesm2015ReflectionHost(false, program.getTypeChecker());
|
const host = new Fesm2015ReflectionHost(false, program.getTypeChecker());
|
||||||
const analyzer = new Analyzer(program.getTypeChecker(), host, [''], false);
|
const analyzer = new DecorationAnalyzer(program.getTypeChecker(), host, [''], false);
|
||||||
const renderer = new Esm2015Renderer(host, false, null);
|
const renderer = new Esm2015Renderer(host, false, null);
|
||||||
return {analyzer, host, program, renderer};
|
return {analyzer, host, program, renderer};
|
||||||
}
|
}
|
||||||
|
|
||||||
function analyze(host: Fesm2015ReflectionHost, analyzer: Analyzer, file: ts.SourceFile) {
|
function analyze(host: Fesm2015ReflectionHost, analyzer: DecorationAnalyzer, file: ts.SourceFile) {
|
||||||
const decoratedFiles = host.findDecoratedFiles(file);
|
const decoratedFiles = host.findDecoratedFiles(file);
|
||||||
return Array.from(decoratedFiles.values()).map(file => analyzer.analyzeFile(file))[0];
|
return Array.from(decoratedFiles.values()).map(file => analyzer.analyzeFile(file))[0];
|
||||||
}
|
}
|
||||||
|
@ -8,19 +8,19 @@
|
|||||||
import * as ts from 'typescript';
|
import * as ts from 'typescript';
|
||||||
import MagicString from 'magic-string';
|
import MagicString from 'magic-string';
|
||||||
import {makeProgram} from '../helpers/utils';
|
import {makeProgram} from '../helpers/utils';
|
||||||
import {Analyzer} from '../../src/analyzer';
|
import {DecorationAnalyzer} from '../../src/analysis/decoration_analyzer';
|
||||||
import {Esm5ReflectionHost} from '../../src/host/esm5_host';
|
import {Esm5ReflectionHost} from '../../src/host/esm5_host';
|
||||||
import {Esm5Renderer} from '../../src/rendering/esm5_renderer';
|
import {Esm5Renderer} from '../../src/rendering/esm5_renderer';
|
||||||
|
|
||||||
function setup(file: {name: string, contents: string}) {
|
function setup(file: {name: string, contents: string}) {
|
||||||
const program = makeProgram(file);
|
const program = makeProgram(file);
|
||||||
const host = new Esm5ReflectionHost(false, program.getTypeChecker());
|
const host = new Esm5ReflectionHost(false, program.getTypeChecker());
|
||||||
const analyzer = new Analyzer(program.getTypeChecker(), host, [''], false);
|
const analyzer = new DecorationAnalyzer(program.getTypeChecker(), host, [''], false);
|
||||||
const renderer = new Esm5Renderer(host, false, null);
|
const renderer = new Esm5Renderer(host, false, null);
|
||||||
return {analyzer, host, program, renderer};
|
return {analyzer, host, program, renderer};
|
||||||
}
|
}
|
||||||
|
|
||||||
function analyze(host: Esm5ReflectionHost, analyzer: Analyzer, file: ts.SourceFile) {
|
function analyze(host: Esm5ReflectionHost, analyzer: DecorationAnalyzer, file: ts.SourceFile) {
|
||||||
const decoratedFiles = host.findDecoratedFiles(file);
|
const decoratedFiles = host.findDecoratedFiles(file);
|
||||||
return Array.from(decoratedFiles.values()).map(file => analyzer.analyzeFile(file))[0];
|
return Array.from(decoratedFiles.values()).map(file => analyzer.analyzeFile(file))[0];
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ import * as ts from 'typescript';
|
|||||||
import MagicString from 'magic-string';
|
import MagicString from 'magic-string';
|
||||||
import {fromObject, generateMapFileComment} from 'convert-source-map';
|
import {fromObject, generateMapFileComment} from 'convert-source-map';
|
||||||
import {makeProgram} from '../helpers/utils';
|
import {makeProgram} from '../helpers/utils';
|
||||||
import {AnalyzedClass, Analyzer} from '../../src/analyzer';
|
import {AnalyzedClass, DecorationAnalyzer} from '../../src/analysis/decoration_analyzer';
|
||||||
import {Fesm2015ReflectionHost} from '../../src/host/fesm2015_host';
|
import {Fesm2015ReflectionHost} from '../../src/host/fesm2015_host';
|
||||||
import {Renderer} from '../../src/rendering/renderer';
|
import {Renderer} from '../../src/rendering/renderer';
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ function createTestRenderer() {
|
|||||||
function analyze(file: {name: string, contents: string}) {
|
function analyze(file: {name: string, contents: string}) {
|
||||||
const program = makeProgram(file);
|
const program = makeProgram(file);
|
||||||
const host = new Fesm2015ReflectionHost(false, program.getTypeChecker());
|
const host = new Fesm2015ReflectionHost(false, program.getTypeChecker());
|
||||||
const analyzer = new Analyzer(program.getTypeChecker(), host, [''], false);
|
const analyzer = new DecorationAnalyzer(program.getTypeChecker(), host, [''], false);
|
||||||
|
|
||||||
const decoratedFiles = host.findDecoratedFiles(program.getSourceFile(file.name) !);
|
const decoratedFiles = host.findDecoratedFiles(program.getSourceFile(file.name) !);
|
||||||
const analyzedFiles = Array.from(decoratedFiles.values()).map(file => analyzer.analyzeFile(file));
|
const analyzedFiles = Array.from(decoratedFiles.values()).map(file => analyzer.analyzeFile(file));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user