diff --git a/goldens/circular-deps/packages.json b/goldens/circular-deps/packages.json index cae92bc971..51e1575bf0 100644 --- a/goldens/circular-deps/packages.json +++ b/goldens/circular-deps/packages.json @@ -41,55 +41,6 @@ "packages/compiler-cli/src/ngtsc/scope/src/component_scope.ts", "packages/compiler-cli/src/ngtsc/scope/src/local.ts" ], - [ - "packages/compiler/src/compile_metadata.ts", - "packages/compiler/src/lifecycle_reflector.ts", - "packages/compiler/src/compile_reflector.ts", - "packages/compiler/src/output/output_ast.ts", - "packages/compiler/src/parse_util.ts" - ], - [ - "packages/compiler/src/compile_metadata.ts", - "packages/compiler/src/ml_parser/parser.ts", - "packages/compiler/src/ml_parser/ast.ts", - "packages/compiler/src/i18n/i18n_ast.ts", - "packages/compiler/src/parse_util.ts" - ], - [ - "packages/compiler/src/compile_metadata.ts", - "packages/compiler/src/ml_parser/parser.ts", - "packages/compiler/src/ml_parser/ast.ts", - "packages/compiler/src/parse_util.ts" - ], - [ - "packages/compiler/src/compile_metadata.ts", - "packages/compiler/src/ml_parser/parser.ts", - "packages/compiler/src/ml_parser/lexer.ts", - "packages/compiler/src/parse_util.ts" - ], - [ - "packages/compiler/src/compile_metadata.ts", - "packages/compiler/src/ml_parser/parser.ts", - "packages/compiler/src/parse_util.ts" - ], - [ - "packages/compiler/src/compile_metadata.ts", - "packages/compiler/src/util.ts", - "packages/compiler/src/constant_pool.ts", - "packages/compiler/src/output/output_ast.ts", - "packages/compiler/src/parse_util.ts" - ], - [ - "packages/compiler/src/compile_metadata.ts", - "packages/compiler/src/util.ts", - "packages/compiler/src/output/output_ast.ts", - "packages/compiler/src/parse_util.ts" - ], - [ - "packages/compiler/src/compile_metadata.ts", - "packages/compiler/src/util.ts", - "packages/compiler/src/parse_util.ts" - ], [ "packages/compiler/src/output/output_ast.ts", "packages/compiler/src/render3/view/i18n/meta.ts" @@ -99,11 +50,6 @@ "packages/compiler/src/render3/view/i18n/meta.ts", "packages/compiler/src/render3/view/i18n/util.ts" ], - [ - "packages/compiler/src/render3/r3_factory.ts", - "packages/compiler/src/render3/view/util.ts", - "packages/compiler/src/render3/view/api.ts" - ], [ "packages/compiler/src/render3/view/styling_builder.ts", "packages/compiler/src/render3/view/template.ts" diff --git a/packages/compiler/src/aot/compiler.ts b/packages/compiler/src/aot/compiler.ts index 47db67073a..f70fd868e9 100644 --- a/packages/compiler/src/aot/compiler.ts +++ b/packages/compiler/src/aot/compiler.ts @@ -6,9 +6,9 @@ * found in the LICENSE file at https://angular.io/license */ -import {CompileDirectiveMetadata, CompileIdentifierMetadata, CompileInjectableMetadata, CompileNgModuleMetadata, CompilePipeMetadata, CompilePipeSummary, CompileProviderMetadata, CompileShallowModuleMetadata, CompileStylesheetMetadata, CompileTypeMetadata, CompileTypeSummary, componentFactoryName, flatten, identifierName, templateSourceUrl} from '../compile_metadata'; +import {CompileDirectiveMetadata, CompileInjectableMetadata, CompileNgModuleMetadata, CompilePipeMetadata, CompilePipeSummary, CompileProviderMetadata, CompileShallowModuleMetadata, CompileStylesheetMetadata, CompileTypeMetadata, CompileTypeSummary, componentFactoryName, flatten, templateSourceUrl} from '../compile_metadata'; import {CompilerConfig} from '../config'; -import {ConstantPool} from '../constant_pool'; +import {ConstantPool, OutputContext} from '../constant_pool'; import {ViewEncapsulation} from '../core'; import {MessageBundle} from '../i18n/message_bundle'; import {createTokenForExternalReference, Identifiers} from '../identifiers'; @@ -19,12 +19,12 @@ import {InterpolationConfig} from '../ml_parser/interpolation_config'; import {NgModuleCompiler} from '../ng_module_compiler'; import {OutputEmitter} from '../output/abstract_emitter'; import * as o from '../output/output_ast'; -import {ParseError} from '../parse_util'; +import {CompileIdentifierMetadata, identifierName, ParseError, syntaxError} from '../parse_util'; import {CompiledStylesheet, StyleCompiler} from '../style_compiler'; import {SummaryResolver} from '../summary_resolver'; import {TemplateAst} from '../template_parser/template_ast'; import {TemplateParser} from '../template_parser/template_parser'; -import {newArray, OutputContext, syntaxError, ValueVisitor, visitValue} from '../util'; +import {newArray, ValueVisitor, visitValue} from '../util'; import {TypeCheckCompiler} from '../view_compiler/type_check_compiler'; import {ViewCompiler, ViewCompileResult} from '../view_compiler/view_compiler'; diff --git a/packages/compiler/src/aot/compiler_factory.ts b/packages/compiler/src/aot/compiler_factory.ts index eeccffff68..7743555b16 100644 --- a/packages/compiler/src/aot/compiler_factory.ts +++ b/packages/compiler/src/aot/compiler_factory.ts @@ -19,12 +19,12 @@ import {HtmlParser} from '../ml_parser/html_parser'; import {NgModuleCompiler} from '../ng_module_compiler'; import {NgModuleResolver} from '../ng_module_resolver'; import {TypeScriptEmitter} from '../output/ts_emitter'; +import {syntaxError} from '../parse_util'; import {PipeResolver} from '../pipe_resolver'; import {DomElementSchemaRegistry} from '../schema/dom_element_schema_registry'; import {StyleCompiler} from '../style_compiler'; import {TemplateParser} from '../template_parser/template_parser'; import {UrlResolver} from '../url_resolver'; -import {syntaxError} from '../util'; import {TypeCheckCompiler} from '../view_compiler/type_check_compiler'; import {ViewCompiler} from '../view_compiler/view_compiler'; diff --git a/packages/compiler/src/aot/formatted_error.ts b/packages/compiler/src/aot/formatted_error.ts index 7a26975c5b..7008329a08 100644 --- a/packages/compiler/src/aot/formatted_error.ts +++ b/packages/compiler/src/aot/formatted_error.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {syntaxError} from '../util'; +import {syntaxError} from '../parse_util'; export interface Position { fileName: string; diff --git a/packages/compiler/src/aot/static_reflector.ts b/packages/compiler/src/aot/static_reflector.ts index c6b8e98503..e2e7b1e4f1 100644 --- a/packages/compiler/src/aot/static_reflector.ts +++ b/packages/compiler/src/aot/static_reflector.ts @@ -10,8 +10,8 @@ import {CompileSummaryKind} from '../compile_metadata'; import {CompileReflector} from '../compile_reflector'; import {createAttribute, createComponent, createContentChild, createContentChildren, createDirective, createHost, createHostBinding, createHostListener, createInject, createInjectable, createInput, createNgModule, createOptional, createOutput, createPipe, createSelf, createSkipSelf, createViewChild, createViewChildren, MetadataFactory} from '../core'; import * as o from '../output/output_ast'; +import {syntaxError} from '../parse_util'; import {SummaryResolver} from '../summary_resolver'; -import {syntaxError} from '../util'; import {formattedError, FormattedMessageChain} from './formatted_error'; import {StaticSymbol} from './static_symbol'; diff --git a/packages/compiler/src/aot/summary_serializer.ts b/packages/compiler/src/aot/summary_serializer.ts index fb7523da5e..0587276e08 100644 --- a/packages/compiler/src/aot/summary_serializer.ts +++ b/packages/compiler/src/aot/summary_serializer.ts @@ -6,9 +6,10 @@ * found in the LICENSE file at https://angular.io/license */ import {CompileDirectiveMetadata, CompileDirectiveSummary, CompileNgModuleMetadata, CompileNgModuleSummary, CompilePipeMetadata, CompileProviderMetadata, CompileSummaryKind, CompileTypeMetadata, CompileTypeSummary} from '../compile_metadata'; +import {OutputContext} from '../constant_pool'; import * as o from '../output/output_ast'; import {Summary, SummaryResolver} from '../summary_resolver'; -import {OutputContext, ValueTransformer, ValueVisitor, visitValue} from '../util'; +import {ValueTransformer, ValueVisitor, visitValue} from '../util'; import {StaticSymbol, StaticSymbolCache} from './static_symbol'; import {ResolvedStaticSymbol, StaticSymbolResolver, unwrapResolvedMetadata} from './static_symbol_resolver'; diff --git a/packages/compiler/src/compile_metadata.ts b/packages/compiler/src/compile_metadata.ts index b111371da6..f24046ab4d 100644 --- a/packages/compiler/src/compile_metadata.ts +++ b/packages/compiler/src/compile_metadata.ts @@ -10,7 +10,8 @@ import {StaticSymbol} from './aot/static_symbol'; import {ChangeDetectionStrategy, SchemaMetadata, Type, ViewEncapsulation} from './core'; import {LifecycleHooks} from './lifecycle_reflector'; import {ParseTreeResult as HtmlParseTreeResult} from './ml_parser/parser'; -import {splitAtColon, stringify} from './util'; +import {CompileIdentifierMetadata, identifierName, sanitizeIdentifier} from './parse_util'; +import {splitAtColon} from './util'; // group 0: "[prop] or (event) or @trigger" // group 1: "prop" from "[prop]" @@ -18,49 +19,6 @@ import {splitAtColon, stringify} from './util'; // group 3: "@trigger" from "@trigger" const HOST_REG_EXP = /^(?:(?:\[([^\]]+)\])|(?:\(([^\)]+)\)))|(\@[-\w]+)$/; -export function sanitizeIdentifier(name: string): string { - return name.replace(/\W/g, '_'); -} - -let _anonymousTypeIndex = 0; - -export function identifierName(compileIdentifier: CompileIdentifierMetadata|null|undefined): string| - null { - if (!compileIdentifier || !compileIdentifier.reference) { - return null; - } - const ref = compileIdentifier.reference; - if (ref instanceof StaticSymbol) { - return ref.name; - } - if (ref['__anonymousType']) { - return ref['__anonymousType']; - } - if (ref['__forward_ref__']) { - // We do not want to try to stringify a `forwardRef()` function because that would cause the - // inner function to be evaluated too early, defeating the whole point of the `forwardRef`. - return '__forward_ref__'; - } - let identifier = stringify(ref); - if (identifier.indexOf('(') >= 0) { - // case: anonymous functions! - identifier = `anonymous_${_anonymousTypeIndex++}`; - ref['__anonymousType'] = identifier; - } else { - identifier = sanitizeIdentifier(identifier); - } - return identifier; -} - -export function identifierModuleUrl(compileIdentifier: CompileIdentifierMetadata): string { - const ref = compileIdentifier.reference; - if (ref instanceof StaticSymbol) { - return ref.filePath; - } - // Runtime type - return `./${stringify(ref)}`; -} - export function viewClassName(compType: any, embeddedTemplateIndex: number): string { return `View_${identifierName({reference: compType})}_${embeddedTemplateIndex}`; } @@ -81,10 +39,6 @@ export interface ProxyClass { setDelegate(delegate: any): void; } -export interface CompileIdentifierMetadata { - reference: any; -} - export enum CompileSummaryKind { Pipe, Directive, diff --git a/packages/compiler/src/compiler.ts b/packages/compiler/src/compiler.ts index af28935d55..7bc7b2e357 100644 --- a/packages/compiler/src/compiler.ts +++ b/packages/compiler/src/compiler.ts @@ -88,7 +88,7 @@ export * from './selector'; export * from './style_compiler'; export * from './template_parser/template_parser'; export {ViewCompiler} from './view_compiler/view_compiler'; -export {getParseErrors, isSyntaxError, syntaxError, Version} from './util'; +export {Version} from './util'; export {SourceMap} from './output/source_map'; export * from './injectable_compiler_2'; export * from './render3/partial/api'; diff --git a/packages/compiler/src/constant_pool.ts b/packages/compiler/src/constant_pool.ts index a380c29807..03d3b2124d 100644 --- a/packages/compiler/src/constant_pool.ts +++ b/packages/compiler/src/constant_pool.ts @@ -7,7 +7,6 @@ */ import * as o from './output/output_ast'; -import {error, OutputContext} from './util'; const CONSTANT_PREFIX = '_c'; @@ -276,6 +275,13 @@ export class ConstantPool { } } +export interface OutputContext { + genFilePath: string; + statements: o.Statement[]; + constantPool: ConstantPool; + importExpr(reference: any, typeParams?: o.Type[]|null, useSummaries?: boolean): o.Expression; +} + /** * Visitor used to determine if 2 expressions are equivalent and can be shared in the * `ConstantPool`. diff --git a/packages/compiler/src/directive_normalizer.ts b/packages/compiler/src/directive_normalizer.ts index 372db43b14..3976338d81 100644 --- a/packages/compiler/src/directive_normalizer.ts +++ b/packages/compiler/src/directive_normalizer.ts @@ -13,11 +13,12 @@ import * as html from './ml_parser/ast'; import {HtmlParser} from './ml_parser/html_parser'; import {InterpolationConfig} from './ml_parser/interpolation_config'; import {ParseTreeResult as HtmlParseTreeResult} from './ml_parser/parser'; +import {syntaxError} from './parse_util'; import {ResourceLoader} from './resource_loader'; import {extractStyleUrls, isStyleUrlResolvable} from './style_url_resolver'; import {PreparsedElementType, preparseElement} from './template_parser/template_preparser'; import {UrlResolver} from './url_resolver'; -import {isDefined, stringify, SyncAsync, syntaxError} from './util'; +import {isDefined, stringify, SyncAsync} from './util'; export interface PrenormalizedTemplateMetadata { ngModuleType: any; diff --git a/packages/compiler/src/i18n/extractor.ts b/packages/compiler/src/i18n/extractor.ts index 26919b38ce..889e986efd 100644 --- a/packages/compiler/src/i18n/extractor.ts +++ b/packages/compiler/src/i18n/extractor.ts @@ -28,7 +28,6 @@ import {NgModuleResolver} from '../ng_module_resolver'; import {ParseError} from '../parse_util'; import {PipeResolver} from '../pipe_resolver'; import {DomElementSchemaRegistry} from '../schema/dom_element_schema_registry'; -import {syntaxError} from '../util'; import {MessageBundle} from './message_bundle'; diff --git a/packages/compiler/src/injectable_compiler.ts b/packages/compiler/src/injectable_compiler.ts index 533577a6dd..8af182b7b3 100644 --- a/packages/compiler/src/injectable_compiler.ts +++ b/packages/compiler/src/injectable_compiler.ts @@ -7,14 +7,15 @@ */ import {StaticSymbol} from './aot/static_symbol'; -import {CompileInjectableMetadata, identifierName} from './compile_metadata'; +import {CompileInjectableMetadata} from './compile_metadata'; import {CompileReflector} from './compile_reflector'; +import {OutputContext} from './constant_pool'; import {InjectFlags} from './core'; import {Identifiers} from './identifiers'; import * as o from './output/output_ast'; import {convertValueToOutputAst} from './output/value_util'; +import {identifierName} from './parse_util'; import {Identifiers as R3} from './render3/r3_identifiers'; -import {OutputContext} from './util'; type MapEntry = { key: string, diff --git a/packages/compiler/src/jit/compiler.ts b/packages/compiler/src/jit/compiler.ts index d7adba8d90..ded36fc38f 100644 --- a/packages/compiler/src/jit/compiler.ts +++ b/packages/compiler/src/jit/compiler.ts @@ -6,21 +6,22 @@ * found in the LICENSE file at https://angular.io/license */ -import {CompileDirectiveMetadata, CompileIdentifierMetadata, CompileNgModuleMetadata, CompilePipeSummary, CompileProviderMetadata, CompileStylesheetMetadata, CompileTypeSummary, identifierName, ngModuleJitUrl, ProviderMeta, ProxyClass, sharedStylesheetJitUrl, templateJitUrl, templateSourceUrl} from '../compile_metadata'; +import {CompileDirectiveMetadata, CompileNgModuleMetadata, CompilePipeSummary, CompileProviderMetadata, CompileTypeSummary, ngModuleJitUrl, ProxyClass, sharedStylesheetJitUrl, templateJitUrl, templateSourceUrl} from '../compile_metadata'; import {CompileReflector} from '../compile_reflector'; import {CompilerConfig} from '../config'; -import {ConstantPool} from '../constant_pool'; +import {ConstantPool, OutputContext} from '../constant_pool'; import {Type} from '../core'; import {CompileMetadataResolver} from '../metadata_resolver'; import {NgModuleCompiler} from '../ng_module_compiler'; import * as ir from '../output/output_ast'; import {interpretStatements} from '../output/output_interpreter'; import {JitEvaluator} from '../output/output_jit'; +import {CompileIdentifierMetadata, identifierName} from '../parse_util'; import {CompiledStylesheet, StyleCompiler} from '../style_compiler'; import {SummaryResolver} from '../summary_resolver'; import {TemplateAst} from '../template_parser/template_ast'; import {TemplateParser} from '../template_parser/template_parser'; -import {Console, OutputContext, stringify, SyncAsync} from '../util'; +import {Console, stringify, SyncAsync} from '../util'; import {ViewCompiler} from '../view_compiler/view_compiler'; export interface ModuleWithComponentFactories { diff --git a/packages/compiler/src/metadata_resolver.ts b/packages/compiler/src/metadata_resolver.ts index 0bf9de3709..5127191c4a 100644 --- a/packages/compiler/src/metadata_resolver.ts +++ b/packages/compiler/src/metadata_resolver.ts @@ -19,11 +19,12 @@ import {Identifiers} from './identifiers'; import {getAllLifecycleHooks} from './lifecycle_reflector'; import {HtmlParser} from './ml_parser/html_parser'; import {NgModuleResolver} from './ng_module_resolver'; +import {CompileIdentifierMetadata, identifierName, syntaxError} from './parse_util'; import {PipeResolver} from './pipe_resolver'; import {ElementSchemaRegistry} from './schema/element_schema_registry'; import {CssSelector} from './selector'; import {SummaryResolver} from './summary_resolver'; -import {Console, isPromise, noUndefined, resolveForwardRef, stringify, SyncAsync, syntaxError, ValueTransformer, visitValue} from './util'; +import {Console, isPromise, noUndefined, resolveForwardRef, stringify, SyncAsync, ValueTransformer, visitValue} from './util'; export type ErrorCollector = (error: any, type?: any) => void; @@ -130,7 +131,7 @@ export class CompileMetadataResolver { } getHostComponentType(dirType: any): StaticSymbol|cpl.ProxyClass { - const name = `${cpl.identifierName({reference: dirType})}_Host`; + const name = `${identifierName({reference: dirType})}_Host`; if (dirType instanceof StaticSymbol) { return this._staticSymbolCache.get(dirType.filePath, name); } @@ -533,14 +534,14 @@ export class CompileMetadataResolver { if (!meta) { return null; } - const declaredDirectives: cpl.CompileIdentifierMetadata[] = []; - const exportedNonModuleIdentifiers: cpl.CompileIdentifierMetadata[] = []; - const declaredPipes: cpl.CompileIdentifierMetadata[] = []; + const declaredDirectives: CompileIdentifierMetadata[] = []; + const exportedNonModuleIdentifiers: CompileIdentifierMetadata[] = []; + const declaredPipes: CompileIdentifierMetadata[] = []; const importedModules: cpl.CompileNgModuleSummary[] = []; const exportedModules: cpl.CompileNgModuleSummary[] = []; const providers: cpl.CompileProviderMetadata[] = []; const entryComponents: cpl.CompileEntryComponentMetadata[] = []; - const bootstrapComponents: cpl.CompileIdentifierMetadata[] = []; + const bootstrapComponents: CompileIdentifierMetadata[] = []; const schemas: SchemaMetadata[] = []; if (meta.imports) { @@ -672,8 +673,8 @@ export class CompileMetadataResolver { }); } - const exportedDirectives: cpl.CompileIdentifierMetadata[] = []; - const exportedPipes: cpl.CompileIdentifierMetadata[] = []; + const exportedDirectives: CompileIdentifierMetadata[] = []; + const exportedPipes: CompileIdentifierMetadata[] = []; exportedNonModuleIdentifiers.forEach((exportedId) => { if (transitiveModule.directivesSet.has(exportedId.reference)) { exportedDirectives.push(exportedId); @@ -836,7 +837,7 @@ export class CompileMetadataResolver { return result; } - private _getIdentifierMetadata(type: Type): cpl.CompileIdentifierMetadata { + private _getIdentifierMetadata(type: Type): CompileIdentifierMetadata { type = resolveForwardRef(type); return {reference: type}; } @@ -1093,7 +1094,7 @@ export class CompileMetadataResolver { private _getEntryComponentsFromProvider(provider: cpl.ProviderMeta, type?: any): cpl.CompileEntryComponentMetadata[] { const components: cpl.CompileEntryComponentMetadata[] = []; - const collectedIdentifiers: cpl.CompileIdentifierMetadata[] = []; + const collectedIdentifiers: CompileIdentifierMetadata[] = []; if (provider.useFactory || provider.useExisting || provider.useClass) { this._reportError( @@ -1263,12 +1264,12 @@ function isValidType(value: any): boolean { return (value instanceof StaticSymbol) || (value instanceof Type); } -function extractIdentifiers(value: any, targetIdentifiers: cpl.CompileIdentifierMetadata[]) { +function extractIdentifiers(value: any, targetIdentifiers: CompileIdentifierMetadata[]) { visitValue(value, new _CompileValueConverter(), targetIdentifiers); } class _CompileValueConverter extends ValueTransformer { - override visitOther(value: any, targetIdentifiers: cpl.CompileIdentifierMetadata[]): any { + override visitOther(value: any, targetIdentifiers: CompileIdentifierMetadata[]): any { targetIdentifiers.push({reference: value}); } } diff --git a/packages/compiler/src/ng_module_compiler.ts b/packages/compiler/src/ng_module_compiler.ts index 7fa3fd14a0..4359a7170e 100644 --- a/packages/compiler/src/ng_module_compiler.ts +++ b/packages/compiler/src/ng_module_compiler.ts @@ -6,14 +6,14 @@ * found in the LICENSE file at https://angular.io/license */ -import {CompileNgModuleMetadata, CompileProviderMetadata, identifierName} from './compile_metadata'; +import {CompileNgModuleMetadata, CompileProviderMetadata} from './compile_metadata'; import {CompileReflector} from './compile_reflector'; +import {OutputContext} from './constant_pool'; import {NodeFlags} from './core'; import {Identifiers} from './identifiers'; import * as o from './output/output_ast'; -import {typeSourceSpan} from './parse_util'; +import {identifierName, typeSourceSpan} from './parse_util'; import {NgModuleProviderAnalyzer} from './provider_analyzer'; -import {OutputContext} from './util'; import {componentFactoryResolverProviderDef, depDef, providerDef} from './view_compiler/provider_compiler'; export class NgModuleCompileResult { diff --git a/packages/compiler/src/output/js_emitter.ts b/packages/compiler/src/output/js_emitter.ts index 1091dd1d3f..b3fdc719c6 100644 --- a/packages/compiler/src/output/js_emitter.ts +++ b/packages/compiler/src/output/js_emitter.ts @@ -5,11 +5,6 @@ * 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 {StaticSymbol} from '../aot/static_symbol'; -import {CompileIdentifierMetadata} from '../compile_metadata'; - import {EmitterVisitorContext, OutputEmitter} from './abstract_emitter'; import {AbstractJsEmitterVisitor} from './abstract_js_emitter'; import * as o from './output_ast'; diff --git a/packages/compiler/src/output/output_ast.ts b/packages/compiler/src/output/output_ast.ts index ae511e6e54..4f8c51547e 100644 --- a/packages/compiler/src/output/output_ast.ts +++ b/packages/compiler/src/output/output_ast.ts @@ -6,7 +6,6 @@ * found in the LICENSE file at https://angular.io/license */ - import {ParseSourceSpan} from '../parse_util'; import {I18nMeta} from '../render3/view/i18n/meta'; diff --git a/packages/compiler/src/output/output_jit.ts b/packages/compiler/src/output/output_jit.ts index 95ec8cc940..5d5f3890aa 100644 --- a/packages/compiler/src/output/output_jit.ts +++ b/packages/compiler/src/output/output_jit.ts @@ -6,8 +6,8 @@ * found in the LICENSE file at https://angular.io/license */ -import {identifierName} from '../compile_metadata'; import {CompileReflector} from '../compile_reflector'; +import {identifierName} from '../parse_util'; import {EmitterVisitorContext} from './abstract_emitter'; import {AbstractJsEmitterVisitor} from './abstract_js_emitter'; diff --git a/packages/compiler/src/output/value_util.ts b/packages/compiler/src/output/value_util.ts index a567004669..904c50b2aa 100644 --- a/packages/compiler/src/output/value_util.ts +++ b/packages/compiler/src/output/value_util.ts @@ -7,7 +7,8 @@ */ -import {OutputContext, ValueTransformer, visitValue} from '../util'; +import {OutputContext} from '../constant_pool'; +import {ValueTransformer, visitValue} from '../util'; import * as o from './output_ast'; diff --git a/packages/compiler/src/parse_util.ts b/packages/compiler/src/parse_util.ts index 190233f413..823f507f45 100644 --- a/packages/compiler/src/parse_util.ts +++ b/packages/compiler/src/parse_util.ts @@ -5,8 +5,9 @@ * 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 {StaticSymbol} from './aot/static_symbol'; import * as chars from './chars'; -import {CompileIdentifierMetadata, identifierModuleUrl, identifierName} from './compile_metadata'; +import {stringify} from './util'; export class ParseLocation { constructor( @@ -178,3 +179,68 @@ export function r3JitTypeSourceSpan( return new ParseSourceSpan( new ParseLocation(sourceFile, -1, -1, -1), new ParseLocation(sourceFile, -1, -1, -1)); } + +export function syntaxError(msg: string, parseErrors?: ParseError[]): Error { + const error = Error(msg); + (error as any)[ERROR_SYNTAX_ERROR] = true; + if (parseErrors) (error as any)[ERROR_PARSE_ERRORS] = parseErrors; + return error; +} + +const ERROR_SYNTAX_ERROR = 'ngSyntaxError'; +const ERROR_PARSE_ERRORS = 'ngParseErrors'; + +export function isSyntaxError(error: Error): boolean { + return (error as any)[ERROR_SYNTAX_ERROR]; +} + +export function getParseErrors(error: Error): ParseError[] { + return (error as any)[ERROR_PARSE_ERRORS] || []; +} + +let _anonymousTypeIndex = 0; + +export function identifierName(compileIdentifier: CompileIdentifierMetadata|null|undefined): string| + null { + if (!compileIdentifier || !compileIdentifier.reference) { + return null; + } + const ref = compileIdentifier.reference; + if (ref instanceof StaticSymbol) { + return ref.name; + } + if (ref['__anonymousType']) { + return ref['__anonymousType']; + } + if (ref['__forward_ref__']) { + // We do not want to try to stringify a `forwardRef()` function because that would cause the + // inner function to be evaluated too early, defeating the whole point of the `forwardRef`. + return '__forward_ref__'; + } + let identifier = stringify(ref); + if (identifier.indexOf('(') >= 0) { + // case: anonymous functions! + identifier = `anonymous_${_anonymousTypeIndex++}`; + ref['__anonymousType'] = identifier; + } else { + identifier = sanitizeIdentifier(identifier); + } + return identifier; +} + +export function identifierModuleUrl(compileIdentifier: CompileIdentifierMetadata): string { + const ref = compileIdentifier.reference; + if (ref instanceof StaticSymbol) { + return ref.filePath; + } + // Runtime type + return `./${stringify(ref)}`; +} + +export interface CompileIdentifierMetadata { + reference: any; +} + +export function sanitizeIdentifier(name: string): string { + return name.replace(/\W/g, '_'); +} diff --git a/packages/compiler/src/render3/r3_factory.ts b/packages/compiler/src/render3/r3_factory.ts index 8cc4dbfc06..e0c94c90b2 100644 --- a/packages/compiler/src/render3/r3_factory.ts +++ b/packages/compiler/src/render3/r3_factory.ts @@ -5,17 +5,10 @@ * 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 {StaticSymbol} from '../aot/static_symbol'; -import {CompileTypeMetadata, tokenReference} from '../compile_metadata'; import {InjectFlags} from '../core'; import * as o from '../output/output_ast'; import {Identifiers as R3} from '../render3/r3_identifiers'; -import {OutputContext} from '../util'; - import {R3CompiledExpression, R3Reference, typeWithParameters} from './util'; -import {unsupported} from './view/util'; - /** diff --git a/packages/compiler/src/render3/view/compiler.ts b/packages/compiler/src/render3/view/compiler.ts index 49825e0a2c..852f967bbc 100644 --- a/packages/compiler/src/render3/view/compiler.ts +++ b/packages/compiler/src/render3/view/compiler.ts @@ -6,13 +6,13 @@ * found in the LICENSE file at https://angular.io/license */ -import {CompileDirectiveSummary, sanitizeIdentifier} from '../../compile_metadata'; +import {CompileDirectiveSummary} from '../../compile_metadata'; import {BindingForm, convertPropertyBinding} from '../../compiler_util/expression_converter'; import {ConstantPool} from '../../constant_pool'; import * as core from '../../core'; import {AST, ParsedEvent, ParsedEventType, ParsedProperty} from '../../expression_parser/ast'; import * as o from '../../output/output_ast'; -import {ParseError, ParseSourceSpan} from '../../parse_util'; +import {ParseError, ParseSourceSpan, sanitizeIdentifier} from '../../parse_util'; import {CssSelector, SelectorMatcher} from '../../selector'; import {ShadowCss} from '../../shadow_css'; import {CONTENT_ATTR, HOST_ATTR} from '../../style_compiler'; diff --git a/packages/compiler/src/render3/view/template.ts b/packages/compiler/src/render3/view/template.ts index 600c955626..a37caa822b 100644 --- a/packages/compiler/src/render3/view/template.ts +++ b/packages/compiler/src/render3/view/template.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {flatten, sanitizeIdentifier} from '../../compile_metadata'; +import {flatten} from '../../compile_metadata'; import {BindingForm, BuiltinFunctionCall, convertActionBinding, convertPropertyBinding, convertUpdateArguments, LocalResolver} from '../../compiler_util/expression_converter'; import {ConstantPool} from '../../constant_pool'; import * as core from '../../core'; @@ -22,7 +22,7 @@ import {LexerRange} from '../../ml_parser/lexer'; import {isNgContainer as checkIsNgContainer, splitNsName} from '../../ml_parser/tags'; import {mapLiteral} from '../../output/map_util'; import * as o from '../../output/output_ast'; -import {ParseError, ParseSourceFile, ParseSourceSpan} from '../../parse_util'; +import {ParseError, ParseSourceSpan, sanitizeIdentifier} from '../../parse_util'; import {DomElementSchemaRegistry} from '../../schema/dom_element_schema_registry'; import {isTrustedTypesSink} from '../../schema/trusted_types_sinks'; import {CssSelector, SelectorMatcher} from '../../selector'; diff --git a/packages/compiler/src/style_compiler.ts b/packages/compiler/src/style_compiler.ts index abd2690c3d..f766f9d450 100644 --- a/packages/compiler/src/style_compiler.ts +++ b/packages/compiler/src/style_compiler.ts @@ -6,12 +6,13 @@ * found in the LICENSE file at https://angular.io/license */ -import {CompileDirectiveMetadata, CompileIdentifierMetadata, CompileStylesheetMetadata, identifierModuleUrl, identifierName} from './compile_metadata'; +import {CompileDirectiveMetadata, CompileStylesheetMetadata} from './compile_metadata'; +import {OutputContext} from './constant_pool'; import {ViewEncapsulation} from './core'; import * as o from './output/output_ast'; +import {identifierModuleUrl, identifierName} from './parse_util'; import {ShadowCss} from './shadow_css'; import {UrlResolver} from './url_resolver'; -import {OutputContext} from './util'; const COMPONENT_VARIABLE = '%COMP%'; export const HOST_ATTR = `_nghost-${COMPONENT_VARIABLE}`; diff --git a/packages/compiler/src/template_parser/template_parser.ts b/packages/compiler/src/template_parser/template_parser.ts index 098e8f4979..65b2613865 100644 --- a/packages/compiler/src/template_parser/template_parser.ts +++ b/packages/compiler/src/template_parser/template_parser.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {CompileDirectiveMetadata, CompileDirectiveSummary, CompilePipeSummary, CompileTokenMetadata, CompileTypeMetadata, identifierName} from '../compile_metadata'; +import {CompileDirectiveMetadata, CompileDirectiveSummary, CompilePipeSummary, CompileTokenMetadata, CompileTypeMetadata} from '../compile_metadata'; import {CompileReflector} from '../compile_reflector'; import {CompilerConfig} from '../config'; import {SchemaMetadata} from '../core'; @@ -19,12 +19,12 @@ import {removeWhitespaces, replaceNgsp} from '../ml_parser/html_whitespaces'; import {expandNodes} from '../ml_parser/icu_ast_expander'; import {InterpolationConfig} from '../ml_parser/interpolation_config'; import {isNgTemplate, splitNsName} from '../ml_parser/tags'; -import {ParseError, ParseErrorLevel, ParseSourceSpan} from '../parse_util'; +import {identifierName, ParseError, ParseErrorLevel, ParseSourceSpan, syntaxError} from '../parse_util'; import {ProviderElementContext, ProviderViewContext} from '../provider_analyzer'; import {ElementSchemaRegistry} from '../schema/element_schema_registry'; import {CssSelector, SelectorMatcher} from '../selector'; import {isStyleUrlResolvable} from '../style_url_resolver'; -import {Console, newArray, syntaxError} from '../util'; +import {Console, newArray} from '../util'; import {BindingParser} from './binding_parser'; import * as t from './template_ast'; diff --git a/packages/compiler/src/util.ts b/packages/compiler/src/util.ts index 8c62653f09..e30d002ca4 100644 --- a/packages/compiler/src/util.ts +++ b/packages/compiler/src/util.ts @@ -6,11 +6,6 @@ * found in the LICENSE file at https://angular.io/license */ -import {ConstantPool} from './constant_pool'; - -import * as o from './output/output_ast'; -import {ParseError} from './parse_util'; - const DASH_CASE_REGEXP = /-+([a-z0-9])/g; export function dashCaseToCamelCase(input: string): string { @@ -104,24 +99,6 @@ export function error(msg: string): never { throw new Error(`Internal Error: ${msg}`); } -export function syntaxError(msg: string, parseErrors?: ParseError[]): Error { - const error = Error(msg); - (error as any)[ERROR_SYNTAX_ERROR] = true; - if (parseErrors) (error as any)[ERROR_PARSE_ERRORS] = parseErrors; - return error; -} - -const ERROR_SYNTAX_ERROR = 'ngSyntaxError'; -const ERROR_PARSE_ERRORS = 'ngParseErrors'; - -export function isSyntaxError(error: Error): boolean { - return (error as any)[ERROR_SYNTAX_ERROR]; -} - -export function getParseErrors(error: Error): ParseError[] { - return (error as any)[ERROR_PARSE_ERRORS] || []; -} - // Escape characters that have a special meaning in Regular Expressions export function escapeRegExp(s: string): string { return s.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1'); @@ -166,13 +143,6 @@ export function utf8Encode(str: string): Byte[] { return encoded; } -export interface OutputContext { - genFilePath: string; - statements: o.Statement[]; - constantPool: ConstantPool; - importExpr(reference: any, typeParams?: o.Type[]|null, useSummaries?: boolean): o.Expression; -} - export function stringify(token: any): string { if (typeof token === 'string') { return token; diff --git a/packages/compiler/src/view_compiler/provider_compiler.ts b/packages/compiler/src/view_compiler/provider_compiler.ts index b893f3848a..844a543293 100644 --- a/packages/compiler/src/view_compiler/provider_compiler.ts +++ b/packages/compiler/src/view_compiler/provider_compiler.ts @@ -8,13 +8,13 @@ import {CompileDiDependencyMetadata, CompileEntryComponentMetadata, CompileProviderMetadata, CompileTokenMetadata} from '../compile_metadata'; import {CompileReflector} from '../compile_reflector'; +import {OutputContext} from '../constant_pool'; import {DepFlags, NodeFlags} from '../core'; import {createTokenForExternalReference, Identifiers} from '../identifiers'; import {LifecycleHooks} from '../lifecycle_reflector'; import * as o from '../output/output_ast'; import {convertValueToOutputAst} from '../output/value_util'; import {ProviderAst, ProviderAstType} from '../template_parser/template_ast'; -import {OutputContext} from '../util'; export function providerDef(ctx: OutputContext, providerAst: ProviderAst): { providerExpr: o.Expression, diff --git a/packages/compiler/src/view_compiler/type_check_compiler.ts b/packages/compiler/src/view_compiler/type_check_compiler.ts index 9518b0a77d..42bd2b959a 100644 --- a/packages/compiler/src/view_compiler/type_check_compiler.ts +++ b/packages/compiler/src/view_compiler/type_check_compiler.ts @@ -11,11 +11,11 @@ import {StaticReflector} from '../aot/static_reflector'; import {StaticSymbol} from '../aot/static_symbol'; import {CompileDirectiveMetadata, CompilePipeSummary} from '../compile_metadata'; import {BindingForm, convertActionBinding, convertPropertyBinding, convertPropertyBindingBuiltins, EventHandlerVars, LocalResolver} from '../compiler_util/expression_converter'; +import {OutputContext} from '../constant_pool'; import {AST, ASTWithSource, Interpolation} from '../expression_parser/ast'; import * as o from '../output/output_ast'; import {ParseSourceSpan} from '../parse_util'; import {AttrAst, BoundDirectivePropertyAst, BoundElementPropertyAst, BoundEventAst, BoundTextAst, DirectiveAst, ElementAst, EmbeddedTemplateAst, NgContentAst, ReferenceAst, TemplateAst, TemplateAstVisitor, templateVisitAll, TextAst, VariableAst} from '../template_parser/template_ast'; -import {OutputContext} from '../util'; /** diff --git a/packages/compiler/src/view_compiler/view_compiler.ts b/packages/compiler/src/view_compiler/view_compiler.ts index 4cbe72a4d8..af14575b9e 100644 --- a/packages/compiler/src/view_compiler/view_compiler.ts +++ b/packages/compiler/src/view_compiler/view_compiler.ts @@ -9,6 +9,7 @@ import {CompileDirectiveMetadata, CompilePipeSummary, CompileQueryMetadata, rendererTypeName, tokenReference, viewClassName} from '../compile_metadata'; import {CompileReflector} from '../compile_reflector'; import {BindingForm, BuiltinConverter, convertActionBinding, convertPropertyBinding, convertPropertyBindingBuiltins, EventHandlerVars, LocalResolver} from '../compiler_util/expression_converter'; +import {OutputContext} from '../constant_pool'; import {ArgumentType, BindingFlags, ChangeDetectionStrategy, NodeFlags, QueryBindingType, QueryValueType, ViewFlags} from '../core'; import {AST, ASTWithSource, Interpolation} from '../expression_parser/ast'; import {Identifiers} from '../identifiers'; @@ -18,7 +19,6 @@ import * as o from '../output/output_ast'; import {convertValueToOutputAst} from '../output/value_util'; import {ParseSourceSpan} from '../parse_util'; import {AttrAst, BoundDirectivePropertyAst, BoundElementPropertyAst, BoundEventAst, BoundTextAst, DirectiveAst, ElementAst, EmbeddedTemplateAst, NgContentAst, PropertyBindingType, ProviderAst, QueryMatch, ReferenceAst, TemplateAst, TemplateAstVisitor, templateVisitAll, TextAst, VariableAst} from '../template_parser/template_ast'; -import {OutputContext} from '../util'; import {componentFactoryResolverProviderDef, depDef, lifecycleHookToNodeFlag, providerDef} from './provider_compiler'; diff --git a/packages/compiler/test/aot/summary_resolver_spec.ts b/packages/compiler/test/aot/summary_resolver_spec.ts index 5384580f7f..2870dc6ca0 100644 --- a/packages/compiler/test/aot/summary_resolver_spec.ts +++ b/packages/compiler/test/aot/summary_resolver_spec.ts @@ -6,11 +6,10 @@ * found in the LICENSE file at https://angular.io/license */ -import {AotSummaryResolver, AotSummaryResolverHost, CompileSummaryKind, CompileTypeSummary, ResolvedStaticSymbol, StaticSymbol, StaticSymbolCache, StaticSymbolResolver} from '@angular/compiler'; -import {deserializeSummaries, serializeSummaries} from '@angular/compiler/src/aot/summary_serializer'; -import {ConstantPool} from '@angular/compiler/src/constant_pool'; +import {AotSummaryResolver, AotSummaryResolverHost, ResolvedStaticSymbol, StaticSymbolCache, StaticSymbolResolver} from '@angular/compiler'; +import {serializeSummaries} from '@angular/compiler/src/aot/summary_serializer'; +import {ConstantPool, OutputContext} from '@angular/compiler/src/constant_pool'; import * as o from '@angular/compiler/src/output/output_ast'; -import {OutputContext} from '@angular/compiler/src/util'; import * as path from 'path'; import {MockStaticSymbolResolverHost, MockSummaryResolver} from './static_symbol_resolver_spec'; diff --git a/packages/compiler/test/metadata_resolver_spec.ts b/packages/compiler/test/metadata_resolver_spec.ts index 040d075b4c..78856d6dba 100644 --- a/packages/compiler/test/metadata_resolver_spec.ts +++ b/packages/compiler/test/metadata_resolver_spec.ts @@ -10,8 +10,9 @@ import {LIFECYCLE_HOOKS_VALUES, LifecycleHooks} from '@angular/compiler/src/life import {AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit, ChangeDetectionStrategy, Component, Directive, DoCheck, Injectable, NgModule, OnChanges, OnDestroy, OnInit, Pipe, SimpleChanges, ViewEncapsulation, ɵstringify as stringify} from '@angular/core'; import {inject, TestBed, waitForAsync} from '@angular/core/testing'; -import {CompileDiDependencyMetadata, identifierName} from '../src/compile_metadata'; +import {CompileDiDependencyMetadata} from '../src/compile_metadata'; import {CompileMetadataResolver} from '../src/metadata_resolver'; +import {identifierName} from '../src/parse_util'; import {ResourceLoader} from '../src/resource_loader'; import {MockResourceLoader} from '../testing/src/resource_loader_mock'; diff --git a/packages/compiler/test/output/js_emitter_node_only_spec.ts b/packages/compiler/test/output/js_emitter_node_only_spec.ts index 3a47c0b1a4..70cd116c6e 100644 --- a/packages/compiler/test/output/js_emitter_node_only_spec.ts +++ b/packages/compiler/test/output/js_emitter_node_only_spec.ts @@ -6,8 +6,6 @@ * found in the LICENSE file at https://angular.io/license */ -import {StaticSymbol} from '@angular/compiler/src/aot/static_symbol'; -import {CompileIdentifierMetadata} from '@angular/compiler/src/compile_metadata'; import {JavaScriptEmitter} from '@angular/compiler/src/output/js_emitter'; import * as o from '@angular/compiler/src/output/output_ast'; import {SourceMap} from '@angular/compiler/src/output/source_map';