From 83a9159063dc9d93c51f462eb28db3f3ad83e87b Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Wed, 8 Apr 2020 10:14:18 -0700 Subject: [PATCH] style(compiler): reformat of codebase with new clang-format version (#36520) This commit reformats the packages/compiler tree using the new version of clang-format. PR Close #36520 --- packages/compiler/src/aot/compiler.ts | 106 +- packages/compiler/src/aot/compiler_factory.ts | 6 +- packages/compiler/src/aot/formatted_error.ts | 6 +- packages/compiler/src/aot/generated_file.ts | 4 +- packages/compiler/src/aot/lazy_routes.ts | 2 +- packages/compiler/src/aot/static_reflector.ts | 62 +- packages/compiler/src/aot/static_symbol.ts | 2 +- .../src/aot/static_symbol_resolver.ts | 35 +- packages/compiler/src/aot/summary_resolver.ts | 12 +- .../compiler/src/aot/summary_serializer.ts | 45 +- packages/compiler/src/ast_path.ts | 26 +- packages/compiler/src/compile_metadata.ts | 140 +- .../compiler/src/compiler_facade_interface.ts | 14 +- .../src/compiler_util/expression_converter.ts | 214 +- packages/compiler/src/config.ts | 27 +- packages/compiler/src/constant_pool.ts | 31 +- packages/compiler/src/core.ts | 57 +- packages/compiler/src/css_parser/css_ast.ts | 32 +- packages/compiler/src/css_parser/css_lexer.ts | 19 +- .../compiler/src/css_parser/css_parser.ts | 65 +- packages/compiler/src/directive_normalizer.ts | 71 +- packages/compiler/src/directive_resolver.ts | 9 +- .../compiler/src/expression_parser/ast.ts | 68 +- .../compiler/src/expression_parser/lexer.ts | 48 +- .../compiler/src/expression_parser/parser.ts | 57 +- packages/compiler/src/i18n/digest.ts | 7 +- packages/compiler/src/i18n/extractor.ts | 4 +- .../compiler/src/i18n/extractor_merger.ts | 53 +- packages/compiler/src/i18n/i18n_ast.ts | 36 +- packages/compiler/src/i18n/i18n_parser.ts | 6 +- packages/compiler/src/i18n/message_bundle.ts | 12 +- packages/compiler/src/i18n/parse_util.ts | 4 +- .../src/i18n/serializers/placeholder.ts | 4 +- .../src/i18n/serializers/serializer.ts | 14 +- .../compiler/src/i18n/serializers/xliff.ts | 46 +- .../compiler/src/i18n/serializers/xliff2.ts | 34 +- packages/compiler/src/i18n/serializers/xmb.ts | 16 +- .../src/i18n/serializers/xml_helper.ts | 32 +- packages/compiler/src/i18n/serializers/xtb.ts | 40 +- .../compiler/src/i18n/translation_bundle.ts | 16 +- packages/compiler/src/injectable_compiler.ts | 2 +- .../compiler/src/injectable_compiler_2.ts | 4 +- packages/compiler/src/jit/compiler.ts | 39 +- packages/compiler/src/jit_compiler_facade.ts | 26 +- packages/compiler/src/metadata_resolver.ts | 135 +- packages/compiler/src/ml_parser/ast.ts | 32 +- .../compiler/src/ml_parser/html_parser.ts | 6 +- packages/compiler/src/ml_parser/html_tags.ts | 29 +- .../src/ml_parser/html_whitespaces.ts | 12 +- .../src/ml_parser/icu_ast_expander.ts | 18 +- packages/compiler/src/ml_parser/lexer.ts | 36 +- packages/compiler/src/ml_parser/parser.ts | 14 +- packages/compiler/src/ml_parser/tags.ts | 4 +- packages/compiler/src/ml_parser/xml_parser.ts | 6 +- packages/compiler/src/ml_parser/xml_tags.ts | 14 +- packages/compiler/src/ng_module_compiler.ts | 6 +- packages/compiler/src/ng_module_resolver.ts | 6 +- .../compiler/src/output/abstract_emitter.ts | 46 +- .../src/output/abstract_js_emitter.ts | 6 +- packages/compiler/src/output/js_emitter.ts | 2 +- packages/compiler/src/output/output_ast.ts | 252 +- .../compiler/src/output/output_interpreter.ts | 40 +- packages/compiler/src/output/output_jit.ts | 8 +- packages/compiler/src/output/source_map.ts | 32 +- packages/compiler/src/output/ts_emitter.ts | 11 +- packages/compiler/src/output/value_util.ts | 6 +- packages/compiler/src/pipe_resolver.ts | 2 +- packages/compiler/src/provider_analyzer.ts | 46 +- packages/compiler/src/render3/r3_ast.ts | 82 +- packages/compiler/src/render3/r3_factory.ts | 10 +- packages/compiler/src/render3/r3_jit.ts | 40 +- .../src/render3/r3_module_compiler.ts | 17 +- .../compiler/src/render3/r3_pipe_compiler.ts | 4 +- .../src/render3/r3_template_transform.ts | 32 +- packages/compiler/src/render3/util.ts | 5 +- packages/compiler/src/render3/view/api.ts | 2 +- .../compiler/src/render3/view/compiler.ts | 45 +- .../compiler/src/render3/view/i18n/context.ts | 20 +- .../src/render3/view/i18n/get_msg_utils.ts | 19 +- .../src/render3/view/i18n/icu_serializer.ts | 11 +- .../src/render3/view/i18n/localize_utils.ts | 4 +- .../compiler/src/render3/view/i18n/meta.ts | 20 +- .../compiler/src/render3/view/i18n/util.ts | 6 +- .../compiler/src/render3/view/style_parser.ts | 10 +- .../src/render3/view/styling_builder.ts | 7 +- packages/compiler/src/render3/view/t2_api.ts | 10 +- .../compiler/src/render3/view/t2_binder.ts | 36 +- .../compiler/src/render3/view/template.ts | 176 +- packages/compiler/src/render3/view/util.ts | 24 +- packages/compiler/src/resource_loader.ts | 4 +- .../src/schema/dom_element_schema_registry.ts | 18 +- .../src/schema/dom_security_schema.ts | 2 +- packages/compiler/src/selector.ts | 24 +- packages/compiler/src/shadow_css.ts | 37 +- packages/compiler/src/style_compiler.ts | 4 +- packages/compiler/src/summary_resolver.ts | 28 +- .../src/template_parser/binding_parser.ts | 47 +- .../src/template_parser/template_ast.ts | 16 +- .../src/template_parser/template_parser.ts | 153 +- .../src/template_parser/template_preparser.ts | 6 +- packages/compiler/src/url_resolver.ts | 22 +- packages/compiler/src/util.ts | 24 +- .../src/view_compiler/provider_compiler.ts | 11 +- .../src/view_compiler/type_check_compiler.ts | 18 +- .../src/view_compiler/view_compiler.ts | 81 +- packages/compiler/test/aot/compiler_spec.ts | 43 +- .../compiler/test/aot/jit_summaries_spec.ts | 38 +- packages/compiler/test/aot/regression_spec.ts | 2 +- .../test/aot/static_reflector_spec.ts | 21 +- .../test/aot/static_symbol_resolver_spec.ts | 71 +- .../test/aot/summary_resolver_spec.ts | 16 +- .../test/aot/summary_serializer_spec.ts | 11 +- packages/compiler/test/aot/test_util.ts | 134 +- .../test/compiler_facade_interface_spec.ts | 84 +- packages/compiler/test/config_spec.ts | 4 +- packages/compiler/test/core_spec.ts | 4 +- .../test/css_parser/css_lexer_spec.ts | 701 +-- .../test/css_parser/css_parser_spec.ts | 10 +- .../test/css_parser/css_visitor_spec.ts | 311 +- .../compiler/test/directive_lifecycle_spec.ts | 33 +- .../test/directive_normalizer_spec.ts | 18 +- .../test/directive_resolver_mock_spec.ts | 2 +- .../compiler/test/directive_resolver_spec.ts | 104 +- .../test/expression_parser/lexer_spec.ts | 34 +- .../test/expression_parser/parser_spec.ts | 169 +- .../test/expression_parser/utils/unparser.ts | 12 +- .../test/expression_parser/utils/validator.ts | 6 +- packages/compiler/test/i18n/digest_spec.ts | 26 +- .../test/i18n/extractor_merger_spec.ts | 29 +- .../test/i18n/i18n_html_parser_spec.ts | 1 - .../compiler/test/i18n/i18n_parser_spec.ts | 18 +- .../compiler/test/i18n/integration_common.ts | 6 +- .../test/i18n/integration_xliff2_spec.ts | 3 +- .../test/i18n/integration_xliff_spec.ts | 3 +- .../test/i18n/integration_xmb_xtb_spec.ts | 3 +- .../compiler/test/i18n/message_bundle_spec.ts | 10 +- .../test/i18n/serializers/i18n_ast_spec.ts | 24 +- .../test/i18n/serializers/placeholder_spec.ts | 5 +- .../test/i18n/serializers/xliff2_spec.ts | 168 +- .../test/i18n/serializers/xliff_spec.ts | 173 +- .../test/i18n/serializers/xmb_spec.ts | 2 +- .../test/i18n/serializers/xml_helper_spec.ts | 11 +- .../test/i18n/serializers/xtb_spec.ts | 22 +- .../test/i18n/translation_bundle_spec.ts | 42 +- packages/compiler/test/integration_spec.ts | 4 +- .../compiler/test/metadata_resolver_spec.ts | 51 +- .../test/ml_parser/ast_serializer_spec.ts | 4 +- .../compiler/test/ml_parser/ast_spec_utils.ts | 2 +- .../test/ml_parser/html_parser_spec.ts | 63 +- .../test/ml_parser/html_whitespaces_spec.ts | 1 - .../test/ml_parser/icu_ast_expander_spec.ts | 22 +- .../compiler/test/ml_parser/lexer_spec.ts | 5 +- packages/compiler/test/ml_parser/util/util.ts | 11 +- .../compiler/test/ng_module_resolver_spec.ts | 5 +- .../output/abstract_emitter_node_only_spec.ts | 16 +- .../test/output/abstract_emitter_spec.ts | 38 +- .../test/output/js_emitter_node_only_spec.ts | 8 +- .../compiler/test/output/js_emitter_spec.ts | 55 +- .../compiler/test/output/output_ast_spec.ts | 4 +- .../compiler/test/output/output_jit_spec.ts | 2 +- .../compiler/test/output/source_map_spec.ts | 12 +- .../test/output/ts_emitter_node_only_spec.ts | 4 +- .../compiler/test/output/ts_emitter_spec.ts | 87 +- packages/compiler/test/pipe_resolver_spec.ts | 5 +- .../test/render3/r3_ast_spans_spec.ts | 14 +- .../render3/r3_template_transform_spec.ts | 23 +- .../test/render3/style_parser_spec.ts | 5 +- .../compiler/test/render3/util/expression.ts | 20 +- .../test/render3/view/binding_spec.ts | 15 +- .../compiler/test/render3/view/i18n_spec.ts | 33 +- packages/compiler/test/render3/view/util.ts | 2 +- .../test/resource_loader_mock_spec.ts | 26 +- .../compiler/test/runtime_compiler_spec.ts | 31 +- .../dom_element_schema_registry_spec.ts | 19 +- .../compiler/test/schema/schema_extractor.ts | 8 +- .../compiler/test/selector/selector_spec.ts | 21 +- packages/compiler/test/shadow_css_spec.ts | 51 +- packages/compiler/test/spies.ts | 4 +- .../compiler/test/style_url_resolver_spec.ts | 30 +- .../template_parser/binding_parser_spec.ts | 15 +- .../template_parser_absolute_span_spec.ts | 2 +- .../template_parser/template_parser_spec.ts | 3825 +++++++++-------- .../template_preparser_spec.ts | 4 +- .../test/template_parser/util/expression.ts | 24 +- .../test/template_parser/util/metadata.ts | 47 +- packages/compiler/test/url_resolver_spec.ts | 9 +- packages/compiler/test/util_spec.ts | 13 +- .../testing/src/directive_resolver_mock.ts | 6 +- .../testing/src/ng_module_resolver_mock.ts | 8 +- .../testing/src/output/source_map_util.ts | 3 +- .../testing/src/pipe_resolver_mock.ts | 12 +- .../testing/src/resource_loader_mock.ts | 18 +- .../testing/src/schema_registry_mock.ts | 20 +- 193 files changed, 5904 insertions(+), 4574 deletions(-) diff --git a/packages/compiler/src/aot/compiler.ts b/packages/compiler/src/aot/compiler.ts index 27e08eeb96..24ea205e9e 100644 --- a/packages/compiler/src/aot/compiler.ts +++ b/packages/compiler/src/aot/compiler.ts @@ -11,7 +11,7 @@ import {CompilerConfig} from '../config'; import {ConstantPool} from '../constant_pool'; import {ViewEncapsulation} from '../core'; import {MessageBundle} from '../i18n/message_bundle'; -import {Identifiers, createTokenForExternalReference} from '../identifiers'; +import {createTokenForExternalReference, Identifiers} from '../identifiers'; import {InjectableCompiler} from '../injectable_compiler'; import {CompileMetadataResolver} from '../metadata_resolver'; import {HtmlParser} from '../ml_parser/html_parser'; @@ -31,9 +31,9 @@ import {SummaryResolver} from '../summary_resolver'; import {BindingParser} from '../template_parser/binding_parser'; import {TemplateAst} from '../template_parser/template_ast'; import {TemplateParser} from '../template_parser/template_parser'; -import {OutputContext, ValueVisitor, error, newArray, syntaxError, visitValue} from '../util'; +import {error, newArray, OutputContext, syntaxError, ValueVisitor, visitValue} from '../util'; import {TypeCheckCompiler} from '../view_compiler/type_check_compiler'; -import {ViewCompileResult, ViewCompiler} from '../view_compiler/view_compiler'; +import {ViewCompiler, ViewCompileResult} from '../view_compiler/view_compiler'; import {AotCompilerHost} from './compiler_host'; import {AotCompilerOptions} from './compiler_options'; @@ -46,7 +46,11 @@ import {StaticSymbolResolver} from './static_symbol_resolver'; import {createForJitStub, serializeSummaries} from './summary_serializer'; import {ngfactoryFilePath, normalizeGenFileSuffix, splitTypescriptSuffix, summaryFileName, summaryForJitFileName} from './util'; -const enum StubEmitFlags { Basic = 1 << 0, TypeCheck = 1 << 1, All = TypeCheck | Basic } +const enum StubEmitFlags { + Basic = 1 << 0, + TypeCheck = 1 << 1, + All = TypeCheck | Basic +} export class AotCompiler { private _templateAstCache = @@ -64,7 +68,9 @@ export class AotCompiler { private _summaryResolver: SummaryResolver, private _symbolResolver: StaticSymbolResolver) {} - clearCache() { this._metadataResolver.clearCache(); } + clearCache() { + this._metadataResolver.clearCache(); + } analyzeModulesSync(rootFiles: string[]): NgAnalyzedModules { const analyzeResult = analyzeAndValidateNgModules( @@ -123,7 +129,7 @@ export class AotCompiler { const fileSuffix = normalizeGenFileSuffix(splitTypescriptSuffix(file.fileName, true)[1]); file.directives.forEach((dirSymbol) => { const compMeta = - this._metadataResolver.getNonNormalizedDirectiveMetadata(dirSymbol) !.metadata; + this._metadataResolver.getNonNormalizedDirectiveMetadata(dirSymbol)!.metadata; if (!compMeta.isComponent) { return; } @@ -149,7 +155,8 @@ export class AotCompiler { if (genFileName.endsWith('.ngfactory.ts')) { if (!originalFileName) { throw new Error( - `Assertion error: require the original file for .ngfactory.ts stubs. File: ${genFileName}`); + `Assertion error: require the original file for .ngfactory.ts stubs. File: ${ + genFileName}`); } const originalFile = this._analyzeFile(originalFileName); this._createNgFactoryStub(outputCtx, originalFile, StubEmitFlags.Basic); @@ -157,7 +164,8 @@ export class AotCompiler { if (this._options.enableSummariesForJit) { if (!originalFileName) { throw new Error( - `Assertion error: require the original file for .ngsummary.ts stubs. File: ${genFileName}`); + `Assertion error: require the original file for .ngsummary.ts stubs. File: ${ + genFileName}`); } const originalFile = this._analyzeFile(originalFileName); _createEmptyStub(outputCtx); @@ -319,10 +327,10 @@ export class AotCompiler { const html = compMeta.template !.template !; // Template URL points to either an HTML or TS file depending on whether // the file is used with `templateUrl:` or `template:`, respectively. - const templateUrl = compMeta.template !.templateUrl !; + const templateUrl = compMeta.template !.templateUrl!; const interpolationConfig = InterpolationConfig.fromArray(compMeta.template !.interpolation); - errors.push(...messageBundle.updateFromTemplate(html, templateUrl, interpolationConfig) !); + errors.push(...messageBundle.updateFromTemplate(html, templateUrl, interpolationConfig)!); }); }); @@ -342,7 +350,7 @@ export class AotCompiler { if (!contextMap.has(fileName)) { contextMap.set(fileName, this._createOutputContext(fileName)); } - return contextMap.get(fileName) !; + return contextMap.get(fileName)!; }; files.forEach( @@ -381,13 +389,13 @@ export class AotCompiler { directives.forEach(directiveType => { const directiveMetadata = this._metadataResolver.getDirectiveMetadata(directiveType); if (directiveMetadata.isComponent) { - const module = ngModuleByPipeOrDirective.get(directiveType) !; + const module = ngModuleByPipeOrDirective.get(directiveType)!; module || - error( - `Cannot determine the module for component '${identifierName(directiveMetadata.type)}'`); + error(`Cannot determine the module for component '${ + identifierName(directiveMetadata.type)}'`); - let htmlAst = directiveMetadata.template !.htmlAst !; - const preserveWhitespaces = directiveMetadata !.template !.preserveWhitespaces; + let htmlAst = directiveMetadata.template !.htmlAst!; + const preserveWhitespaces = directiveMetadata!.template !.preserveWhitespaces; if (!preserveWhitespaces) { htmlAst = removeWhitespaces(htmlAst); @@ -412,7 +420,9 @@ export class AotCompiler { const pipes = module.transitiveModule.pipes.map( pipe => this._metadataResolver.getPipeSummary(pipe.reference)); - pipes.forEach(pipe => { pipeTypeByName.set(pipe.name, pipe.type.reference); }); + pipes.forEach(pipe => { + pipeTypeByName.set(pipe.name, pipe.type.reference); + }); compileR3Component( context, directiveMetadata, render3Ast, this.reflector, hostBindingParser, @@ -484,8 +494,8 @@ export class AotCompiler { } const ngModule = ngModuleByPipeOrDirective.get(dirType); if (!ngModule) { - throw new Error( - `Internal Error: cannot determine the module for component ${identifierName(compMeta.type)}!`); + throw new Error(`Internal Error: cannot determine the module for component ${ + identifierName(compMeta.type)}!`); } // compile styles @@ -524,27 +534,27 @@ export class AotCompiler { .map(symbol => this._symbolResolver.resolveSymbol(symbol)); const typeData: { summary: CompileTypeSummary, - metadata: CompileNgModuleMetadata | CompileDirectiveMetadata | CompilePipeMetadata | - CompileTypeMetadata + metadata: CompileNgModuleMetadata|CompileDirectiveMetadata|CompilePipeMetadata| + CompileTypeMetadata }[] = [ ...ngModules.map( meta => ({ - summary: this._metadataResolver.getNgModuleSummary(meta.type.reference) !, - metadata: this._metadataResolver.getNgModuleMetadata(meta.type.reference) ! + summary: this._metadataResolver.getNgModuleSummary(meta.type.reference)!, + metadata: this._metadataResolver.getNgModuleMetadata(meta.type.reference)! })), ...directives.map(ref => ({ - summary: this._metadataResolver.getDirectiveSummary(ref) !, - metadata: this._metadataResolver.getDirectiveMetadata(ref) ! + summary: this._metadataResolver.getDirectiveSummary(ref)!, + metadata: this._metadataResolver.getDirectiveMetadata(ref)! })), ...pipes.map(ref => ({ - summary: this._metadataResolver.getPipeSummary(ref) !, - metadata: this._metadataResolver.getPipeMetadata(ref) ! + summary: this._metadataResolver.getPipeSummary(ref)!, + metadata: this._metadataResolver.getPipeMetadata(ref)! })), ...injectables.map( ref => ({ - summary: this._metadataResolver.getInjectableSummary(ref.symbol) !, - metadata: this._metadataResolver.getInjectableSummary(ref.symbol) !.type + summary: this._metadataResolver.getInjectableSummary(ref.symbol)!, + metadata: this._metadataResolver.getInjectableSummary(ref.symbol)!.type })) ]; const forJitOutputCtx = this._options.enableSummariesForJit ? @@ -621,7 +631,7 @@ export class AotCompiler { .toDeclStmt( o.importType( Identifiers.ComponentFactory, - [o.expressionType(outputCtx.importExpr(compMeta.type.reference)) !], + [o.expressionType(outputCtx.importExpr(compMeta.type.reference))!], [o.TypeModifier.Const]), [o.StmtModifier.Final, o.StmtModifier.Exported])); } @@ -648,15 +658,15 @@ export class AotCompiler { directiveIdentifiers: CompileIdentifierMetadata[]): {template: TemplateAst[], pipes: CompilePipeSummary[]} { if (this._templateAstCache.has(compMeta.type.reference)) { - return this._templateAstCache.get(compMeta.type.reference) !; + return this._templateAstCache.get(compMeta.type.reference)!; } - const preserveWhitespaces = compMeta !.template !.preserveWhitespaces; + const preserveWhitespaces = compMeta!.template !.preserveWhitespaces; const directives = directiveIdentifiers.map(dir => this._metadataResolver.getDirectiveSummary(dir.reference)); const pipes = ngModule.transitiveModule.pipes.map( pipe => this._metadataResolver.getPipeSummary(pipe.reference)); const result = this._templateParser.parse( - compMeta, compMeta.template !.htmlAst !, directives, pipes, ngModule.schemas, + compMeta, compMeta.template !.htmlAst!, directives, pipes, ngModule.schemas, templateSourceUrl(ngModule.type, compMeta, compMeta.template !), preserveWhitespaces); this._templateAstCache.set(compMeta.type.reference, result); return result; @@ -664,8 +674,7 @@ export class AotCompiler { private _createOutputContext(genFilePath: string): OutputContext { const importExpr = - (symbol: StaticSymbol, typeParams: o.Type[] | null = null, - useSummaries: boolean = true) => { + (symbol: StaticSymbol, typeParams: o.Type[]|null = null, useSummaries: boolean = true) => { if (!(symbol instanceof StaticSymbol)) { throw new Error(`Internal error: unknown identifier ${JSON.stringify(symbol)}`); } @@ -710,7 +719,7 @@ export class AotCompiler { stylesheetMetadata: CompileStylesheetMetadata, isShimmed: boolean, fileSuffix: string): GeneratedFile { const outputCtx = this._createOutputContext( - _stylesModuleUrl(stylesheetMetadata.moduleUrl !, isShimmed, fileSuffix)); + _stylesModuleUrl(stylesheetMetadata.moduleUrl!, isShimmed, fileSuffix)); const compiledStylesheet = this._styleCompiler.compileStyles(outputCtx, compMeta, stylesheetMetadata, isShimmed); _resolveStyleStatements(this._symbolResolver, compiledStylesheet, isShimmed, fileSuffix); @@ -748,8 +757,8 @@ export class AotCompiler { return allLazyRoutes; } seenRoutes.add(symbol); - const lazyRoutes = listLazyRoutes( - self._metadataResolver.getNgModuleMetadata(symbol, true) !, self.reflector); + const lazyRoutes = + listLazyRoutes(self._metadataResolver.getNgModuleMetadata(symbol, true)!, self.reflector); for (const lazyRoute of lazyRoutes) { allLazyRoutes.push(lazyRoute); visitLazyRoute(lazyRoute.referencedModule, seenRoutes, allLazyRoutes); @@ -803,7 +812,9 @@ export interface NgAnalyzedFile { exportsNonSourceFiles: boolean; } -export interface NgAnalyzeModulesHost { isSourceFile(filePath: string): boolean; } +export interface NgAnalyzeModulesHost { + isSourceFile(filePath: string): boolean; +} export function analyzeNgModules( fileNames: string[], host: NgAnalyzeModulesHost, staticSymbolResolver: StaticSymbolResolver, @@ -823,8 +834,8 @@ export function analyzeAndValidateNgModules( function validateAnalyzedModules(analyzedModules: NgAnalyzedModules): NgAnalyzedModules { if (analyzedModules.symbolsMissingModule && analyzedModules.symbolsMissingModule.length) { const messages = analyzedModules.symbolsMissingModule.map( - s => - `Cannot determine the module for class ${s.name} in ${s.filePath}! Add ${s.name} to the NgModule to fix it.`); + s => `Cannot determine the module for class ${s.name} in ${s.filePath}! Add ${ + s.name} to the NgModule to fix it.`); throw syntaxError(messages.join('\n')); } return analyzedModules; @@ -918,8 +929,13 @@ export function analyzeFile( }); } return { - fileName, directives, abstractDirectives, pipes, - ngModules, injectables, exportsNonSourceFiles, + fileName, + directives, + abstractDirectives, + pipes, + ngModules, + injectables, + exportsNonSourceFiles, }; } @@ -957,7 +973,9 @@ function isValueExportingNonSourceFile(host: NgAnalyzeModulesHost, metadata: any let exportsNonSourceFiles = false; class Visitor implements ValueVisitor { - visitArray(arr: any[], context: any): any { arr.forEach(v => visitValue(v, this, context)); } + visitArray(arr: any[], context: any): any { + arr.forEach(v => visitValue(v, this, context)); + } visitStringMap(map: {[key: string]: any}, context: any): any { Object.keys(map).forEach((key) => visitValue(map[key], this, context)); } diff --git a/packages/compiler/src/aot/compiler_factory.ts b/packages/compiler/src/aot/compiler_factory.ts index 6122764f43..e6cdaeaad9 100644 --- a/packages/compiler/src/aot/compiler_factory.ts +++ b/packages/compiler/src/aot/compiler_factory.ts @@ -36,9 +36,9 @@ import {StaticSymbol, StaticSymbolCache} from './static_symbol'; import {StaticSymbolResolver} from './static_symbol_resolver'; import {AotSummaryResolver} from './summary_resolver'; -export function createAotUrlResolver(host: { - resourceNameToFileName(resourceName: string, containingFileName: string): string | null; -}): UrlResolver { +export function createAotUrlResolver( + host: {resourceNameToFileName(resourceName: string, containingFileName: string): string|null;}): + UrlResolver { return { resolve: (basePath: string, url: string) => { const filePath = host.resourceNameToFileName(url, basePath); diff --git a/packages/compiler/src/aot/formatted_error.ts b/packages/compiler/src/aot/formatted_error.ts index b99e0f2f28..2f47a6c98c 100644 --- a/packages/compiler/src/aot/formatted_error.ts +++ b/packages/compiler/src/aot/formatted_error.ts @@ -20,7 +20,7 @@ export interface FormattedMessageChain { next?: FormattedMessageChain[]; } -export type FormattedError = Error & { +export type FormattedError = Error&{ chain: FormattedMessageChain; position?: Position; }; @@ -34,10 +34,10 @@ function indentStr(level: number): string { return half + half + (level % 2 === 1 ? ' ' : ''); } -function formatChain(chain: FormattedMessageChain | undefined, indent: number = 0): string { +function formatChain(chain: FormattedMessageChain|undefined, indent: number = 0): string { if (!chain) return ''; const position = chain.position ? - `${chain.position.fileName}(${chain.position.line+1},${chain.position.column+1})` : + `${chain.position.fileName}(${chain.position.line + 1},${chain.position.column + 1})` : ''; const prefix = position && indent === 0 ? `${position}: ` : ''; const postfix = position && indent !== 0 ? ` at ${position}` : ''; diff --git a/packages/compiler/src/aot/generated_file.ts b/packages/compiler/src/aot/generated_file.ts index 0973fbaa60..ff1fa07cc2 100644 --- a/packages/compiler/src/aot/generated_file.ts +++ b/packages/compiler/src/aot/generated_file.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {Statement, areAllEquivalent} from '../output/output_ast'; +import {areAllEquivalent, Statement} from '../output/output_ast'; import {TypeScriptEmitter} from '../output/ts_emitter'; export class GeneratedFile { @@ -36,7 +36,7 @@ export class GeneratedFile { } // Note: the constructor guarantees that if this.source is not filled, // then this.stmts is. - return areAllEquivalent(this.stmts !, other.stmts !); + return areAllEquivalent(this.stmts!, other.stmts!); } } diff --git a/packages/compiler/src/aot/lazy_routes.ts b/packages/compiler/src/aot/lazy_routes.ts index ab32b34d1c..9351518315 100644 --- a/packages/compiler/src/aot/lazy_routes.ts +++ b/packages/compiler/src/aot/lazy_routes.ts @@ -34,7 +34,7 @@ export function listLazyRoutes( return allLazyRoutes; } -function _collectLoadChildren(routes: string | Route | Route[], target: string[] = []): string[] { +function _collectLoadChildren(routes: string|Route|Route[], target: string[] = []): string[] { if (typeof routes === 'string') { target.push(routes); } else if (Array.isArray(routes)) { diff --git a/packages/compiler/src/aot/static_reflector.ts b/packages/compiler/src/aot/static_reflector.ts index 6b6008ab49..c3d4d54597 100644 --- a/packages/compiler/src/aot/static_reflector.ts +++ b/packages/compiler/src/aot/static_reflector.ts @@ -8,12 +8,12 @@ import {CompileSummaryKind} from '../compile_metadata'; import {CompileReflector} from '../compile_reflector'; -import {MetadataFactory, createAttribute, createComponent, createContentChild, createContentChildren, createDirective, createHost, createHostBinding, createHostListener, createInject, createInjectable, createInput, createNgModule, createOptional, createOutput, createPipe, createSelf, createSkipSelf, createViewChild, createViewChildren} from '../core'; +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 {SummaryResolver} from '../summary_resolver'; import {syntaxError} from '../util'; -import {FormattedMessageChain, formattedError} from './formatted_error'; +import {formattedError, FormattedMessageChain} from './formatted_error'; import {StaticSymbol} from './static_symbol'; import {StaticSymbolResolver} from './static_symbol_resolver'; @@ -50,13 +50,13 @@ export class StaticReflector implements CompileReflector { private conversionMap = new Map any>(); private resolvedExternalReferences = new Map(); // TODO(issue/24571): remove '!'. - private injectionToken !: StaticSymbol; + private injectionToken!: StaticSymbol; // TODO(issue/24571): remove '!'. - private opaqueToken !: StaticSymbol; + private opaqueToken!: StaticSymbol; // TODO(issue/24571): remove '!'. - ROUTES !: StaticSymbol; + ROUTES!: StaticSymbol; // TODO(issue/24571): remove '!'. - private ANALYZE_FOR_ENTRY_COMPONENTS !: StaticSymbol; + private ANALYZE_FOR_ENTRY_COMPONENTS!: StaticSymbol; private annotationForParentClassWithSummaryKind = new Map[]>(); @@ -110,10 +110,10 @@ export class StaticReflector implements CompileReflector { if (declarationSymbol) return declarationSymbol; } const refSymbol = - this.symbolResolver.getSymbolByModule(ref.moduleName !, ref.name !, containingFile); + this.symbolResolver.getSymbolByModule(ref.moduleName!, ref.name!, containingFile); const declarationSymbol = this.findSymbolDeclaration(refSymbol); if (!containingFile) { - this.symbolResolver.recordModuleNameForFileName(refSymbol.filePath, ref.moduleName !); + this.symbolResolver.recordModuleNameForFileName(refSymbol.filePath, ref.moduleName!); this.symbolResolver.recordImportAs(declarationSymbol, refSymbol); } if (key) { @@ -192,16 +192,20 @@ export class StaticReflector implements CompileReflector { const summary = this.summaryResolver.resolveSummary(parentType); if (summary && summary.type) { const requiredAnnotationTypes = - this.annotationForParentClassWithSummaryKind.get(summary.type.summaryKind !) !; + this.annotationForParentClassWithSummaryKind.get(summary.type.summaryKind!)!; const typeHasRequiredAnnotation = requiredAnnotationTypes.some( (requiredType) => ownAnnotations.some(ann => requiredType.isTypeOf(ann))); if (!typeHasRequiredAnnotation) { this.reportError( formatMetadataError( metadataError( - `Class ${type.name} in ${type.filePath} extends from a ${CompileSummaryKind[summary.type.summaryKind!]} in another compilation unit without duplicating the decorator`, + `Class ${type.name} in ${type.filePath} extends from a ${ + CompileSummaryKind[summary.type.summaryKind! + ]} in another compilation unit without duplicating the decorator`, /* summary */ undefined, - `Please add a ${requiredAnnotationTypes.map((type) => type.ngMetadataName).join(' or ')} decorator to the class`), + `Please add a ${ + requiredAnnotationTypes.map((type) => type.ngMetadataName) + .join(' or ')} decorator to the class`), type), type); } @@ -221,7 +225,7 @@ export class StaticReflector implements CompileReflector { if (parentType) { const parentPropMetadata = this.propMetadata(parentType); Object.keys(parentPropMetadata).forEach((parentProp) => { - propMetadata ![parentProp] = parentPropMetadata[parentProp]; + propMetadata![parentProp] = parentPropMetadata[parentProp]; }); } @@ -231,10 +235,10 @@ export class StaticReflector implements CompileReflector { const prop = (propData) .find(a => a['__symbolic'] == 'property' || a['__symbolic'] == 'method'); const decorators: any[] = []; - if (propMetadata ![propName]) { - decorators.push(...propMetadata ![propName]); + if (propMetadata![propName]) { + decorators.push(...propMetadata![propName]); } - propMetadata ![propName] = decorators; + propMetadata![propName] = decorators; if (prop && prop['decorators']) { decorators.push(...this.simplify(type, prop['decorators'])); } @@ -271,7 +275,7 @@ export class StaticReflector implements CompileReflector { if (decorators) { nestedResult.push(...decorators); } - parameters !.push(nestedResult); + parameters!.push(nestedResult); }); } else if (parentType) { parameters = this.parameters(parentType); @@ -297,7 +301,7 @@ export class StaticReflector implements CompileReflector { if (parentType) { const parentMethodNames = this._methodNames(parentType); Object.keys(parentMethodNames).forEach((parentProp) => { - methodNames ![parentProp] = parentMethodNames[parentProp]; + methodNames![parentProp] = parentMethodNames[parentProp]; }); } @@ -305,7 +309,7 @@ export class StaticReflector implements CompileReflector { Object.keys(members).forEach((propName) => { const propData = members[propName]; const isMethod = (propData).some(a => a['__symbolic'] == 'method'); - methodNames ![propName] = methodNames ![propName] || isMethod; + methodNames![propName] = methodNames![propName] || isMethod; }); this.methodCache.set(type, methodNames); } @@ -485,7 +489,7 @@ export class StaticReflector implements CompileReflector { // Propagate the message text up but add a message to the chain that explains how we got // here. // e.chain implies e.symbol - const summaryMsg = e.chain ? 'references \'' + e.symbol !.name + '\'' : errorSummary(e); + const summaryMsg = e.chain ? 'references \'' + e.symbol!.name + '\'' : errorSummary(e); const summary = `'${nestedContext.name}' ${summaryMsg}`; const chain = {message: summary, position: e.position, next: e.chain}; // TODO(chuckj): retrieve the position information indirectly from the collectors node @@ -494,7 +498,8 @@ export class StaticReflector implements CompileReflector { { message: e.message, advise: e.advise, - context: e.context, chain, + context: e.context, + chain, symbol: nestedContext }, context); @@ -566,7 +571,8 @@ export class StaticReflector implements CompileReflector { { message: FUNCTION_CALL_NOT_SUPPORTED, context: functionSymbol, - value: targetFunction, position + value: targetFunction, + position }, context); } @@ -875,7 +881,7 @@ interface MetadataMessageChain { next?: MetadataMessageChain; } -type MetadataError = Error & { +type MetadataError = Error&{ position?: Position; advise?: string; summary?: string; @@ -916,7 +922,8 @@ function expandedMessage(message: string, context: any): string { switch (message) { case REFERENCE_TO_NONEXPORTED_CLASS: if (context && context.className) { - return `References to a non-exported class are not supported in decorators but ${context.className} was referenced.`; + return `References to a non-exported class are not supported in decorators but ${ + context.className} was referenced.`; } break; case VARIABLE_NOT_INITIALIZED: @@ -935,7 +942,8 @@ function expandedMessage(message: string, context: any): string { return 'Function calls are not supported in decorators'; case REFERENCE_TO_LOCAL_SYMBOL: if (context && context.name) { - return `Reference to a local (non-exported) symbols are not supported in decorators but '${context.name}' was referenced`; + return `Reference to a local (non-exported) symbols are not supported in decorators but '${ + context.name}' was referenced`; } break; case LAMBDA_NOT_SUPPORTED: @@ -1040,7 +1048,9 @@ abstract class BindingScope { } class PopulatedScope extends BindingScope { - constructor(private bindings: Map) { super(); } + constructor(private bindings: Map) { + super(); + } resolve(name: string): any { return this.bindings.has(name) ? this.bindings.get(name) : BindingScope.missing; @@ -1048,7 +1058,7 @@ class PopulatedScope extends BindingScope { } function formatMetadataMessageChain( - chain: MetadataMessageChain, advise: string | undefined): FormattedMessageChain { + chain: MetadataMessageChain, advise: string|undefined): FormattedMessageChain { const expanded = expandedMessage(chain.message, chain.context); const nesting = chain.symbol ? ` in '${chain.symbol.name}'` : ''; const message = `${expanded}${nesting}`; diff --git a/packages/compiler/src/aot/static_symbol.ts b/packages/compiler/src/aot/static_symbol.ts index cb47dd9e67..aaec3f0f2d 100644 --- a/packages/compiler/src/aot/static_symbol.ts +++ b/packages/compiler/src/aot/static_symbol.ts @@ -31,7 +31,7 @@ export class StaticSymbolCache { get(declarationFile: string, name: string, members?: string[]): StaticSymbol { members = members || []; - const memberSuffix = members.length ? `.${ members.join('.')}` : ''; + const memberSuffix = members.length ? `.${members.join('.')}` : ''; const key = `"${declarationFile}".${name}${memberSuffix}`; let result = this.cache.get(key); if (!result) { diff --git a/packages/compiler/src/aot/static_symbol_resolver.ts b/packages/compiler/src/aot/static_symbol_resolver.ts index 8ec20dcd2a..de5db1f246 100644 --- a/packages/compiler/src/aot/static_symbol_resolver.ts +++ b/packages/compiler/src/aot/static_symbol_resolver.ts @@ -76,12 +76,12 @@ export class StaticSymbolResolver { resolveSymbol(staticSymbol: StaticSymbol): ResolvedStaticSymbol { if (staticSymbol.members.length > 0) { - return this._resolveSymbolMembers(staticSymbol) !; + return this._resolveSymbolMembers(staticSymbol)!; } // Note: always ask for a summary first, // as we might have read shallow metadata via a .d.ts file // for the symbol. - const resultFromSummary = this._resolveSymbolFromSummary(staticSymbol) !; + const resultFromSummary = this._resolveSymbolFromSummary(staticSymbol)!; if (resultFromSummary) { return resultFromSummary; } @@ -93,7 +93,7 @@ export class StaticSymbolResolver { // have summaries, only .d.ts files. So we always need to check both, the summary // and metadata. this._createSymbolsOf(staticSymbol.filePath); - return this.resolvedSymbols.get(staticSymbol) !; + return this.resolvedSymbols.get(staticSymbol)!; } /** @@ -119,15 +119,14 @@ export class StaticSymbolResolver { const baseSymbol = this.getStaticSymbol(summarizedFileName, summarizedName, staticSymbol.members); const baseImportAs = this.getImportAs(baseSymbol, useSummaries); - return baseImportAs ? - this.getStaticSymbol( - summaryForJitFileName(baseImportAs.filePath), summaryForJitName(baseImportAs.name), - baseSymbol.members) : - null; + return baseImportAs ? this.getStaticSymbol( + summaryForJitFileName(baseImportAs.filePath), + summaryForJitName(baseImportAs.name), baseSymbol.members) : + null; } let result = (useSummaries && this.summaryResolver.getImportAs(staticSymbol)) || null; if (!result) { - result = this.importAs.get(staticSymbol) !; + result = this.importAs.get(staticSymbol)!; } return result; } @@ -347,8 +346,8 @@ export class StaticSymbolResolver { // correctly. const originFilePath = this.resolveModule(origin, filePath); if (!originFilePath) { - this.reportError(new Error( - `Couldn't resolve original symbol for ${origin} from ${this.host.getOutputName(filePath)}`)); + this.reportError(new Error(`Couldn't resolve original symbol for ${origin} from ${ + this.host.getOutputName(filePath)}`)); } else { this.symbolResourcePaths.set(symbol, originFilePath); } @@ -413,7 +412,7 @@ export class StaticSymbolResolver { } let filePath: string; if (module) { - filePath = self.resolveModule(module, sourceSymbol.filePath) !; + filePath = self.resolveModule(module, sourceSymbol.filePath)!; if (!filePath) { return { __symbolic: 'error', @@ -501,8 +500,11 @@ export class StaticSymbolResolver { } if (moduleMetadata['version'] != SUPPORTED_SCHEMA_VERSION) { const errorMessage = moduleMetadata['version'] == 2 ? - `Unsupported metadata version ${moduleMetadata['version']} for module ${module}. This module should be compiled with a newer version of ngc` : - `Metadata version mismatch for module ${this.host.getOutputName(module)}, found version ${moduleMetadata['version']}, expected ${SUPPORTED_SCHEMA_VERSION}`; + `Unsupported metadata version ${moduleMetadata['version']} for module ${ + module}. This module should be compiled with a newer version of ngc` : + `Metadata version mismatch for module ${ + this.host.getOutputName(module)}, found version ${ + moduleMetadata['version']}, expected ${SUPPORTED_SCHEMA_VERSION}`; this.reportError(new Error(errorMessage)); } this.metadataCache.set(module, moduleMetadata); @@ -514,9 +516,8 @@ export class StaticSymbolResolver { getSymbolByModule(module: string, symbolName: string, containingFile?: string): StaticSymbol { const filePath = this.resolveModule(module, containingFile); if (!filePath) { - this.reportError( - new Error(`Could not resolve module ${module}${containingFile ? ' relative to ' + - this.host.getOutputName(containingFile) : ''}`)); + this.reportError(new Error(`Could not resolve module ${module}${ + containingFile ? ' relative to ' + this.host.getOutputName(containingFile) : ''}`)); return this.getStaticSymbol(`ERROR:${module}`, symbolName); } return this.getStaticSymbol(filePath, symbolName); diff --git a/packages/compiler/src/aot/summary_resolver.ts b/packages/compiler/src/aot/summary_resolver.ts index 0f2c1f855e..9d795b287b 100644 --- a/packages/compiler/src/aot/summary_resolver.ts +++ b/packages/compiler/src/aot/summary_resolver.ts @@ -71,7 +71,7 @@ export class AotSummaryResolver implements SummaryResolver { let summary = this.summaryCache.get(rootSymbol); if (!summary) { this._loadSummaryFile(staticSymbol.filePath); - summary = this.summaryCache.get(staticSymbol) !; + summary = this.summaryCache.get(staticSymbol)!; } return (rootSymbol === staticSymbol && summary) || null; } @@ -85,7 +85,7 @@ export class AotSummaryResolver implements SummaryResolver { getImportAs(staticSymbol: StaticSymbol): StaticSymbol { staticSymbol.assertNoMembers(); - return this.importAs.get(staticSymbol) !; + return this.importAs.get(staticSymbol)!; } /** @@ -95,7 +95,9 @@ export class AotSummaryResolver implements SummaryResolver { return this.knownFileNameToModuleNames.get(importedFilePath) || null; } - addSummary(summary: Summary) { this.summaryCache.set(summary.symbol, summary); } + addSummary(summary: Summary) { + this.summaryCache.set(summary.symbol, summary); + } private _loadSummaryFile(filePath: string): boolean { let hasSummary = this.loadedFilePaths.get(filePath); @@ -121,7 +123,9 @@ export class AotSummaryResolver implements SummaryResolver { if (moduleName) { this.knownFileNameToModuleNames.set(filePath, moduleName); } - importAs.forEach((importAs) => { this.importAs.set(importAs.symbol, importAs.importAs); }); + importAs.forEach((importAs) => { + this.importAs.set(importAs.symbol, importAs.importAs); + }); } return hasSummary; } diff --git a/packages/compiler/src/aot/summary_serializer.ts b/packages/compiler/src/aot/summary_serializer.ts index fbc7941491..8157d3d228 100644 --- a/packages/compiler/src/aot/summary_serializer.ts +++ b/packages/compiler/src/aot/summary_serializer.ts @@ -15,12 +15,12 @@ import {ResolvedStaticSymbol, StaticSymbolResolver, unwrapResolvedMetadata} from import {isLoweredSymbol, ngfactoryFilePath, summaryForJitFileName, summaryForJitName} from './util'; export function serializeSummaries( - srcFileName: string, forJitCtx: OutputContext | null, + srcFileName: string, forJitCtx: OutputContext|null, summaryResolver: SummaryResolver, symbolResolver: StaticSymbolResolver, symbols: ResolvedStaticSymbol[], types: { summary: CompileTypeSummary, - metadata: CompileNgModuleMetadata | CompileDirectiveMetadata | CompilePipeMetadata | - CompileTypeMetadata + metadata: CompileNgModuleMetadata|CompileDirectiveMetadata|CompilePipeMetadata| + CompileTypeMetadata }[], createExternalSymbolReexports = false): {json: string, exportAs: {symbol: StaticSymbol, exportAs: string}[]} { @@ -41,7 +41,9 @@ export function serializeSummaries( const {json, exportAs} = toJsonSerializer.serialize(createExternalSymbolReexports); if (forJitCtx) { const forJitSerializer = new ForJitSerializer(forJitCtx, symbolResolver, summaryResolver); - types.forEach(({summary, metadata}) => { forJitSerializer.addSourceType(summary, metadata); }); + types.forEach(({summary, metadata}) => { + forJitSerializer.addSourceType(summary, metadata); + }); toJsonSerializer.unprocessedSymbolSummariesBySymbol.forEach((summary) => { if (summaryResolver.isLibraryFile(summary.symbol.filePath) && summary.type) { forJitSerializer.addLibType(summary.type); @@ -55,7 +57,7 @@ export function serializeSummaries( export function deserializeSummaries( symbolCache: StaticSymbolCache, summaryResolver: SummaryResolver, libraryFileName: string, json: string): { - moduleName: string | null, + moduleName: string|null, summaries: Summary[], importAs: {symbol: StaticSymbol, importAs: StaticSymbol}[] } { @@ -144,7 +146,7 @@ class ToJsonSerializer extends ValueTransformer { processedSummary.metadata = this.processValue(metadata, SerializationFlags.ResolveValue); if (metadata instanceof StaticSymbol && this.summaryResolver.isLibraryFile(metadata.filePath)) { - const declarationSymbol = this.symbols[this.indexBySymbol.get(metadata) !]; + const declarationSymbol = this.symbols[this.indexBySymbol.get(metadata)!]; if (!isLoweredSymbol(declarationSymbol.name)) { // Note: symbols that were introduced during codegen in the user file can have a reexport // if a user used `export *`. However, we can't rely on this as tsickle will change @@ -194,7 +196,7 @@ class ToJsonSerializer extends ValueTransformer { summaries: this.processedSummaries, symbols: this.symbols.map((symbol, index) => { symbol.assertNoMembers(); - let importAs: string|number = undefined !; + let importAs: string|number = undefined!; if (this.summaryResolver.isLibraryFile(symbol.filePath)) { const reexportSymbol = this.reexportedBy.get(symbol); if (reexportSymbol) { @@ -202,7 +204,7 @@ class ToJsonSerializer extends ValueTransformer { // user, we just proxy the external static symbol reference to the manual export. // This ensures that the AOT compiler imports the external symbol through the // user export and does not introduce another dependency which is not needed. - importAs = this.indexBySymbol.get(reexportSymbol) !; + importAs = this.indexBySymbol.get(reexportSymbol)!; } else if (createExternalSymbolReexports) { // In this case, the given external static symbol is *not* manually exported by // the user, and we manually create a re-export in the factory file so that we @@ -270,7 +272,7 @@ class ToJsonSerializer extends ValueTransformer { if (this.unprocessedSymbolSummariesBySymbol.has(baseSymbol)) { // the summary for this symbol was already added // -> nothing to do. - return index !; + return index!; } summary = this.loadSummary(baseSymbol); if (summary && summary.metadata instanceof StaticSymbol) { @@ -324,8 +326,9 @@ class ForJitSerializer { private summaryResolver: SummaryResolver) {} addSourceType( - summary: CompileTypeSummary, metadata: CompileNgModuleMetadata|CompileDirectiveMetadata| - CompilePipeMetadata|CompileTypeMetadata) { + summary: CompileTypeSummary, + metadata: CompileNgModuleMetadata|CompileDirectiveMetadata|CompilePipeMetadata| + CompileTypeMetadata) { this.data.push({summary, metadata, isLibrary: false}); } @@ -356,7 +359,7 @@ class ForJitSerializer { const fnName = summaryForJitName(summary.type.reference.name); createSummaryForJitFunction( this.outputCtx, summary.type.reference, - this.serializeSummaryWithDeps(summary, metadata !)); + this.serializeSummaryWithDeps(summary, metadata!)); } } @@ -372,8 +375,9 @@ class ForJitSerializer { } private serializeSummaryWithDeps( - summary: CompileTypeSummary, metadata: CompileNgModuleMetadata|CompileDirectiveMetadata| - CompilePipeMetadata|CompileTypeMetadata): o.Expression { + summary: CompileTypeSummary, + metadata: CompileNgModuleMetadata|CompileDirectiveMetadata|CompilePipeMetadata| + CompileTypeMetadata): o.Expression { const expressions: o.Expression[] = [this.serializeSummary(summary)]; let providers: CompileProviderMetadata[] = []; if (metadata instanceof CompileNgModuleMetadata) { @@ -403,7 +407,8 @@ class ForJitSerializer { // i.e. we didn't generate .ngsummary.ts files for these. expressions.push( ...providers.filter(provider => !!provider.useClass).map(provider => this.serializeSummary({ - summaryKind: CompileSummaryKind.Injectable, type: provider.useClass + summaryKind: CompileSummaryKind.Injectable, + type: provider.useClass } as CompileTypeSummary))); return o.literalArr(expressions); } @@ -425,7 +430,9 @@ class ForJitSerializer { return new o.LiteralMapExpr(Object.keys(map).map( (key) => new o.LiteralMapEntry(key, visitValue(map[key], this, context), false))); } - visitPrimitive(value: any, context: any): any { return o.literal(value); } + visitPrimitive(value: any, context: any): any { + return o.literal(value); + } visitOther(value: any, context: any): any { if (value instanceof StaticSymbol) { return outputCtx.importExpr(value); @@ -441,7 +448,7 @@ class ForJitSerializer { class FromJsonDeserializer extends ValueTransformer { // TODO(issue/24571): remove '!'. - private symbols !: StaticSymbol[]; + private symbols!: StaticSymbol[]; constructor( private symbolCache: StaticSymbolCache, @@ -450,11 +457,11 @@ class FromJsonDeserializer extends ValueTransformer { } deserialize(libraryFileName: string, json: string): { - moduleName: string | null, + moduleName: string|null, summaries: Summary[], importAs: {symbol: StaticSymbol, importAs: StaticSymbol}[] } { - const data: {moduleName: string | null, summaries: any[], symbols: any[]} = JSON.parse(json); + const data: {moduleName: string|null, summaries: any[], symbols: any[]} = JSON.parse(json); const allImportAs: {symbol: StaticSymbol, importAs: StaticSymbol}[] = []; this.symbols = data.symbols.map( (serializedSymbol) => this.symbolCache.get( diff --git a/packages/compiler/src/ast_path.ts b/packages/compiler/src/ast_path.ts index cb019a47eb..2d1e3a95b1 100644 --- a/packages/compiler/src/ast_path.ts +++ b/packages/compiler/src/ast_path.ts @@ -26,23 +26,35 @@ export class AstPath { constructor(private path: T[], public position: number = -1) {} - get empty(): boolean { return !this.path || !this.path.length; } - get head(): T|undefined { return this.path[0]; } - get tail(): T|undefined { return this.path[this.path.length - 1]; } + get empty(): boolean { + return !this.path || !this.path.length; + } + get head(): T|undefined { + return this.path[0]; + } + get tail(): T|undefined { + return this.path[this.path.length - 1]; + } parentOf(node: T|undefined): T|undefined { return node && this.path[this.path.indexOf(node) - 1]; } - childOf(node: T): T|undefined { return this.path[this.path.indexOf(node) + 1]; } + childOf(node: T): T|undefined { + return this.path[this.path.indexOf(node) + 1]; + } - first(ctor: {new (...args: any[]): N}): N|undefined { + first(ctor: {new(...args: any[]): N}): N|undefined { for (let i = this.path.length - 1; i >= 0; i--) { let item = this.path[i]; if (item instanceof ctor) return item; } } - push(node: T) { this.path.push(node); } + push(node: T) { + this.path.push(node); + } - pop(): T { return this.path.pop() !; } + pop(): T { + return this.path.pop()!; + } } diff --git a/packages/compiler/src/compile_metadata.ts b/packages/compiler/src/compile_metadata.ts index 7d8b3be1f6..adba967933 100644 --- a/packages/compiler/src/compile_metadata.ts +++ b/packages/compiler/src/compile_metadata.ts @@ -24,8 +24,8 @@ export function sanitizeIdentifier(name: string): string { let _anonymousTypeIndex = 0; -export function identifierName(compileIdentifier: CompileIdentifierMetadata | null | undefined): - string|null { +export function identifierName(compileIdentifier: CompileIdentifierMetadata|null|undefined): string| + null { if (!compileIdentifier || !compileIdentifier.reference) { return null; } @@ -72,9 +72,13 @@ export function componentFactoryName(compType: any): string { return `${identifierName({reference: compType})}NgFactory`; } -export interface ProxyClass { setDelegate(delegate: any): void; } +export interface ProxyClass { + setDelegate(delegate: any): void; +} -export interface CompileIdentifierMetadata { reference: any; } +export interface CompileIdentifierMetadata { + reference: any; +} export enum CompileSummaryKind { Pipe, @@ -175,8 +179,8 @@ export class CompileStylesheetMetadata { styles: string[]; styleUrls: string[]; constructor( - {moduleUrl, styles, - styleUrls}: {moduleUrl?: string, styles?: string[], styleUrls?: string[]} = {}) { + {moduleUrl, styles, styleUrls}: + {moduleUrl?: string, styles?: string[], styleUrls?: string[]} = {}) { this.moduleUrl = moduleUrl || null; this.styles = _normalizeArray(styles); this.styleUrls = _normalizeArray(styleUrls); @@ -209,10 +213,21 @@ export class CompileTemplateMetadata { ngContentSelectors: string[]; interpolation: [string, string]|null; preserveWhitespaces: boolean; - constructor({encapsulation, template, templateUrl, htmlAst, styles, styleUrls, - externalStylesheets, animations, ngContentSelectors, interpolation, isInline, - preserveWhitespaces}: { - encapsulation: ViewEncapsulation | null, + constructor({ + encapsulation, + template, + templateUrl, + htmlAst, + styles, + styleUrls, + externalStylesheets, + animations, + ngContentSelectors, + interpolation, + isInline, + preserveWhitespaces + }: { + encapsulation: ViewEncapsulation|null, template: string|null, templateUrl: string|null, htmlAst: HtmlParseTreeResult|null, @@ -286,9 +301,27 @@ export interface CompileDirectiveSummary extends CompileTypeSummary { * Metadata regarding compilation of a directive. */ export class CompileDirectiveMetadata { - static create({isHost, type, isComponent, selector, exportAs, changeDetection, inputs, outputs, - host, providers, viewProviders, queries, guards, viewQueries, entryComponents, - template, componentViewType, rendererType, componentFactory}: { + static create({ + isHost, + type, + isComponent, + selector, + exportAs, + changeDetection, + inputs, + outputs, + host, + providers, + viewProviders, + queries, + guards, + viewQueries, + entryComponents, + template, + componentViewType, + rendererType, + componentFactory + }: { isHost: boolean, type: CompileTypeMetadata, isComponent: boolean, @@ -347,7 +380,10 @@ export class CompileDirectiveMetadata { return new CompileDirectiveMetadata({ isHost, type, - isComponent: !!isComponent, selector, exportAs, changeDetection, + isComponent: !!isComponent, + selector, + exportAs, + changeDetection, inputs: inputsMap, outputs: outputsMap, hostListeners, @@ -389,27 +425,29 @@ export class CompileDirectiveMetadata { rendererType: StaticSymbol|object|null; componentFactory: StaticSymbol|object|null; - constructor({isHost, - type, - isComponent, - selector, - exportAs, - changeDetection, - inputs, - outputs, - hostListeners, - hostProperties, - hostAttributes, - providers, - viewProviders, - queries, - guards, - viewQueries, - entryComponents, - template, - componentViewType, - rendererType, - componentFactory}: { + constructor({ + isHost, + type, + isComponent, + selector, + exportAs, + changeDetection, + inputs, + outputs, + hostListeners, + hostProperties, + hostAttributes, + providers, + viewProviders, + queries, + guards, + viewQueries, + entryComponents, + template, + componentViewType, + rendererType, + componentFactory + }: { isHost: boolean, type: CompileTypeMetadata, isComponent: boolean, @@ -534,7 +572,7 @@ export interface CompileNgModuleSummary extends CompileTypeSummary { export class CompileShallowModuleMetadata { // TODO(issue/24571): remove '!'. - type !: CompileTypeMetadata; + type!: CompileTypeMetadata; rawExports: any; rawImports: any; @@ -562,9 +600,21 @@ export class CompileNgModuleMetadata { transitiveModule: TransitiveCompileNgModuleMetadata; - constructor({type, providers, declaredDirectives, exportedDirectives, declaredPipes, - exportedPipes, entryComponents, bootstrapComponents, importedModules, - exportedModules, schemas, transitiveModule, id}: { + constructor({ + type, + providers, + declaredDirectives, + exportedDirectives, + declaredPipes, + exportedPipes, + entryComponents, + bootstrapComponents, + importedModules, + exportedModules, + schemas, + transitiveModule, + id + }: { type: CompileTypeMetadata, providers: CompileProviderMetadata[], declaredDirectives: CompileIdentifierMetadata[], @@ -595,7 +645,7 @@ export class CompileNgModuleMetadata { } toSummary(): CompileNgModuleSummary { - const module = this.transitiveModule !; + const module = this.transitiveModule!; return { summaryKind: CompileSummaryKind.NgModule, type: this.type, @@ -666,7 +716,7 @@ export class TransitiveCompileNgModuleMetadata { } } -function _normalizeArray(obj: any[] | undefined | null): any[] { +function _normalizeArray(obj: any[]|undefined|null): any[] { return obj || []; } @@ -698,7 +748,7 @@ export class ProviderMeta { } export function flatten(list: Array): T[] { - return list.reduce((flat: any[], item: T | T[]): T[] => { + return list.reduce((flat: any[], item: T|T[]): T[] => { const flatItem = Array.isArray(item) ? flatten(item) : item; return (flat).concat(flatItem); }, []); @@ -712,7 +762,7 @@ function jitSourceUrl(url: string) { export function templateSourceUrl( ngModuleType: CompileIdentifierMetadata, compMeta: {type: CompileIdentifierMetadata}, - templateMeta: {isInline: boolean, templateUrl: string | null}) { + templateMeta: {isInline: boolean, templateUrl: string|null}) { let url: string; if (templateMeta.isInline) { if (compMeta.type.reference instanceof StaticSymbol) { @@ -723,13 +773,13 @@ export function templateSourceUrl( url = `${identifierName(ngModuleType)}/${identifierName(compMeta.type)}.html`; } } else { - url = templateMeta.templateUrl !; + url = templateMeta.templateUrl!; } return compMeta.type.reference instanceof StaticSymbol ? url : jitSourceUrl(url); } export function sharedStylesheetJitUrl(meta: CompileStylesheetMetadata, id: number) { - const pathParts = meta.moduleUrl !.split(/\/\\/g); + const pathParts = meta.moduleUrl!.split(/\/\\/g); const baseName = pathParts[pathParts.length - 1]; return jitSourceUrl(`css/${id}${baseName}.ngstyle.js`); } diff --git a/packages/compiler/src/compiler_facade_interface.ts b/packages/compiler/src/compiler_facade_interface.ts index 79d06d41c1..e563344f23 100644 --- a/packages/compiler/src/compiler_facade_interface.ts +++ b/packages/compiler/src/compiler_facade_interface.ts @@ -22,7 +22,9 @@ * ``` */ -export interface ExportedCompilerFacade { ɵcompilerFacade: CompilerFacade; } +export interface ExportedCompilerFacade { + ɵcompilerFacade: CompilerFacade; +} export interface CompilerFacade { compilePipe(angularCoreEnv: CoreEnvironment, sourceMapUrl: string, meta: R3PipeMetadataFacade): @@ -44,13 +46,15 @@ export interface CompilerFacade { R3ResolvedDependencyType: typeof R3ResolvedDependencyType; R3FactoryTarget: typeof R3FactoryTarget; - ResourceLoader: {new (): ResourceLoader}; + ResourceLoader: {new(): ResourceLoader}; } -export interface CoreEnvironment { [name: string]: Function; } +export interface CoreEnvironment { + [name: string]: Function; +} export type ResourceLoader = { - get(url: string): Promise| string; + get(url: string): Promise|string; }; export type StringMap = { @@ -58,7 +62,7 @@ export type StringMap = { }; export type StringMapWithRename = { - [key: string]: string | [string, string]; + [key: string]: string|[string, string]; }; export type Provider = any; diff --git a/packages/compiler/src/compiler_util/expression_converter.ts b/packages/compiler/src/compiler_util/expression_converter.ts index b446f6a750..dd7c741d80 100644 --- a/packages/compiler/src/compiler_util/expression_converter.ts +++ b/packages/compiler/src/compiler_util/expression_converter.ts @@ -11,7 +11,9 @@ import {Identifiers} from '../identifiers'; import * as o from '../output/output_ast'; import {ParseSourceSpan} from '../parse_util'; -export class EventHandlerVars { static event = o.variable('$event'); } +export class EventHandlerVars { + static event = o.variable('$event'); +} export interface LocalResolver { getLocal(name: string): o.Expression|null; @@ -68,7 +70,7 @@ export type InterpolationFunction = (args: o.Expression[]) => o.Expression; * used in an action binding (e.g. an event handler). */ export function convertActionBinding( - localResolver: LocalResolver | null, implicitReceiver: o.Expression, action: cdAst.AST, + localResolver: LocalResolver|null, implicitReceiver: o.Expression, action: cdAst.AST, bindingId: string, interpolationFunction?: InterpolationFunction, baseSourceSpan?: ParseSourceSpan, implicitReceiverAccesses?: Set): ConvertActionBindingResult { @@ -110,7 +112,7 @@ export function convertActionBinding( } const lastIndex = actionStmts.length - 1; - let preventDefaultVar: o.ReadVarExpr = null !; + let preventDefaultVar: o.ReadVarExpr = null!; if (lastIndex >= 0) { const lastStatement = actionStmts[lastIndex]; const returnExpr = convertStmtIntoExpression(lastStatement); @@ -126,7 +128,9 @@ export function convertActionBinding( return new ConvertActionBindingResult(actionStmts, preventDefaultVar); } -export interface BuiltinConverter { (args: o.Expression[]): o.Expression; } +export interface BuiltinConverter { + (args: o.Expression[]): o.Expression; +} export interface BuiltinConverterFactory { createLiteralArrayConverter(argCount: number): BuiltinConverter; @@ -158,7 +162,7 @@ export enum BindingForm { * `convertPropertyBindingBuiltins`. */ export function convertPropertyBinding( - localResolver: LocalResolver | null, implicitReceiver: o.Expression, + localResolver: LocalResolver|null, implicitReceiver: o.Expression, expressionWithoutBuiltins: cdAst.AST, bindingId: string, form: BindingForm, interpolationFunction?: InterpolationFunction): ConvertPropertyBindingResult { if (!localResolver) { @@ -284,7 +288,9 @@ function convertToStatementIfNeeded(mode: _Mode, expr: o.Expression): o.Expressi } class _BuiltinAstConverter extends cdAst.AstTransformer { - constructor(private _converterFactory: BuiltinConverterFactory) { super(); } + constructor(private _converterFactory: BuiltinConverterFactory) { + super(); + } visitPipe(ast: cdAst.BindingPipe, context: any): any { const args = [ast.exp, ...ast.args].map(ast => ast.visit(this, context)); return new BuiltinFunctionCall( @@ -384,9 +390,10 @@ class _AstToIrVisitor implements cdAst.AstVisitor { visitConditional(ast: cdAst.Conditional, mode: _Mode): any { const value: o.Expression = this._visit(ast.condition, _Mode.Expression); return convertToStatementIfNeeded( - mode, value.conditional( - this._visit(ast.trueExp, _Mode.Expression), - this._visit(ast.falseExp, _Mode.Expression), this.convertSourceSpan(ast.span))); + mode, + value.conditional( + this._visit(ast.trueExp, _Mode.Expression), this._visit(ast.falseExp, _Mode.Expression), + this.convertSourceSpan(ast.span))); } visitPipe(ast: cdAst.BindingPipe, mode: _Mode): any { @@ -400,7 +407,7 @@ class _AstToIrVisitor implements cdAst.AstVisitor { if (ast instanceof BuiltinFunctionCall) { fnResult = ast.converter(convertedArgs); } else { - fnResult = this._visit(ast.target !, _Mode.Expression) + fnResult = this._visit(ast.target!, _Mode.Expression) .callFn(convertedArgs, this.convertSourceSpan(ast.span)); } return convertToStatementIfNeeded(mode, fnResult); @@ -467,7 +474,9 @@ class _AstToIrVisitor implements cdAst.AstVisitor { mode, o.literal(ast.value, type, this.convertSourceSpan(ast.span))); } - private _getLocal(name: string): o.Expression|null { return this._localResolver.getLocal(name); } + private _getLocal(name: string): o.Expression|null { + return this._localResolver.getLocal(name); + } visitMethodCall(ast: cdAst.MethodCall, mode: _Mode): any { if (ast.receiver instanceof cdAst.ImplicitReceiver && ast.name == '$any') { @@ -558,8 +567,8 @@ class _AstToIrVisitor implements cdAst.AstVisitor { // Otherwise it's an error. const receiver = ast.name; const value = (ast.value instanceof cdAst.PropertyRead) ? ast.value.name : undefined; - throw new Error( - `Cannot assign value "${value}" to template variable "${receiver}". Template variables are read-only.`); + throw new Error(`Cannot assign value "${value}" to template variable "${ + receiver}". Template variables are read-only.`); } } } @@ -579,7 +588,9 @@ class _AstToIrVisitor implements cdAst.AstVisitor { return this.convertSafeAccess(ast, this.leftMostSafeNode(ast), mode); } - visitAll(asts: cdAst.AST[], mode: _Mode): any { return asts.map(ast => this._visit(ast, mode)); } + visitAll(asts: cdAst.AST[], mode: _Mode): any { + return asts.map(ast => this._visit(ast, mode)); + } visitQuote(ast: cdAst.Quote, mode: _Mode): any { throw new Error(`Quotes are not supported for evaluation! @@ -634,7 +645,7 @@ class _AstToIrVisitor implements cdAst.AstVisitor { // which comes in as leftMostSafe to this routine. let guardedExpression = this._visit(leftMostSafe.receiver, _Mode.Expression); - let temporary: o.ReadVarExpr = undefined !; + let temporary: o.ReadVarExpr = undefined!; if (this.needsTemporary(leftMostSafe.receiver)) { // If the expression has method calls or pipes then we need to save the result into a // temporary variable to avoid calling stateful or impure code more than once. @@ -652,14 +663,16 @@ class _AstToIrVisitor implements cdAst.AstVisitor { // leftMostNode with its unguarded version in the call to `this.visit()`. if (leftMostSafe instanceof cdAst.SafeMethodCall) { this._nodeMap.set( - leftMostSafe, new cdAst.MethodCall( - leftMostSafe.span, leftMostSafe.sourceSpan, leftMostSafe.receiver, - leftMostSafe.name, leftMostSafe.args)); + leftMostSafe, + new cdAst.MethodCall( + leftMostSafe.span, leftMostSafe.sourceSpan, leftMostSafe.receiver, leftMostSafe.name, + leftMostSafe.args)); } else { this._nodeMap.set( - leftMostSafe, new cdAst.PropertyRead( - leftMostSafe.span, leftMostSafe.sourceSpan, leftMostSafe.receiver, - leftMostSafe.name)); + leftMostSafe, + new cdAst.PropertyRead( + leftMostSafe.span, leftMostSafe.sourceSpan, leftMostSafe.receiver, + leftMostSafe.name)); } // Recursively convert the node now without the guarded member access. @@ -690,25 +703,63 @@ class _AstToIrVisitor implements cdAst.AstVisitor { return (this._nodeMap.get(ast) || ast).visit(visitor); }; return ast.visit({ - visitBinary(ast: cdAst.Binary) { return null; }, - visitChain(ast: cdAst.Chain) { return null; }, - visitConditional(ast: cdAst.Conditional) { return null; }, - visitFunctionCall(ast: cdAst.FunctionCall) { return null; }, - visitImplicitReceiver(ast: cdAst.ImplicitReceiver) { return null; }, - visitInterpolation(ast: cdAst.Interpolation) { return null; }, - visitKeyedRead(ast: cdAst.KeyedRead) { return visit(this, ast.obj); }, - visitKeyedWrite(ast: cdAst.KeyedWrite) { return null; }, - visitLiteralArray(ast: cdAst.LiteralArray) { return null; }, - visitLiteralMap(ast: cdAst.LiteralMap) { return null; }, - visitLiteralPrimitive(ast: cdAst.LiteralPrimitive) { return null; }, - visitMethodCall(ast: cdAst.MethodCall) { return visit(this, ast.receiver); }, - visitPipe(ast: cdAst.BindingPipe) { return null; }, - visitPrefixNot(ast: cdAst.PrefixNot) { return null; }, - visitNonNullAssert(ast: cdAst.NonNullAssert) { return null; }, - visitPropertyRead(ast: cdAst.PropertyRead) { return visit(this, ast.receiver); }, - visitPropertyWrite(ast: cdAst.PropertyWrite) { return null; }, - visitQuote(ast: cdAst.Quote) { return null; }, - visitSafeMethodCall(ast: cdAst.SafeMethodCall) { return visit(this, ast.receiver) || ast; }, + visitBinary(ast: cdAst.Binary) { + return null; + }, + visitChain(ast: cdAst.Chain) { + return null; + }, + visitConditional(ast: cdAst.Conditional) { + return null; + }, + visitFunctionCall(ast: cdAst.FunctionCall) { + return null; + }, + visitImplicitReceiver(ast: cdAst.ImplicitReceiver) { + return null; + }, + visitInterpolation(ast: cdAst.Interpolation) { + return null; + }, + visitKeyedRead(ast: cdAst.KeyedRead) { + return visit(this, ast.obj); + }, + visitKeyedWrite(ast: cdAst.KeyedWrite) { + return null; + }, + visitLiteralArray(ast: cdAst.LiteralArray) { + return null; + }, + visitLiteralMap(ast: cdAst.LiteralMap) { + return null; + }, + visitLiteralPrimitive(ast: cdAst.LiteralPrimitive) { + return null; + }, + visitMethodCall(ast: cdAst.MethodCall) { + return visit(this, ast.receiver); + }, + visitPipe(ast: cdAst.BindingPipe) { + return null; + }, + visitPrefixNot(ast: cdAst.PrefixNot) { + return null; + }, + visitNonNullAssert(ast: cdAst.NonNullAssert) { + return null; + }, + visitPropertyRead(ast: cdAst.PropertyRead) { + return visit(this, ast.receiver); + }, + visitPropertyWrite(ast: cdAst.PropertyWrite) { + return null; + }, + visitQuote(ast: cdAst.Quote) { + return null; + }, + visitSafeMethodCall(ast: cdAst.SafeMethodCall) { + return visit(this, ast.receiver) || ast; + }, visitSafePropertyRead(ast: cdAst.SafePropertyRead) { return visit(this, ast.receiver) || ast; } @@ -726,29 +777,66 @@ class _AstToIrVisitor implements cdAst.AstVisitor { return ast.some(ast => visit(visitor, ast)); }; return ast.visit({ - visitBinary(ast: cdAst.Binary): - boolean{return visit(this, ast.left) || visit(this, ast.right);}, - visitChain(ast: cdAst.Chain) { return false; }, - visitConditional(ast: cdAst.Conditional): - boolean{return visit(this, ast.condition) || visit(this, ast.trueExp) || - visit(this, ast.falseExp);}, - visitFunctionCall(ast: cdAst.FunctionCall) { return true; }, - visitImplicitReceiver(ast: cdAst.ImplicitReceiver) { return false; }, - visitInterpolation(ast: cdAst.Interpolation) { return visitSome(this, ast.expressions); }, - visitKeyedRead(ast: cdAst.KeyedRead) { return false; }, - visitKeyedWrite(ast: cdAst.KeyedWrite) { return false; }, - visitLiteralArray(ast: cdAst.LiteralArray) { return true; }, - visitLiteralMap(ast: cdAst.LiteralMap) { return true; }, - visitLiteralPrimitive(ast: cdAst.LiteralPrimitive) { return false; }, - visitMethodCall(ast: cdAst.MethodCall) { return true; }, - visitPipe(ast: cdAst.BindingPipe) { return true; }, - visitPrefixNot(ast: cdAst.PrefixNot) { return visit(this, ast.expression); }, - visitNonNullAssert(ast: cdAst.PrefixNot) { return visit(this, ast.expression); }, - visitPropertyRead(ast: cdAst.PropertyRead) { return false; }, - visitPropertyWrite(ast: cdAst.PropertyWrite) { return false; }, - visitQuote(ast: cdAst.Quote) { return false; }, - visitSafeMethodCall(ast: cdAst.SafeMethodCall) { return true; }, - visitSafePropertyRead(ast: cdAst.SafePropertyRead) { return false; } + visitBinary(ast: cdAst.Binary): boolean { + return visit(this, ast.left) || visit(this, ast.right); + }, + visitChain(ast: cdAst.Chain) { + return false; + }, + visitConditional(ast: cdAst.Conditional): boolean { + return visit(this, ast.condition) || visit(this, ast.trueExp) || visit(this, ast.falseExp); + }, + visitFunctionCall(ast: cdAst.FunctionCall) { + return true; + }, + visitImplicitReceiver(ast: cdAst.ImplicitReceiver) { + return false; + }, + visitInterpolation(ast: cdAst.Interpolation) { + return visitSome(this, ast.expressions); + }, + visitKeyedRead(ast: cdAst.KeyedRead) { + return false; + }, + visitKeyedWrite(ast: cdAst.KeyedWrite) { + return false; + }, + visitLiteralArray(ast: cdAst.LiteralArray) { + return true; + }, + visitLiteralMap(ast: cdAst.LiteralMap) { + return true; + }, + visitLiteralPrimitive(ast: cdAst.LiteralPrimitive) { + return false; + }, + visitMethodCall(ast: cdAst.MethodCall) { + return true; + }, + visitPipe(ast: cdAst.BindingPipe) { + return true; + }, + visitPrefixNot(ast: cdAst.PrefixNot) { + return visit(this, ast.expression); + }, + visitNonNullAssert(ast: cdAst.PrefixNot) { + return visit(this, ast.expression); + }, + visitPropertyRead(ast: cdAst.PropertyRead) { + return false; + }, + visitPropertyWrite(ast: cdAst.PropertyWrite) { + return false; + }, + visitQuote(ast: cdAst.Quote) { + return false; + }, + visitSafeMethodCall(ast: cdAst.SafeMethodCall) { + return true; + }, + visitSafePropertyRead(ast: cdAst.SafePropertyRead) { + return false; + } }); } diff --git a/packages/compiler/src/config.ts b/packages/compiler/src/config.ts index c3f50e6a33..69e0589e9f 100644 --- a/packages/compiler/src/config.ts +++ b/packages/compiler/src/config.ts @@ -20,16 +20,21 @@ export class CompilerConfig { public preserveWhitespaces: boolean; public strictInjectionParameters: boolean; - constructor( - {defaultEncapsulation = ViewEncapsulation.Emulated, useJit = true, jitDevMode = false, - missingTranslation = null, preserveWhitespaces, strictInjectionParameters}: { - defaultEncapsulation?: ViewEncapsulation, - useJit?: boolean, - jitDevMode?: boolean, - missingTranslation?: MissingTranslationStrategy|null, - preserveWhitespaces?: boolean, - strictInjectionParameters?: boolean, - } = {}) { + constructor({ + defaultEncapsulation = ViewEncapsulation.Emulated, + useJit = true, + jitDevMode = false, + missingTranslation = null, + preserveWhitespaces, + strictInjectionParameters + }: { + defaultEncapsulation?: ViewEncapsulation, + useJit?: boolean, + jitDevMode?: boolean, + missingTranslation?: MissingTranslationStrategy|null, + preserveWhitespaces?: boolean, + strictInjectionParameters?: boolean, + } = {}) { this.defaultEncapsulation = defaultEncapsulation; this.useJit = !!useJit; this.jitDevMode = !!jitDevMode; @@ -40,6 +45,6 @@ export class CompilerConfig { } export function preserveWhitespacesDefault( - preserveWhitespacesOption: boolean | null, defaultSetting = false): boolean { + preserveWhitespacesOption: boolean|null, defaultSetting = false): boolean { return preserveWhitespacesOption === null ? defaultSetting : preserveWhitespacesOption; } diff --git a/packages/compiler/src/constant_pool.ts b/packages/compiler/src/constant_pool.ts index 7be8ff12a2..ae58887171 100644 --- a/packages/compiler/src/constant_pool.ts +++ b/packages/compiler/src/constant_pool.ts @@ -7,7 +7,7 @@ */ import * as o from './output/output_ast'; -import {OutputContext, error} from './util'; +import {error, OutputContext} from './util'; const CONSTANT_PREFIX = '_c'; @@ -21,7 +21,12 @@ const CONSTANT_PREFIX = '_c'; */ const UNKNOWN_VALUE_KEY = o.variable(''); -export const enum DefinitionKind {Injector, Directive, Component, Pipe} +export const enum DefinitionKind { + Injector, + Directive, + Component, + Pipe +} /** * Context to use when producing a key. @@ -43,7 +48,7 @@ class FixupExpression extends o.Expression { private original: o.Expression; // TODO(issue/24571): remove '!'. - shared !: boolean; + shared!: boolean; constructor(public resolved: o.Expression) { super(resolved.type); @@ -64,7 +69,9 @@ class FixupExpression extends o.Expression { return e instanceof FixupExpression && this.resolved.isEquivalent(e.resolved); } - isConstant() { return true; } + isConstant() { + return true; + } fixup(expression: o.Expression) { this.resolved = expression; @@ -169,7 +176,7 @@ export class ConstantPool { const resultExpressions = values.map( (e, index) => e.isConstant() ? this.getConstLiteral(e, true) : o.variable(`a${index}`)); const parameters = - resultExpressions.filter(isVariable).map(e => new o.FnParam(e.name !, o.DYNAMIC_TYPE)); + resultExpressions.filter(isVariable).map(e => new o.FnParam(e.name!, o.DYNAMIC_TYPE)); const pureFunctionDeclaration = o.fn(parameters, [new o.ReturnStatement(resultMap(resultExpressions))], o.INFERRED_TYPE); const name = this.freshName(); @@ -190,7 +197,9 @@ export class ConstantPool { * a digit so the prefix should be a constant string (not based on user input) and * must not end in a digit. */ - uniqueName(prefix: string): string { return `${prefix}${this.nextNameIndex++}`; } + uniqueName(prefix: string): string { + return `${prefix}${this.nextNameIndex++}`; + } private definitionsOf(kind: DefinitionKind): Map { switch (kind) { @@ -222,7 +231,9 @@ export class ConstantPool { return ''; } - private freshName(): string { return this.uniqueName(CONSTANT_PREFIX); } + private freshName(): string { + return this.uniqueName(CONSTANT_PREFIX); + } private keyOf(expression: o.Expression) { return expression.visitExpression(new KeyVisitor(), KEY_CONTEXT); @@ -259,7 +270,9 @@ class KeyVisitor implements o.ExpressionVisitor { `EX:${ast.value.runtime.name}`; } - visitReadVarExpr(node: o.ReadVarExpr) { return `VAR:${node.name}`; } + visitReadVarExpr(node: o.ReadVarExpr) { + return `VAR:${node.name}`; + } visitTypeofExpr(node: o.TypeofExpr, context: any): string { return `TYPEOF:${node.expr.visitExpression(this, context)}`; @@ -284,7 +297,7 @@ class KeyVisitor implements o.ExpressionVisitor { visitLocalizedString = invalid; } -function invalid(this: o.ExpressionVisitor, arg: o.Expression | o.Statement): never { +function invalid(this: o.ExpressionVisitor, arg: o.Expression|o.Statement): never { throw new Error( `Invalid state: Visitor ${this.constructor.name} doesn't handle ${arg.constructor.name}`); } diff --git a/packages/compiler/src/core.ts b/packages/compiler/src/core.ts index 70da45b3aa..f5d29c5d59 100644 --- a/packages/compiler/src/core.ts +++ b/packages/compiler/src/core.ts @@ -14,12 +14,16 @@ import {CssSelector} from './selector'; -export interface Inject { token: any; } +export interface Inject { + token: any; +} export const createInject = makeMetadataFactory('Inject', (token: any) => ({token})); export const createInjectionToken = makeMetadataFactory( 'InjectionToken', (desc: string) => ({_desc: desc, ɵprov: undefined})); -export interface Attribute { attributeName?: string; } +export interface Attribute { + attributeName?: string; +} export const createAttribute = makeMetadataFactory('Attribute', (attributeName?: string) => ({attributeName})); @@ -37,14 +41,17 @@ export const createContentChildren = makeMetadataFactory( (selector?: any, data: any = {}) => ({selector, first: false, isViewQuery: false, descendants: false, ...data})); export const createContentChild = makeMetadataFactory( - 'ContentChild', (selector?: any, data: any = {}) => - ({selector, first: true, isViewQuery: false, descendants: true, ...data})); + 'ContentChild', + (selector?: any, data: any = {}) => + ({selector, first: true, isViewQuery: false, descendants: true, ...data})); export const createViewChildren = makeMetadataFactory( - 'ViewChildren', (selector?: any, data: any = {}) => - ({selector, first: false, isViewQuery: true, descendants: true, ...data})); + 'ViewChildren', + (selector?: any, data: any = {}) => + ({selector, first: false, isViewQuery: true, descendants: true, ...data})); export const createViewChild = makeMetadataFactory( - 'ViewChild', (selector: any, data: any) => - ({selector, first: true, isViewQuery: true, descendants: true, ...data})); + 'ViewChild', + (selector: any, data: any) => + ({selector, first: true, isViewQuery: true, descendants: true, ...data})); export interface Directive { selector?: string; @@ -94,15 +101,21 @@ export interface Pipe { } export const createPipe = makeMetadataFactory('Pipe', (p: Pipe) => ({pure: true, ...p})); -export interface Input { bindingPropertyName?: string; } +export interface Input { + bindingPropertyName?: string; +} export const createInput = makeMetadataFactory('Input', (bindingPropertyName?: string) => ({bindingPropertyName})); -export interface Output { bindingPropertyName?: string; } +export interface Output { + bindingPropertyName?: string; +} export const createOutput = makeMetadataFactory( 'Output', (bindingPropertyName?: string) => ({bindingPropertyName})); -export interface HostBinding { hostPropertyName?: string; } +export interface HostBinding { + hostPropertyName?: string; +} export const createHostBinding = makeMetadataFactory( 'HostBinding', (hostPropertyName?: string) => ({hostPropertyName})); @@ -140,7 +153,9 @@ export interface Injectable { } export const createInjectable = makeMetadataFactory('Injectable', (injectable: Injectable = {}) => injectable); -export interface SchemaMetadata { name: string; } +export interface SchemaMetadata { + name: string; +} export const CUSTOM_ELEMENTS_SCHEMA: SchemaMetadata = { name: 'custom-elements' @@ -155,7 +170,9 @@ export const createSelf = makeMetadataFactory('Self'); export const createSkipSelf = makeMetadataFactory('SkipSelf'); export const createHost = makeMetadataFactory('Host'); -export interface Type extends Function { new (...args: any[]): any; } +export interface Type extends Function { + new(...args: any[]): any; +} export const Type = Function; export enum SecurityContext { @@ -240,7 +257,10 @@ export const enum InjectFlags { Optional = 1 << 3, } -export const enum ArgumentType {Inline = 0, Dynamic = 1} +export const enum ArgumentType { + Inline = 0, + Dynamic = 1 +} export const enum BindingFlags { TypeElementAttribute = 1 << 0, @@ -255,7 +275,10 @@ export const enum BindingFlags { Types = TypeElementAttribute | TypeElementClass | TypeElementStyle | TypeProperty } -export const enum QueryBindingType {First = 0, All = 1} +export const enum QueryBindingType { + First = 0, + All = 1 +} export const enum QueryValueType { ElementRef = 0, @@ -324,7 +347,7 @@ export const enum SelectorFlags { // These are a copy the CSS types from core/src/render3/interfaces/projection.ts // They are duplicated here as they cannot be directly referenced from core. -export type R3CssSelector = (string | SelectorFlags)[]; +export type R3CssSelector = (string|SelectorFlags)[]; export type R3CssSelectorList = R3CssSelector[]; function parserSelectorToSimpleSelector(selector: CssSelector): R3CssSelector { @@ -363,7 +386,7 @@ function parserSelectorToR3Selector(selector: CssSelector): R3CssSelector { return positive.concat(...negative); } -export function parseSelectorToR3Selector(selector: string | null): R3CssSelectorList { +export function parseSelectorToR3Selector(selector: string|null): R3CssSelectorList { return selector ? CssSelector.parse(selector).map(parserSelectorToR3Selector) : []; } diff --git a/packages/compiler/src/css_parser/css_ast.ts b/packages/compiler/src/css_parser/css_ast.ts index b217b089b6..08e86510a5 100644 --- a/packages/compiler/src/css_parser/css_ast.ts +++ b/packages/compiler/src/css_parser/css_ast.ts @@ -46,8 +46,12 @@ export interface CssAstVisitor { export abstract class CssAst { constructor(public location: ParseSourceSpan) {} - get start(): ParseLocation { return this.location.start; } - get end(): ParseLocation { return this.location.end; } + get start(): ParseLocation { + return this.location.start; + } + get end(): ParseLocation { + return this.location.end; + } abstract visit(visitor: CssAstVisitor, context?: any): any; } @@ -55,11 +59,15 @@ export class CssStyleValueAst extends CssAst { constructor(location: ParseSourceSpan, public tokens: CssToken[], public strValue: string) { super(location); } - visit(visitor: CssAstVisitor, context?: any): any { return visitor.visitCssValue(this); } + visit(visitor: CssAstVisitor, context?: any): any { + return visitor.visitCssValue(this); + } } export abstract class CssRuleAst extends CssAst { - constructor(location: ParseSourceSpan) { super(location); } + constructor(location: ParseSourceSpan) { + super(location); + } } export class CssBlockRuleAst extends CssRuleAst { @@ -158,7 +166,9 @@ export class CssDefinitionAst extends CssAst { } export abstract class CssSelectorPartAst extends CssAst { - constructor(location: ParseSourceSpan) { super(location); } + constructor(location: ParseSourceSpan) { + super(location); + } } export class CssSelectorAst extends CssSelectorPartAst { @@ -195,8 +205,12 @@ export class CssPseudoSelectorAst extends CssSelectorPartAst { } export class CssBlockAst extends CssAst { - constructor(location: ParseSourceSpan, public entries: CssAst[]) { super(location); } - visit(visitor: CssAstVisitor, context?: any): any { return visitor.visitCssBlock(this, context); } + constructor(location: ParseSourceSpan, public entries: CssAst[]) { + super(location); + } + visit(visitor: CssAstVisitor, context?: any): any { + return visitor.visitCssBlock(this, context); + } } /* @@ -213,7 +227,9 @@ export class CssStylesBlockAst extends CssBlockAst { } export class CssStyleSheetAst extends CssAst { - constructor(location: ParseSourceSpan, public rules: CssAst[]) { super(location); } + constructor(location: ParseSourceSpan, public rules: CssAst[]) { + super(location); + } visit(visitor: CssAstVisitor, context?: any): any { return visitor.visitCssStyleSheet(this, context); } diff --git a/packages/compiler/src/css_parser/css_lexer.ts b/packages/compiler/src/css_parser/css_lexer.ts index ea574dbf95..31fc632eba 100644 --- a/packages/compiler/src/css_parser/css_lexer.ts +++ b/packages/compiler/src/css_parser/css_lexer.ts @@ -117,7 +117,7 @@ function _trackWhitespace(mode: CssLexerMode) { export class CssScanner { // TODO(issue/24571): remove '!'. - peek !: number; + peek!: number; peekPeek: number; length: number = 0; index: number = -1; @@ -135,7 +135,9 @@ export class CssScanner { this.advance(); } - getMode(): CssLexerMode { return this._currentMode; } + getMode(): CssLexerMode { + return this._currentMode; + } setMode(mode: CssLexerMode) { if (this._currentMode != mode) { @@ -198,7 +200,7 @@ export class CssScanner { const previousLine = this.line; const previousColumn = this.column; - let next: CssToken = undefined !; + let next: CssToken = undefined!; const output = this.scan(); if (output != null) { // just incase the inner scan method returned an error @@ -236,9 +238,10 @@ export class CssScanner { } error = cssScannerError( - next, generateErrorMessage( - this.input, errorMessage, next.strValue, previousIndex, previousLine, - previousColumn)); + next, + generateErrorMessage( + this.input, errorMessage, next.strValue, previousIndex, previousLine, + previousColumn)); } return new LexedCssResult(error, next); @@ -254,7 +257,7 @@ export class CssScanner { const token = this._scan(); if (token == null) return null; - const error = this._currentError !; + const error = this._currentError!; this._currentError = null; if (!trackWS) { @@ -461,7 +464,7 @@ export class CssScanner { const startingColumn = this.column; this.advance(); if (isIdentifierStart(this.peek, this.peekPeek)) { - const ident = this.scanIdentifier() !; + const ident = this.scanIdentifier()!; const strValue = '@' + ident.strValue; return new CssToken(start, startingColumn, this.line, CssTokenType.AtKeyword, strValue); } else { diff --git a/packages/compiler/src/css_parser/css_parser.ts b/packages/compiler/src/css_parser/css_parser.ts index 2650e3deec..eeebbf8d61 100644 --- a/packages/compiler/src/css_parser/css_parser.ts +++ b/packages/compiler/src/css_parser/css_parser.ts @@ -9,7 +9,7 @@ import * as chars from '../chars'; import {ParseError, ParseLocation, ParseSourceFile, ParseSourceSpan} from '../parse_util'; -import {BlockType, CssAst, CssAtRulePredicateAst, CssBlockAst, CssBlockDefinitionRuleAst, CssBlockRuleAst, CssDefinitionAst, CssInlineRuleAst, CssKeyframeDefinitionAst, CssKeyframeRuleAst, CssMediaQueryRuleAst, CssPseudoSelectorAst, CssRuleAst, CssSelectorAst, CssSelectorRuleAst, CssSimpleSelectorAst, CssStyleSheetAst, CssStyleValueAst, CssStylesBlockAst, CssUnknownRuleAst, CssUnknownTokenListAst, mergeTokens} from './css_ast'; +import {BlockType, CssAst, CssAtRulePredicateAst, CssBlockAst, CssBlockDefinitionRuleAst, CssBlockRuleAst, CssDefinitionAst, CssInlineRuleAst, CssKeyframeDefinitionAst, CssKeyframeRuleAst, CssMediaQueryRuleAst, CssPseudoSelectorAst, CssRuleAst, CssSelectorAst, CssSelectorRuleAst, CssSimpleSelectorAst, CssStylesBlockAst, CssStyleSheetAst, CssStyleValueAst, CssUnknownRuleAst, CssUnknownTokenListAst, mergeTokens} from './css_ast'; import {CssLexer, CssLexerMode, CssScanner, CssToken, CssTokenType, generateErrorMessage, getRawMessage, isNewline} from './css_lexer'; const SPACE_OPERATOR = ' '; @@ -84,11 +84,11 @@ export class ParsedCssResult { export class CssParser { private _errors: CssParseError[] = []; // TODO(issue/24571): remove '!'. - private _file !: ParseSourceFile; + private _file!: ParseSourceFile; // TODO(issue/24571): remove '!'. - private _scanner !: CssScanner; + private _scanner!: CssScanner; // TODO(issue/24571): remove '!'. - private _lastToken !: CssToken; + private _lastToken!: CssToken; /** * @param css the CSS code that will be parsed @@ -125,11 +125,13 @@ export class CssParser { // EOF token that was emitted sometime during the lexing span = this._generateSourceSpan(firstRule, this._lastToken); } - return new CssStyleSheetAst(span !, results); + return new CssStyleSheetAst(span!, results); } /** @internal */ - _getSourceContent(): string { return this._scanner != null ? this._scanner.input : ''; } + _getSourceContent(): string { + return this._scanner != null ? this._scanner.input : ''; + } /** @internal */ _extractSourceContent(start: number, end: number): string { @@ -159,9 +161,9 @@ export class CssParser { let endColumn: number = -1; let endIndex: number = -1; if (end instanceof CssAst) { - endLine = end.location.end.line !; - endColumn = end.location.end.col !; - endIndex = end.location.end.offset !; + endLine = end.location.end.line!; + endColumn = end.location.end.col!; + endIndex = end.location.end.offset!; } else if (end instanceof CssToken) { endLine = end.line; endColumn = end.column; @@ -253,7 +255,7 @@ export class CssParser { case BlockType.Viewport: case BlockType.FontFace: - block = this._parseStyleBlock(delimiters) !; + block = this._parseStyleBlock(delimiters)!; span = this._generateSourceSpan(startToken, block); return new CssBlockRuleAst(span, type, block); @@ -293,7 +295,7 @@ export class CssParser { span = this._generateSourceSpan(startToken, tokens[tokens.length - 1]); query = new CssAtRulePredicateAst(span, strValue, tokens); block = this._parseBlock(delimiters); - strValue = this._extractSourceContent(start, block.end.offset !); + strValue = this._extractSourceContent(start, block.end.offset!); span = this._generateSourceSpan(startToken, block); return new CssBlockDefinitionRuleAst(span, strValue, type, query, block); @@ -310,11 +312,15 @@ export class CssParser { token); this._collectUntilDelim(delimiters | LBRACE_DELIM_FLAG | SEMICOLON_DELIM_FLAG) - .forEach((token) => { listOfTokens.push(token); }); + .forEach((token) => { + listOfTokens.push(token); + }); if (this._scanner.peek == chars.$LBRACE) { listOfTokens.push(this._consume(CssTokenType.Character, '{')); this._collectUntilDelim(delimiters | RBRACE_DELIM_FLAG | LBRACE_DELIM_FLAG) - .forEach((token) => { listOfTokens.push(token); }); + .forEach((token) => { + listOfTokens.push(token); + }); listOfTokens.push(this._consume(CssTokenType.Character, '}')); } endToken = listOfTokens[listOfTokens.length - 1]; @@ -339,7 +345,9 @@ export class CssParser { const innerTokens: CssToken[] = []; selectors.forEach((selector: CssSelectorAst) => { selector.selectorParts.forEach((part: CssSimpleSelectorAst) => { - part.tokens.forEach((token: CssToken) => { innerTokens.push(token); }); + part.tokens.forEach((token: CssToken) => { + innerTokens.push(token); + }); }); }); const endToken = innerTokens[innerTokens.length - 1]; @@ -376,7 +384,7 @@ export class CssParser { /** @internal */ _scan(): CssToken { - const output = this._scanner.scan() !; + const output = this._scanner.scan()!; const token = output.token; const error = output.error; if (error != null) { @@ -387,7 +395,9 @@ export class CssParser { } /** @internal */ - _getScannerIndex(): number { return this._scanner.index; } + _getScannerIndex(): number { + return this._scanner.index; + } /** @internal */ _consume(type: CssTokenType, value: string|null = null): CssToken { @@ -432,7 +442,7 @@ export class CssParser { } const stylesBlock = this._parseStyleBlock(delimiters | RBRACE_DELIM_FLAG); const span = this._generateSourceSpan(stepTokens[0], stylesBlock); - const ast = new CssKeyframeDefinitionAst(span, stepTokens, stylesBlock !); + const ast = new CssKeyframeDefinitionAst(span, stepTokens, stylesBlock!); this._scanner.setMode(CssLexerMode.BLOCK); return ast; @@ -523,7 +533,7 @@ export class CssParser { const selectorCssTokens: CssToken[] = []; const pseudoSelectors: CssPseudoSelectorAst[] = []; - let previousToken: CssToken = undefined !; + let previousToken: CssToken = undefined!; const selectorPartDelimiters = delimiters | SPACE_DELIM_FLAG; let loopOverSelector = !characterContainsDelimiter(this._scanner.peek, selectorPartDelimiters); @@ -576,7 +586,8 @@ export class CssParser { hasAttributeError || this._scanner.getMode() == CssLexerMode.ATTRIBUTE_SELECTOR; if (hasAttributeError) { this._error( - `Unbalanced CSS attribute selector at column ${previousToken.line}:${previousToken.column}`, + `Unbalanced CSS attribute selector at column ${previousToken.line}:${ + previousToken.column}`, previousToken); } @@ -671,8 +682,8 @@ export class CssParser { endTokenOrAst = operator; } - const span = this._generateSourceSpan(startTokenOrAst !, endTokenOrAst); - return new CssSimpleSelectorAst(span, selectorCssTokens, strValue, pseudoSelectors, operator !); + const span = this._generateSourceSpan(startTokenOrAst!, endTokenOrAst); + return new CssSimpleSelectorAst(span, selectorCssTokens, strValue, pseudoSelectors, operator!); } /** @internal */ @@ -701,7 +712,7 @@ export class CssParser { const tokens: CssToken[] = []; let wsStr = ''; - let previous: CssToken = undefined !; + let previous: CssToken = undefined!; while (!characterContainsDelimiter(this._scanner.peek, delimiters)) { let token: CssToken; if (previous != null && previous.type == CssTokenType.Identifier && @@ -841,7 +852,9 @@ export class CssParser { const remainingTokens = this._collectUntilDelim( delimiters | COLON_DELIM_FLAG | SEMICOLON_DELIM_FLAG, CssTokenType.Identifier); if (remainingTokens.length > 0) { - remainingTokens.forEach((token) => { propStr.push(token.strValue); }); + remainingTokens.forEach((token) => { + propStr.push(token.strValue); + }); } endToken = prop = @@ -868,7 +881,7 @@ export class CssParser { } const span = this._generateSourceSpan(prop, endToken); - return new CssDefinitionAst(span, prop, value !); + return new CssDefinitionAst(span, prop, value!); } /** @internal */ @@ -899,5 +912,7 @@ export class CssParseError extends ParseError { return new CssParseError(span, 'CSS Parse Error: ' + errMsg); } - constructor(span: ParseSourceSpan, message: string) { super(span, message); } + constructor(span: ParseSourceSpan, message: string) { + super(span, message); + } } diff --git a/packages/compiler/src/directive_normalizer.ts b/packages/compiler/src/directive_normalizer.ts index 38e54961ba..366cea285a 100644 --- a/packages/compiler/src/directive_normalizer.ts +++ b/packages/compiler/src/directive_normalizer.ts @@ -17,7 +17,7 @@ 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 {SyncAsync, isDefined, stringify, syntaxError} from './util'; +import {isDefined, stringify, SyncAsync, syntaxError} from './util'; export interface PrenormalizedTemplateMetadata { ngModuleType: any; @@ -40,16 +40,19 @@ export class DirectiveNormalizer { private _resourceLoader: ResourceLoader, private _urlResolver: UrlResolver, private _htmlParser: HtmlParser, private _config: CompilerConfig) {} - clearCache(): void { this._resourceLoaderCache.clear(); } + clearCache(): void { + this._resourceLoaderCache.clear(); + } clearCacheFor(normalizedDirective: CompileDirectiveMetadata): void { if (!normalizedDirective.isComponent) { return; } const template = normalizedDirective.template !; - this._resourceLoaderCache.delete(template.templateUrl !); - template.externalStylesheets.forEach( - (stylesheet) => { this._resourceLoaderCache.delete(stylesheet.moduleUrl !); }); + this._resourceLoaderCache.delete(template.templateUrl!); + template.externalStylesheets.forEach((stylesheet) => { + this._resourceLoaderCache.delete(stylesheet.moduleUrl!); + }); } private _fetch(url: string): SyncAsync { @@ -65,17 +68,18 @@ export class DirectiveNormalizer { SyncAsync { if (isDefined(prenormData.template)) { if (isDefined(prenormData.templateUrl)) { - throw syntaxError( - `'${stringify(prenormData.componentType)}' component cannot define both template and templateUrl`); + throw syntaxError(`'${ + stringify(prenormData + .componentType)}' component cannot define both template and templateUrl`); } if (typeof prenormData.template !== 'string') { - throw syntaxError( - `The template specified for component ${stringify(prenormData.componentType)} is not a string`); + throw syntaxError(`The template specified for component ${ + stringify(prenormData.componentType)} is not a string`); } } else if (isDefined(prenormData.templateUrl)) { if (typeof prenormData.templateUrl !== 'string') { - throw syntaxError( - `The templateUrl specified for component ${stringify(prenormData.componentType)} is not a string`); + throw syntaxError(`The templateUrl specified for component ${ + stringify(prenormData.componentType)} is not a string`); } } else { throw syntaxError( @@ -84,8 +88,8 @@ export class DirectiveNormalizer { if (isDefined(prenormData.preserveWhitespaces) && typeof prenormData.preserveWhitespaces !== 'boolean') { - throw syntaxError( - `The preserveWhitespaces option for component ${stringify(prenormData.componentType)} must be a boolean`); + throw syntaxError(`The preserveWhitespaces option for component ${ + stringify(prenormData.componentType)} must be a boolean`); } return SyncAsync.then( @@ -101,7 +105,7 @@ export class DirectiveNormalizer { template = prenomData.template; templateUrl = prenomData.moduleUrl; } else { - templateUrl = this._urlResolver.resolve(prenomData.moduleUrl, prenomData.templateUrl !); + templateUrl = this._urlResolver.resolve(prenomData.moduleUrl, prenomData.templateUrl!); template = this._fetch(templateUrl); } return SyncAsync.then( @@ -112,7 +116,7 @@ export class DirectiveNormalizer { prenormData: PrenormalizedTemplateMetadata, template: string, templateAbsUrl: string): PreparsedTemplate { const isInline = !!prenormData.template; - const interpolationConfig = InterpolationConfig.fromArray(prenormData.interpolation !); + const interpolationConfig = InterpolationConfig.fromArray(prenormData.interpolation!); const templateUrl = templateSourceUrl( {reference: prenormData.ngModuleType}, {type: {reference: prenormData.componentType}}, {isInline, templateUrl: templateAbsUrl}); @@ -140,8 +144,12 @@ export class DirectiveNormalizer { .styleUrls; return { template, - templateUrl: templateAbsUrl, isInline, - htmlAst: rootNodesAndErrors, styles, inlineStyleUrls, styleUrls, + templateUrl: templateAbsUrl, + isInline, + htmlAst: rootNodesAndErrors, + styles, + inlineStyleUrls, + styleUrls, ngContentSelectors: visitor.ngContentSelectors, }; } @@ -172,7 +180,7 @@ export class DirectiveNormalizer { const styleUrls = preparsedTemplate.styleUrls; const externalStylesheets = styleUrls.map(styleUrl => { - const stylesheet = stylesheets.get(styleUrl) !; + const stylesheet = stylesheets.get(styleUrl)!; const styles = [...stylesheet.styles]; this._inlineStyles(stylesheet.styleUrls, stylesheets, styles); return new CompileStylesheetMetadata({moduleUrl: styleUrl, styles: styles}); @@ -190,11 +198,14 @@ export class DirectiveNormalizer { encapsulation, template: preparsedTemplate.template, templateUrl: preparsedTemplate.templateUrl, - htmlAst: preparsedTemplate.htmlAst, styles, styleUrls, + htmlAst: preparsedTemplate.htmlAst, + styles, + styleUrls, ngContentSelectors: preparsedTemplate.ngContentSelectors, animations: prenormData.animations, interpolation: prenormData.interpolation, - isInline: preparsedTemplate.isInline, externalStylesheets, + isInline: preparsedTemplate.isInline, + externalStylesheets, preserveWhitespaces: preserveWhitespacesDefault( prenormData.preserveWhitespaces, this._config.preserveWhitespaces), }); @@ -204,7 +215,7 @@ export class DirectiveNormalizer { styleUrls: string[], stylesheets: Map, targetStyles: string[]) { styleUrls.forEach(styleUrl => { - const stylesheet = stylesheets.get(styleUrl) !; + const stylesheet = stylesheets.get(styleUrl)!; stylesheet.styles.forEach(style => targetStyles.push(style)); this._inlineStyles(stylesheet.styleUrls, stylesheets, targetStyles); }); @@ -232,7 +243,7 @@ export class DirectiveNormalizer { } private _normalizeStylesheet(stylesheet: CompileStylesheetMetadata): CompileStylesheetMetadata { - const moduleUrl = stylesheet.moduleUrl !; + const moduleUrl = stylesheet.moduleUrl!; const allStyleUrls = stylesheet.styleUrls.filter(isStyleUrlResolvable) .map(url => this._urlResolver.resolve(moduleUrl, url)); @@ -297,13 +308,21 @@ class TemplatePreparseVisitor implements html.Visitor { return null; } - visitExpansion(ast: html.Expansion, context: any): any { html.visitAll(this, ast.cases); } + visitExpansion(ast: html.Expansion, context: any): any { + html.visitAll(this, ast.cases); + } visitExpansionCase(ast: html.ExpansionCase, context: any): any { html.visitAll(this, ast.expression); } - visitComment(ast: html.Comment, context: any): any { return null; } - visitAttribute(ast: html.Attribute, context: any): any { return null; } - visitText(ast: html.Text, context: any): any { return null; } + visitComment(ast: html.Comment, context: any): any { + return null; + } + visitAttribute(ast: html.Attribute, context: any): any { + return null; + } + visitText(ast: html.Text, context: any): any { + return null; + } } diff --git a/packages/compiler/src/directive_resolver.ts b/packages/compiler/src/directive_resolver.ts index be786b4a9b..78764f5f59 100644 --- a/packages/compiler/src/directive_resolver.ts +++ b/packages/compiler/src/directive_resolver.ts @@ -7,7 +7,7 @@ */ import {CompileReflector} from './compile_reflector'; -import {Component, Directive, Type, createComponent, createContentChild, createContentChildren, createDirective, createHostBinding, createHostListener, createInput, createOutput, createViewChild, createViewChildren} from './core'; +import {Component, createComponent, createContentChild, createContentChildren, createDirective, createHostBinding, createHostListener, createInput, createOutput, createViewChild, createViewChildren, Directive, Type} from './core'; import {resolveForwardRef, splitAtColon, stringify} from './util'; const QUERY_METADATA_IDENTIFIERS = [ @@ -109,7 +109,9 @@ export class DirectiveResolver { return this._merge(dm, inputs, outputs, host, queries, guards, directiveType); } - private _extractPublicName(def: string) { return splitAtColon(def, [null !, def])[1].trim(); } + private _extractPublicName(def: string) { + return splitAtColon(def, [null!, def])[1].trim(); + } private _dedupeBindings(bindings: string[]): string[] { const names = new Set(); @@ -168,7 +170,8 @@ export class DirectiveResolver { host: mergedHost, exportAs: directive.exportAs, queries: mergedQueries, - providers: directive.providers, guards + providers: directive.providers, + guards }); } } diff --git a/packages/compiler/src/expression_parser/ast.ts b/packages/compiler/src/expression_parser/ast.ts index ecf5492c5e..756831b97b 100644 --- a/packages/compiler/src/expression_parser/ast.ts +++ b/packages/compiler/src/expression_parser/ast.ts @@ -31,8 +31,12 @@ export class AST { * Absolute location of the expression AST in a source code file. */ public sourceSpan: AbsoluteSourceSpan) {} - visit(visitor: AstVisitor, context: any = null): any { return null; } - toString(): string { return 'AST'; } + visit(visitor: AstVisitor, context: any = null): any { + return null; + } + toString(): string { + return 'AST'; + } } /** @@ -54,8 +58,12 @@ export class Quote extends AST { public uninterpretedExpression: string, public location: any) { super(span, sourceSpan); } - visit(visitor: AstVisitor, context: any = null): any { return visitor.visitQuote(this, context); } - toString(): string { return 'Quote'; } + visit(visitor: AstVisitor, context: any = null): any { + return visitor.visitQuote(this, context); + } + toString(): string { + return 'Quote'; + } } export class EmptyExpr extends AST { @@ -77,7 +85,9 @@ export class Chain extends AST { constructor(span: ParseSpan, sourceSpan: AbsoluteSourceSpan, public expressions: any[]) { super(span, sourceSpan); } - visit(visitor: AstVisitor, context: any = null): any { return visitor.visitChain(this, context); } + visit(visitor: AstVisitor, context: any = null): any { + return visitor.visitChain(this, context); + } } export class Conditional extends AST { @@ -148,7 +158,9 @@ export class BindingPipe extends AST { public args: any[], public nameSpan: AbsoluteSourceSpan) { super(span, sourceSpan); } - visit(visitor: AstVisitor, context: any = null): any { return visitor.visitPipe(this, context); } + visit(visitor: AstVisitor, context: any = null): any { + return visitor.visitPipe(this, context); + } } export class LiteralPrimitive extends AST { @@ -280,7 +292,9 @@ export class ASTWithSource extends AST { } return this.ast.visit(visitor, context); } - toString(): string { return `${this.source} in ${this.location}`; } + toString(): string { + return `${this.source} in ${this.location}`; + } } /** @@ -302,7 +316,7 @@ export class ASTWithSource extends AST { * the LHS of a HTML attribute to the expression in the RHS. All other bindings * in the example above are derived solely from the RHS. */ -export type TemplateBinding = VariableBinding | ExpressionBinding; +export type TemplateBinding = VariableBinding|ExpressionBinding; export class VariableBinding { /** @@ -379,7 +393,9 @@ export class RecursiveAstVisitor implements AstVisitor { this.visit(ast.left, context); this.visit(ast.right, context); } - visitChain(ast: Chain, context: any): any { this.visitAll(ast.expressions, context); } + visitChain(ast: Chain, context: any): any { + this.visitAll(ast.expressions, context); + } visitConditional(ast: Conditional, context: any): any { this.visit(ast.condition, context); this.visit(ast.trueExp, context); @@ -411,15 +427,23 @@ export class RecursiveAstVisitor implements AstVisitor { visitLiteralArray(ast: LiteralArray, context: any): any { this.visitAll(ast.expressions, context); } - visitLiteralMap(ast: LiteralMap, context: any): any { this.visitAll(ast.values, context); } + visitLiteralMap(ast: LiteralMap, context: any): any { + this.visitAll(ast.values, context); + } visitLiteralPrimitive(ast: LiteralPrimitive, context: any): any {} visitMethodCall(ast: MethodCall, context: any): any { this.visit(ast.receiver, context); this.visitAll(ast.args, context); } - visitPrefixNot(ast: PrefixNot, context: any): any { this.visit(ast.expression, context); } - visitNonNullAssert(ast: NonNullAssert, context: any): any { this.visit(ast.expression, context); } - visitPropertyRead(ast: PropertyRead, context: any): any { this.visit(ast.receiver, context); } + visitPrefixNot(ast: PrefixNot, context: any): any { + this.visit(ast.expression, context); + } + visitNonNullAssert(ast: NonNullAssert, context: any): any { + this.visit(ast.expression, context); + } + visitPropertyRead(ast: PropertyRead, context: any): any { + this.visit(ast.receiver, context); + } visitPropertyWrite(ast: PropertyWrite, context: any): any { this.visit(ast.receiver, context); this.visit(ast.value, context); @@ -441,7 +465,9 @@ export class RecursiveAstVisitor implements AstVisitor { } export class AstTransformer implements AstVisitor { - visitImplicitReceiver(ast: ImplicitReceiver, context: any): AST { return ast; } + visitImplicitReceiver(ast: ImplicitReceiver, context: any): AST { + return ast; + } visitInterpolation(ast: Interpolation, context: any): AST { return new Interpolation(ast.span, ast.sourceSpan, ast.strings, this.visitAll(ast.expressions)); @@ -476,7 +502,7 @@ export class AstTransformer implements AstVisitor { visitFunctionCall(ast: FunctionCall, context: any): AST { return new FunctionCall( - ast.span, ast.sourceSpan, ast.target !.visit(this), this.visitAll(ast.args)); + ast.span, ast.sourceSpan, ast.target!.visit(this), this.visitAll(ast.args)); } visitLiteralArray(ast: LiteralArray, context: any): AST { @@ -542,7 +568,9 @@ export class AstTransformer implements AstVisitor { // A transformer that only creates new nodes if the transformer makes a change or // a change is made a child node. export class AstMemoryEfficientTransformer implements AstVisitor { - visitImplicitReceiver(ast: ImplicitReceiver, context: any): AST { return ast; } + visitImplicitReceiver(ast: ImplicitReceiver, context: any): AST { + return ast; + } visitInterpolation(ast: Interpolation, context: any): Interpolation { const expressions = this.visitAll(ast.expressions); @@ -551,7 +579,9 @@ export class AstMemoryEfficientTransformer implements AstVisitor { return ast; } - visitLiteralPrimitive(ast: LiteralPrimitive, context: any): AST { return ast; } + visitLiteralPrimitive(ast: LiteralPrimitive, context: any): AST { + return ast; + } visitPropertyRead(ast: PropertyRead, context: any): AST { const receiver = ast.receiver.visit(this); @@ -704,7 +734,9 @@ export class AstMemoryEfficientTransformer implements AstVisitor { return ast; } - visitQuote(ast: Quote, context: any): AST { return ast; } + visitQuote(ast: Quote, context: any): AST { + return ast; + } } // Bindings diff --git a/packages/compiler/src/expression_parser/lexer.ts b/packages/compiler/src/expression_parser/lexer.ts index c4ccbb87af..b942ecd4f7 100644 --- a/packages/compiler/src/expression_parser/lexer.ts +++ b/packages/compiler/src/expression_parser/lexer.ts @@ -42,37 +42,61 @@ export class Token { return this.type == TokenType.Character && this.numValue == code; } - isNumber(): boolean { return this.type == TokenType.Number; } + isNumber(): boolean { + return this.type == TokenType.Number; + } - isString(): boolean { return this.type == TokenType.String; } + isString(): boolean { + return this.type == TokenType.String; + } isOperator(operator: string): boolean { return this.type == TokenType.Operator && this.strValue == operator; } - isIdentifier(): boolean { return this.type == TokenType.Identifier; } + isIdentifier(): boolean { + return this.type == TokenType.Identifier; + } - isKeyword(): boolean { return this.type == TokenType.Keyword; } + isKeyword(): boolean { + return this.type == TokenType.Keyword; + } - isKeywordLet(): boolean { return this.type == TokenType.Keyword && this.strValue == 'let'; } + isKeywordLet(): boolean { + return this.type == TokenType.Keyword && this.strValue == 'let'; + } - isKeywordAs(): boolean { return this.type == TokenType.Keyword && this.strValue == 'as'; } + isKeywordAs(): boolean { + return this.type == TokenType.Keyword && this.strValue == 'as'; + } - isKeywordNull(): boolean { return this.type == TokenType.Keyword && this.strValue == 'null'; } + isKeywordNull(): boolean { + return this.type == TokenType.Keyword && this.strValue == 'null'; + } isKeywordUndefined(): boolean { return this.type == TokenType.Keyword && this.strValue == 'undefined'; } - isKeywordTrue(): boolean { return this.type == TokenType.Keyword && this.strValue == 'true'; } + isKeywordTrue(): boolean { + return this.type == TokenType.Keyword && this.strValue == 'true'; + } - isKeywordFalse(): boolean { return this.type == TokenType.Keyword && this.strValue == 'false'; } + isKeywordFalse(): boolean { + return this.type == TokenType.Keyword && this.strValue == 'false'; + } - isKeywordThis(): boolean { return this.type == TokenType.Keyword && this.strValue == 'this'; } + isKeywordThis(): boolean { + return this.type == TokenType.Keyword && this.strValue == 'this'; + } - isError(): boolean { return this.type == TokenType.Error; } + isError(): boolean { + return this.type == TokenType.Error; + } - toNumber(): number { return this.type == TokenType.Number ? this.numValue : -1; } + toNumber(): number { + return this.type == TokenType.Number ? this.numValue : -1; + } toString(): string|null { switch (this.type) { diff --git a/packages/compiler/src/expression_parser/parser.ts b/packages/compiler/src/expression_parser/parser.ts index cb5f2dba08..d003edff14 100644 --- a/packages/compiler/src/expression_parser/parser.ts +++ b/packages/compiler/src/expression_parser/parser.ts @@ -10,8 +10,8 @@ import * as chars from '../chars'; import {DEFAULT_INTERPOLATION_CONFIG, InterpolationConfig} from '../ml_parser/interpolation_config'; import {escapeRegExp} from '../util'; -import {AST, ASTWithSource, AbsoluteSourceSpan, AstVisitor, Binary, BindingPipe, Chain, Conditional, EmptyExpr, ExpressionBinding, FunctionCall, ImplicitReceiver, Interpolation, KeyedRead, KeyedWrite, LiteralArray, LiteralMap, LiteralMapKey, LiteralPrimitive, MethodCall, NonNullAssert, ParseSpan, ParserError, PrefixNot, PropertyRead, PropertyWrite, Quote, SafeMethodCall, SafePropertyRead, TemplateBinding, TemplateBindingIdentifier, VariableBinding} from './ast'; -import {EOF, Lexer, Token, TokenType, isIdentifier, isQuote} from './lexer'; +import {AbsoluteSourceSpan, AST, AstVisitor, ASTWithSource, Binary, BindingPipe, Chain, Conditional, EmptyExpr, ExpressionBinding, FunctionCall, ImplicitReceiver, Interpolation, KeyedRead, KeyedWrite, LiteralArray, LiteralMap, LiteralMapKey, LiteralPrimitive, MethodCall, NonNullAssert, ParserError, ParseSpan, PrefixNot, PropertyRead, PropertyWrite, Quote, SafeMethodCall, SafePropertyRead, TemplateBinding, TemplateBindingIdentifier, VariableBinding} from './ast'; +import {EOF, isIdentifier, isQuote, Lexer, Token, TokenType} from './lexer'; export class SplitInterpolation { constructor(public strings: string[], public expressions: string[], public offsets: number[]) {} @@ -253,7 +253,8 @@ export class Parser { const parts = input.split(regexp); if (parts.length > 1) { this._reportError( - `Got interpolation (${interpolationConfig.start}${interpolationConfig.end}) where expression was expected`, + `Got interpolation (${interpolationConfig.start}${ + interpolationConfig.end}) where expression was expected`, input, `at column ${this._findInterpolationErrorColumn(parts, 1, interpolationConfig)} in`, location); @@ -300,7 +301,9 @@ export class _ParseAST { return i < this.tokens.length ? this.tokens[i] : EOF; } - get next(): Token { return this.peek(0); } + get next(): Token { + return this.peek(0); + } get inputIndex(): number { return (this.index < this.tokens.length) ? this.next.index + this.offset : @@ -310,7 +313,9 @@ export class _ParseAST { /** * Returns the absolute offset of the start of the current token. */ - get currentAbsoluteOffset(): number { return this.absoluteOffset + this.inputIndex; } + get currentAbsoluteOffset(): number { + return this.absoluteOffset + this.inputIndex; + } span(start: number) { // `end` is either the @@ -326,10 +331,12 @@ export class _ParseAST { if (!this.sourceSpanCache.has(serial)) { this.sourceSpanCache.set(serial, this.span(start).toAbsolute(this.absoluteOffset)); } - return this.sourceSpanCache.get(serial) !; + return this.sourceSpanCache.get(serial)!; } - advance() { this.index++; } + advance() { + this.index++; + } consumeOptionalCharacter(code: number): boolean { if (this.next.isCharacter(code)) { @@ -340,8 +347,12 @@ export class _ParseAST { } } - peekKeywordLet(): boolean { return this.next.isKeywordLet(); } - peekKeywordAs(): boolean { return this.next.isKeywordAs(); } + peekKeywordLet(): boolean { + return this.next.isKeywordLet(); + } + peekKeywordAs(): boolean { + return this.next.isKeywordAs(); + } expectCharacter(code: number) { if (this.consumeOptionalCharacter(code)) return; @@ -428,7 +439,9 @@ export class _ParseAST { return result; } - parseExpression(): AST { return this.parseConditional(); } + parseExpression(): AST { + return this.parseConditional(); + } parseConditional(): AST { const start = this.inputIndex; @@ -984,8 +997,8 @@ export class _ParseAST { (this.rbracesExpected <= 0 || !n.isCharacter(chars.$RBRACE)) && (this.rbracketsExpected <= 0 || !n.isCharacter(chars.$RBRACKET))) { if (this.next.isError()) { - this.errors.push(new ParserError( - this.next.toString() !, this.input, this.locationText(), this.location)); + this.errors.push( + new ParserError(this.next.toString()!, this.input, this.locationText(), this.location)); } this.advance(); n = this.next; @@ -1014,9 +1027,13 @@ class SimpleExpressionChecker implements AstVisitor { visitFunctionCall(ast: FunctionCall, context: any) {} - visitLiteralArray(ast: LiteralArray, context: any) { this.visitAll(ast.expressions); } + visitLiteralArray(ast: LiteralArray, context: any) { + this.visitAll(ast.expressions); + } - visitLiteralMap(ast: LiteralMap, context: any) { this.visitAll(ast.values); } + visitLiteralMap(ast: LiteralMap, context: any) { + this.visitAll(ast.values); + } visitBinary(ast: Binary, context: any) {} @@ -1026,13 +1043,17 @@ class SimpleExpressionChecker implements AstVisitor { visitConditional(ast: Conditional, context: any) {} - visitPipe(ast: BindingPipe, context: any) { this.errors.push('pipes'); } + visitPipe(ast: BindingPipe, context: any) { + this.errors.push('pipes'); + } visitKeyedRead(ast: KeyedRead, context: any) {} visitKeyedWrite(ast: KeyedWrite, context: any) {} - visitAll(asts: any[]): any[] { return asts.map(node => node.visit(this)); } + visitAll(asts: any[]): any[] { + return asts.map(node => node.visit(this)); + } visitChain(ast: Chain, context: any) {} @@ -1052,5 +1073,7 @@ class IvySimpleExpressionChecker extends SimpleExpressionChecker { ast.right.visit(this); } - visitPrefixNot(ast: PrefixNot, context: any) { ast.expression.visit(this); } + visitPrefixNot(ast: PrefixNot, context: any) { + ast.expression.visit(this); + } } diff --git a/packages/compiler/src/i18n/digest.ts b/packages/compiler/src/i18n/digest.ts index e81a26e6a1..58ff21d394 100644 --- a/packages/compiler/src/i18n/digest.ts +++ b/packages/compiler/src/i18n/digest.ts @@ -48,7 +48,9 @@ export function computeDecimalDigest(message: i18n.Message): string { * @internal */ class _SerializerVisitor implements i18n.Visitor { - visitText(text: i18n.Text, context: any): any { return text.value; } + visitText(text: i18n.Text, context: any): any { + return text.value; + } visitContainer(container: i18n.Container, context: any): any { return `[${container.children.map(child => child.visit(this)).join(', ')}]`; @@ -63,7 +65,8 @@ class _SerializerVisitor implements i18n.Visitor { visitTagPlaceholder(ph: i18n.TagPlaceholder, context: any): any { return ph.isVoid ? `` : - `${ph.children.map(child => child.visit(this)).join(', ')}`; + `${ + ph.children.map(child => child.visit(this)).join(', ')}`; } visitPlaceholder(ph: i18n.Placeholder, context: any): any { diff --git a/packages/compiler/src/i18n/extractor.ts b/packages/compiler/src/i18n/extractor.ts index 8b950feb6d..43a6d06489 100644 --- a/packages/compiler/src/i18n/extractor.ts +++ b/packages/compiler/src/i18n/extractor.ts @@ -78,11 +78,11 @@ export class Extractor { // Template URL points to either an HTML or TS file depending on // whether the file is used with `templateUrl:` or `template:`, // respectively. - const templateUrl = compMeta.template !.templateUrl !; + const templateUrl = compMeta.template !.templateUrl!; const interpolationConfig = InterpolationConfig.fromArray(compMeta.template !.interpolation); errors.push(...this.messageBundle.updateFromTemplate( - html, templateUrl, interpolationConfig) !); + html, templateUrl, interpolationConfig)!); }); }); diff --git a/packages/compiler/src/i18n/extractor_merger.ts b/packages/compiler/src/i18n/extractor_merger.ts index 0a45f6db38..5133826c93 100644 --- a/packages/compiler/src/i18n/extractor_merger.ts +++ b/packages/compiler/src/i18n/extractor_merger.ts @@ -9,8 +9,9 @@ import * as html from '../ml_parser/ast'; import {InterpolationConfig} from '../ml_parser/interpolation_config'; import {ParseTreeResult} from '../ml_parser/parser'; + import * as i18n from './i18n_ast'; -import {I18nMessageFactory, createI18nMessageFactory} from './i18n_parser'; +import {createI18nMessageFactory, I18nMessageFactory} from './i18n_parser'; import {I18nError} from './parse_util'; import {TranslationBundle} from './translation_bundle'; @@ -56,44 +57,44 @@ enum _VisitorMode { */ class _Visitor implements html.Visitor { // TODO(issue/24571): remove '!'. - private _depth !: number; + private _depth!: number; // ... // TODO(issue/24571): remove '!'. - private _inI18nNode !: boolean; + private _inI18nNode!: boolean; // TODO(issue/24571): remove '!'. - private _inImplicitNode !: boolean; + private _inImplicitNode!: boolean; // ... // TODO(issue/24571): remove '!'. - private _inI18nBlock !: boolean; + private _inI18nBlock!: boolean; // TODO(issue/24571): remove '!'. - private _blockMeaningAndDesc !: string; + private _blockMeaningAndDesc!: string; // TODO(issue/24571): remove '!'. - private _blockChildren !: html.Node[]; + private _blockChildren!: html.Node[]; // TODO(issue/24571): remove '!'. - private _blockStartDepth !: number; + private _blockStartDepth!: number; // {} // TODO(issue/24571): remove '!'. - private _inIcu !: boolean; + private _inIcu!: boolean; // set to void 0 when not in a section private _msgCountAtSectionStart: number|undefined; // TODO(issue/24571): remove '!'. - private _errors !: I18nError[]; + private _errors!: I18nError[]; // TODO(issue/24571): remove '!'. - private _mode !: _VisitorMode; + private _mode!: _VisitorMode; // _VisitorMode.Extract only // TODO(issue/24571): remove '!'. - private _messages !: i18n.Message[]; + private _messages!: i18n.Message[]; // _VisitorMode.Merge only // TODO(issue/24571): remove '!'. - private _translations !: TranslationBundle; + private _translations!: TranslationBundle; // TODO(issue/24571): remove '!'. - private _createI18nMessage !: I18nMessageFactory; + private _createI18nMessage!: I18nMessageFactory; constructor(private _implicitTags: string[], private _implicitAttrs: {[k: string]: string[]}) {} @@ -123,7 +124,7 @@ class _Visitor implements html.Visitor { this._translations = translations; // Construct a single fake root element - const wrapper = new html.Element('wrapper', [], nodes, undefined !, undefined, undefined); + const wrapper = new html.Element('wrapper', [], nodes, undefined!, undefined, undefined); const translatedNode = wrapper.visit(this, null); @@ -193,14 +194,14 @@ class _Visitor implements html.Visitor { i18nCommentsWarned = true; const details = comment.sourceSpan.details ? `, ${comment.sourceSpan.details}` : ''; // TODO(ocombe): use a log service once there is a public one available - console.warn( - `I18n comments are deprecated, use an element instead (${comment.sourceSpan.start}${details})`); + console.warn(`I18n comments are deprecated, use an element instead (${ + comment.sourceSpan.start}${details})`); } this._inI18nBlock = true; this._blockStartDepth = this._depth; this._blockChildren = []; this._blockMeaningAndDesc = - comment.value !.replace(_I18N_COMMENT_PREFIX_REGEXP, '').trim(); + comment.value!.replace(_I18N_COMMENT_PREFIX_REGEXP, '').trim(); this._openTranslatableSection(comment); } } else { @@ -208,7 +209,7 @@ class _Visitor implements html.Visitor { if (this._depth == this._blockStartDepth) { this._closeTranslatableSection(comment, this._blockChildren); this._inI18nBlock = false; - const message = this._addMessage(this._blockChildren, this._blockMeaningAndDesc) !; + const message = this._addMessage(this._blockChildren, this._blockMeaningAndDesc)!; // merge attributes in sections const nodes = this._translateMessage(comment, message); return html.visitAll(this, nodes); @@ -234,7 +235,7 @@ class _Visitor implements html.Visitor { const wasInI18nNode = this._inI18nNode; const wasInImplicitNode = this._inImplicitNode; let childNodes: html.Node[] = []; - let translatedChildNodes: html.Node[] = undefined !; + let translatedChildNodes: html.Node[] = undefined!; // Extract: // - top level nodes with the (implicit) "i18n" attribute if not already in a section @@ -249,7 +250,7 @@ class _Visitor implements html.Visitor { if (!this._isInTranslatableSection && !this._inIcu) { if (i18nAttr || isTopLevelImplicit) { this._inI18nNode = true; - const message = this._addMessage(el.children, i18nMeta) !; + const message = this._addMessage(el.children, i18nMeta)!; translatedChildNodes = this._translateMessage(el, message); } @@ -400,12 +401,14 @@ class _Visitor implements html.Visitor { } else { this._reportError( el, - `Unexpected translation for attribute "${attr.name}" (id="${id || this._translations.digest(message)}")`); + `Unexpected translation for attribute "${attr.name}" (id="${ + id || this._translations.digest(message)}")`); } } else { this._reportError( el, - `Translation unavailable for attribute "${attr.name}" (id="${id || this._translations.digest(message)}")`); + `Translation unavailable for attribute "${attr.name}" (id="${ + id || this._translations.digest(message)}")`); } } else { translatedAttributes.push(attr); @@ -476,7 +479,7 @@ class _Visitor implements html.Visitor { 0); if (significantChildren == 1) { - for (let i = this._messages.length - 1; i >= startIndex !; i--) { + for (let i = this._messages.length - 1; i >= startIndex!; i--) { const ast = this._messages[i].nodes; if (!(ast.length == 1 && ast[0] instanceof i18n.Text)) { this._messages.splice(i, 1); @@ -489,7 +492,7 @@ class _Visitor implements html.Visitor { } private _reportError(node: html.Node, msg: string): void { - this._errors.push(new I18nError(node.sourceSpan !, msg)); + this._errors.push(new I18nError(node.sourceSpan!, msg)); } } diff --git a/packages/compiler/src/i18n/i18n_ast.ts b/packages/compiler/src/i18n/i18n_ast.ts index 9ec6afe1ba..ef0b1156f0 100644 --- a/packages/compiler/src/i18n/i18n_ast.ts +++ b/packages/compiler/src/i18n/i18n_ast.ts @@ -57,24 +57,30 @@ export interface Node { export class Text implements Node { constructor(public value: string, public sourceSpan: ParseSourceSpan) {} - visit(visitor: Visitor, context?: any): any { return visitor.visitText(this, context); } + visit(visitor: Visitor, context?: any): any { + return visitor.visitText(this, context); + } } // TODO(vicb): do we really need this node (vs an array) ? export class Container implements Node { constructor(public children: Node[], public sourceSpan: ParseSourceSpan) {} - visit(visitor: Visitor, context?: any): any { return visitor.visitContainer(this, context); } + visit(visitor: Visitor, context?: any): any { + return visitor.visitContainer(this, context); + } } export class Icu implements Node { // TODO(issue/24571): remove '!'. - public expressionPlaceholder !: string; + public expressionPlaceholder!: string; constructor( public expression: string, public type: string, public cases: {[k: string]: Node}, public sourceSpan: ParseSourceSpan) {} - visit(visitor: Visitor, context?: any): any { return visitor.visitIcu(this, context); } + visit(visitor: Visitor, context?: any): any { + return visitor.visitIcu(this, context); + } } export class TagPlaceholder implements Node { @@ -83,13 +89,17 @@ export class TagPlaceholder implements Node { public closeName: string, public children: Node[], public isVoid: boolean, public sourceSpan: ParseSourceSpan) {} - visit(visitor: Visitor, context?: any): any { return visitor.visitTagPlaceholder(this, context); } + visit(visitor: Visitor, context?: any): any { + return visitor.visitTagPlaceholder(this, context); + } } export class Placeholder implements Node { constructor(public value: string, public name: string, public sourceSpan: ParseSourceSpan) {} - visit(visitor: Visitor, context?: any): any { return visitor.visitPlaceholder(this, context); } + visit(visitor: Visitor, context?: any): any { + return visitor.visitPlaceholder(this, context); + } } export class IcuPlaceholder implements Node { @@ -97,7 +107,9 @@ export class IcuPlaceholder implements Node { previousMessage?: Message; constructor(public value: Icu, public name: string, public sourceSpan: ParseSourceSpan) {} - visit(visitor: Visitor, context?: any): any { return visitor.visitIcuPlaceholder(this, context); } + visit(visitor: Visitor, context?: any): any { + return visitor.visitIcuPlaceholder(this, context); + } } /** @@ -106,7 +118,7 @@ export class IcuPlaceholder implements Node { * This information is either a `Message`, which indicates it is the root of an i18n message, or a * `Node`, which indicates is it part of a containing `Message`. */ -export type I18nMeta = Message | Node; +export type I18nMeta = Message|Node; export interface Visitor { visitText(text: Text, context?: any): any; @@ -119,7 +131,9 @@ export interface Visitor { // Clone the AST export class CloneVisitor implements Visitor { - visitText(text: Text, context?: any): Text { return new Text(text.value, text.sourceSpan); } + visitText(text: Text, context?: any): Text { + return new Text(text.value, text.sourceSpan); + } visitContainer(container: Container, context?: any): Container { const children = container.children.map(n => n.visit(this, context)); @@ -158,7 +172,9 @@ export class RecurseVisitor implements Visitor { } visitIcu(icu: Icu, context?: any): any { - Object.keys(icu.cases).forEach(k => { icu.cases[k].visit(this); }); + Object.keys(icu.cases).forEach(k => { + icu.cases[k].visit(this); + }); } visitTagPlaceholder(ph: TagPlaceholder, context?: any): any { diff --git a/packages/compiler/src/i18n/i18n_parser.ts b/packages/compiler/src/i18n/i18n_parser.ts index f1be0895c8..5c19595f72 100644 --- a/packages/compiler/src/i18n/i18n_parser.ts +++ b/packages/compiler/src/i18n/i18n_parser.ts @@ -83,7 +83,7 @@ class _I18nVisitor implements html.Visitor { const isVoid: boolean = getHtmlTagDefinition(el.name).isVoid; const startPhName = context.placeholderRegistry.getStartTagPlaceholderName(el.name, attrs, isVoid); - context.placeholderToContent[startPhName] = el.sourceSpan !.toString(); + context.placeholderToContent[startPhName] = el.sourceSpan!.toString(); let closePhName = ''; @@ -93,7 +93,7 @@ class _I18nVisitor implements html.Visitor { } const node = new i18n.TagPlaceholder( - el.name, attrs, startPhName, closePhName, children, isVoid, el.sourceSpan !); + el.name, attrs, startPhName, closePhName, children, isVoid, el.sourceSpan!); return context.visitNodeFn(el, node); } @@ -103,7 +103,7 @@ class _I18nVisitor implements html.Visitor { } visitText(text: html.Text, context: I18nMessageVisitorContext): i18n.Node { - const node = this._visitTextWithInterpolation(text.value, text.sourceSpan !, context); + const node = this._visitTextWithInterpolation(text.value, text.sourceSpan!, context); return context.visitNodeFn(text, node); } diff --git a/packages/compiler/src/i18n/message_bundle.ts b/packages/compiler/src/i18n/message_bundle.ts index 882ac54eb3..dcc7269c23 100644 --- a/packages/compiler/src/i18n/message_bundle.ts +++ b/packages/compiler/src/i18n/message_bundle.ts @@ -47,7 +47,9 @@ export class MessageBundle { // Return the message in the internal format // The public (serialized) format might be different, see the `write` method. - getMessages(): i18n.Message[] { return this._messages; } + getMessages(): i18n.Message[] { + return this._messages; + } write(serializer: Serializer, filterSources?: (path: string) => string): string { const messages: {[id: string]: i18n.Message} = {}; @@ -88,18 +90,18 @@ class MapPlaceholderNames extends i18n.CloneVisitor { } visitTagPlaceholder(ph: i18n.TagPlaceholder, mapper: PlaceholderMapper): i18n.TagPlaceholder { - const startName = mapper.toPublicName(ph.startName) !; - const closeName = ph.closeName ? mapper.toPublicName(ph.closeName) ! : ph.closeName; + const startName = mapper.toPublicName(ph.startName)!; + const closeName = ph.closeName ? mapper.toPublicName(ph.closeName)! : ph.closeName; const children = ph.children.map(n => n.visit(this, mapper)); return new i18n.TagPlaceholder( ph.tag, ph.attrs, startName, closeName, children, ph.isVoid, ph.sourceSpan); } visitPlaceholder(ph: i18n.Placeholder, mapper: PlaceholderMapper): i18n.Placeholder { - return new i18n.Placeholder(ph.value, mapper.toPublicName(ph.name) !, ph.sourceSpan); + return new i18n.Placeholder(ph.value, mapper.toPublicName(ph.name)!, ph.sourceSpan); } visitIcuPlaceholder(ph: i18n.IcuPlaceholder, mapper: PlaceholderMapper): i18n.IcuPlaceholder { - return new i18n.IcuPlaceholder(ph.value, mapper.toPublicName(ph.name) !, ph.sourceSpan); + return new i18n.IcuPlaceholder(ph.value, mapper.toPublicName(ph.name)!, ph.sourceSpan); } } diff --git a/packages/compiler/src/i18n/parse_util.ts b/packages/compiler/src/i18n/parse_util.ts index 37acd859ac..0a02a5b416 100644 --- a/packages/compiler/src/i18n/parse_util.ts +++ b/packages/compiler/src/i18n/parse_util.ts @@ -12,5 +12,7 @@ import {ParseError, ParseSourceSpan} from '../parse_util'; * An i18n error. */ export class I18nError extends ParseError { - constructor(span: ParseSourceSpan, msg: string) { super(span, msg); } + constructor(span: ParseSourceSpan, msg: string) { + super(span, msg); + } } diff --git a/packages/compiler/src/i18n/serializers/placeholder.ts b/packages/compiler/src/i18n/serializers/placeholder.ts index def0996964..34b131cb6b 100644 --- a/packages/compiler/src/i18n/serializers/placeholder.ts +++ b/packages/compiler/src/i18n/serializers/placeholder.ts @@ -106,7 +106,9 @@ export class PlaceholderRegistry { return start + strAttrs + end; } - private _hashClosingTag(tag: string): string { return this._hashTag(`/${tag}`, {}, false); } + private _hashClosingTag(tag: string): string { + return this._hashTag(`/${tag}`, {}, false); + } private _generateUniqueName(base: string): string { const seen = this._placeHolderNameCounts.hasOwnProperty(base); diff --git a/packages/compiler/src/i18n/serializers/serializer.ts b/packages/compiler/src/i18n/serializers/serializer.ts index 4fb7dc8aee..5cca7d2543 100644 --- a/packages/compiler/src/i18n/serializers/serializer.ts +++ b/packages/compiler/src/i18n/serializers/serializer.ts @@ -15,13 +15,15 @@ export abstract class Serializer { abstract write(messages: i18n.Message[], locale: string|null): string; abstract load(content: string, url: string): - {locale: string | null, i18nNodesByMsgId: {[msgId: string]: i18n.Node[]}}; + {locale: string|null, i18nNodesByMsgId: {[msgId: string]: i18n.Node[]}}; abstract digest(message: i18n.Message): string; // Creates a name mapper, see `PlaceholderMapper` // Returning `null` means that no name mapping is used. - createNameMapper(message: i18n.Message): PlaceholderMapper|null { return null; } + createNameMapper(message: i18n.Message): PlaceholderMapper|null { + return null; + } } /** @@ -61,7 +63,9 @@ export class SimplePlaceholderMapper extends i18n.RecurseVisitor implements Plac null; } - visitText(text: i18n.Text, context?: any): any { return null; } + visitText(text: i18n.Text, context?: any): any { + return null; + } visitTagPlaceholder(ph: i18n.TagPlaceholder, context?: any): any { this.visitPlaceholderName(ph.startName); @@ -69,7 +73,9 @@ export class SimplePlaceholderMapper extends i18n.RecurseVisitor implements Plac this.visitPlaceholderName(ph.closeName); } - visitPlaceholder(ph: i18n.Placeholder, context?: any): any { this.visitPlaceholderName(ph.name); } + visitPlaceholder(ph: i18n.Placeholder, context?: any): any { + this.visitPlaceholderName(ph.name); + } visitIcuPlaceholder(ph: i18n.IcuPlaceholder, context?: any): any { this.visitPlaceholderName(ph.name); diff --git a/packages/compiler/src/i18n/serializers/xliff.ts b/packages/compiler/src/i18n/serializers/xliff.ts index b546ce14e1..732d4eecfb 100644 --- a/packages/compiler/src/i18n/serializers/xliff.ts +++ b/packages/compiler/src/i18n/serializers/xliff.ts @@ -46,9 +46,9 @@ export class Xliff extends Serializer { new xml.CR(10), new xml.Tag( _CONTEXT_TAG, {'context-type': 'sourcefile'}, [new xml.Text(source.filePath)]), - new xml.CR(10), new xml.Tag( - _CONTEXT_TAG, {'context-type': 'linenumber'}, - [new xml.Text(`${source.startLine}`)]), + new xml.CR(10), + new xml.Tag(_CONTEXT_TAG, {'context-type': 'linenumber'}, [new xml.Text( + `${source.startLine}`)]), new xml.CR(8)); contextTags.push(new xml.CR(8), contextGroupTag); }); @@ -112,14 +112,18 @@ export class Xliff extends Serializer { throw new Error(`xliff parse errors:\n${errors.join('\n')}`); } - return {locale: locale !, i18nNodesByMsgId}; + return {locale: locale!, i18nNodesByMsgId}; } - digest(message: i18n.Message): string { return digest(message); } + digest(message: i18n.Message): string { + return digest(message); + } } class _WriteVisitor implements i18n.Visitor { - visitText(text: i18n.Text, context?: any): xml.Node[] { return [new xml.Text(text.value)]; } + visitText(text: i18n.Text, context?: any): xml.Node[] { + return [new xml.Text(text.value)]; + } visitContainer(container: i18n.Container, context?: any): xml.Node[] { const nodes: xml.Node[] = []; @@ -161,8 +165,8 @@ class _WriteVisitor implements i18n.Visitor { } visitIcuPlaceholder(ph: i18n.IcuPlaceholder, context?: any): xml.Node[] { - const equivText = - `{${ph.value.expression}, ${ph.value.type}, ${Object.keys(ph.value.cases).map((value: string) => value + ' {...}').join(' ')}}`; + const equivText = `{${ph.value.expression}, ${ph.value.type}, ${ + Object.keys(ph.value.cases).map((value: string) => value + ' {...}').join(' ')}}`; return [new xml.Tag(_PLACEHOLDER_TAG, {id: ph.name, 'equiv-text': equivText})]; } @@ -175,11 +179,11 @@ class _WriteVisitor implements i18n.Visitor { // Extract messages as xml nodes from the xliff file class XliffParser implements ml.Visitor { // TODO(issue/24571): remove '!'. - private _unitMlString !: string | null; + private _unitMlString!: string|null; // TODO(issue/24571): remove '!'. - private _errors !: I18nError[]; + private _errors!: I18nError[]; // TODO(issue/24571): remove '!'. - private _msgIdToHtml !: {[msgId: string]: string}; + private _msgIdToHtml!: {[msgId: string]: string}; private _locale: string|null = null; parse(xliff: string, url: string) { @@ -201,7 +205,7 @@ class XliffParser implements ml.Visitor { visitElement(element: ml.Element, context: any): any { switch (element.name) { case _UNIT_TAG: - this._unitMlString = null !; + this._unitMlString = null!; const idAttr = element.attrs.find((attr) => attr.name === 'id'); if (!idAttr) { this._addError(element, `<${_UNIT_TAG}> misses the "id" attribute`); @@ -227,9 +231,9 @@ class XliffParser implements ml.Visitor { break; case _TARGET_TAG: - const innerTextStart = element.startSourceSpan !.end.offset; - const innerTextEnd = element.endSourceSpan !.start.offset; - const content = element.startSourceSpan !.start.file.content; + const innerTextStart = element.startSourceSpan!.end.offset; + const innerTextEnd = element.endSourceSpan!.start.offset; + const content = element.startSourceSpan!.start.file.content; const innerText = content.slice(innerTextStart, innerTextEnd); this._unitMlString = innerText; break; @@ -260,14 +264,14 @@ class XliffParser implements ml.Visitor { visitExpansionCase(expansionCase: ml.ExpansionCase, context: any): any {} private _addError(node: ml.Node, message: string): void { - this._errors.push(new I18nError(node.sourceSpan !, message)); + this._errors.push(new I18nError(node.sourceSpan!, message)); } } // Convert ml nodes (xliff syntax) to i18n nodes class XmlToI18n implements ml.Visitor { // TODO(issue/24571): remove '!'. - private _errors !: I18nError[]; + private _errors!: I18nError[]; convert(message: string, url: string) { const xmlIcu = new XmlParser().parse(message, url, {tokenizeExpansionForms: true}); @@ -283,13 +287,15 @@ class XmlToI18n implements ml.Visitor { }; } - visitText(text: ml.Text, context: any) { return new i18n.Text(text.value, text.sourceSpan !); } + visitText(text: ml.Text, context: any) { + return new i18n.Text(text.value, text.sourceSpan!); + } visitElement(el: ml.Element, context: any): i18n.Placeholder|ml.Node[]|null { if (el.name === _PLACEHOLDER_TAG) { const nameAttr = el.attrs.find((attr) => attr.name === 'id'); if (nameAttr) { - return new i18n.Placeholder('', nameAttr.value, el.sourceSpan !); + return new i18n.Placeholder('', nameAttr.value, el.sourceSpan!); } this._addError(el, `<${_PLACEHOLDER_TAG}> misses the "id" attribute`); @@ -326,7 +332,7 @@ class XmlToI18n implements ml.Visitor { visitAttribute(attribute: ml.Attribute, context: any) {} private _addError(node: ml.Node, message: string): void { - this._errors.push(new I18nError(node.sourceSpan !, message)); + this._errors.push(new I18nError(node.sourceSpan!, message)); } } diff --git a/packages/compiler/src/i18n/serializers/xliff2.ts b/packages/compiler/src/i18n/serializers/xliff2.ts index a8ba85d597..a0d62bc7dc 100644 --- a/packages/compiler/src/i18n/serializers/xliff2.ts +++ b/packages/compiler/src/i18n/serializers/xliff2.ts @@ -54,8 +54,8 @@ export class Xliff2 extends Serializer { message.sources.forEach((source: i18n.MessageSpan) => { notes.children.push(new xml.CR(8), new xml.Tag('note', {category: 'location'}, [ - new xml.Text( - `${source.filePath}:${source.startLine}${source.endLine !== source.startLine ? ',' + source.endLine : ''}`) + new xml.Text(`${source.filePath}:${source.startLine}${ + source.endLine !== source.startLine ? ',' + source.endLine : ''}`) ])); }); @@ -105,17 +105,21 @@ export class Xliff2 extends Serializer { throw new Error(`xliff2 parse errors:\n${errors.join('\n')}`); } - return {locale: locale !, i18nNodesByMsgId}; + return {locale: locale!, i18nNodesByMsgId}; } - digest(message: i18n.Message): string { return decimalDigest(message); } + digest(message: i18n.Message): string { + return decimalDigest(message); + } } class _WriteVisitor implements i18n.Visitor { // TODO(issue/24571): remove '!'. - private _nextPlaceholderId !: number; + private _nextPlaceholderId!: number; - visitText(text: i18n.Text, context?: any): xml.Node[] { return [new xml.Text(text.value)]; } + visitText(text: i18n.Text, context?: any): xml.Node[] { + return [new xml.Text(text.value)]; + } visitContainer(container: i18n.Container, context?: any): xml.Node[] { const nodes: xml.Node[] = []; @@ -192,11 +196,11 @@ class _WriteVisitor implements i18n.Visitor { // Extract messages as xml nodes from the xliff file class Xliff2Parser implements ml.Visitor { // TODO(issue/24571): remove '!'. - private _unitMlString !: string | null; + private _unitMlString!: string|null; // TODO(issue/24571): remove '!'. - private _errors !: I18nError[]; + private _errors!: I18nError[]; // TODO(issue/24571): remove '!'. - private _msgIdToHtml !: {[msgId: string]: string}; + private _msgIdToHtml!: {[msgId: string]: string}; private _locale: string|null = null; parse(xliff: string, url: string) { @@ -242,9 +246,9 @@ class Xliff2Parser implements ml.Visitor { break; case _TARGET_TAG: - const innerTextStart = element.startSourceSpan !.end.offset; - const innerTextEnd = element.endSourceSpan !.start.offset; - const content = element.startSourceSpan !.start.file.content; + const innerTextStart = element.startSourceSpan!.end.offset; + const innerTextEnd = element.endSourceSpan!.start.offset; + const content = element.startSourceSpan!.start.file.content; const innerText = content.slice(innerTextStart, innerTextEnd); this._unitMlString = innerText; break; @@ -290,7 +294,7 @@ class Xliff2Parser implements ml.Visitor { // Convert ml nodes (xliff syntax) to i18n nodes class XmlToI18n implements ml.Visitor { // TODO(issue/24571): remove '!'. - private _errors !: I18nError[]; + private _errors!: I18nError[]; convert(message: string, url: string) { const xmlIcu = new XmlParser().parse(message, url, {tokenizeExpansionForms: true}); @@ -306,7 +310,9 @@ class XmlToI18n implements ml.Visitor { }; } - visitText(text: ml.Text, context: any) { return new i18n.Text(text.value, text.sourceSpan); } + visitText(text: ml.Text, context: any) { + return new i18n.Text(text.value, text.sourceSpan); + } visitElement(el: ml.Element, context: any): i18n.Node[]|null { switch (el.name) { diff --git a/packages/compiler/src/i18n/serializers/xmb.ts b/packages/compiler/src/i18n/serializers/xmb.ts index eeddddc869..a12e785506 100644 --- a/packages/compiler/src/i18n/serializers/xmb.ts +++ b/packages/compiler/src/i18n/serializers/xmb.ts @@ -57,10 +57,10 @@ export class Xmb extends Serializer { let sourceTags: xml.Tag[] = []; message.sources.forEach((source: i18n.MessageSpan) => { - sourceTags.push(new xml.Tag(_SOURCE_TAG, {}, [ - new xml.Text( - `${source.filePath}:${source.startLine}${source.endLine !== source.startLine ? ',' + source.endLine : ''}`) - ])); + sourceTags.push(new xml.Tag( + _SOURCE_TAG, {}, + [new xml.Text(`${source.filePath}:${source.startLine}${ + source.endLine !== source.startLine ? ',' + source.endLine : ''}`)])); }); rootNode.children.push( @@ -85,7 +85,9 @@ export class Xmb extends Serializer { throw new Error('Unsupported'); } - digest(message: i18n.Message): string { return digest(message); } + digest(message: i18n.Message): string { + return digest(message); + } createNameMapper(message: i18n.Message): PlaceholderMapper { @@ -94,7 +96,9 @@ export class Xmb extends Serializer { } class _Visitor implements i18n.Visitor { - visitText(text: i18n.Text, context?: any): xml.Node[] { return [new xml.Text(text.value)]; } + visitText(text: i18n.Text, context?: any): xml.Node[] { + return [new xml.Text(text.value)]; + } visitContainer(container: i18n.Container, context: any): xml.Node[] { const nodes: xml.Node[] = []; diff --git a/packages/compiler/src/i18n/serializers/xml_helper.ts b/packages/compiler/src/i18n/serializers/xml_helper.ts index 27a8d10c5a..6863cce5df 100644 --- a/packages/compiler/src/i18n/serializers/xml_helper.ts +++ b/packages/compiler/src/i18n/serializers/xml_helper.ts @@ -25,7 +25,9 @@ class _Visitor implements IVisitor { return `<${tag.name}${strAttrs}>${strChildren.join('')}`; } - visitText(text: Text): string { return text.value; } + visitText(text: Text): string { + return text.value; + } visitDeclaration(decl: Declaration): string { return ``; @@ -47,7 +49,9 @@ export function serialize(nodes: Node[]): string { return nodes.map((node: Node): string => node.visit(_visitor)).join(''); } -export interface Node { visit(visitor: IVisitor): any; } +export interface Node { + visit(visitor: IVisitor): any; +} export class Declaration implements Node { public attrs: {[k: string]: string} = {}; @@ -58,13 +62,17 @@ export class Declaration implements Node { }); } - visit(visitor: IVisitor): any { return visitor.visitDeclaration(this); } + visit(visitor: IVisitor): any { + return visitor.visitDeclaration(this); + } } export class Doctype implements Node { constructor(public rootTag: string, public dtd: string) {} - visit(visitor: IVisitor): any { return visitor.visitDoctype(this); } + visit(visitor: IVisitor): any { + return visitor.visitDoctype(this); + } } export class Tag implements Node { @@ -78,18 +86,26 @@ export class Tag implements Node { }); } - visit(visitor: IVisitor): any { return visitor.visitTag(this); } + visit(visitor: IVisitor): any { + return visitor.visitTag(this); + } } export class Text implements Node { value: string; - constructor(unescapedValue: string) { this.value = escapeXml(unescapedValue); } + constructor(unescapedValue: string) { + this.value = escapeXml(unescapedValue); + } - visit(visitor: IVisitor): any { return visitor.visitText(this); } + visit(visitor: IVisitor): any { + return visitor.visitText(this); + } } export class CR extends Text { - constructor(ws: number = 0) { super(`\n${new Array(ws + 1).join(' ')}`); } + constructor(ws: number = 0) { + super(`\n${new Array(ws + 1).join(' ')}`); + } } const _ESCAPED_CHARS: [RegExp, string][] = [ diff --git a/packages/compiler/src/i18n/serializers/xtb.ts b/packages/compiler/src/i18n/serializers/xtb.ts index 441c03fa04..dede307882 100644 --- a/packages/compiler/src/i18n/serializers/xtb.ts +++ b/packages/compiler/src/i18n/serializers/xtb.ts @@ -19,7 +19,9 @@ const _TRANSLATION_TAG = 'translation'; const _PLACEHOLDER_TAG = 'ph'; export class Xtb extends Serializer { - write(messages: i18n.Message[], locale: string|null): string { throw new Error('Unsupported'); } + write(messages: i18n.Message[], locale: string|null): string { + throw new Error('Unsupported'); + } load(content: string, url: string): {locale: string, i18nNodesByMsgId: {[msgId: string]: i18n.Node[]}} { @@ -49,10 +51,12 @@ export class Xtb extends Serializer { throw new Error(`xtb parse errors:\n${errors.join('\n')}`); } - return {locale: locale !, i18nNodesByMsgId}; + return {locale: locale!, i18nNodesByMsgId}; } - digest(message: i18n.Message): string { return digest(message); } + digest(message: i18n.Message): string { + return digest(message); + } createNameMapper(message: i18n.Message): PlaceholderMapper { return new SimplePlaceholderMapper(message, toPublicName); @@ -68,18 +72,20 @@ function createLazyProperty(messages: any, id: string, valueFn: () => any) { Object.defineProperty(messages, id, {enumerable: true, value}); return value; }, - set: _ => { throw new Error('Could not overwrite an XTB translation'); }, + set: _ => { + throw new Error('Could not overwrite an XTB translation'); + }, }); } // Extract messages as xml nodes from the xtb file class XtbParser implements ml.Visitor { // TODO(issue/24571): remove '!'. - private _bundleDepth !: number; + private _bundleDepth!: number; // TODO(issue/24571): remove '!'. - private _errors !: I18nError[]; + private _errors!: I18nError[]; // TODO(issue/24571): remove '!'. - private _msgIdToHtml !: {[msgId: string]: string}; + private _msgIdToHtml!: {[msgId: string]: string}; private _locale: string|null = null; parse(xtb: string, url: string) { @@ -124,10 +130,10 @@ class XtbParser implements ml.Visitor { if (this._msgIdToHtml.hasOwnProperty(id)) { this._addError(element, `Duplicated translations for msg ${id}`); } else { - const innerTextStart = element.startSourceSpan !.end.offset; - const innerTextEnd = element.endSourceSpan !.start.offset; - const content = element.startSourceSpan !.start.file.content; - const innerText = content.slice(innerTextStart !, innerTextEnd !); + const innerTextStart = element.startSourceSpan!.end.offset; + const innerTextEnd = element.endSourceSpan!.start.offset; + const content = element.startSourceSpan!.start.file.content; + const innerText = content.slice(innerTextStart!, innerTextEnd!); this._msgIdToHtml[id] = innerText; } } @@ -149,14 +155,14 @@ class XtbParser implements ml.Visitor { visitExpansionCase(expansionCase: ml.ExpansionCase, context: any): any {} private _addError(node: ml.Node, message: string): void { - this._errors.push(new I18nError(node.sourceSpan !, message)); + this._errors.push(new I18nError(node.sourceSpan!, message)); } } // Convert ml nodes (xtb syntax) to i18n nodes class XmlToI18n implements ml.Visitor { // TODO(issue/24571): remove '!'. - private _errors !: I18nError[]; + private _errors!: I18nError[]; convert(message: string, url: string) { const xmlIcu = new XmlParser().parse(message, url, {tokenizeExpansionForms: true}); @@ -172,7 +178,9 @@ class XmlToI18n implements ml.Visitor { }; } - visitText(text: ml.Text, context: any) { return new i18n.Text(text.value, text.sourceSpan !); } + visitText(text: ml.Text, context: any) { + return new i18n.Text(text.value, text.sourceSpan!); + } visitExpansion(icu: ml.Expansion, context: any) { const caseMap: {[value: string]: i18n.Node} = {}; @@ -195,7 +203,7 @@ class XmlToI18n implements ml.Visitor { if (el.name === _PLACEHOLDER_TAG) { const nameAttr = el.attrs.find((attr) => attr.name === 'name'); if (nameAttr) { - return new i18n.Placeholder('', nameAttr.value, el.sourceSpan !); + return new i18n.Placeholder('', nameAttr.value, el.sourceSpan!); } this._addError(el, `<${_PLACEHOLDER_TAG}> misses the "name" attribute`); @@ -210,6 +218,6 @@ class XmlToI18n implements ml.Visitor { visitAttribute(attribute: ml.Attribute, context: any) {} private _addError(node: ml.Node, message: string): void { - this._errors.push(new I18nError(node.sourceSpan !, message)); + this._errors.push(new I18nError(node.sourceSpan!, message)); } } diff --git a/packages/compiler/src/i18n/translation_bundle.ts b/packages/compiler/src/i18n/translation_bundle.ts index 30da42a421..62bfb357e9 100644 --- a/packages/compiler/src/i18n/translation_bundle.ts +++ b/packages/compiler/src/i18n/translation_bundle.ts @@ -30,7 +30,7 @@ export class TranslationBundle { missingTranslationStrategy: MissingTranslationStrategy = MissingTranslationStrategy.Warning, console?: Console) { this._i18nToHtml = new I18nToHtmlVisitor( - _i18nNodesByMsgId, locale, digest, mapperFactory !, missingTranslationStrategy, console); + _i18nNodesByMsgId, locale, digest, mapperFactory!, missingTranslationStrategy, console); } // Creates a `TranslationBundle` by parsing the given `content` with the `serializer`. @@ -40,7 +40,7 @@ export class TranslationBundle { console?: Console): TranslationBundle { const {locale, i18nNodesByMsgId} = serializer.load(content, url); const digestFn = (m: i18n.Message) => serializer.digest(m); - const mapperFactory = (m: i18n.Message) => serializer.createNameMapper(m) !; + const mapperFactory = (m: i18n.Message) => serializer.createNameMapper(m)!; return new TranslationBundle( i18nNodesByMsgId, locale, digestFn, mapperFactory, missingTranslationStrategy, console); } @@ -56,16 +56,18 @@ export class TranslationBundle { return html.nodes; } - has(srcMsg: i18n.Message): boolean { return this.digest(srcMsg) in this._i18nNodesByMsgId; } + has(srcMsg: i18n.Message): boolean { + return this.digest(srcMsg) in this._i18nNodesByMsgId; + } } class I18nToHtmlVisitor implements i18n.Visitor { // TODO(issue/24571): remove '!'. - private _srcMsg !: i18n.Message; + private _srcMsg!: i18n.Message; private _contextStack: {msg: i18n.Message, mapper: (name: string) => string}[] = []; private _errors: I18nError[] = []; // TODO(issue/24571): remove '!'. - private _mapper !: (name: string) => string; + private _mapper!: (name: string) => string; constructor( private _i18nNodesByMsgId: {[msgId: string]: i18n.Node[]} = {}, private _locale: string|null, @@ -166,7 +168,7 @@ class I18nToHtmlVisitor implements i18n.Visitor { // When there is a translation use its nodes as the source // And create a mapper to convert serialized placeholder names to internal names nodes = this._i18nNodesByMsgId[id]; - this._mapper = (name: string) => mapper ? mapper.toInternalName(name) ! : name; + this._mapper = (name: string) => mapper ? mapper.toInternalName(name)! : name; } else { // When no translation has been found // - report an error / a warning / nothing, @@ -185,7 +187,7 @@ class I18nToHtmlVisitor implements i18n.Visitor { this._mapper = (name: string) => name; } const text = nodes.map(node => node.visit(this)).join(''); - const context = this._contextStack.pop() !; + const context = this._contextStack.pop()!; this._srcMsg = context.msg; this._mapper = context.mapper; return text; diff --git a/packages/compiler/src/injectable_compiler.ts b/packages/compiler/src/injectable_compiler.ts index 6018fb8809..d503c868b4 100644 --- a/packages/compiler/src/injectable_compiler.ts +++ b/packages/compiler/src/injectable_compiler.ts @@ -121,7 +121,7 @@ export class InjectableCompiler { compile(injectable: CompileInjectableMetadata, ctx: OutputContext): void { if (this.alwaysGenerateDef || injectable.providedIn !== undefined) { - const className = identifierName(injectable.type) !; + const className = identifierName(injectable.type)!; const clazz = new o.ClassStmt( className, null, [ diff --git a/packages/compiler/src/injectable_compiler_2.ts b/packages/compiler/src/injectable_compiler_2.ts index b3b0a2221b..6fbe107c16 100644 --- a/packages/compiler/src/injectable_compiler_2.ts +++ b/packages/compiler/src/injectable_compiler_2.ts @@ -8,8 +8,8 @@ import {Identifiers} from './identifiers'; import * as o from './output/output_ast'; -import {R3DependencyMetadata, R3FactoryDelegateType, R3FactoryMetadata, R3FactoryTarget, compileFactoryFunction} from './render3/r3_factory'; -import {R3Reference, mapToMapExpression, typeWithParameters} from './render3/util'; +import {compileFactoryFunction, R3DependencyMetadata, R3FactoryDelegateType, R3FactoryMetadata, R3FactoryTarget} from './render3/r3_factory'; +import {mapToMapExpression, R3Reference, typeWithParameters} from './render3/util'; export interface InjectableDef { expression: o.Expression; diff --git a/packages/compiler/src/jit/compiler.ts b/packages/compiler/src/jit/compiler.ts index 8fd53b03c8..ed6d8da08e 100644 --- a/packages/compiler/src/jit/compiler.ts +++ b/packages/compiler/src/jit/compiler.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {CompileDirectiveMetadata, CompileIdentifierMetadata, CompileNgModuleMetadata, CompilePipeSummary, CompileProviderMetadata, CompileStylesheetMetadata, CompileTypeSummary, ProviderMeta, ProxyClass, identifierName, ngModuleJitUrl, sharedStylesheetJitUrl, templateJitUrl, templateSourceUrl} from '../compile_metadata'; +import {CompileDirectiveMetadata, CompileIdentifierMetadata, CompileNgModuleMetadata, CompilePipeSummary, CompileProviderMetadata, CompileStylesheetMetadata, CompileTypeSummary, identifierName, ngModuleJitUrl, ProviderMeta, ProxyClass, sharedStylesheetJitUrl, templateJitUrl, templateSourceUrl} from '../compile_metadata'; import {CompileReflector} from '../compile_reflector'; import {CompilerConfig} from '../config'; import {ConstantPool} from '../constant_pool'; @@ -20,7 +20,7 @@ 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, SyncAsync, stringify} from '../util'; +import {Console, OutputContext, stringify, SyncAsync} from '../util'; import {ViewCompiler} from '../view_compiler/view_compiler'; export interface ModuleWithComponentFactories { @@ -97,7 +97,9 @@ export class JitCompiler { } } - hasAotSummary(ref: Type) { return !!this._summaryResolver.resolveSummary(ref); } + hasAotSummary(ref: Type) { + return !!this._summaryResolver.resolveSummary(ref); + } private _filterJitIdentifiers(ids: CompileIdentifierMetadata[]): any[] { return ids.map(mod => mod.reference).filter((ref) => !this.hasAotSummary(ref)); @@ -124,12 +126,12 @@ export class JitCompiler { private _loadModules(mainModule: any, isSync: boolean): SyncAsync { const loading: Promise[] = []; - const mainNgModule = this._metadataResolver.getNgModuleMetadata(mainModule) !; + const mainNgModule = this._metadataResolver.getNgModuleMetadata(mainModule)!; // Note: for runtime compilation, we want to transitively compile all modules, // so we also need to load the declared directives / pipes for all nested modules. this._filterJitIdentifiers(mainNgModule.transitiveModule.modules).forEach((nestedNgModule) => { // getNgModuleMetadata only returns null if the value passed in is not an NgModule - const moduleMeta = this._metadataResolver.getNgModuleMetadata(nestedNgModule) !; + const moduleMeta = this._metadataResolver.getNgModuleMetadata(nestedNgModule)!; this._filterJitIdentifiers(moduleMeta.declaredDirectives).forEach((ref) => { const promise = this._metadataResolver.loadDirectiveMetadata(moduleMeta.type.reference, ref, isSync); @@ -144,9 +146,9 @@ export class JitCompiler { } private _compileModule(moduleType: Type): object { - let ngModuleFactory = this._compiledNgModuleCache.get(moduleType) !; + let ngModuleFactory = this._compiledNgModuleCache.get(moduleType)!; if (!ngModuleFactory) { - const moduleMeta = this._metadataResolver.getNgModuleMetadata(moduleType) !; + const moduleMeta = this._metadataResolver.getNgModuleMetadata(moduleType)!; // Always provide a bound Compiler const extraProviders = this.getExtraNgModuleProviders(moduleMeta.type.reference); const outputCtx = createOutputContext(); @@ -162,13 +164,13 @@ export class JitCompiler { * @internal */ _compileComponents(mainModule: Type, allComponentFactories: object[]|null) { - const ngModule = this._metadataResolver.getNgModuleMetadata(mainModule) !; + const ngModule = this._metadataResolver.getNgModuleMetadata(mainModule)!; const moduleByJitDirective = new Map(); const templates = new Set(); const transJitModules = this._filterJitIdentifiers(ngModule.transitiveModule.modules); transJitModules.forEach((localMod) => { - const localModuleMeta = this._metadataResolver.getNgModuleMetadata(localMod) !; + const localModuleMeta = this._metadataResolver.getNgModuleMetadata(localMod)!; this._filterJitIdentifiers(localModuleMeta.declaredDirectives).forEach((dirRef) => { moduleByJitDirective.set(dirRef, localModuleMeta); const dirMeta = this._metadataResolver.getDirectiveMetadata(dirRef); @@ -184,12 +186,12 @@ export class JitCompiler { }); }); transJitModules.forEach((localMod) => { - const localModuleMeta = this._metadataResolver.getNgModuleMetadata(localMod) !; + const localModuleMeta = this._metadataResolver.getNgModuleMetadata(localMod)!; this._filterJitIdentifiers(localModuleMeta.declaredDirectives).forEach((dirRef) => { const dirMeta = this._metadataResolver.getDirectiveMetadata(dirRef); if (dirMeta.isComponent) { dirMeta.entryComponents.forEach((entryComponentType) => { - const moduleMeta = moduleByJitDirective.get(entryComponentType.componentType) !; + const moduleMeta = moduleByJitDirective.get(entryComponentType.componentType)!; templates.add( this._createCompiledHostTemplate(entryComponentType.componentType, moduleMeta)); }); @@ -197,7 +199,7 @@ export class JitCompiler { }); localModuleMeta.entryComponents.forEach((entryComponentType) => { if (!this.hasAotSummary(entryComponentType.componentType)) { - const moduleMeta = moduleByJitDirective.get(entryComponentType.componentType) !; + const moduleMeta = moduleByJitDirective.get(entryComponentType.componentType)!; templates.add( this._createCompiledHostTemplate(entryComponentType.componentType, moduleMeta)); } @@ -227,8 +229,9 @@ export class JitCompiler { private _createCompiledHostTemplate(compType: Type, ngModule: CompileNgModuleMetadata): CompiledTemplate { if (!ngModule) { - throw new Error( - `Component ${stringify(compType)} is not part of any NgModule or the module has not been imported into your module.`); + throw new Error(`Component ${ + stringify( + compType)} is not part of any NgModule or the module has not been imported into your module.`); } let compiledTemplate = this._compiledHostTemplateCache.get(compType); if (!compiledTemplate) { @@ -267,7 +270,7 @@ export class JitCompiler { compMeta.template !.externalStylesheets.forEach((stylesheetMeta) => { const compiledStylesheet = this._styleCompiler.compileStyles(createOutputContext(), compMeta, stylesheetMeta); - externalStylesheetsByModuleUrl.set(stylesheetMeta.moduleUrl !, compiledStylesheet); + externalStylesheetsByModuleUrl.set(stylesheetMeta.moduleUrl!, compiledStylesheet); }); this._resolveStylesCompileResult(componentStylesheet, externalStylesheetsByModuleUrl); const pipes = template.ngModule.transitiveModule.pipes.map( @@ -295,14 +298,14 @@ export class JitCompiler { const pipes = ngModule.transitiveModule.pipes.map( pipe => this._metadataResolver.getPipeSummary(pipe.reference)); return this._templateParser.parse( - compMeta, compMeta.template !.htmlAst !, directives, pipes, ngModule.schemas, + compMeta, compMeta.template !.htmlAst!, directives, pipes, ngModule.schemas, templateSourceUrl(ngModule.type, compMeta, compMeta.template !), preserveWhitespaces); } private _resolveStylesCompileResult( result: CompiledStylesheet, externalStylesheetsByModuleUrl: Map) { result.dependencies.forEach((dep, i) => { - const nestedCompileResult = externalStylesheetsByModuleUrl.get(dep.moduleUrl) !; + const nestedCompileResult = externalStylesheetsByModuleUrl.get(dep.moduleUrl)!; const nestedStylesArr = this._resolveAndEvalStylesCompileResult( nestedCompileResult, externalStylesheetsByModuleUrl); dep.setValue(nestedStylesArr); @@ -329,7 +332,7 @@ export class JitCompiler { } class CompiledTemplate { - private _viewClass: Function = null !; + private _viewClass: Function = null!; isCompiled = false; constructor( diff --git a/packages/compiler/src/jit_compiler_facade.ts b/packages/compiler/src/jit_compiler_facade.ts index 172eaa2a3f..f2fb2c8559 100644 --- a/packages/compiler/src/jit_compiler_facade.ts +++ b/packages/compiler/src/jit_compiler_facade.ts @@ -16,13 +16,13 @@ import {DEFAULT_INTERPOLATION_CONFIG, InterpolationConfig} from './ml_parser/int import {DeclareVarStmt, Expression, LiteralExpr, Statement, StmtModifier, WrappedNodeExpr} from './output/output_ast'; import {JitEvaluator} from './output/output_jit'; import {ParseError, ParseSourceSpan, r3JitTypeSourceSpan} from './parse_util'; -import {R3DependencyMetadata, R3FactoryTarget, R3ResolvedDependencyType, compileFactoryFunction} from './render3/r3_factory'; +import {compileFactoryFunction, R3DependencyMetadata, R3FactoryTarget, R3ResolvedDependencyType} from './render3/r3_factory'; import {R3JitReflector} from './render3/r3_jit'; -import {R3InjectorMetadata, R3NgModuleMetadata, compileInjector, compileNgModule} from './render3/r3_module_compiler'; -import {R3PipeMetadata, compilePipeFromMetadata} from './render3/r3_pipe_compiler'; +import {compileInjector, compileNgModule, R3InjectorMetadata, R3NgModuleMetadata} from './render3/r3_module_compiler'; +import {compilePipeFromMetadata, R3PipeMetadata} from './render3/r3_pipe_compiler'; import {R3Reference} from './render3/util'; import {R3DirectiveMetadata, R3QueryMetadata} from './render3/view/api'; -import {ParsedHostBindings, compileComponentFromMetadata, compileDirectiveFromMetadata, parseHostBindings, verifyHostBindings} from './render3/view/compiler'; +import {compileComponentFromMetadata, compileDirectiveFromMetadata, ParsedHostBindings, parseHostBindings, verifyHostBindings} from './render3/view/compiler'; import {makeBindingParser, parseTemplate} from './render3/view/template'; import {ResourceLoader} from './resource_loader'; import {DomElementSchemaRegistry} from './schema/dom_element_schema_registry'; @@ -277,7 +277,7 @@ function wrapExpression(obj: any, property: string): WrappedNodeExpr|undefi } } -function computeProvidedIn(providedIn: Type | string | null | undefined): Expression { +function computeProvidedIn(providedIn: Type|string|null|undefined): Expression { if (providedIn == null || typeof providedIn === 'string') { return new LiteralExpr(providedIn); } else { @@ -305,8 +305,8 @@ function convertR3DependencyMetadata(facade: R3DependencyMetadataFacade): R3Depe }; } -function convertR3DependencyMetadataArray(facades: R3DependencyMetadataFacade[] | null | undefined): - R3DependencyMetadata[]|null { +function convertR3DependencyMetadataArray(facades: R3DependencyMetadataFacade[]|null| + undefined): R3DependencyMetadata[]|null { return facades == null ? null : facades.map(convertR3DependencyMetadata); } @@ -356,13 +356,11 @@ function isOutput(value: any): value is Output { } function parseInputOutputs(values: string[]): StringMap { - return values.reduce( - (map, value) => { - const [field, property] = value.split(',').map(piece => piece.trim()); - map[field] = property || field; - return map; - }, - {} as StringMap); + return values.reduce((map, value) => { + const [field, property] = value.split(',').map(piece => piece.trim()); + map[field] = property || field; + return map; + }, {} as StringMap); } export function publishFacade(global: any) { diff --git a/packages/compiler/src/metadata_resolver.ts b/packages/compiler/src/metadata_resolver.ts index b5e28006a3..ff85eb216c 100644 --- a/packages/compiler/src/metadata_resolver.ts +++ b/packages/compiler/src/metadata_resolver.ts @@ -12,7 +12,7 @@ import {assertArrayOfStrings, assertInterpolationSymbols} from './assertions'; import * as cpl from './compile_metadata'; import {CompileReflector} from './compile_reflector'; import {CompilerConfig} from './config'; -import {ChangeDetectionStrategy, Component, Directive, Injectable, ModuleWithProviders, Provider, Query, SchemaMetadata, Type, ViewEncapsulation, createAttribute, createComponent, createHost, createInject, createInjectable, createInjectionToken, createNgModule, createOptional, createSelf, createSkipSelf} from './core'; +import {ChangeDetectionStrategy, Component, createAttribute, createComponent, createHost, createInject, createInjectable, createInjectionToken, createNgModule, createOptional, createSelf, createSkipSelf, Directive, Injectable, ModuleWithProviders, Provider, Query, SchemaMetadata, Type, ViewEncapsulation} from './core'; import {DirectiveNormalizer} from './directive_normalizer'; import {DirectiveResolver, findLast} from './directive_resolver'; import {Identifiers} from './identifiers'; @@ -23,7 +23,7 @@ import {PipeResolver} from './pipe_resolver'; import {ElementSchemaRegistry} from './schema/element_schema_registry'; import {CssSelector} from './selector'; import {SummaryResolver} from './summary_resolver'; -import {Console, SyncAsync, ValueTransformer, isPromise, noUndefined, resolveForwardRef, stringify, syntaxError, visitValue} from './util'; +import {Console, isPromise, noUndefined, resolveForwardRef, stringify, SyncAsync, syntaxError, ValueTransformer, visitValue} from './util'; export type ErrorCollector = (error: any, type?: any) => void; @@ -55,7 +55,9 @@ export class CompileMetadataResolver { private _staticSymbolCache: StaticSymbolCache, private _reflector: CompileReflector, private _errorCollector?: ErrorCollector) {} - getReflector(): CompileReflector { return this._reflector; } + getReflector(): CompileReflector { + return this._reflector; + } clearCacheFor(type: Type) { const dirMeta = this._directiveCache.get(type); @@ -176,7 +178,7 @@ export class CompileMetadataResolver { } // Note: ! is ok here as this method should only be called with normalized directive // metadata, which always fills in the selector. - const template = CssSelector.parse(compMeta.selector !)[0].getMatchingElementTemplate(); + const template = CssSelector.parse(compMeta.selector!)[0].getMatchingElementTemplate(); const templateUrl = ''; const htmlAst = this._htmlParser.parse(template, templateUrl); return cpl.CompileDirectiveMetadata.create({ @@ -209,8 +211,8 @@ export class CompileMetadataResolver { guards: {}, viewQueries: [], componentViewType: hostViewType, - rendererType: - {id: '__Host__', encapsulation: ViewEncapsulation.None, styles: [], data: {}} as object, + rendererType: {id: '__Host__', encapsulation: ViewEncapsulation.None, styles: [], data: {}} as + object, entryComponents: [], componentFactory: null }); @@ -221,9 +223,9 @@ export class CompileMetadataResolver { return null; } directiveType = resolveForwardRef(directiveType); - const {annotation, metadata} = this.getNonNormalizedDirectiveMetadata(directiveType) !; + const {annotation, metadata} = this.getNonNormalizedDirectiveMetadata(directiveType)!; - const createDirectiveMetadata = (templateMetadata: cpl.CompileTemplateMetadata | null) => { + const createDirectiveMetadata = (templateMetadata: cpl.CompileTemplateMetadata|null) => { const normalizedDirMeta = new cpl.CompileDirectiveMetadata({ isHost: false, type: metadata.type, @@ -248,7 +250,7 @@ export class CompileMetadataResolver { template: templateMetadata }); if (templateMetadata) { - this.initComponentFactory(metadata.componentFactory !, templateMetadata.ngContentSelectors); + this.initComponentFactory(metadata.componentFactory!, templateMetadata.ngContentSelectors); } this._directiveCache.set(directiveType, normalizedDirMeta); this._summaryCache.set(directiveType, normalizedDirMeta.toSummary()); @@ -296,7 +298,7 @@ export class CompileMetadataResolver { if (!dirMeta) { return null; } - let nonNormalizedTemplateMetadata: cpl.CompileTemplateMetadata = undefined !; + let nonNormalizedTemplateMetadata: cpl.CompileTemplateMetadata = undefined!; if (createComponent.isTypeOf(dirMeta)) { // component @@ -323,7 +325,7 @@ export class CompileMetadataResolver { }); } - let changeDetectionStrategy: ChangeDetectionStrategy = null !; + let changeDetectionStrategy: ChangeDetectionStrategy = null!; let viewProviders: cpl.CompileProviderMetadata[] = []; let entryComponentMetadata: cpl.CompileEntryComponentMetadata[] = []; let selector = dirMeta.selector; @@ -331,7 +333,7 @@ export class CompileMetadataResolver { if (createComponent.isTypeOf(dirMeta)) { // Component const compMeta = dirMeta as Component; - changeDetectionStrategy = compMeta.changeDetection !; + changeDetectionStrategy = compMeta.changeDetection!; if (compMeta.viewProviders) { viewProviders = this._getProvidersMetadata( compMeta.viewProviders, entryComponentMetadata, @@ -339,7 +341,7 @@ export class CompileMetadataResolver { } if (compMeta.entryComponents) { entryComponentMetadata = flattenAndDedupeArray(compMeta.entryComponents) - .map((type) => this._getEntryComponentMetadata(type) !) + .map((type) => this._getEntryComponentMetadata(type)!) .concat(entryComponentMetadata); } if (!selector) { @@ -348,7 +350,7 @@ export class CompileMetadataResolver { } else { // Directive if (!selector) { - selector = null !; + selector = null!; } } @@ -401,11 +403,12 @@ export class CompileMetadataResolver { * This assumes `loadNgModuleDirectiveAndPipeMetadata` has been called first. */ getDirectiveMetadata(directiveType: any): cpl.CompileDirectiveMetadata { - const dirMeta = this._directiveCache.get(directiveType) !; + const dirMeta = this._directiveCache.get(directiveType)!; if (!dirMeta) { this._reportError( syntaxError( - `Illegal state: getDirectiveMetadata can only be called after loadNgModuleDirectiveAndPipeMetadata for a module that declares it. Directive ${stringifyType(directiveType)}.`), + `Illegal state: getDirectiveMetadata can only be called after loadNgModuleDirectiveAndPipeMetadata for a module that declares it. Directive ${ + stringifyType(directiveType)}.`), directiveType); } return dirMeta; @@ -530,7 +533,7 @@ export class CompileMetadataResolver { if (meta.imports) { flattenAndDedupeArray(meta.imports).forEach((importedType) => { - let importedModuleType: Type = undefined !; + let importedModuleType: Type = undefined!; if (isValidType(importedType)) { importedModuleType = importedType; } else if (importedType && importedType.ngModule) { @@ -549,8 +552,9 @@ export class CompileMetadataResolver { if (!alreadyCollecting) alreadyCollecting = new Set(); if (alreadyCollecting.has(importedModuleType)) { this._reportError( - syntaxError( - `${this._getTypeDescriptor(importedModuleType)} '${stringifyType(importedType)}' is imported recursively by the module '${stringifyType(moduleType)}'.`), + syntaxError(`${this._getTypeDescriptor(importedModuleType)} '${ + stringifyType(importedType)}' is imported recursively by the module '${ + stringifyType(moduleType)}'.`), moduleType); return; } @@ -560,8 +564,9 @@ export class CompileMetadataResolver { alreadyCollecting.delete(importedModuleType); if (!importedModuleSummary) { this._reportError( - syntaxError( - `Unexpected ${this._getTypeDescriptor(importedType)} '${stringifyType(importedType)}' imported by the module '${stringifyType(moduleType)}'. Please add a @NgModule annotation.`), + syntaxError(`Unexpected ${this._getTypeDescriptor(importedType)} '${ + stringifyType(importedType)}' imported by the module '${ + stringifyType(moduleType)}'. Please add a @NgModule annotation.`), moduleType); return; } @@ -569,7 +574,8 @@ export class CompileMetadataResolver { } else { this._reportError( syntaxError( - `Unexpected value '${stringifyType(importedType)}' imported by the module '${stringifyType(moduleType)}'`), + `Unexpected value '${stringifyType(importedType)}' imported by the module '${ + stringifyType(moduleType)}'`), moduleType); return; } @@ -581,15 +587,17 @@ export class CompileMetadataResolver { if (!isValidType(exportedType)) { this._reportError( syntaxError( - `Unexpected value '${stringifyType(exportedType)}' exported by the module '${stringifyType(moduleType)}'`), + `Unexpected value '${stringifyType(exportedType)}' exported by the module '${ + stringifyType(moduleType)}'`), moduleType); return; } if (!alreadyCollecting) alreadyCollecting = new Set(); if (alreadyCollecting.has(exportedType)) { this._reportError( - syntaxError( - `${this._getTypeDescriptor(exportedType)} '${stringify(exportedType)}' is exported recursively by the module '${stringifyType(moduleType)}'`), + syntaxError(`${this._getTypeDescriptor(exportedType)} '${ + stringify(exportedType)}' is exported recursively by the module '${ + stringifyType(moduleType)}'`), moduleType); return; } @@ -612,7 +620,8 @@ export class CompileMetadataResolver { if (!isValidType(declaredType)) { this._reportError( syntaxError( - `Unexpected value '${stringifyType(declaredType)}' declared by the module '${stringifyType(moduleType)}'`), + `Unexpected value '${stringifyType(declaredType)}' declared by the module '${ + stringifyType(moduleType)}'`), moduleType); return; } @@ -634,8 +643,10 @@ export class CompileMetadataResolver { this._addTypeToModule(declaredType, moduleType); } else { this._reportError( - syntaxError( - `Unexpected ${this._getTypeDescriptor(declaredType)} '${stringifyType(declaredType)}' declared by the module '${stringifyType(moduleType)}'. Please add a @Pipe/@Directive/@Component annotation.`), + syntaxError(`Unexpected ${this._getTypeDescriptor(declaredType)} '${ + stringifyType(declaredType)}' declared by the module '${ + stringifyType( + moduleType)}'. Please add a @Pipe/@Directive/@Component annotation.`), moduleType); return; } @@ -653,8 +664,9 @@ export class CompileMetadataResolver { transitiveModule.addExportedPipe(exportedId); } else { this._reportError( - syntaxError( - `Can't export ${this._getTypeDescriptor(exportedId.reference)} ${stringifyType(exportedId.reference)} from ${stringifyType(moduleType)} as it was neither declared nor imported!`), + syntaxError(`Can't export ${this._getTypeDescriptor(exportedId.reference)} ${ + stringifyType(exportedId.reference)} from ${ + stringifyType(moduleType)} as it was neither declared nor imported!`), moduleType); return; } @@ -670,15 +682,16 @@ export class CompileMetadataResolver { if (meta.entryComponents) { entryComponents.push(...flattenAndDedupeArray(meta.entryComponents) - .map(type => this._getEntryComponentMetadata(type) !)); + .map(type => this._getEntryComponentMetadata(type)!)); } if (meta.bootstrap) { flattenAndDedupeArray(meta.bootstrap).forEach(type => { if (!isValidType(type)) { this._reportError( - syntaxError( - `Unexpected value '${stringifyType(type)}' used in the bootstrap property of module '${stringifyType(moduleType)}'`), + syntaxError(`Unexpected value '${ + stringifyType(type)}' used in the bootstrap property of module '${ + stringifyType(moduleType)}'`), moduleType); return; } @@ -687,7 +700,7 @@ export class CompileMetadataResolver { } entryComponents.push( - ...bootstrapComponents.map(type => this._getEntryComponentMetadata(type.reference) !)); + ...bootstrapComponents.map(type => this._getEntryComponentMetadata(type.reference)!)); if (meta.schemas) { schemas.push(...flattenAndDedupeArray(meta.schemas)); @@ -710,7 +723,7 @@ export class CompileMetadataResolver { }); entryComponents.forEach((id) => transitiveModule.addEntryComponent(id)); - providers.forEach((provider) => transitiveModule.addProvider(provider, compileMeta !.type)); + providers.forEach((provider) => transitiveModule.addProvider(provider, compileMeta!.type)); transitiveModule.addModule(compileMeta.type); this._ngModuleCache.set(moduleType, compileMeta); return compileMeta; @@ -753,9 +766,13 @@ export class CompileMetadataResolver { if (oldModule && oldModule !== moduleType) { this._reportError( syntaxError( - `Type ${stringifyType(type)} is part of the declarations of 2 modules: ${stringifyType(oldModule)} and ${stringifyType(moduleType)}! ` + - `Please consider moving ${stringifyType(type)} to a higher module that imports ${stringifyType(oldModule)} and ${stringifyType(moduleType)}. ` + - `You can also create a new NgModule that exports and includes ${stringifyType(type)} then import that NgModule in ${stringifyType(oldModule)} and ${stringifyType(moduleType)}.`), + `Type ${stringifyType(type)} is part of the declarations of 2 modules: ${ + stringifyType(oldModule)} and ${stringifyType(moduleType)}! ` + + `Please consider moving ${stringifyType(type)} to a higher module that imports ${ + stringifyType(oldModule)} and ${stringifyType(moduleType)}. ` + + `You can also create a new NgModule that exports and includes ${ + stringifyType(type)} then import that NgModule in ${ + stringifyType(oldModule)} and ${stringifyType(moduleType)}.`), moduleType); return; } @@ -870,7 +887,8 @@ export class CompileMetadataResolver { if (!pipeMeta) { this._reportError( syntaxError( - `Illegal state: getPipeMetadata can only be called after loadNgModuleDirectiveAndPipeMetadata for a module that declares it. Pipe ${stringifyType(pipeType)}.`), + `Illegal state: getPipeMetadata can only be called after loadNgModuleDirectiveAndPipeMetadata for a module that declares it. Pipe ${ + stringifyType(pipeType)}.`), pipeType); } return pipeMeta || null; @@ -898,7 +916,7 @@ export class CompileMetadataResolver { private _loadPipeMetadata(pipeType: any): cpl.CompilePipeMetadata { pipeType = resolveForwardRef(pipeType); - const pipeAnnotation = this._pipeResolver.resolve(pipeType) !; + const pipeAnnotation = this._pipeResolver.resolve(pipeType)!; const pipeMeta = new cpl.CompilePipeMetadata({ type: this._getTypeMetadata(pipeType), @@ -962,7 +980,6 @@ export class CompileMetadataResolver { isOptional, token: this._getTokenMetadata(token) }; - }); if (hasUnknownDeps) { @@ -1000,7 +1017,7 @@ export class CompileMetadataResolver { this._getProvidersMetadata(provider, targetEntryComponents, debugInfo, compileProviders); } else { provider = resolveForwardRef(provider); - let providerMeta: cpl.ProviderMeta = undefined !; + let providerMeta: cpl.ProviderMeta = undefined!; if (provider && typeof provider === 'object' && provider.hasOwnProperty('provide')) { this._validateProvider(provider); providerMeta = new cpl.ProviderMeta(provider.provide, provider); @@ -1027,8 +1044,11 @@ export class CompileMetadataResolver { []) .join(', '); this._reportError( - syntaxError( - `Invalid ${debugInfo ? debugInfo : 'provider'} - only instances of Provider and Type are allowed, got: [${providersInfo}]`), + syntaxError(`Invalid ${ + debugInfo ? + debugInfo : + 'provider'} - only instances of Provider and Type are allowed, got: [${ + providersInfo}]`), type); return; } @@ -1045,8 +1065,8 @@ export class CompileMetadataResolver { private _validateProvider(provider: any): void { if (provider.hasOwnProperty('useClass') && provider.useClass == null) { - this._reportError(syntaxError( - `Invalid provider for ${stringifyType(provider.provide)}. useClass cannot be ${provider.useClass}. + this._reportError(syntaxError(`Invalid provider for ${ + stringifyType(provider.provide)}. useClass cannot be ${provider.useClass}. Usually it happens when: 1. There's a circular dependency (might be caused by using index.ts (barrel) files). 2. Class was used before it was declared. Use forwardRef in this case.`)); @@ -1085,12 +1105,12 @@ export class CompileMetadataResolver { cpl.CompileEntryComponentMetadata|null { const dirMeta = this.getNonNormalizedDirectiveMetadata(dirType); if (dirMeta && dirMeta.metadata.isComponent) { - return {componentType: dirType, componentFactory: dirMeta.metadata.componentFactory !}; + return {componentType: dirType, componentFactory: dirMeta.metadata.componentFactory!}; } const dirSummary = this._loadSummary(dirType, cpl.CompileSummaryKind.Directive); if (dirSummary && dirSummary.isComponent) { - return {componentType: dirType, componentFactory: dirSummary.componentFactory !}; + return {componentType: dirType, componentFactory: dirSummary.componentFactory!}; } if (throwIfNotFound) { throw syntaxError(`${dirType.name} cannot be used as an entry component.`); @@ -1108,9 +1128,9 @@ export class CompileMetadataResolver { } getProviderMetadata(provider: cpl.ProviderMeta): cpl.CompileProviderMetadata { - let compileDeps: cpl.CompileDiDependencyMetadata[] = undefined !; - let compileTypeMetadata: cpl.CompileTypeMetadata = null !; - let compileFactoryMetadata: cpl.CompileFactoryMetadata = null !; + let compileDeps: cpl.CompileDiDependencyMetadata[] = undefined!; + let compileTypeMetadata: cpl.CompileTypeMetadata = null!; + let compileFactoryMetadata: cpl.CompileFactoryMetadata = null!; let token: cpl.CompileTokenMetadata = this._getTokenMetadata(provider.token); if (provider.useClass) { @@ -1152,7 +1172,9 @@ export class CompileMetadataResolver { return res; } - private _queryVarBindings(selector: any): string[] { return selector.split(/\s*,\s*/); } + private _queryVarBindings(selector: any): string[] { + return selector.split(/\s*,\s*/); + } private _getQueryMetadata(q: Query, propertyName: string, typeOrFunc: Type|Function): cpl.CompileQueryMetadata { @@ -1163,8 +1185,8 @@ export class CompileMetadataResolver { } else { if (!q.selector) { this._reportError( - syntaxError( - `Can't construct a query for the property "${propertyName}" of "${stringifyType(typeOrFunc)}" since the query selector wasn't defined.`), + syntaxError(`Can't construct a query for the property "${propertyName}" of "${ + stringifyType(typeOrFunc)}" since the query selector wasn't defined.`), typeOrFunc); selectors = []; } else { @@ -1175,8 +1197,9 @@ export class CompileMetadataResolver { return { selectors, first: q.first, - descendants: q.descendants, propertyName, - read: q.read ? this._getTokenMetadata(q.read) : null !, + descendants: q.descendants, + propertyName, + read: q.read ? this._getTokenMetadata(q.read) : null!, static: q.static }; } diff --git a/packages/compiler/src/ml_parser/ast.ts b/packages/compiler/src/ml_parser/ast.ts index 20e8343553..8dc7761030 100644 --- a/packages/compiler/src/ml_parser/ast.ts +++ b/packages/compiler/src/ml_parser/ast.ts @@ -24,7 +24,9 @@ export class Text extends NodeWithI18n { constructor(public value: string, sourceSpan: ParseSourceSpan, i18n?: I18nMeta) { super(sourceSpan, i18n); } - visit(visitor: Visitor, context: any): any { return visitor.visitText(this, context); } + visit(visitor: Visitor, context: any): any { + return visitor.visitText(this, context); + } } export class Expansion extends NodeWithI18n { @@ -33,7 +35,9 @@ export class Expansion extends NodeWithI18n { sourceSpan: ParseSourceSpan, public switchValueSourceSpan: ParseSourceSpan, i18n?: I18nMeta) { super(sourceSpan, i18n); } - visit(visitor: Visitor, context: any): any { return visitor.visitExpansion(this, context); } + visit(visitor: Visitor, context: any): any { + return visitor.visitExpansion(this, context); + } } export class ExpansionCase implements Node { @@ -41,7 +45,9 @@ export class ExpansionCase implements Node { public value: string, public expression: Node[], public sourceSpan: ParseSourceSpan, public valueSourceSpan: ParseSourceSpan, public expSourceSpan: ParseSourceSpan) {} - visit(visitor: Visitor, context: any): any { return visitor.visitExpansionCase(this, context); } + visit(visitor: Visitor, context: any): any { + return visitor.visitExpansionCase(this, context); + } } export class Attribute extends NodeWithI18n { @@ -50,7 +56,9 @@ export class Attribute extends NodeWithI18n { public valueSpan?: ParseSourceSpan, i18n?: I18nMeta) { super(sourceSpan, i18n); } - visit(visitor: Visitor, context: any): any { return visitor.visitAttribute(this, context); } + visit(visitor: Visitor, context: any): any { + return visitor.visitAttribute(this, context); + } } export class Element extends NodeWithI18n { @@ -60,12 +68,16 @@ export class Element extends NodeWithI18n { public endSourceSpan: ParseSourceSpan|null = null, i18n?: I18nMeta) { super(sourceSpan, i18n); } - visit(visitor: Visitor, context: any): any { return visitor.visitElement(this, context); } + visit(visitor: Visitor, context: any): any { + return visitor.visitElement(this, context); + } } export class Comment implements Node { constructor(public value: string|null, public sourceSpan: ParseSourceSpan) {} - visit(visitor: Visitor, context: any): any { return visitor.visitComment(this, context); } + visit(visitor: Visitor, context: any): any { + return visitor.visitComment(this, context); + } } export interface Visitor { @@ -85,7 +97,7 @@ export function visitAll(visitor: Visitor, nodes: Node[], context: any = null): const result: any[] = []; const visit = visitor.visit ? - (ast: Node) => visitor.visit !(ast, context) || ast.visit(visitor, context) : + (ast: Node) => visitor.visit!(ast, context) || ast.visit(visitor, context) : (ast: Node) => ast.visit(visitor, context); nodes.forEach(ast => { const astResult = visit(ast); @@ -111,7 +123,9 @@ export class RecursiveVisitor implements Visitor { visitComment(ast: Comment, context: any): any {} visitExpansion(ast: Expansion, context: any): any { - return this.visitChildren(context, visit => { visit(ast.cases); }); + return this.visitChildren(context, visit => { + visit(ast.cases); + }); } visitExpansionCase(ast: ExpansionCase, context: any): any {} @@ -120,7 +134,7 @@ export class RecursiveVisitor implements Visitor { context: any, cb: (visit: ((children: V[]|undefined) => void)) => void) { let results: any[][] = []; let t = this; - function visit(children: T[] | undefined) { + function visit(children: T[]|undefined) { if (children) results.push(visitAll(t, children, context)); } cb(visit); diff --git a/packages/compiler/src/ml_parser/html_parser.ts b/packages/compiler/src/ml_parser/html_parser.ts index 5e788523db..bc1d332820 100644 --- a/packages/compiler/src/ml_parser/html_parser.ts +++ b/packages/compiler/src/ml_parser/html_parser.ts @@ -8,12 +8,14 @@ import {getHtmlTagDefinition} from './html_tags'; import {TokenizeOptions} from './lexer'; -import {ParseTreeResult, Parser} from './parser'; +import {Parser, ParseTreeResult} from './parser'; export {ParseTreeResult, TreeError} from './parser'; export class HtmlParser extends Parser { - constructor() { super(getHtmlTagDefinition); } + constructor() { + super(getHtmlTagDefinition); + } parse(source: string, url: string, options?: TokenizeOptions): ParseTreeResult { return super.parse(source, url, options); diff --git a/packages/compiler/src/ml_parser/html_tags.ts b/packages/compiler/src/ml_parser/html_tags.ts index f2dc36a415..5e978fdb63 100644 --- a/packages/compiler/src/ml_parser/html_tags.ts +++ b/packages/compiler/src/ml_parser/html_tags.ts @@ -18,16 +18,21 @@ export class HtmlTagDefinition implements TagDefinition { ignoreFirstLf: boolean; canSelfClose: boolean = false; - constructor( - {closedByChildren, implicitNamespacePrefix, contentType = TagContentType.PARSABLE_DATA, - closedByParent = false, isVoid = false, ignoreFirstLf = false}: { - closedByChildren?: string[], - closedByParent?: boolean, - implicitNamespacePrefix?: string, - contentType?: TagContentType, - isVoid?: boolean, - ignoreFirstLf?: boolean - } = {}) { + constructor({ + closedByChildren, + implicitNamespacePrefix, + contentType = TagContentType.PARSABLE_DATA, + closedByParent = false, + isVoid = false, + ignoreFirstLf = false + }: { + closedByChildren?: string[], + closedByParent?: boolean, + implicitNamespacePrefix?: string, + contentType?: TagContentType, + isVoid?: boolean, + ignoreFirstLf?: boolean + } = {}) { if (closedByChildren && closedByChildren.length > 0) { closedByChildren.forEach(tagName => this.closedByChildren[tagName] = true); } @@ -43,11 +48,11 @@ export class HtmlTagDefinition implements TagDefinition { } } -let _DEFAULT_TAG_DEFINITION !: HtmlTagDefinition; +let _DEFAULT_TAG_DEFINITION!: HtmlTagDefinition; // see http://www.w3.org/TR/html51/syntax.html#optional-tags // This implementation does not fully conform to the HTML5 spec. -let TAG_DEFINITIONS !: {[key: string]: HtmlTagDefinition}; +let TAG_DEFINITIONS!: {[key: string]: HtmlTagDefinition}; export function getHtmlTagDefinition(tagName: string): HtmlTagDefinition { if (!TAG_DEFINITIONS) { diff --git a/packages/compiler/src/ml_parser/html_whitespaces.ts b/packages/compiler/src/ml_parser/html_whitespaces.ts index b2431c15c2..9dfb172107 100644 --- a/packages/compiler/src/ml_parser/html_whitespaces.ts +++ b/packages/compiler/src/ml_parser/html_whitespaces.ts @@ -81,11 +81,17 @@ export class WhitespaceVisitor implements html.Visitor { return null; } - visitComment(comment: html.Comment, context: any): any { return comment; } + visitComment(comment: html.Comment, context: any): any { + return comment; + } - visitExpansion(expansion: html.Expansion, context: any): any { return expansion; } + visitExpansion(expansion: html.Expansion, context: any): any { + return expansion; + } - visitExpansionCase(expansionCase: html.ExpansionCase, context: any): any { return expansionCase; } + visitExpansionCase(expansionCase: html.ExpansionCase, context: any): any { + return expansionCase; + } } export function removeWhitespaces(htmlAstWithErrors: ParseTreeResult): ParseTreeResult { diff --git a/packages/compiler/src/ml_parser/icu_ast_expander.ts b/packages/compiler/src/ml_parser/icu_ast_expander.ts index 3e7d5d15ac..1285aabb9a 100644 --- a/packages/compiler/src/ml_parser/icu_ast_expander.ts +++ b/packages/compiler/src/ml_parser/icu_ast_expander.ts @@ -46,7 +46,9 @@ export class ExpansionResult { } export class ExpansionError extends ParseError { - constructor(span: ParseSourceSpan, errorMsg: string) { super(span, errorMsg); } + constructor(span: ParseSourceSpan, errorMsg: string) { + super(span, errorMsg); + } } /** @@ -64,11 +66,17 @@ class _Expander implements html.Visitor { element.startSourceSpan, element.endSourceSpan); } - visitAttribute(attribute: html.Attribute, context: any): any { return attribute; } + visitAttribute(attribute: html.Attribute, context: any): any { + return attribute; + } - visitText(text: html.Text, context: any): any { return text; } + visitText(text: html.Text, context: any): any { + return text; + } - visitComment(comment: html.Comment, context: any): any { return comment; } + visitComment(comment: html.Comment, context: any): any { + return comment; + } visitExpansion(icu: html.Expansion, context: any): any { this.isExpanded = true; @@ -87,7 +95,7 @@ function _expandPluralForm(ast: html.Expansion, errors: ParseError[]): html.Elem if (PLURAL_CASES.indexOf(c.value) == -1 && !c.value.match(/^=\d+$/)) { errors.push(new ExpansionError( c.valueSourceSpan, - `Plural cases should be "=" or one of ${PLURAL_CASES.join(", ")}`)); + `Plural cases should be "=" or one of ${PLURAL_CASES.join(', ')}`)); } const expansionResult = expandNodes(c.expression); diff --git a/packages/compiler/src/ml_parser/lexer.ts b/packages/compiler/src/ml_parser/lexer.ts index 98ff8f423b..e499f5994c 100644 --- a/packages/compiler/src/ml_parser/lexer.ts +++ b/packages/compiler/src/ml_parser/lexer.ts @@ -764,7 +764,7 @@ function mergeTextTokens(srcTokens: Token[]): Token[] { for (let i = 0; i < srcTokens.length; i++) { const token = srcTokens[i]; if (lastDstToken && lastDstToken.type == TokenType.TEXT && token.type == TokenType.TEXT) { - lastDstToken.parts[0] ! += token.parts[0]; + lastDstToken.parts[0]! += token.parts[0]; lastDstToken.sourceSpan.end = token.sourceSpan.end; } else { lastDstToken = token; @@ -849,15 +849,27 @@ class PlainCharacterCursor implements CharacterCursor { } } - clone(): PlainCharacterCursor { return new PlainCharacterCursor(this); } + clone(): PlainCharacterCursor { + return new PlainCharacterCursor(this); + } - peek() { return this.state.peek; } - charsLeft() { return this.end - this.state.offset; } - diff(other: this) { return this.state.offset - other.state.offset; } + peek() { + return this.state.peek; + } + charsLeft() { + return this.end - this.state.offset; + } + diff(other: this) { + return this.state.offset - other.state.offset; + } - advance(): void { this.advanceState(this.state); } + advance(): void { + this.advanceState(this.state); + } - init(): void { this.updatePeek(this.state); } + init(): void { + this.updatePeek(this.state); + } getSpan(start?: this, leadingTriviaCodePoints?: number[]): ParseSourceSpan { start = start || this; @@ -880,7 +892,9 @@ class PlainCharacterCursor implements CharacterCursor { return this.input.substring(start.state.offset, this.state.offset); } - charAt(pos: number): number { return this.input.charCodeAt(pos); } + charAt(pos: number): number { + return this.input.charCodeAt(pos); + } protected advanceState(state: CursorState) { if (state.offset >= this.end) { @@ -913,7 +927,7 @@ class EscapedCharacterCursor extends PlainCharacterCursor { super(fileOrCursor); this.internalState = {...fileOrCursor.internalState}; } else { - super(fileOrCursor, range !); + super(fileOrCursor, range!); this.internalState = this.state; } } @@ -929,7 +943,9 @@ class EscapedCharacterCursor extends PlainCharacterCursor { this.processEscapeSequence(); } - clone(): EscapedCharacterCursor { return new EscapedCharacterCursor(this); } + clone(): EscapedCharacterCursor { + return new EscapedCharacterCursor(this); + } getChars(start: this): string { const cursor = start.clone(); diff --git a/packages/compiler/src/ml_parser/parser.ts b/packages/compiler/src/ml_parser/parser.ts index 88aca6d6f3..cbb46723ed 100644 --- a/packages/compiler/src/ml_parser/parser.ts +++ b/packages/compiler/src/ml_parser/parser.ts @@ -10,7 +10,7 @@ import {ParseError, ParseSourceSpan} from '../parse_util'; import * as html from './ast'; import * as lex from './lexer'; -import {TagDefinition, getNsPrefix, isNgContainer, mergeNsAndName} from './tags'; +import {getNsPrefix, isNgContainer, mergeNsAndName, TagDefinition} from './tags'; export class TreeError extends ParseError { static create(elementName: string|null, span: ParseSourceSpan, msg: string): TreeError { @@ -43,7 +43,7 @@ export class Parser { class _TreeBuilder { private _index: number = -1; // TODO(issue/24571): remove '!'. - private _peek !: lex.Token; + private _peek!: lex.Token; private _rootNodes: html.Node[] = []; private _errors: TreeError[] = []; @@ -283,7 +283,7 @@ class _TreeBuilder { endTagToken.parts[0], endTagToken.parts[1], this._getParentElement()); if (this._getParentElement()) { - this._getParentElement() !.endSourceSpan = endTagToken.sourceSpan; + this._getParentElement()!.endSourceSpan = endTagToken.sourceSpan; } if (this.getTagDefinition(fullName).isVoid) { @@ -291,8 +291,8 @@ class _TreeBuilder { fullName, endTagToken.sourceSpan, `Void elements do not have end tags "${endTagToken.parts[1]}"`)); } else if (!this._popElement(fullName)) { - const errMsg = - `Unexpected closing tag "${fullName}". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags`; + const errMsg = `Unexpected closing tag "${ + fullName}". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags`; this._errors.push(TreeError.create(fullName, endTagToken.sourceSpan, errMsg)); } } @@ -316,7 +316,7 @@ class _TreeBuilder { const fullName = mergeNsAndName(attrName.parts[0], attrName.parts[1]); let end = attrName.sourceSpan.end; let value = ''; - let valueSpan: ParseSourceSpan = undefined !; + let valueSpan: ParseSourceSpan = undefined!; if (this._peek.type === lex.TokenType.ATTR_QUOTE) { this._advance(); } @@ -344,7 +344,7 @@ class _TreeBuilder { * `` elements are skipped as they are not rendered as DOM element. */ private _getParentElementSkippingContainers(): - {parent: html.Element | null, container: html.Element|null} { + {parent: html.Element|null, container: html.Element|null} { let container: html.Element|null = null; for (let i = this._elementStack.length - 1; i >= 0; i--) { diff --git a/packages/compiler/src/ml_parser/tags.ts b/packages/compiler/src/ml_parser/tags.ts index 038f4fe053..386db35d1e 100644 --- a/packages/compiler/src/ml_parser/tags.ts +++ b/packages/compiler/src/ml_parser/tags.ts @@ -23,7 +23,7 @@ export interface TagDefinition { isClosedByChild(name: string): boolean; } -export function splitNsName(elementName: string): [string | null, string] { +export function splitNsName(elementName: string): [string|null, string] { if (elementName[0] != ':') { return [null, elementName]; } @@ -54,7 +54,7 @@ export function isNgTemplate(tagName: string): boolean { export function getNsPrefix(fullName: string): string; export function getNsPrefix(fullName: null): null; -export function getNsPrefix(fullName: string | null): string|null { +export function getNsPrefix(fullName: string|null): string|null { return fullName === null ? null : splitNsName(fullName)[0]; } diff --git a/packages/compiler/src/ml_parser/xml_parser.ts b/packages/compiler/src/ml_parser/xml_parser.ts index dc6dffb042..2f7933f356 100644 --- a/packages/compiler/src/ml_parser/xml_parser.ts +++ b/packages/compiler/src/ml_parser/xml_parser.ts @@ -7,13 +7,15 @@ */ import {TokenizeOptions} from './lexer'; -import {ParseTreeResult, Parser} from './parser'; +import {Parser, ParseTreeResult} from './parser'; import {getXmlTagDefinition} from './xml_tags'; export {ParseTreeResult, TreeError} from './parser'; export class XmlParser extends Parser { - constructor() { super(getXmlTagDefinition); } + constructor() { + super(getXmlTagDefinition); + } parse(source: string, url: string, options?: TokenizeOptions): ParseTreeResult { return super.parse(source, url, options); diff --git a/packages/compiler/src/ml_parser/xml_tags.ts b/packages/compiler/src/ml_parser/xml_tags.ts index ab52c15dd1..75d14a3d8e 100644 --- a/packages/compiler/src/ml_parser/xml_tags.ts +++ b/packages/compiler/src/ml_parser/xml_tags.ts @@ -11,19 +11,23 @@ import {TagContentType, TagDefinition} from './tags'; export class XmlTagDefinition implements TagDefinition { closedByParent: boolean = false; // TODO(issue/24571): remove '!'. - requiredParents !: {[key: string]: boolean}; + requiredParents!: {[key: string]: boolean}; // TODO(issue/24571): remove '!'. - parentToAdd !: string; + parentToAdd!: string; // TODO(issue/24571): remove '!'. - implicitNamespacePrefix !: string; + implicitNamespacePrefix!: string; contentType: TagContentType = TagContentType.PARSABLE_DATA; isVoid: boolean = false; ignoreFirstLf: boolean = false; canSelfClose: boolean = true; - requireExtraParent(currentParent: string): boolean { return false; } + requireExtraParent(currentParent: string): boolean { + return false; + } - isClosedByChild(name: string): boolean { return false; } + isClosedByChild(name: string): boolean { + return false; + } } const _TAG_DEFINITION = new XmlTagDefinition(); diff --git a/packages/compiler/src/ng_module_compiler.ts b/packages/compiler/src/ng_module_compiler.ts index 6ff46bcb76..f26dfc7092 100644 --- a/packages/compiler/src/ng_module_compiler.ts +++ b/packages/compiler/src/ng_module_compiler.ts @@ -43,8 +43,8 @@ export class NgModuleCompiler { }); const ngModuleDef = o.importExpr(Identifiers.moduleDef).callFn([o.literalArr(providerDefs)]); - const ngModuleDefFactory = o.fn( - [new o.FnParam(LOG_VAR.name !)], [new o.ReturnStatement(ngModuleDef)], o.INFERRED_TYPE); + const ngModuleDefFactory = + o.fn([new o.FnParam(LOG_VAR.name!)], [new o.ReturnStatement(ngModuleDef)], o.INFERRED_TYPE); const ngModuleFactoryVar = `${identifierName(ngModuleMeta.type)}NgFactory`; this._createNgModuleFactory( @@ -77,7 +77,7 @@ export class NgModuleCompiler { .set(value) .toDeclStmt( o.importType( - Identifiers.NgModuleFactory, [o.expressionType(ctx.importExpr(reference)) !], + Identifiers.NgModuleFactory, [o.expressionType(ctx.importExpr(reference))!], [o.TypeModifier.Const]), [o.StmtModifier.Final, o.StmtModifier.Exported]); diff --git a/packages/compiler/src/ng_module_resolver.ts b/packages/compiler/src/ng_module_resolver.ts index 6996f376a1..fa1ca5010b 100644 --- a/packages/compiler/src/ng_module_resolver.ts +++ b/packages/compiler/src/ng_module_resolver.ts @@ -7,7 +7,7 @@ */ import {CompileReflector} from './compile_reflector'; -import {NgModule, Type, createNgModule} from './core'; +import {createNgModule, NgModule, Type} from './core'; import {findLast} from './directive_resolver'; import {stringify} from './util'; @@ -18,7 +18,9 @@ import {stringify} from './util'; export class NgModuleResolver { constructor(private _reflector: CompileReflector) {} - isNgModule(type: any) { return this._reflector.annotations(type).some(createNgModule.isTypeOf); } + isNgModule(type: any) { + return this._reflector.annotations(type).some(createNgModule.isTypeOf); + } resolve(type: Type, throwIfNotFound = true): NgModule|null { const ngModuleMeta: NgModule = diff --git a/packages/compiler/src/output/abstract_emitter.ts b/packages/compiler/src/output/abstract_emitter.ts index 3c809fa590..0e1463890d 100644 --- a/packages/compiler/src/output/abstract_emitter.ts +++ b/packages/compiler/src/output/abstract_emitter.ts @@ -28,31 +28,39 @@ class _EmittedLine { } export class EmitterVisitorContext { - static createRoot(): EmitterVisitorContext { return new EmitterVisitorContext(0); } + static createRoot(): EmitterVisitorContext { + return new EmitterVisitorContext(0); + } private _lines: _EmittedLine[]; private _classes: o.ClassStmt[] = []; private _preambleLineCount = 0; - constructor(private _indent: number) { this._lines = [new _EmittedLine(_indent)]; } + constructor(private _indent: number) { + this._lines = [new _EmittedLine(_indent)]; + } /** * @internal strip this from published d.ts files due to * https://github.com/microsoft/TypeScript/issues/36216 */ - private get _currentLine(): _EmittedLine { return this._lines[this._lines.length - 1]; } + private get _currentLine(): _EmittedLine { + return this._lines[this._lines.length - 1]; + } - println(from?: {sourceSpan: ParseSourceSpan | null}|null, lastPart: string = ''): void { + println(from?: {sourceSpan: ParseSourceSpan|null}|null, lastPart: string = ''): void { this.print(from || null, lastPart, true); } - lineIsEmpty(): boolean { return this._currentLine.parts.length === 0; } + lineIsEmpty(): boolean { + return this._currentLine.parts.length === 0; + } lineLength(): number { return this._currentLine.indent * _INDENT_WITH.length + this._currentLine.partsLength; } - print(from: {sourceSpan: ParseSourceSpan | null}|null, part: string, newLine: boolean = false) { + print(from: {sourceSpan: ParseSourceSpan|null}|null, part: string, newLine: boolean = false) { if (part.length > 0) { this._currentLine.parts.push(part); this._currentLine.partsLength += part.length; @@ -83,9 +91,13 @@ export class EmitterVisitorContext { } } - pushClass(clazz: o.ClassStmt) { this._classes.push(clazz); } + pushClass(clazz: o.ClassStmt) { + this._classes.push(clazz); + } - popClass(): o.ClassStmt { return this._classes.pop() !; } + popClass(): o.ClassStmt { + return this._classes.pop()!; + } get currentClass(): o.ClassStmt|null { return this._classes.length > 0 ? this._classes[this._classes.length - 1] : null; @@ -135,7 +147,7 @@ export class EmitterVisitorContext { } while (spanIdx < spans.length) { - const span = spans[spanIdx] !; + const span = spans[spanIdx]!; const source = span.start.file; const sourceLine = span.start.line; const sourceCol = span.start.col; @@ -156,7 +168,9 @@ export class EmitterVisitorContext { return map; } - setPreambleLineCount(count: number) { return this._preambleLineCount = count; } + setPreambleLineCount(count: number) { + return this._preambleLineCount = count; + } spanOf(line: number, column: number): ParseSourceSpan|null { const emittedLine = this._lines[line - this._preambleLineCount]; @@ -243,7 +257,9 @@ export abstract class AbstractEmitterVisitor implements o.StatementVisitor, o.Ex if (stmt.multiline) { ctx.println(stmt, `/* ${stmt.comment} */`); } else { - stmt.comment.split('\n').forEach((line) => { ctx.println(stmt, `// ${line}`); }); + stmt.comment.split('\n').forEach((line) => { + ctx.println(stmt, `// ${line}`); + }); } return null; } @@ -327,7 +343,7 @@ export abstract class AbstractEmitterVisitor implements o.StatementVisitor, o.Ex expr.expr.visitExpression(this, ctx); } visitReadVarExpr(ast: o.ReadVarExpr, ctx: EmitterVisitorContext): any { - let varName = ast.name !; + let varName = ast.name!; if (ast.builtin != null) { switch (ast.builtin) { case o.BuiltinVar.Super: @@ -337,10 +353,10 @@ export abstract class AbstractEmitterVisitor implements o.StatementVisitor, o.Ex varName = 'this'; break; case o.BuiltinVar.CatchError: - varName = CATCH_ERROR_VAR.name !; + varName = CATCH_ERROR_VAR.name!; break; case o.BuiltinVar.CatchStack: - varName = CATCH_STACK_VAR.name !; + varName = CATCH_STACK_VAR.name!; break; default: throw new Error(`Unknown builtin variable ${ast.builtin}`); @@ -388,7 +404,7 @@ export abstract class AbstractEmitterVisitor implements o.StatementVisitor, o.Ex ctx.print(ast, '? '); ast.trueCase.visitExpression(this, ctx); ctx.print(ast, ': '); - ast.falseCase !.visitExpression(this, ctx); + ast.falseCase!.visitExpression(this, ctx); ctx.print(ast, `)`); return null; } diff --git a/packages/compiler/src/output/abstract_js_emitter.ts b/packages/compiler/src/output/abstract_js_emitter.ts index fde1fbe80b..3802f1a98d 100644 --- a/packages/compiler/src/output/abstract_js_emitter.ts +++ b/packages/compiler/src/output/abstract_js_emitter.ts @@ -11,7 +11,9 @@ import {AbstractEmitterVisitor, CATCH_ERROR_VAR, CATCH_STACK_VAR, EmitterVisitor import * as o from './output_ast'; export abstract class AbstractJsEmitterVisitor extends AbstractEmitterVisitor { - constructor() { super(false); } + constructor() { + super(false); + } visitDeclareClassStmt(stmt: o.ClassStmt, ctx: EmitterVisitorContext): any { ctx.pushClass(stmt); this._visitClassConstructor(stmt, ctx); @@ -101,7 +103,7 @@ export abstract class AbstractJsEmitterVisitor extends AbstractEmitterVisitor { visitInvokeFunctionExpr(expr: o.InvokeFunctionExpr, ctx: EmitterVisitorContext): string|null { const fnExpr = expr.fn; if (fnExpr instanceof o.ReadVarExpr && fnExpr.builtin === o.BuiltinVar.Super) { - ctx.currentClass !.parent !.visitExpression(this, ctx); + ctx.currentClass!.parent!.visitExpression(this, ctx); ctx.print(expr, `.call(this`); if (expr.args.length > 0) { ctx.print(expr, `, `); diff --git a/packages/compiler/src/output/js_emitter.ts b/packages/compiler/src/output/js_emitter.ts index 16ae3138e7..4589676db6 100644 --- a/packages/compiler/src/output/js_emitter.ts +++ b/packages/compiler/src/output/js_emitter.ts @@ -51,7 +51,7 @@ class JsEmitterVisitor extends AbstractJsEmitterVisitor { } ctx.print(ast, `${prefix}.`); } - ctx.print(ast, name !); + ctx.print(ast, name!); return null; } visitDeclareVarStmt(stmt: o.DeclareVarStmt, ctx: EmitterVisitorContext): any { diff --git a/packages/compiler/src/output/output_ast.ts b/packages/compiler/src/output/output_ast.ts index 5e6660af2f..d8d313f561 100644 --- a/packages/compiler/src/output/output_ast.ts +++ b/packages/compiler/src/output/output_ast.ts @@ -24,7 +24,9 @@ export abstract class Type { } abstract visitType(visitor: TypeVisitor, context: any): any; - hasModifier(modifier: TypeModifier): boolean { return this.modifiers !.indexOf(modifier) !== -1; } + hasModifier(modifier: TypeModifier): boolean { + return this.modifiers!.indexOf(modifier) !== -1; + } } export enum BuiltinTypeName { @@ -60,7 +62,9 @@ export class ExpressionType extends Type { export class ArrayType extends Type { - constructor(public of : Type, modifiers: TypeModifier[]|null = null) { super(modifiers); } + constructor(public of: Type, modifiers: TypeModifier[]|null = null) { + super(modifiers); + } visitType(visitor: TypeVisitor, context: any): any { return visitor.visitArrayType(this, context); } @@ -73,7 +77,9 @@ export class MapType extends Type { super(modifiers); this.valueType = valueType || null; } - visitType(visitor: TypeVisitor, context: any): any { return visitor.visitMapType(this, context); } + visitType(visitor: TypeVisitor, context: any): any { + return visitor.visitMapType(this, context); + } } export const DYNAMIC_TYPE = new BuiltinType(BuiltinTypeName.Dynamic); @@ -113,15 +119,15 @@ export enum BinaryOperator { BiggerEquals } -export function nullSafeIsEquivalent( - base: T | null, other: T | null) { +export function nullSafeIsEquivalent( + base: T|null, other: T|null) { if (base == null || other == null) { return base == other; } return base.isEquivalent(other); } -export function areAllEquivalent( +export function areAllEquivalent( base: T[], other: T[]) { const len = base.length; if (len !== other.length) { @@ -243,7 +249,9 @@ export abstract class Expression { return new CastExpr(this, type, sourceSpan); } - toStmt(): Statement { return new ExpressionStatement(this, null); } + toStmt(): Statement { + return new ExpressionStatement(this, null); + } } export enum BuiltinVar { @@ -272,7 +280,9 @@ export class ReadVarExpr extends Expression { return e instanceof ReadVarExpr && this.name === e.name && this.builtin === e.builtin; } - isConstant() { return false; } + isConstant() { + return false; + } visitExpression(visitor: ExpressionVisitor, context: any): any { return visitor.visitReadVarExpr(this, context); @@ -299,7 +309,9 @@ export class TypeofExpr extends Expression { return e instanceof TypeofExpr && e.expr.isEquivalent(this.expr); } - isConstant(): boolean { return this.expr.isConstant(); } + isConstant(): boolean { + return this.expr.isConstant(); + } } export class WrappedNodeExpr extends Expression { @@ -311,7 +323,9 @@ export class WrappedNodeExpr extends Expression { return e instanceof WrappedNodeExpr && this.node === e.node; } - isConstant() { return false; } + isConstant() { + return false; + } visitExpression(visitor: ExpressionVisitor, context: any): any { return visitor.visitWrappedNodeExpr(this, context); @@ -330,7 +344,9 @@ export class WriteVarExpr extends Expression { return e instanceof WriteVarExpr && this.name === e.name && this.value.isEquivalent(e.value); } - isConstant() { return false; } + isConstant() { + return false; + } visitExpression(visitor: ExpressionVisitor, context: any): any { return visitor.visitWriteVarExpr(this, context); @@ -340,7 +356,9 @@ export class WriteVarExpr extends Expression { return new DeclareVarStmt(this.name, this.value, type, modifiers, this.sourceSpan); } - toConstDecl(): DeclareVarStmt { return this.toDeclStmt(INFERRED_TYPE, [StmtModifier.Final]); } + toConstDecl(): DeclareVarStmt { + return this.toDeclStmt(INFERRED_TYPE, [StmtModifier.Final]); + } } @@ -358,7 +376,9 @@ export class WriteKeyExpr extends Expression { this.index.isEquivalent(e.index) && this.value.isEquivalent(e.value); } - isConstant() { return false; } + isConstant() { + return false; + } visitExpression(visitor: ExpressionVisitor, context: any): any { return visitor.visitWriteKeyExpr(this, context); @@ -380,7 +400,9 @@ export class WritePropExpr extends Expression { this.name === e.name && this.value.isEquivalent(e.value); } - isConstant() { return false; } + isConstant() { + return false; + } visitExpression(visitor: ExpressionVisitor, context: any): any { return visitor.visitWritePropExpr(this, context); @@ -414,7 +436,9 @@ export class InvokeMethodExpr extends Expression { this.name === e.name && this.builtin === e.builtin && areAllEquivalent(this.args, e.args); } - isConstant() { return false; } + isConstant() { + return false; + } visitExpression(visitor: ExpressionVisitor, context: any): any { return visitor.visitInvokeMethodExpr(this, context); @@ -434,7 +458,9 @@ export class InvokeFunctionExpr extends Expression { areAllEquivalent(this.args, e.args) && this.pure === e.pure; } - isConstant() { return false; } + isConstant() { + return false; + } visitExpression(visitor: ExpressionVisitor, context: any): any { return visitor.visitInvokeFunctionExpr(this, context); @@ -454,7 +480,9 @@ export class InstantiateExpr extends Expression { areAllEquivalent(this.args, e.args); } - isConstant() { return false; } + isConstant() { + return false; + } visitExpression(visitor: ExpressionVisitor, context: any): any { return visitor.visitInstantiateExpr(this, context); @@ -473,7 +501,9 @@ export class LiteralExpr extends Expression { return e instanceof LiteralExpr && this.value === e.value; } - isConstant() { return true; } + isConstant() { + return true; + } visitExpression(visitor: ExpressionVisitor, context: any): any { return visitor.visitLiteralExpr(this, context); @@ -494,7 +524,9 @@ export class LocalizedString extends Expression { return false; } - isConstant() { return false; } + isConstant() { + return false; + } visitExpression(visitor: ExpressionVisitor, context: any): any { return visitor.visitLocalizedString(this, context); @@ -521,8 +553,9 @@ export class LocalizedString extends Expression { metaBlock = `${metaBlock}${ID_SEPARATOR}${this.metaBlock.customId}`; } if (this.metaBlock.legacyIds) { - this.metaBlock.legacyIds.forEach( - legacyId => { metaBlock = `${metaBlock}${LEGACY_ID_INDICATOR}${legacyId}`; }); + this.metaBlock.legacyIds.forEach(legacyId => { + metaBlock = `${metaBlock}${LEGACY_ID_INDICATOR}${legacyId}`; + }); } return createCookedRawString(metaBlock, this.messageParts[0]); } @@ -588,7 +621,9 @@ export class ExternalExpr extends Expression { this.value.moduleName === e.value.moduleName && this.value.runtime === e.value.runtime; } - isConstant() { return false; } + isConstant() { + return false; + } visitExpression(visitor: ExpressionVisitor, context: any): any { return visitor.visitExternalExpr(this, context); @@ -616,7 +651,9 @@ export class ConditionalExpr extends Expression { this.trueCase.isEquivalent(e.trueCase) && nullSafeIsEquivalent(this.falseCase, e.falseCase); } - isConstant() { return false; } + isConstant() { + return false; + } visitExpression(visitor: ExpressionVisitor, context: any): any { return visitor.visitConditionalExpr(this, context); @@ -633,7 +670,9 @@ export class NotExpr extends Expression { return e instanceof NotExpr && this.condition.isEquivalent(e.condition); } - isConstant() { return false; } + isConstant() { + return false; + } visitExpression(visitor: ExpressionVisitor, context: any): any { return visitor.visitNotExpr(this, context); @@ -649,7 +688,9 @@ export class AssertNotNull extends Expression { return e instanceof AssertNotNull && this.condition.isEquivalent(e.condition); } - isConstant() { return false; } + isConstant() { + return false; + } visitExpression(visitor: ExpressionVisitor, context: any): any { return visitor.visitAssertNotNullExpr(this, context); @@ -665,7 +706,9 @@ export class CastExpr extends Expression { return e instanceof CastExpr && this.value.isEquivalent(e.value); } - isConstant() { return false; } + isConstant() { + return false; + } visitExpression(visitor: ExpressionVisitor, context: any): any { return visitor.visitCastExpr(this, context); @@ -676,7 +719,9 @@ export class CastExpr extends Expression { export class FnParam { constructor(public name: string, public type: Type|null = null) {} - isEquivalent(param: FnParam): boolean { return this.name === param.name; } + isEquivalent(param: FnParam): boolean { + return this.name === param.name; + } } @@ -692,7 +737,9 @@ export class FunctionExpr extends Expression { areAllEquivalent(this.statements, e.statements); } - isConstant() { return false; } + isConstant() { + return false; + } visitExpression(visitor: ExpressionVisitor, context: any): any { return visitor.visitFunctionExpr(this, context); @@ -719,7 +766,9 @@ export class BinaryOperatorExpr extends Expression { this.lhs.isEquivalent(e.lhs) && this.rhs.isEquivalent(e.rhs); } - isConstant() { return false; } + isConstant() { + return false; + } visitExpression(visitor: ExpressionVisitor, context: any): any { return visitor.visitBinaryOperatorExpr(this, context); @@ -739,7 +788,9 @@ export class ReadPropExpr extends Expression { this.name === e.name; } - isConstant() { return false; } + isConstant() { + return false; + } visitExpression(visitor: ExpressionVisitor, context: any): any { return visitor.visitReadPropExpr(this, context); @@ -763,7 +814,9 @@ export class ReadKeyExpr extends Expression { this.index.isEquivalent(e.index); } - isConstant() { return false; } + isConstant() { + return false; + } visitExpression(visitor: ExpressionVisitor, context: any): any { return visitor.visitReadKeyExpr(this, context); @@ -782,7 +835,9 @@ export class LiteralArrayExpr extends Expression { this.entries = entries; } - isConstant() { return this.entries.every(e => e.isConstant()); } + isConstant() { + return this.entries.every(e => e.isConstant()); + } isEquivalent(e: Expression): boolean { return e instanceof LiteralArrayExpr && areAllEquivalent(this.entries, e.entries); @@ -813,7 +868,9 @@ export class LiteralMapExpr extends Expression { return e instanceof LiteralMapExpr && areAllEquivalent(this.entries, e.entries); } - isConstant() { return this.entries.every(e => e.value.isConstant()); } + isConstant() { + return this.entries.every(e => e.value.isConstant()); + } visitExpression(visitor: ExpressionVisitor, context: any): any { return visitor.visitLiteralMapExpr(this, context); @@ -829,7 +886,9 @@ export class CommaExpr extends Expression { return e instanceof CommaExpr && areAllEquivalent(this.parts, e.parts); } - isConstant() { return false; } + isConstant() { + return false; + } visitExpression(visitor: ExpressionVisitor, context: any): any { return visitor.visitCommaExpr(this, context); @@ -892,7 +951,9 @@ export abstract class Statement { abstract visitStatement(visitor: StatementVisitor, context: any): any; - hasModifier(modifier: StmtModifier): boolean { return this.modifiers !.indexOf(modifier) !== -1; } + hasModifier(modifier: StmtModifier): boolean { + return this.modifiers!.indexOf(modifier) !== -1; + } } @@ -965,7 +1026,9 @@ export class AbstractClassPart { } this.type = type || null; } - hasModifier(modifier: StmtModifier): boolean { return this.modifiers !.indexOf(modifier) !== -1; } + hasModifier(modifier: StmtModifier): boolean { + return this.modifiers!.indexOf(modifier) !== -1; + } } export class ClassField extends AbstractClassPart { @@ -974,7 +1037,9 @@ export class ClassField extends AbstractClassPart { public initializer?: Expression) { super(type, modifiers); } - isEquivalent(f: ClassField) { return this.name === f.name; } + isEquivalent(f: ClassField) { + return this.name === f.name; + } } @@ -1044,7 +1109,9 @@ export class CommentStmt extends Statement { constructor(public comment: string, public multiline = false, sourceSpan?: ParseSourceSpan|null) { super(null, sourceSpan); } - isEquivalent(stmt: Statement): boolean { return stmt instanceof CommentStmt; } + isEquivalent(stmt: Statement): boolean { + return stmt instanceof CommentStmt; + } visitStatement(visitor: StatementVisitor, context: any): any { return visitor.visitCommentStmt(this, context); } @@ -1060,7 +1127,9 @@ export class JSDocCommentStmt extends Statement { visitStatement(visitor: StatementVisitor, context: any): any { return visitor.visitJSDocCommentStmt(this, context); } - toString(): string { return serializeTags(this.tags); } + toString(): string { + return serializeTags(this.tags); + } } export class TryCatchStmt extends Statement { @@ -1105,11 +1174,17 @@ export interface StatementVisitor { } export class AstTransformer implements StatementVisitor, ExpressionVisitor { - transformExpr(expr: Expression, context: any): Expression { return expr; } + transformExpr(expr: Expression, context: any): Expression { + return expr; + } - transformStmt(stmt: Statement, context: any): Statement { return stmt; } + transformStmt(stmt: Statement, context: any): Statement { + return stmt; + } - visitReadVarExpr(ast: ReadVarExpr, context: any): any { return this.transformExpr(ast, context); } + visitReadVarExpr(ast: ReadVarExpr, context: any): any { + return this.transformExpr(ast, context); + } visitWrappedNodeExpr(ast: WrappedNodeExpr, context: any): any { return this.transformExpr(ast, context); @@ -1148,7 +1223,7 @@ export class AstTransformer implements StatementVisitor, ExpressionVisitor { const method = ast.builtin || ast.name; return this.transformExpr( new InvokeMethodExpr( - ast.receiver.visitExpression(this, context), method !, + ast.receiver.visitExpression(this, context), method!, this.visitAllExpressions(ast.args, context), ast.type, ast.sourceSpan), context); } @@ -1169,7 +1244,9 @@ export class AstTransformer implements StatementVisitor, ExpressionVisitor { context); } - visitLiteralExpr(ast: LiteralExpr, context: any): any { return this.transformExpr(ast, context); } + visitLiteralExpr(ast: LiteralExpr, context: any): any { + return this.transformExpr(ast, context); + } visitLocalizedString(ast: LocalizedString, context: any): any { return this.transformExpr( @@ -1188,7 +1265,7 @@ export class AstTransformer implements StatementVisitor, ExpressionVisitor { new ConditionalExpr( ast.condition.visitExpression(this, context), ast.trueCase.visitExpression(this, context), - ast.falseCase !.visitExpression(this, context), ast.type, ast.sourceSpan), + ast.falseCase!.visitExpression(this, context), ast.type, ast.sourceSpan), context); } @@ -1284,7 +1361,7 @@ export class AstTransformer implements StatementVisitor, ExpressionVisitor { } visitDeclareClassStmt(stmt: ClassStmt, context: any): any { - const parent = stmt.parent !.visitExpression(this, context); + const parent = stmt.parent!.visitExpression(this, context); const getters = stmt.getters.map( getter => new ClassGetter( getter.name, this.visitAllStatements(getter.body, context), getter.type, @@ -1341,14 +1418,18 @@ export class AstTransformer implements StatementVisitor, ExpressionVisitor { export class RecursiveAstVisitor implements StatementVisitor, ExpressionVisitor { - visitType(ast: Type, context: any): any { return ast; } + visitType(ast: Type, context: any): any { + return ast; + } visitExpression(ast: Expression, context: any): any { if (ast.type) { ast.type.visitType(this, context); } return ast; } - visitBuiltinType(type: BuiltinType, context: any): any { return this.visitType(type, context); } + visitBuiltinType(type: BuiltinType, context: any): any { + return this.visitType(type, context); + } visitExpressionType(type: ExpressionType, context: any): any { type.value.visitExpression(this, context); if (type.typeParams !== null) { @@ -1356,10 +1437,18 @@ export class RecursiveAstVisitor implements StatementVisitor, ExpressionVisitor } return this.visitType(type, context); } - visitArrayType(type: ArrayType, context: any): any { return this.visitType(type, context); } - visitMapType(type: MapType, context: any): any { return this.visitType(type, context); } - visitWrappedNodeExpr(ast: WrappedNodeExpr, context: any): any { return ast; } - visitTypeofExpr(ast: TypeofExpr, context: any): any { return this.visitExpression(ast, context); } + visitArrayType(type: ArrayType, context: any): any { + return this.visitType(type, context); + } + visitMapType(type: MapType, context: any): any { + return this.visitType(type, context); + } + visitWrappedNodeExpr(ast: WrappedNodeExpr, context: any): any { + return ast; + } + visitTypeofExpr(ast: TypeofExpr, context: any): any { + return this.visitExpression(ast, context); + } visitReadVarExpr(ast: ReadVarExpr, context: any): any { return this.visitExpression(ast, context); } @@ -1408,7 +1497,7 @@ export class RecursiveAstVisitor implements StatementVisitor, ExpressionVisitor visitConditionalExpr(ast: ConditionalExpr, context: any): any { ast.condition.visitExpression(this, context); ast.trueCase.visitExpression(this, context); - ast.falseCase !.visitExpression(this, context); + ast.falseCase!.visitExpression(this, context); return this.visitExpression(ast, context); } visitNotExpr(ast: NotExpr, context: any): any { @@ -1482,7 +1571,7 @@ export class RecursiveAstVisitor implements StatementVisitor, ExpressionVisitor return stmt; } visitDeclareClassStmt(stmt: ClassStmt, context: any): any { - stmt.parent !.visitExpression(this, context); + stmt.parent!.visitExpression(this, context); stmt.getters.forEach(getter => this.visitAllStatements(getter.body, context)); if (stmt.constructorMethod) { this.visitAllStatements(stmt.constructorMethod.body, context); @@ -1505,8 +1594,12 @@ export class RecursiveAstVisitor implements StatementVisitor, ExpressionVisitor stmt.error.visitExpression(this, context); return stmt; } - visitCommentStmt(stmt: CommentStmt, context: any): any { return stmt; } - visitJSDocCommentStmt(stmt: JSDocCommentStmt, context: any): any { return stmt; } + visitCommentStmt(stmt: CommentStmt, context: any): any { + return stmt; + } + visitJSDocCommentStmt(stmt: JSDocCommentStmt, context: any): any { + return stmt; + } visitAllStatements(stmts: Statement[], context: any): void { stmts.forEach(stmt => stmt.visitStatement(this, context)); } @@ -1551,7 +1644,7 @@ class _FindExternalReferencesVisitor extends RecursiveAstVisitor { } export function applySourceSpanToStatementIfNeeded( - stmt: Statement, sourceSpan: ParseSourceSpan | null): Statement { + stmt: Statement, sourceSpan: ParseSourceSpan|null): Statement { if (!sourceSpan) { return stmt; } @@ -1560,7 +1653,7 @@ export function applySourceSpanToStatementIfNeeded( } export function applySourceSpanToExpressionIfNeeded( - expr: Expression, sourceSpan: ParseSourceSpan | null): Expression { + expr: Expression, sourceSpan: ParseSourceSpan|null): Expression { if (!sourceSpan) { return expr; } @@ -1569,7 +1662,9 @@ export function applySourceSpanToExpressionIfNeeded( } class _ApplySourceSpanTransformer extends AstTransformer { - constructor(private sourceSpan: ParseSourceSpan) { super(); } + constructor(private sourceSpan: ParseSourceSpan) { + super(); + } private _clone(obj: any): any { const clone = Object.create(obj.constructor.prototype); for (let prop of Object.keys(obj)) { @@ -1596,25 +1691,25 @@ class _ApplySourceSpanTransformer extends AstTransformer { } export function variable( - name: string, type?: Type | null, sourceSpan?: ParseSourceSpan | null): ReadVarExpr { + name: string, type?: Type|null, sourceSpan?: ParseSourceSpan|null): ReadVarExpr { return new ReadVarExpr(name, type, sourceSpan); } export function importExpr( - id: ExternalReference, typeParams: Type[] | null = null, - sourceSpan?: ParseSourceSpan | null): ExternalExpr { + id: ExternalReference, typeParams: Type[]|null = null, + sourceSpan?: ParseSourceSpan|null): ExternalExpr { return new ExternalExpr(id, null, typeParams, sourceSpan); } export function importType( - id: ExternalReference, typeParams: Type[] | null = null, - typeModifiers: TypeModifier[] | null = null): ExpressionType|null { + id: ExternalReference, typeParams: Type[]|null = null, + typeModifiers: TypeModifier[]|null = null): ExpressionType|null { return id != null ? expressionType(importExpr(id, typeParams, null), typeModifiers) : null; } export function expressionType( - expr: Expression, typeModifiers: TypeModifier[] | null = null, - typeParams: Type[] | null = null): ExpressionType { + expr: Expression, typeModifiers: TypeModifier[]|null = null, + typeParams: Type[]|null = null): ExpressionType { return new ExpressionType(expr, typeModifiers, typeParams); } @@ -1623,30 +1718,28 @@ export function typeofExpr(expr: Expression) { } export function literalArr( - values: Expression[], type?: Type | null, - sourceSpan?: ParseSourceSpan | null): LiteralArrayExpr { + values: Expression[], type?: Type|null, sourceSpan?: ParseSourceSpan|null): LiteralArrayExpr { return new LiteralArrayExpr(values, type, sourceSpan); } export function literalMap( values: {key: string, quoted: boolean, value: Expression}[], - type: MapType | null = null): LiteralMapExpr { + type: MapType|null = null): LiteralMapExpr { return new LiteralMapExpr( values.map(e => new LiteralMapEntry(e.key, e.value, e.quoted)), type, null); } -export function not(expr: Expression, sourceSpan?: ParseSourceSpan | null): NotExpr { +export function not(expr: Expression, sourceSpan?: ParseSourceSpan|null): NotExpr { return new NotExpr(expr, sourceSpan); } -export function assertNotNull( - expr: Expression, sourceSpan?: ParseSourceSpan | null): AssertNotNull { +export function assertNotNull(expr: Expression, sourceSpan?: ParseSourceSpan|null): AssertNotNull { return new AssertNotNull(expr, sourceSpan); } export function fn( - params: FnParam[], body: Statement[], type?: Type | null, sourceSpan?: ParseSourceSpan | null, - name?: string | null): FunctionExpr { + params: FnParam[], body: Statement[], type?: Type|null, sourceSpan?: ParseSourceSpan|null, + name?: string|null): FunctionExpr { return new FunctionExpr(params, body, type, sourceSpan, name); } @@ -1655,13 +1748,13 @@ export function ifStmt(condition: Expression, thenClause: Statement[], elseClaus } export function literal( - value: any, type?: Type | null, sourceSpan?: ParseSourceSpan | null): LiteralExpr { + value: any, type?: Type|null, sourceSpan?: ParseSourceSpan|null): LiteralExpr { return new LiteralExpr(value, type, sourceSpan); } export function localizedString( metaBlock: I18nMeta, messageParts: string[], placeholderNames: string[], - expressions: Expression[], sourceSpan?: ParseSourceSpan | null): LocalizedString { + expressions: Expression[], sourceSpan?: ParseSourceSpan|null): LocalizedString { return new LocalizedString(metaBlock, messageParts, placeholderNames, expressions, sourceSpan); } @@ -1684,13 +1777,12 @@ export const enum JSDocTagName { */ export type JSDocTag = { // `tagName` is e.g. "param" in an `@param` declaration - tagName: JSDocTagName | string, + tagName: JSDocTagName|string, // Any remaining text on the tag, e.g. the description text?: string, -} | { +}|{ // no `tagName` for plain text documentation that occurs before any `@param` lines - tagName?: undefined, - text: string, + tagName?: undefined, text: string, }; /* diff --git a/packages/compiler/src/output/output_interpreter.ts b/packages/compiler/src/output/output_interpreter.ts index 6fbde79917..c9a33b8155 100644 --- a/packages/compiler/src/output/output_interpreter.ts +++ b/packages/compiler/src/output/output_interpreter.ts @@ -15,7 +15,9 @@ export function interpretStatements( const visitor = new StatementInterpreter(reflector); visitor.visitAllStatements(statements, ctx); const result: {[key: string]: any} = {}; - ctx.exports.forEach((exportName) => { result[exportName] = ctx.vars.get(exportName); }); + ctx.exports.forEach((exportName) => { + result[exportName] = ctx.vars.get(exportName); + }); return result; } @@ -63,7 +65,7 @@ function createDynamicClass( _classStmt.methods.forEach(function(method: o.ClassMethod) { const paramNames = method.params.map(param => param.name); // Note: use `function` instead of arrow function to capture `this` - propertyDescriptors[method.name !] = { + propertyDescriptors[method.name!] = { writable: false, configurable: false, value: function(...args: any[]) { @@ -77,7 +79,9 @@ function createDynamicClass( // Note: use `function` instead of arrow function to capture `this` const ctor = function(this: Object, ...args: any[]) { const instanceCtx = new _ExecutionContext(_ctx, this, _classStmt.name, _ctx.vars); - _classStmt.fields.forEach((field) => { (this as any)[field.name] = undefined; }); + _classStmt.fields.forEach((field) => { + (this as any)[field.name] = undefined; + }); _executeFunctionStatements( ctorParamNames, args, _classStmt.constructorMethod.body, instanceCtx, _visitor); }; @@ -88,7 +92,9 @@ function createDynamicClass( class StatementInterpreter implements o.StatementVisitor, o.ExpressionVisitor { constructor(private reflector: CompileReflector) {} - debugAst(ast: o.Expression|o.Statement|o.Type): string { return debugOutputAstAsTypeScript(ast); } + debugAst(ast: o.Expression|o.Statement|o.Type): string { + return debugOutputAstAsTypeScript(ast); + } visitDeclareVarStmt(stmt: o.DeclareVarStmt, ctx: _ExecutionContext): any { const initialValue = stmt.value ? stmt.value.visitExpression(this, ctx) : undefined; @@ -106,7 +112,7 @@ class StatementInterpreter implements o.StatementVisitor, o.ExpressionVisitor { currCtx.vars.set(expr.name, value); return value; } - currCtx = currCtx.parent !; + currCtx = currCtx.parent!; } throw new Error(`Not declared variable ${expr.name}`); } @@ -117,7 +123,7 @@ class StatementInterpreter implements o.StatementVisitor, o.ExpressionVisitor { throw new Error('Cannot interpret a TypeofExpr'); } visitReadVarExpr(ast: o.ReadVarExpr, ctx: _ExecutionContext): any { - let varName = ast.name !; + let varName = ast.name!; if (ast.builtin != null) { switch (ast.builtin) { case o.BuiltinVar.Super: @@ -139,7 +145,7 @@ class StatementInterpreter implements o.StatementVisitor, o.ExpressionVisitor { if (currCtx.vars.has(varName)) { return currCtx.vars.get(varName); } - currCtx = currCtx.parent !; + currCtx = currCtx.parent!; } throw new Error(`Not declared variable ${varName}`); } @@ -176,7 +182,7 @@ class StatementInterpreter implements o.StatementVisitor, o.ExpressionVisitor { throw new Error(`Unknown builtin method ${expr.builtin}`); } } else { - result = receiver[expr.name !].apply(receiver, args); + result = receiver[expr.name!].apply(receiver, args); } return result; } @@ -184,7 +190,7 @@ class StatementInterpreter implements o.StatementVisitor, o.ExpressionVisitor { const args = this.visitAllExpressions(stmt.args, ctx); const fnExpr = stmt.fn; if (fnExpr instanceof o.ReadVarExpr && fnExpr.builtin === o.BuiltinVar.Super) { - ctx.instance !.constructor.prototype.constructor.apply(ctx.instance, args); + ctx.instance!.constructor.prototype.constructor.apply(ctx.instance, args); return null; } else { const fn = stmt.fn.visitExpression(this, ctx); @@ -227,15 +233,23 @@ class StatementInterpreter implements o.StatementVisitor, o.ExpressionVisitor { visitThrowStmt(stmt: o.ThrowStmt, ctx: _ExecutionContext): any { throw stmt.error.visitExpression(this, ctx); } - visitCommentStmt(stmt: o.CommentStmt, context?: any): any { return null; } - visitJSDocCommentStmt(stmt: o.JSDocCommentStmt, context?: any): any { return null; } + visitCommentStmt(stmt: o.CommentStmt, context?: any): any { + return null; + } + visitJSDocCommentStmt(stmt: o.JSDocCommentStmt, context?: any): any { + return null; + } visitInstantiateExpr(ast: o.InstantiateExpr, ctx: _ExecutionContext): any { const args = this.visitAllExpressions(ast.args, ctx); const clazz = ast.classExpr.visitExpression(this, ctx); return new clazz(...args); } - visitLiteralExpr(ast: o.LiteralExpr, ctx: _ExecutionContext): any { return ast.value; } - visitLocalizedString(ast: o.LocalizedString, context: any): any { return null; } + visitLiteralExpr(ast: o.LiteralExpr, ctx: _ExecutionContext): any { + return ast.value; + } + visitLocalizedString(ast: o.LocalizedString, context: any): any { + return null; + } visitExternalExpr(ast: o.ExternalExpr, ctx: _ExecutionContext): any { return this.reflector.resolveExternalReference(ast.value); } diff --git a/packages/compiler/src/output/output_jit.ts b/packages/compiler/src/output/output_jit.ts index e446a1351b..6e570cfe05 100644 --- a/packages/compiler/src/output/output_jit.ts +++ b/packages/compiler/src/output/output_jit.ts @@ -87,7 +87,9 @@ export class JitEvaluator { * @param args The arguments to pass to the function being executed. * @returns The return value of the executed function. */ - executeFunction(fn: Function, args: any[]) { return fn(...args); } + executeFunction(fn: Function, args: any[]) { + return fn(...args); + } } /** @@ -98,7 +100,9 @@ export class JitEmitterVisitor extends AbstractJsEmitterVisitor { private _evalArgValues: any[] = []; private _evalExportedVars: string[] = []; - constructor(private reflector: CompileReflector) { super(); } + constructor(private reflector: CompileReflector) { + super(); + } createReturnStmt(ctx: EmitterVisitorContext) { const stmt = new o.ReturnStatement(new o.LiteralMapExpr(this._evalExportedVars.map( diff --git a/packages/compiler/src/output/source_map.ts b/packages/compiler/src/output/source_map.ts index 359e5fb071..739f563b8f 100644 --- a/packages/compiler/src/output/source_map.ts +++ b/packages/compiler/src/output/source_map.ts @@ -23,10 +23,10 @@ type Segment = { export type SourceMap = { version: number, file?: string, - sourceRoot: string, - sources: string[], - sourcesContent: (string | null)[], - mappings: string, + sourceRoot: string, + sources: string[], + sourcesContent: (string|null)[], + mappings: string, }; export class SourceMapGenerator { @@ -75,10 +75,12 @@ export class SourceMapGenerator { } /** - * @internal strip this from published d.ts files due to - * https://github.com/microsoft/TypeScript/issues/36216 - */ - private get currentLine(): Segment[]|null { return this.lines.slice(-1)[0]; } + * @internal strip this from published d.ts files due to + * https://github.com/microsoft/TypeScript/issues/36216 + */ + private get currentLine(): Segment[]|null { + return this.lines.slice(-1)[0]; + } toJSON(): SourceMap|null { if (!this.hasMappings) { @@ -87,7 +89,7 @@ export class SourceMapGenerator { const sourcesIndex = new Map(); const sources: string[] = []; - const sourcesContent: (string | null)[] = []; + const sourcesContent: (string|null)[] = []; Array.from(this.sourcesContent.keys()).forEach((url: string, i: number) => { sourcesIndex.set(url, i); @@ -113,14 +115,14 @@ export class SourceMapGenerator { if (segment.sourceUrl != null) { // zero-based index into the “sources” list segAsStr += - toBase64VLQ(sourcesIndex.get(segment.sourceUrl) ! - lastSourceIndex); - lastSourceIndex = sourcesIndex.get(segment.sourceUrl) !; + toBase64VLQ(sourcesIndex.get(segment.sourceUrl)! - lastSourceIndex); + lastSourceIndex = sourcesIndex.get(segment.sourceUrl)!; // the zero-based starting line in the original source - segAsStr += toBase64VLQ(segment.sourceLine0 ! - lastSourceLine0); - lastSourceLine0 = segment.sourceLine0 !; + segAsStr += toBase64VLQ(segment.sourceLine0! - lastSourceLine0); + lastSourceLine0 = segment.sourceLine0!; // the zero-based starting column in the original source - segAsStr += toBase64VLQ(segment.sourceCol0 ! - lastSourceCol0); - lastSourceCol0 = segment.sourceCol0 !; + segAsStr += toBase64VLQ(segment.sourceCol0! - lastSourceCol0); + lastSourceCol0 = segment.sourceCol0!; } return segAsStr; diff --git a/packages/compiler/src/output/ts_emitter.ts b/packages/compiler/src/output/ts_emitter.ts index e82a3258e0..514d570448 100644 --- a/packages/compiler/src/output/ts_emitter.ts +++ b/packages/compiler/src/output/ts_emitter.ts @@ -15,8 +15,7 @@ import * as o from './output_ast'; const _debugFilePath = '/debug/lib'; -export function debugOutputAstAsTypeScript(ast: o.Statement | o.Expression | o.Type | any[]): - string { +export function debugOutputAstAsTypeScript(ast: o.Statement|o.Expression|o.Type|any[]): string { const converter = new _TsEmitterVisitor(); const ctx = EmitterVisitorContext.createRoot(); const asts: any[] = Array.isArray(ast) ? ast : [ast]; @@ -147,7 +146,7 @@ class _TsEmitterVisitor extends AbstractEmitterVisitor implements o.TypeVisitor reexports = []; this.reexports.set(moduleName, reexports); } - reexports.push({name: name !, as: stmt.name}); + reexports.push({name: name!, as: stmt.name}); return null; } } @@ -175,7 +174,7 @@ class _TsEmitterVisitor extends AbstractEmitterVisitor implements o.TypeVisitor visitCastExpr(ast: o.CastExpr, ctx: EmitterVisitorContext): any { ctx.print(ast, `(<`); - ast.type !.visitType(this, ctx); + ast.type!.visitType(this, ctx); ctx.print(ast, `>`); ast.value.visitExpression(this, ctx); ctx.print(ast, `)`); @@ -422,7 +421,7 @@ class _TsEmitterVisitor extends AbstractEmitterVisitor implements o.TypeVisitor } ctx.print(null, `${prefix}.`); } - ctx.print(null, name !); + ctx.print(null, name!); if (this.typeExpression > 0) { // If we are in a type expression that refers to a generic type then supply @@ -433,7 +432,7 @@ class _TsEmitterVisitor extends AbstractEmitterVisitor implements o.TypeVisitor const suppliedParameters = typeParams || []; if (suppliedParameters.length > 0) { ctx.print(null, `<`); - this.visitAllObjects(type => type.visitType(this, ctx), typeParams !, ctx, ','); + this.visitAllObjects(type => type.visitType(this, ctx), typeParams!, ctx, ','); ctx.print(null, `>`); } } diff --git a/packages/compiler/src/output/value_util.ts b/packages/compiler/src/output/value_util.ts index dd621c3817..b568069f36 100644 --- a/packages/compiler/src/output/value_util.ts +++ b/packages/compiler/src/output/value_util.ts @@ -14,7 +14,7 @@ import * as o from './output_ast'; export const QUOTED_KEYS = '$quoted$'; export function convertValueToOutputAst( - ctx: OutputContext, value: any, type: o.Type | null = null): o.Expression { + ctx: OutputContext, value: any, type: o.Type|null = null): o.Expression { return visitValue(value, new _ValueOutputAstTransformer(ctx), type); } @@ -43,7 +43,9 @@ class _ValueOutputAstTransformer implements ValueTransformer { return new o.LiteralMapExpr(entries, type); } - visitPrimitive(value: any, type: o.Type): o.Expression { return o.literal(value, type); } + visitPrimitive(value: any, type: o.Type): o.Expression { + return o.literal(value, type); + } visitOther(value: any, type: o.Type): o.Expression { if (value instanceof o.Expression) { diff --git a/packages/compiler/src/pipe_resolver.ts b/packages/compiler/src/pipe_resolver.ts index 4f15e9a3ee..c965571e6a 100644 --- a/packages/compiler/src/pipe_resolver.ts +++ b/packages/compiler/src/pipe_resolver.ts @@ -7,7 +7,7 @@ */ import {CompileReflector} from './compile_reflector'; -import {Pipe, Type, createPipe} from './core'; +import {createPipe, Pipe, Type} from './core'; import {findLast} from './directive_resolver'; import {resolveForwardRef, stringify} from './util'; diff --git a/packages/compiler/src/provider_analyzer.ts b/packages/compiler/src/provider_analyzer.ts index f4b335957a..677dbbaf00 100644 --- a/packages/compiler/src/provider_analyzer.ts +++ b/packages/compiler/src/provider_analyzer.ts @@ -9,12 +9,14 @@ import {CompileDiDependencyMetadata, CompileDirectiveMetadata, CompileDirectiveSummary, CompileNgModuleMetadata, CompileProviderMetadata, CompileQueryMetadata, CompileTokenMetadata, CompileTypeMetadata, tokenName, tokenReference} from './compile_metadata'; import {CompileReflector} from './compile_reflector'; -import {Identifiers, createTokenForExternalReference} from './identifiers'; +import {createTokenForExternalReference, Identifiers} from './identifiers'; import {ParseError, ParseSourceSpan} from './parse_util'; import {AttrAst, DirectiveAst, ProviderAst, ProviderAstType, QueryMatch, ReferenceAst} from './template_parser/template_ast'; export class ProviderError extends ParseError { - constructor(message: string, span: ParseSourceSpan) { super(span, message); } + constructor(message: string, span: ParseSourceSpan) { + super(span, message); + } } export interface QueryWithId { @@ -125,7 +127,9 @@ export class ProviderElementContext { get queryMatches(): QueryMatch[] { const allMatches: QueryMatch[] = []; - this._queriedTokens.forEach((matches: QueryMatch[]) => { allMatches.push(...matches); }); + this._queriedTokens.forEach((matches: QueryMatch[]) => { + allMatches.push(...matches); + }); return allMatches; } @@ -171,9 +175,10 @@ export class ProviderElementContext { requestingProviderType: ProviderAstType, token: CompileTokenMetadata, eager: boolean): ProviderAst|null { const resolvedProvider = this._allProviders.get(tokenReference(token)); - if (!resolvedProvider || ((requestingProviderType === ProviderAstType.Directive || - requestingProviderType === ProviderAstType.PublicService) && - resolvedProvider.providerType === ProviderAstType.PrivateService) || + if (!resolvedProvider || + ((requestingProviderType === ProviderAstType.Directive || + requestingProviderType === ProviderAstType.PublicService) && + resolvedProvider.providerType === ProviderAstType.PrivateService) || ((requestingProviderType === ProviderAstType.PrivateService || requestingProviderType === ProviderAstType.PublicService) && resolvedProvider.providerType === ProviderAstType.Builtin)) { @@ -191,25 +196,25 @@ export class ProviderElementContext { this._seenProviders.set(tokenReference(token), true); const transformedProviders = resolvedProvider.providers.map((provider) => { let transformedUseValue = provider.useValue; - let transformedUseExisting = provider.useExisting !; - let transformedDeps: CompileDiDependencyMetadata[] = undefined !; + let transformedUseExisting = provider.useExisting!; + let transformedDeps: CompileDiDependencyMetadata[] = undefined!; if (provider.useExisting != null) { const existingDiDep = this._getDependency( - resolvedProvider.providerType, {token: provider.useExisting}, eager) !; + resolvedProvider.providerType, {token: provider.useExisting}, eager)!; if (existingDiDep.token != null) { transformedUseExisting = existingDiDep.token; } else { - transformedUseExisting = null !; + transformedUseExisting = null!; transformedUseValue = existingDiDep.value; } } else if (provider.useFactory) { const deps = provider.deps || provider.useFactory.diDeps; transformedDeps = - deps.map((dep) => this._getDependency(resolvedProvider.providerType, dep, eager) !); + deps.map((dep) => this._getDependency(resolvedProvider.providerType, dep, eager)!); } else if (provider.useClass) { const deps = provider.deps || provider.useClass.diDeps; transformedDeps = - deps.map((dep) => this._getDependency(resolvedProvider.providerType, dep, eager) !); + deps.map((dep) => this._getDependency(resolvedProvider.providerType, dep, eager)!); } return _transformProvider(provider, { useExisting: transformedUseExisting, @@ -227,7 +232,7 @@ export class ProviderElementContext { requestingProviderType: ProviderAstType, dep: CompileDiDependencyMetadata, eager: boolean = false): CompileDiDependencyMetadata|null { if (dep.isAttribute) { - const attrValue = this._attrs[dep.token !.value]; + const attrValue = this._attrs[dep.token!.value]; return {isValue: true, value: attrValue == null ? null : attrValue}; } @@ -248,7 +253,7 @@ export class ProviderElementContext { } if (tokenReference(dep.token) === this.viewContext.reflector.resolveExternalReference(Identifiers.ViewContainerRef)) { - (this as{transformedHasViewContainer: boolean}).transformedHasViewContainer = true; + (this as {transformedHasViewContainer: boolean}).transformedHasViewContainer = true; } } // access the injector @@ -290,8 +295,8 @@ export class ProviderElementContext { // check @Host restriction if (!result) { if (!dep.isHost || this.viewContext.component.isHost || - this.viewContext.component.type.reference === tokenReference(dep.token !) || - this.viewContext.viewProviders.get(tokenReference(dep.token !)) != null) { + this.viewContext.component.type.reference === tokenReference(dep.token!) || + this.viewContext.viewProviders.get(tokenReference(dep.token!)) != null) { result = dep; } else { result = dep.isOptional ? {isValue: true, value: null} : null; @@ -368,15 +373,15 @@ export class NgModuleProviderAnalyzer { this._seenProviders.set(tokenReference(token), true); const transformedProviders = resolvedProvider.providers.map((provider) => { let transformedUseValue = provider.useValue; - let transformedUseExisting = provider.useExisting !; - let transformedDeps: CompileDiDependencyMetadata[] = undefined !; + let transformedUseExisting = provider.useExisting!; + let transformedDeps: CompileDiDependencyMetadata[] = undefined!; if (provider.useExisting != null) { const existingDiDep = this._getDependency({token: provider.useExisting}, eager, resolvedProvider.sourceSpan); if (existingDiDep.token != null) { transformedUseExisting = existingDiDep.token; } else { - transformedUseExisting = null !; + transformedUseExisting = null!; transformedUseValue = existingDiDep.value; } } else if (provider.useFactory) { @@ -478,7 +483,8 @@ function _resolveProviders( let resolvedProvider = targetProvidersByToken.get(tokenReference(provider.token)); if (resolvedProvider != null && !!resolvedProvider.multiProvider !== !!provider.multi) { targetErrors.push(new ProviderError( - `Mixing multi and non multi provider is not possible for token ${tokenName(resolvedProvider.token)}`, + `Mixing multi and non multi provider is not possible for token ${ + tokenName(resolvedProvider.token)}`, sourceSpan)); } if (!resolvedProvider) { diff --git a/packages/compiler/src/render3/r3_ast.ts b/packages/compiler/src/render3/r3_ast.ts index 45ae7dd79d..3fa682a768 100644 --- a/packages/compiler/src/render3/r3_ast.ts +++ b/packages/compiler/src/render3/r3_ast.ts @@ -18,19 +18,25 @@ export interface Node { export class Text implements Node { constructor(public value: string, public sourceSpan: ParseSourceSpan) {} - visit(visitor: Visitor): Result { return visitor.visitText(this); } + visit(visitor: Visitor): Result { + return visitor.visitText(this); + } } export class BoundText implements Node { constructor(public value: AST, public sourceSpan: ParseSourceSpan, public i18n?: I18nMeta) {} - visit(visitor: Visitor): Result { return visitor.visitBoundText(this); } + visit(visitor: Visitor): Result { + return visitor.visitBoundText(this); + } } export class TextAttribute implements Node { constructor( public name: string, public value: string, public sourceSpan: ParseSourceSpan, public valueSpan?: ParseSourceSpan, public i18n?: I18nMeta) {} - visit(visitor: Visitor): Result { return visitor.visitTextAttribute(this); } + visit(visitor: Visitor): Result { + return visitor.visitTextAttribute(this); + } } export class BoundAttribute implements Node { @@ -45,7 +51,9 @@ export class BoundAttribute implements Node { prop.valueSpan, i18n); } - visit(visitor: Visitor): Result { return visitor.visitBoundAttribute(this); } + visit(visitor: Visitor): Result { + return visitor.visitBoundAttribute(this); + } } export class BoundEvent implements Node { @@ -62,7 +70,9 @@ export class BoundEvent implements Node { event.name, event.type, event.handler, target, phase, event.sourceSpan, event.handlerSpan); } - visit(visitor: Visitor): Result { return visitor.visitBoundEvent(this); } + visit(visitor: Visitor): Result { + return visitor.visitBoundEvent(this); + } } export class Element implements Node { @@ -76,7 +86,9 @@ export class Element implements Node { this.sourceSpan = new ParseSourceSpan(sourceSpan.start, endSourceSpan.end); } } - visit(visitor: Visitor): Result { return visitor.visitElement(this); } + visit(visitor: Visitor): Result { + return visitor.visitElement(this); + } } export class Template implements Node { @@ -86,36 +98,46 @@ export class Template implements Node { public children: Node[], public references: Reference[], public variables: Variable[], public sourceSpan: ParseSourceSpan, public startSourceSpan: ParseSourceSpan|null, public endSourceSpan: ParseSourceSpan|null, public i18n?: I18nMeta) {} - visit(visitor: Visitor): Result { return visitor.visitTemplate(this); } + visit(visitor: Visitor): Result { + return visitor.visitTemplate(this); + } } export class Content implements Node { constructor( public selector: string, public attributes: TextAttribute[], public sourceSpan: ParseSourceSpan, public i18n?: I18nMeta) {} - visit(visitor: Visitor): Result { return visitor.visitContent(this); } + visit(visitor: Visitor): Result { + return visitor.visitContent(this); + } } export class Variable implements Node { constructor( public name: string, public value: string, public sourceSpan: ParseSourceSpan, public valueSpan?: ParseSourceSpan) {} - visit(visitor: Visitor): Result { return visitor.visitVariable(this); } + visit(visitor: Visitor): Result { + return visitor.visitVariable(this); + } } export class Reference implements Node { constructor( public name: string, public value: string, public sourceSpan: ParseSourceSpan, public valueSpan?: ParseSourceSpan) {} - visit(visitor: Visitor): Result { return visitor.visitReference(this); } + visit(visitor: Visitor): Result { + return visitor.visitReference(this); + } } export class Icu implements Node { constructor( public vars: {[name: string]: BoundText}, - public placeholders: {[name: string]: Text | BoundText}, public sourceSpan: ParseSourceSpan, + public placeholders: {[name: string]: Text|BoundText}, public sourceSpan: ParseSourceSpan, public i18n?: I18nMeta) {} - visit(visitor: Visitor): Result { return visitor.visitIcu(this); } + visit(visitor: Visitor): Result { + return visitor.visitIcu(this); + } } export interface Visitor { @@ -210,16 +232,34 @@ export class TransformVisitor implements Visitor { return template; } - visitContent(content: Content): Node { return content; } + visitContent(content: Content): Node { + return content; + } - visitVariable(variable: Variable): Node { return variable; } - visitReference(reference: Reference): Node { return reference; } - visitTextAttribute(attribute: TextAttribute): Node { return attribute; } - visitBoundAttribute(attribute: BoundAttribute): Node { return attribute; } - visitBoundEvent(attribute: BoundEvent): Node { return attribute; } - visitText(text: Text): Node { return text; } - visitBoundText(text: BoundText): Node { return text; } - visitIcu(icu: Icu): Node { return icu; } + visitVariable(variable: Variable): Node { + return variable; + } + visitReference(reference: Reference): Node { + return reference; + } + visitTextAttribute(attribute: TextAttribute): Node { + return attribute; + } + visitBoundAttribute(attribute: BoundAttribute): Node { + return attribute; + } + visitBoundEvent(attribute: BoundEvent): Node { + return attribute; + } + visitText(text: Text): Node { + return text; + } + visitBoundText(text: BoundText): Node { + return text; + } + visitIcu(icu: Icu): Node { + return icu; + } } export function visitAll(visitor: Visitor, nodes: Node[]): Result[] { diff --git a/packages/compiler/src/render3/r3_factory.ts b/packages/compiler/src/render3/r3_factory.ts index e0bcc39ad8..dfb6fea44b 100644 --- a/packages/compiler/src/render3/r3_factory.ts +++ b/packages/compiler/src/render3/r3_factory.ts @@ -89,8 +89,8 @@ export interface R3ExpressionFactoryMetadata extends R3ConstructorFactoryMetadat expression: o.Expression; } -export type R3FactoryMetadata = R3ConstructorFactoryMetadata | R3DelegatedFactoryMetadata | - R3DelegatedFnOrClassMetadata | R3ExpressionFactoryMetadata; +export type R3FactoryMetadata = R3ConstructorFactoryMetadata|R3DelegatedFactoryMetadata| + R3DelegatedFnOrClassMetadata|R3ExpressionFactoryMetadata; export enum R3FactoryTarget { Directive = 0, @@ -280,8 +280,7 @@ export function compileFactoryFunction(meta: R3FactoryMetadata): R3FactoryFn { `${meta.name}_Factory`), statements, type: o.expressionType(o.importExpr( - R3.FactoryDef, - [typeWithParameters(meta.type.type, meta.typeArgumentCount), ctorDepsType])) + R3.FactoryDef, [typeWithParameters(meta.type.type, meta.typeArgumentCount), ctorDepsType])) }; } @@ -402,7 +401,8 @@ export function dependenciesFromGlobalMetadata( // Construct the dependency. deps.push({ token, - attribute: null, resolved, + attribute: null, + resolved, host: !!dependency.isHost, optional: !!dependency.isOptional, self: !!dependency.isSelf, diff --git a/packages/compiler/src/render3/r3_jit.ts b/packages/compiler/src/render3/r3_jit.ts index 6c73d6a6a8..89b7cc9c0c 100644 --- a/packages/compiler/src/render3/r3_jit.ts +++ b/packages/compiler/src/render3/r3_jit.ts @@ -21,28 +21,44 @@ export class R3JitReflector implements CompileReflector { resolveExternalReference(ref: o.ExternalReference): any { // This reflector only handles @angular/core imports. if (ref.moduleName !== '@angular/core') { - throw new Error( - `Cannot resolve external reference to ${ref.moduleName}, only references to @angular/core are supported.`); + throw new Error(`Cannot resolve external reference to ${ + ref.moduleName}, only references to @angular/core are supported.`); } - if (!this.context.hasOwnProperty(ref.name !)) { + if (!this.context.hasOwnProperty(ref.name!)) { throw new Error(`No value provided for @angular/core symbol '${ref.name!}'.`); } - return this.context[ref.name !]; + return this.context[ref.name!]; } - parameters(typeOrFunc: any): any[][] { throw new Error('Not implemented.'); } + parameters(typeOrFunc: any): any[][] { + throw new Error('Not implemented.'); + } - annotations(typeOrFunc: any): any[] { throw new Error('Not implemented.'); } + annotations(typeOrFunc: any): any[] { + throw new Error('Not implemented.'); + } - shallowAnnotations(typeOrFunc: any): any[] { throw new Error('Not implemented.'); } + shallowAnnotations(typeOrFunc: any): any[] { + throw new Error('Not implemented.'); + } - tryAnnotations(typeOrFunc: any): any[] { throw new Error('Not implemented.'); } + tryAnnotations(typeOrFunc: any): any[] { + throw new Error('Not implemented.'); + } - propMetadata(typeOrFunc: any): {[key: string]: any[];} { throw new Error('Not implemented.'); } + propMetadata(typeOrFunc: any): {[key: string]: any[];} { + throw new Error('Not implemented.'); + } - hasLifecycleHook(type: any, lcProperty: string): boolean { throw new Error('Not implemented.'); } + hasLifecycleHook(type: any, lcProperty: string): boolean { + throw new Error('Not implemented.'); + } - guards(typeOrFunc: any): {[key: string]: any;} { throw new Error('Not implemented.'); } + guards(typeOrFunc: any): {[key: string]: any;} { + throw new Error('Not implemented.'); + } - componentModuleUrl(type: any, cmpMetadata: any): string { throw new Error('Not implemented.'); } + componentModuleUrl(type: any, cmpMetadata: any): string { + throw new Error('Not implemented.'); + } } diff --git a/packages/compiler/src/render3/r3_module_compiler.ts b/packages/compiler/src/render3/r3_module_compiler.ts index 833a88bc53..69379d08b2 100644 --- a/packages/compiler/src/render3/r3_module_compiler.ts +++ b/packages/compiler/src/render3/r3_module_compiler.ts @@ -12,9 +12,9 @@ import {mapLiteral} from '../output/map_util'; import * as o from '../output/output_ast'; import {OutputContext} from '../util'; -import {R3DependencyMetadata, R3FactoryTarget, compileFactoryFunction} from './r3_factory'; +import {compileFactoryFunction, R3DependencyMetadata, R3FactoryTarget} from './r3_factory'; import {Identifiers as R3} from './r3_identifiers'; -import {R3Reference, convertMetaToOutput, jitOnlyGuardedExpression, mapToMapExpression} from './util'; +import {convertMetaToOutput, jitOnlyGuardedExpression, mapToMapExpression, R3Reference} from './util'; export interface R3NgModuleDef { expression: o.Expression; @@ -108,9 +108,7 @@ export function compileNgModule(meta: R3NgModuleMetadata): R3NgModuleDef { } = meta; const additionalStatements: o.Statement[] = []; - const definitionMap = { - type: internalType - } as{ + const definitionMap = {type: internalType} as { type: o.Expression, bootstrap: o.Expression, declarations: o.Expression, @@ -177,7 +175,7 @@ export function compileNgModule(meta: R3NgModuleMetadata): R3NgModuleDef { function generateSetNgModuleScopeCall(meta: R3NgModuleMetadata): o.Statement|null { const {adjacentType: moduleType, declarations, imports, exports, containsForwardDecls} = meta; - const scopeMap = {} as{ + const scopeMap = {} as { declarations: o.Expression, imports: o.Expression, exports: o.Expression, @@ -247,7 +245,7 @@ export function compileInjector(meta: R3InjectorMetadata): R3InjectorDef { }); const definitionMap = { factory: result.factory, - } as{factory: o.Expression, providers: o.Expression, imports: o.Expression}; + } as {factory: o.Expression, providers: o.Expression, imports: o.Expression}; if (meta.providers !== null) { definitionMap.providers = meta.providers; @@ -267,7 +265,7 @@ export function compileInjector(meta: R3InjectorMetadata): R3InjectorDef { export function compileNgModuleFromRender2( ctx: OutputContext, ngModule: CompileShallowModuleMetadata, injectableCompiler: InjectableCompiler): void { - const className = identifierName(ngModule.type) !; + const className = identifierName(ngModule.type)!; const rawImports = ngModule.rawImports ? [ngModule.rawImports] : []; const rawExports = ngModule.rawExports ? [ngModule.rawExports] : []; @@ -288,7 +286,8 @@ export function compileNgModuleFromRender2( /* name */ 'ɵinj', /* type */ o.INFERRED_TYPE, /* modifiers */[o.StmtModifier.Static], - /* initializer */ injectorDef, )], + /* initializer */ injectorDef, + )], /* getters */[], /* constructorMethod */ new o.ClassMethod(null, [], []), /* methods */[])); diff --git a/packages/compiler/src/render3/r3_pipe_compiler.ts b/packages/compiler/src/render3/r3_pipe_compiler.ts index 703a001a48..25797c6635 100644 --- a/packages/compiler/src/render3/r3_pipe_compiler.ts +++ b/packages/compiler/src/render3/r3_pipe_compiler.ts @@ -10,9 +10,9 @@ import {CompilePipeMetadata, identifierName} from '../compile_metadata'; import {CompileReflector} from '../compile_reflector'; import {DefinitionKind} from '../constant_pool'; import * as o from '../output/output_ast'; -import {OutputContext, error} from '../util'; +import {error, OutputContext} from '../util'; -import {R3DependencyMetadata, R3FactoryTarget, compileFactoryFunction, dependenciesFromGlobalMetadata} from './r3_factory'; +import {compileFactoryFunction, dependenciesFromGlobalMetadata, R3DependencyMetadata, R3FactoryTarget} from './r3_factory'; import {Identifiers as R3} from './r3_identifiers'; import {R3Reference, typeWithParameters, wrapReference} from './util'; diff --git a/packages/compiler/src/render3/r3_template_transform.ts b/packages/compiler/src/render3/r3_template_transform.ts index 84245d0066..7ab7acd4ab 100644 --- a/packages/compiler/src/render3/r3_template_transform.ts +++ b/packages/compiler/src/render3/r3_template_transform.ts @@ -215,7 +215,7 @@ class HtmlAstToIvyAst implements html.Visitor { // Moreover, if the node is an element, then we need to hoist its attributes to the template // node for matching against content projection selectors. const attrs = this.extractAttributes('ng-template', templateParsedProperties, i18nAttrsMeta); - const templateAttrs: (t.TextAttribute | t.BoundAttribute)[] = []; + const templateAttrs: (t.TextAttribute|t.BoundAttribute)[] = []; attrs.literal.forEach(attr => templateAttrs.push(attr)); attrs.bound.forEach(attr => templateAttrs.push(attr)); const hoistedAttrs = parsedElement instanceof t.Element ? @@ -260,12 +260,12 @@ class HtmlAstToIvyAst implements html.Visitor { return null; } if (!isI18nRootNode(expansion.i18n)) { - throw new Error( - `Invalid type "${expansion.i18n.constructor}" for "i18n" property of ${expansion.sourceSpan.toString()}. Expected a "Message"`); + throw new Error(`Invalid type "${expansion.i18n.constructor}" for "i18n" property of ${ + expansion.sourceSpan.toString()}. Expected a "Message"`); } const message = expansion.i18n; const vars: {[name: string]: t.BoundText} = {}; - const placeholders: {[name: string]: t.Text | t.BoundText} = {}; + const placeholders: {[name: string]: t.Text|t.BoundText} = {}; // extract VARs from ICUs - we process them separately while // assembling resulting message via goog.getMsg function, since // we need to pass them to top-level goog.getMsg call @@ -284,9 +284,13 @@ class HtmlAstToIvyAst implements html.Visitor { return new t.Icu(vars, placeholders, expansion.sourceSpan, message); } - visitExpansionCase(expansionCase: html.ExpansionCase): null { return null; } + visitExpansionCase(expansionCase: html.ExpansionCase): null { + return null; + } - visitComment(comment: html.Comment): null { return null; } + visitComment(comment: html.Comment): null { + return null; + } // convert view engine `ParsedProperty` to a format suitable for IVY private extractAttributes( @@ -460,18 +464,26 @@ class NonBindableVisitor implements html.Visitor { ast.startSourceSpan, ast.endSourceSpan); } - visitComment(comment: html.Comment): any { return null; } + visitComment(comment: html.Comment): any { + return null; + } visitAttribute(attribute: html.Attribute): t.TextAttribute { return new t.TextAttribute( attribute.name, attribute.value, attribute.sourceSpan, undefined, attribute.i18n); } - visitText(text: html.Text): t.Text { return new t.Text(text.value, text.sourceSpan); } + visitText(text: html.Text): t.Text { + return new t.Text(text.value, text.sourceSpan); + } - visitExpansion(expansion: html.Expansion): any { return null; } + visitExpansion(expansion: html.Expansion): any { + return null; + } - visitExpansionCase(expansionCase: html.ExpansionCase): any { return null; } + visitExpansionCase(expansionCase: html.ExpansionCase): any { + return null; + } } const NON_BINDABLE_VISITOR = new NonBindableVisitor(); diff --git a/packages/compiler/src/render3/util.ts b/packages/compiler/src/render3/util.ts index 5572495dcf..a450f8c0f2 100644 --- a/packages/compiler/src/render3/util.ts +++ b/packages/compiler/src/render3/util.ts @@ -13,14 +13,13 @@ import {OutputContext} from '../util'; /** * Convert an object map with `Expression` values into a `LiteralMapExpr`. */ -export function mapToMapExpression(map: {[key: string]: o.Expression | undefined}): - o.LiteralMapExpr { +export function mapToMapExpression(map: {[key: string]: o.Expression|undefined}): o.LiteralMapExpr { const result = Object.keys(map).map( key => ({ key, // The assertion here is because really TypeScript doesn't allow us to express that if the // key is present, it will have a value, but this is true in reality. - value: map[key] !, + value: map[key]!, quoted: false, })); return o.literalMap(result); diff --git a/packages/compiler/src/render3/view/api.ts b/packages/compiler/src/render3/view/api.ts index 5b6a2d5594..a369af694d 100644 --- a/packages/compiler/src/render3/view/api.ts +++ b/packages/compiler/src/render3/view/api.ts @@ -88,7 +88,7 @@ export interface R3DirectiveMetadata { /** * A mapping of input field names to the property names. */ - inputs: {[field: string]: string | [string, string]}; + inputs: {[field: string]: string|[string, string]}; /** * A mapping of output field names to the property names. diff --git a/packages/compiler/src/render3/view/compiler.ts b/packages/compiler/src/render3/view/compiler.ts index eb924357e8..08b6344011 100644 --- a/packages/compiler/src/render3/view/compiler.ts +++ b/packages/compiler/src/render3/view/compiler.ts @@ -20,17 +20,17 @@ import {CssSelector, SelectorMatcher} from '../../selector'; import {ShadowCss} from '../../shadow_css'; import {CONTENT_ATTR, HOST_ATTR} from '../../style_compiler'; import {BindingParser} from '../../template_parser/binding_parser'; -import {OutputContext, error} from '../../util'; +import {error, OutputContext} from '../../util'; import {BoundEvent} from '../r3_ast'; -import {R3DependencyMetadata, R3FactoryTarget, R3ResolvedDependencyType, compileFactoryFunction} from '../r3_factory'; +import {compileFactoryFunction, R3DependencyMetadata, R3FactoryTarget, R3ResolvedDependencyType} from '../r3_factory'; import {Identifiers as R3} from '../r3_identifiers'; import {Render3ParseResult} from '../r3_template_transform'; import {prepareSyntheticListenerFunctionName, prepareSyntheticPropertyName, typeWithParameters} from '../util'; import {R3ComponentDef, R3ComponentMetadata, R3DirectiveDef, R3DirectiveMetadata, R3HostMetadata, R3QueryMetadata} from './api'; import {MIN_STYLING_BINDING_SLOTS_REQUIRED, StylingBuilder, StylingInstructionCall} from './styling_builder'; -import {BindingScope, TemplateDefinitionBuilder, ValueConverter, makeBindingParser, prepareEventListenerParameters, renderFlagCheckIfStmt, resolveSanitizationFn} from './template'; -import {CONTEXT_NAME, DefinitionMap, RENDER_FLAGS, TEMPORARY_NAME, asLiteral, chainedInstruction, conditionallyCreateMapObjectLiteral, getQueryPredicate, temporaryAllocator} from './util'; +import {BindingScope, makeBindingParser, prepareEventListenerParameters, renderFlagCheckIfStmt, resolveSanitizationFn, TemplateDefinitionBuilder, ValueConverter} from './template'; +import {asLiteral, chainedInstruction, conditionallyCreateMapObjectLiteral, CONTEXT_NAME, DefinitionMap, getQueryPredicate, RENDER_FLAGS, TEMPORARY_NAME, temporaryAllocator} from './util'; const EMPTY_ARRAY: any[] = []; @@ -65,9 +65,10 @@ function baseDirectiveFields( // e.g. `hostBindings: (rf, ctx) => { ... } definitionMap.set( - 'hostBindings', createHostBindingsFunction( - meta.host, meta.typeSourceSpan, bindingParser, constantPool, - meta.selector || '', meta.name, definitionMap)); + 'hostBindings', + createHostBindingsFunction( + meta.host, meta.typeSourceSpan, bindingParser, constantPool, meta.selector || '', + meta.name, definitionMap)); // e.g 'inputs: {a: 'a'}` definitionMap.set('inputs', conditionallyCreateMapObjectLiteral(meta.inputs, true)); @@ -85,8 +86,7 @@ function baseDirectiveFields( /** * Add features to the definition map. */ -function addFeatures( - definitionMap: DefinitionMap, meta: R3DirectiveMetadata | R3ComponentMetadata) { +function addFeatures(definitionMap: DefinitionMap, meta: R3DirectiveMetadata|R3ComponentMetadata) { // e.g. `features: [NgOnChangesFeature]` const features: o.Expression[] = []; @@ -148,10 +148,11 @@ export function compileComponentFromMetadata( const selectorAttributes = firstSelector.getAttrs(); if (selectorAttributes.length) { definitionMap.set( - 'attrs', constantPool.getConstLiteral( - o.literalArr(selectorAttributes.map( - value => value != null ? o.literal(value) : o.literal(undefined))), - /* forceShared */ true)); + 'attrs', + constantPool.getConstLiteral( + o.literalArr(selectorAttributes.map( + value => value != null ? o.literal(value) : o.literal(undefined))), + /* forceShared */ true)); } } @@ -273,7 +274,7 @@ export function compileComponentFromMetadata( export function compileDirectiveFromRender2( outputCtx: OutputContext, directive: CompileDirectiveMetadata, reflector: CompileReflector, bindingParser: BindingParser) { - const name = identifierName(directive.type) !; + const name = identifierName(directive.type)!; name || error(`Cannot resolver the name of ${directive.type}`); const definitionField = outputCtx.constantPool.propertyNameOf(DefinitionKind.Directive); @@ -306,7 +307,7 @@ export function compileComponentFromRender2( outputCtx: OutputContext, component: CompileDirectiveMetadata, render3Ast: Render3ParseResult, reflector: CompileReflector, bindingParser: BindingParser, directiveTypeBySel: Map, pipeTypeByName: Map) { - const name = identifierName(component.type) !; + const name = identifierName(component.type)!; name || error(`Cannot resolver the name of ${component.type}`); const definitionField = outputCtx.constantPool.propertyNameOf(DefinitionKind.Component); @@ -372,7 +373,8 @@ function queriesFromGlobalMetadata( propertyName: query.propertyName, first: query.first, predicate: selectorsFromGlobalMetadata(query.selectors, outputCtx), - descendants: query.descendants, read, + descendants: query.descendants, + read, static: !!query.static }; }); @@ -464,7 +466,7 @@ function stringAsType(str: string): o.Type { return o.expressionType(o.literal(str)); } -function stringMapAsType(map: {[key: string]: string | string[]}): o.Type { +function stringMapAsType(map: {[key: string]: string|string[]}): o.Type { const mapValues = Object.keys(map).map(key => { const value = Array.isArray(map[key]) ? map[key][0] : map[key]; return { @@ -723,7 +725,7 @@ function convertStylingCall( function getBindingNameAndInstruction(binding: ParsedProperty): {bindingName: string, instruction: o.ExternalReference, isAttribute: boolean} { let bindingName = binding.name; - let instruction !: o.ExternalReference; + let instruction!: o.ExternalReference; // Check to see if this is an attr binding or a property binding const attrMatches = bindingName.match(ATTR_REGEX); @@ -817,8 +819,7 @@ export interface ParsedHostBindings { specialAttributes: {styleAttr?: string; classAttr?: string;}; } -export function parseHostBindings(host: {[key: string]: string | o.Expression}): - ParsedHostBindings { +export function parseHostBindings(host: {[key: string]: string|o.Expression}): ParsedHostBindings { const attributes: {[key: string]: o.Expression} = {}; const listeners: {[key: string]: string} = {}; const properties: {[key: string]: string} = {}; @@ -893,5 +894,7 @@ export function verifyHostBindings( function compileStyles(styles: string[], selector: string, hostSelector: string): string[] { const shadowCss = new ShadowCss(); - return styles.map(style => { return shadowCss !.shimCssText(style, selector, hostSelector); }); + return styles.map(style => { + return shadowCss!.shimCssText(style, selector, hostSelector); + }); } diff --git a/packages/compiler/src/render3/view/i18n/context.ts b/packages/compiler/src/render3/view/i18n/context.ts index 2151b19938..7ca7b64d9f 100644 --- a/packages/compiler/src/render3/view/i18n/context.ts +++ b/packages/compiler/src/render3/view/i18n/context.ts @@ -46,7 +46,7 @@ export class I18nContext { public placeholders = new Map(); public isEmitted: boolean = false; - private _registry !: any; + private _registry!: any; private _unresolvedCtxCount: number = 0; constructor( @@ -66,9 +66,15 @@ export class I18nContext { updatePlaceholderMap(this.placeholders, ph, content); } - get icus() { return this._registry.icus; } - get isRoot() { return this.level === 0; } - get isResolved() { return this._unresolvedCtxCount === 0; } + get icus() { + return this._registry.icus; + } + get isRoot() { + return this.level === 0; + } + get isResolved() { + return this._unresolvedCtxCount === 0; + } getSerializedPlaceholders() { const result = new Map(); @@ -78,7 +84,9 @@ export class I18nContext { } // public API to accumulate i18n-related content - appendBinding(binding: AST) { this.bindings.add(binding); } + appendBinding(binding: AST) { + this.bindings.add(binding); + } appendIcu(name: string, ref: o.Expression) { updatePlaceholderMap(this._registry.icus, name, ref); } @@ -181,7 +189,7 @@ function wrapTag(symbol: string, {index, ctx, isVoid}: any, closed?: boolean): s wrap(symbol, index, ctx, closed); } -function findTemplateFn(ctx: number, templateIndex: number | null) { +function findTemplateFn(ctx: number, templateIndex: number|null) { return (token: any) => typeof token === 'object' && token.type === TagType.TEMPLATE && token.index === templateIndex && token.ctx === ctx; } diff --git a/packages/compiler/src/render3/view/i18n/get_msg_utils.ts b/packages/compiler/src/render3/view/i18n/get_msg_utils.ts index e601904793..d03fa7660a 100644 --- a/packages/compiler/src/render3/view/i18n/get_msg_utils.ts +++ b/packages/compiler/src/render3/view/i18n/get_msg_utils.ts @@ -47,23 +47,32 @@ export function createGoogleGetMsgStatements( * placeholders in `{$placeholder}` (for plain messages) or `{PLACEHOLDER}` (inside ICUs) format. */ class GetMsgSerializerVisitor implements i18n.Visitor { - private formatPh(value: string): string { return `{$${formatI18nPlaceholderName(value)}}`; } + private formatPh(value: string): string { + return `{$${formatI18nPlaceholderName(value)}}`; + } - visitText(text: i18n.Text): any { return text.value; } + visitText(text: i18n.Text): any { + return text.value; + } visitContainer(container: i18n.Container): any { return container.children.map(child => child.visit(this)).join(''); } - visitIcu(icu: i18n.Icu): any { return serializeIcuNode(icu); } + visitIcu(icu: i18n.Icu): any { + return serializeIcuNode(icu); + } visitTagPlaceholder(ph: i18n.TagPlaceholder): any { return ph.isVoid ? this.formatPh(ph.startName) : - `${this.formatPh(ph.startName)}${ph.children.map(child => child.visit(this)).join('')}${this.formatPh(ph.closeName)}`; + `${this.formatPh(ph.startName)}${ph.children.map(child => child.visit(this)).join('')}${ + this.formatPh(ph.closeName)}`; } - visitPlaceholder(ph: i18n.Placeholder): any { return this.formatPh(ph.name); } + visitPlaceholder(ph: i18n.Placeholder): any { + return this.formatPh(ph.name); + } visitIcuPlaceholder(ph: i18n.IcuPlaceholder, context?: any): any { return this.formatPh(ph.name); diff --git a/packages/compiler/src/render3/view/i18n/icu_serializer.ts b/packages/compiler/src/render3/view/i18n/icu_serializer.ts index 8e5aa0b7ff..c0f645bf3f 100644 --- a/packages/compiler/src/render3/view/i18n/icu_serializer.ts +++ b/packages/compiler/src/render3/view/i18n/icu_serializer.ts @@ -11,7 +11,9 @@ import * as i18n from '../../../i18n/i18n_ast'; import {formatI18nPlaceholderName} from './util'; class IcuSerializerVisitor implements i18n.Visitor { - visitText(text: i18n.Text): any { return text.value; } + visitText(text: i18n.Text): any { + return text.value; + } visitContainer(container: i18n.Container): any { return container.children.map(child => child.visit(this)).join(''); @@ -27,10 +29,13 @@ class IcuSerializerVisitor implements i18n.Visitor { visitTagPlaceholder(ph: i18n.TagPlaceholder): any { return ph.isVoid ? this.formatPh(ph.startName) : - `${this.formatPh(ph.startName)}${ph.children.map(child => child.visit(this)).join('')}${this.formatPh(ph.closeName)}`; + `${this.formatPh(ph.startName)}${ph.children.map(child => child.visit(this)).join('')}${ + this.formatPh(ph.closeName)}`; } - visitPlaceholder(ph: i18n.Placeholder): any { return this.formatPh(ph.name); } + visitPlaceholder(ph: i18n.Placeholder): any { + return this.formatPh(ph.name); + } visitIcuPlaceholder(ph: i18n.IcuPlaceholder, context?: any): any { return this.formatPh(ph.name); diff --git a/packages/compiler/src/render3/view/i18n/localize_utils.ts b/packages/compiler/src/render3/view/i18n/localize_utils.ts index 2e8dc4cdb6..e3c79c2898 100644 --- a/packages/compiler/src/render3/view/i18n/localize_utils.ts +++ b/packages/compiler/src/render3/view/i18n/localize_utils.ts @@ -28,7 +28,9 @@ class MessagePiece { } class LiteralPiece extends MessagePiece {} class PlaceholderPiece extends MessagePiece { - constructor(name: string) { super(formatI18nPlaceholderName(name, /* useCamelCase */ false)); } + constructor(name: string) { + super(formatI18nPlaceholderName(name, /* useCamelCase */ false)); + } } /** diff --git a/packages/compiler/src/render3/view/i18n/meta.ts b/packages/compiler/src/render3/view/i18n/meta.ts index 6269e69e27..d294b40258 100644 --- a/packages/compiler/src/render3/view/i18n/meta.ts +++ b/packages/compiler/src/render3/view/i18n/meta.ts @@ -8,12 +8,12 @@ import {computeDecimalDigest, computeDigest, decimalDigest} from '../../../i18n/digest'; import * as i18n from '../../../i18n/i18n_ast'; -import {VisitNodeFn, createI18nMessageFactory} from '../../../i18n/i18n_parser'; +import {createI18nMessageFactory, VisitNodeFn} from '../../../i18n/i18n_parser'; import * as html from '../../../ml_parser/ast'; import {DEFAULT_INTERPOLATION_CONFIG, InterpolationConfig} from '../../../ml_parser/interpolation_config'; import * as o from '../../../output/output_ast'; -import {I18N_ATTR, I18N_ATTR_PREFIX, hasI18nAttrs, icuFromI18nMessage} from './util'; +import {hasI18nAttrs, I18N_ATTR, I18N_ATTR_PREFIX, icuFromI18nMessage} from './util'; export type I18nMeta = { id?: string, @@ -134,10 +134,18 @@ export class I18nMetaVisitor implements html.Visitor { return expansion; } - visitText(text: html.Text): any { return text; } - visitAttribute(attribute: html.Attribute): any { return attribute; } - visitComment(comment: html.Comment): any { return comment; } - visitExpansionCase(expansionCase: html.ExpansionCase): any { return expansionCase; } + visitText(text: html.Text): any { + return text; + } + visitAttribute(attribute: html.Attribute): any { + return attribute; + } + visitComment(comment: html.Comment): any { + return comment; + } + visitExpansionCase(expansionCase: html.ExpansionCase): any { + return expansionCase; + } /** * Parse the general form `meta` passed into extract the explicit metadata needed to create a diff --git a/packages/compiler/src/render3/view/i18n/util.ts b/packages/compiler/src/render3/view/i18n/util.ts index baa0e2e9f7..c6b48ee840 100644 --- a/packages/compiler/src/render3/view/i18n/util.ts +++ b/packages/compiler/src/render3/view/i18n/util.ts @@ -49,7 +49,7 @@ export function icuFromI18nMessage(message: i18n.Message) { return message.nodes[0] as i18n.IcuPlaceholder; } -export function wrapI18nPlaceholder(content: string | number, contextId: number = 0): string { +export function wrapI18nPlaceholder(content: string|number, contextId: number = 0): string { const blockId = contextId > 0 ? `:${contextId}` : ''; return `${I18N_PLACEHOLDER_SYMBOL}${content}${blockId}${I18N_PLACEHOLDER_SYMBOL}`; } @@ -147,7 +147,7 @@ export function formatI18nPlaceholderName(name: string, useCamelCase: boolean = if (/^\d+$/.test(chunks[chunks.length - 1])) { postfix = chunks.pop(); } - let raw = chunks.shift() !.toLowerCase(); + let raw = chunks.shift()!.toLowerCase(); if (chunks.length) { raw += chunks.map(c => c.charAt(0).toUpperCase() + c.slice(1).toLowerCase()).join(''); } @@ -170,5 +170,5 @@ export function getTranslationConstPrefix(extra: string): string { */ export function declareI18nVariable(variable: o.ReadVarExpr): o.Statement { return new o.DeclareVarStmt( - variable.name !, undefined, o.INFERRED_TYPE, null, variable.sourceSpan); + variable.name!, undefined, o.INFERRED_TYPE, null, variable.sourceSpan); } diff --git a/packages/compiler/src/render3/view/style_parser.ts b/packages/compiler/src/render3/view/style_parser.ts index f0de8e362b..668df7d27e 100644 --- a/packages/compiler/src/render3/view/style_parser.ts +++ b/packages/compiler/src/render3/view/style_parser.ts @@ -110,7 +110,11 @@ export function stripUnnecessaryQuotes(value: string): string { } export function hyphenate(value: string): string { - return value.replace(/[a-z][A-Z]/g, v => { - return v.charAt(0) + '-' + v.charAt(1); - }).toLowerCase(); + return value + .replace( + /[a-z][A-Z]/g, + v => { + return v.charAt(0) + '-' + v.charAt(1); + }) + .toLowerCase(); } diff --git a/packages/compiler/src/render3/view/styling_builder.ts b/packages/compiler/src/render3/view/styling_builder.ts index 2ac2d6114e..6860d3e543 100644 --- a/packages/compiler/src/render3/view/styling_builder.ts +++ b/packages/compiler/src/render3/view/styling_builder.ts @@ -226,7 +226,10 @@ export class StylingBuilder { const entry: BoundStylingEntry = { name: property, sanitize: property ? isStyleSanitizable(property) : true, - unit: unit || bindingUnit, value, sourceSpan, hasOverrideFlag + unit: unit || bindingUnit, + value, + sourceSpan, + hasOverrideFlag }; if (isMapBased) { this._styleMapInput = entry; @@ -385,7 +388,7 @@ export class StylingBuilder { supportsInterpolation: true, sourceSpan: stylingInput.sourceSpan, allocateBindingSlots: totalBindingSlotsRequired, - params: (convertFn: (value: any) => o.Expression | o.Expression[]) => { + params: (convertFn: (value: any) => o.Expression|o.Expression[]) => { const convertResult = convertFn(mapValue); const params = Array.isArray(convertResult) ? convertResult : [convertResult]; return params; diff --git a/packages/compiler/src/render3/view/t2_api.ts b/packages/compiler/src/render3/view/t2_api.ts index ed651075b8..c0d2de5b2c 100644 --- a/packages/compiler/src/render3/view/t2_api.ts +++ b/packages/compiler/src/render3/view/t2_api.ts @@ -22,7 +22,9 @@ import {BoundAttribute, BoundEvent, Element, Node, Reference, Template, TextAttr /** * A logical target for analysis, which could contain a template or other types of bindings. */ -export interface Target { template?: Node[]; } +export interface Target { + template?: Node[]; +} /** * Metadata regarding a directive that's needed to match it against template elements. This is @@ -44,7 +46,7 @@ export interface DirectiveMeta { * * Goes from property names to field names. */ - inputs: {[property: string]: string | [string, string]}; + inputs: {[property: string]: string|[string, string]}; /** * Set of outputs which this directive claims. @@ -67,7 +69,9 @@ export interface DirectiveMeta { * * The returned `BoundTarget` has an API for extracting information about the processed target. */ -export interface TargetBinder { bind(target: Target): BoundTarget; } +export interface TargetBinder { + bind(target: Target): BoundTarget; +} /** * Result of performing the binding operation against a `Target`. diff --git a/packages/compiler/src/render3/view/t2_binder.ts b/packages/compiler/src/render3/view/t2_binder.ts index e56270c972..2f6eba2ce6 100644 --- a/packages/compiler/src/render3/view/t2_binder.ts +++ b/packages/compiler/src/render3/view/t2_binder.ts @@ -152,7 +152,7 @@ class Scope implements Visitor { lookup(name: string): Reference|Variable|null { if (this.namedEntities.has(name)) { // Found in the local scope. - return this.namedEntities.get(name) !; + return this.namedEntities.get(name)!; } else if (this.parentScope !== undefined) { // Not in the local scope, but there's a parent scope so check there. return this.parentScope.lookup(name); @@ -217,11 +217,17 @@ class DirectiveBinder implements Visitor { return {directives, bindings, references}; } - private ingest(template: Node[]): void { template.forEach(node => node.visit(this)); } + private ingest(template: Node[]): void { + template.forEach(node => node.visit(this)); + } - visitElement(element: Element): void { this.visitElementOrTemplate(element.name, element); } + visitElement(element: Element): void { + this.visitElementOrTemplate(element.name, element); + } - visitTemplate(template: Template): void { this.visitElementOrTemplate('ng-template', template); } + visitTemplate(template: Template): void { + this.visitElementOrTemplate('ng-template', template); + } visitElementOrTemplate(elementName: string, node: Element|Template): void { // First, determine the HTML shape of the node for the purpose of directive matching. @@ -269,7 +275,7 @@ class DirectiveBinder implements Visitor { }); // Associate attributes/bindings on the node with directives or with the node itself. - type BoundNode = BoundAttribute | BoundEvent | TextAttribute; + type BoundNode = BoundAttribute|BoundEvent|TextAttribute; const setAttributeBinding = (attribute: BoundNode, ioType: keyof Pick) => { const dir = directives.find(dir => dir[ioType].hasOwnProperty(attribute.name)); @@ -432,11 +438,17 @@ class TemplateBinder extends RecursiveAstVisitor implements Visitor { // The remaining visitors are concerned with processing AST expressions within template bindings - visitBoundAttribute(attribute: BoundAttribute) { attribute.value.visit(this); } + visitBoundAttribute(attribute: BoundAttribute) { + attribute.value.visit(this); + } - visitBoundEvent(event: BoundEvent) { event.handler.visit(this); } + visitBoundEvent(event: BoundEvent) { + event.handler.visit(this); + } - visitBoundText(text: BoundText) { text.value.visit(this); } + visitBoundText(text: BoundText) { + text.value.visit(this); + } visitPipe(ast: BindingPipe, context: any): any { this.usedPipes.add(ast.name); return super.visitPipe(ast, context); @@ -526,7 +538,9 @@ export class R3BoundTarget implements BoundTar return this.symbols.get(symbol) || null; } - getNestingLevel(template: Template): number { return this.nestingLevel.get(template) || 0; } + getNestingLevel(template: Template): number { + return this.nestingLevel.get(template) || 0; + } getUsedDirectives(): DirectiveT[] { const set = new Set(); @@ -534,5 +548,7 @@ export class R3BoundTarget implements BoundTar return Array.from(set.values()); } - getUsedPipes(): string[] { return Array.from(this.usedPipes); } + getUsedPipes(): string[] { + return Array.from(this.usedPipes); + } } diff --git a/packages/compiler/src/render3/view/template.ts b/packages/compiler/src/render3/view/template.ts index afbee7b6d4..f92a3904ce 100644 --- a/packages/compiler/src/render3/view/template.ts +++ b/packages/compiler/src/render3/view/template.ts @@ -7,7 +7,7 @@ */ import {flatten, sanitizeIdentifier} from '../../compile_metadata'; -import {BindingForm, BuiltinFunctionCall, LocalResolver, convertActionBinding, convertPropertyBinding, convertUpdateArguments} from '../../compiler_util/expression_converter'; +import {BindingForm, BuiltinFunctionCall, convertActionBinding, convertPropertyBinding, convertUpdateArguments, LocalResolver} from '../../compiler_util/expression_converter'; import {ConstantPool} from '../../constant_pool'; import * as core from '../../core'; import {AST, AstMemoryEfficientTransformer, BindingPipe, BindingType, FunctionCall, ImplicitReceiver, Interpolation, LiteralArray, LiteralMap, LiteralPrimitive, ParsedEventType, PropertyRead} from '../../expression_parser/ast'; @@ -36,9 +36,9 @@ import {I18nContext} from './i18n/context'; import {createGoogleGetMsgStatements} from './i18n/get_msg_utils'; import {createLocalizeStatements} from './i18n/localize_utils'; import {I18nMetaVisitor} from './i18n/meta'; -import {I18N_ICU_MAPPING_PREFIX, TRANSLATION_PREFIX, assembleBoundTextPlaceholders, assembleI18nBoundString, declareI18nVariable, getTranslationConstPrefix, i18nFormatPlaceholderNames, icuFromI18nMessage, isI18nRootNode, isSingleI18nIcu, placeholdersToParams, wrapI18nPlaceholder} from './i18n/util'; +import {assembleBoundTextPlaceholders, assembleI18nBoundString, declareI18nVariable, getTranslationConstPrefix, I18N_ICU_MAPPING_PREFIX, i18nFormatPlaceholderNames, icuFromI18nMessage, isI18nRootNode, isSingleI18nIcu, placeholdersToParams, TRANSLATION_PREFIX, wrapI18nPlaceholder} from './i18n/util'; import {StylingBuilder, StylingInstruction} from './styling_builder'; -import {CONTEXT_NAME, IMPLICIT_REFERENCE, NON_BINDABLE_ATTR, REFERENCE_PREFIX, RENDER_FLAGS, asLiteral, chainedInstruction, getAttrsForDirectiveMatching, getInterpolationArgsLength, invalid, trimTrailingNulls, unsupported} from './util'; +import {asLiteral, chainedInstruction, CONTEXT_NAME, getAttrsForDirectiveMatching, getInterpolationArgsLength, IMPLICIT_REFERENCE, invalid, NON_BINDABLE_ATTR, REFERENCE_PREFIX, RENDER_FLAGS, trimTrailingNulls, unsupported} from './util'; @@ -61,8 +61,8 @@ export function renderFlagCheckIfStmt( } export function prepareEventListenerParameters( - eventAst: t.BoundEvent, handlerName: string | null = null, - scope: BindingScope | null = null): o.Expression[] { + eventAst: t.BoundEvent, handlerName: string|null = null, + scope: BindingScope|null = null): o.Expression[] { const {type, name, target, phase, handler} = eventAst; if (target && !GLOBAL_TARGET_RESOLVERS.has(target)) { throw new Error(`Unexpected global target '${target}' defined for '${name}' event. @@ -85,7 +85,7 @@ export function prepareEventListenerParameters( statements.push(...bindingExpr.render3Stmts); const eventName: string = - type === ParsedEventType.Animation ? prepareSyntheticListenerName(name, phase !) : name; + type === ParsedEventType.Animation ? prepareSyntheticListenerName(name, phase!) : name; const fnName = handlerName && sanitizeIdentifier(handlerName); const fnArgs: o.FnParam[] = []; @@ -98,7 +98,7 @@ export function prepareEventListenerParameters( if (target) { params.push( o.literal(false), // `useCapture` flag, defaults to `false` - o.importExpr(GLOBAL_TARGET_RESOLVERS.get(target) !)); + o.importExpr(GLOBAL_TARGET_RESOLVERS.get(target)!)); } return params; } @@ -207,12 +207,12 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver // - this template has parent i18n context // - or the template has i18n meta associated with it, // but it's not initiated by the Element (e.g. ) - const initI18nContext = - this.i18nContext || (isI18nRootNode(i18n) && !isSingleI18nIcu(i18n) && - !(isSingleElementTemplate(nodes) && nodes[0].i18n === i18n)); + const initI18nContext = this.i18nContext || + (isI18nRootNode(i18n) && !isSingleI18nIcu(i18n) && + !(isSingleElementTemplate(nodes) && nodes[0].i18n === i18n)); const selfClosingI18nInstruction = hasTextChildrenOnly(nodes); if (initI18nContext) { - this.i18nStart(null, i18n !, selfClosingI18nInstruction); + this.i18nStart(null, i18n!, selfClosingI18nInstruction); } // This is the initial pass through the nodes of this template. In this pass, we @@ -295,10 +295,14 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver } // LocalResolver - getLocal(name: string): o.Expression|null { return this._bindingScope.get(name); } + getLocal(name: string): o.Expression|null { + return this._bindingScope.get(name); + } // LocalResolver - notifyImplicitReceiverUse(): void { this._bindingScope.notifyImplicitReceiverUse(); } + notifyImplicitReceiverUse(): void { + this._bindingScope.notifyImplicitReceiverUse(); + } private i18nTranslate( message: i18n.Message, params: {[name: string]: o.Expression} = {}, ref?: o.ReadVarExpr, @@ -335,12 +339,11 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver private i18nAppendBindings(expressions: AST[]) { if (expressions.length > 0) { - expressions.forEach(expression => this.i18n !.appendBinding(expression)); + expressions.forEach(expression => this.i18n!.appendBinding(expression)); } } - private i18nBindProps(props: {[key: string]: t.Text | t.BoundText}): - {[key: string]: o.Expression} { + private i18nBindProps(props: {[key: string]: t.Text|t.BoundText}): {[key: string]: o.Expression} { const bound: {[key: string]: o.Expression} = {}; Object.keys(props).forEach(key => { const prop = props[key]; @@ -351,7 +354,7 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver this.allocateBindingSlots(value); if (value instanceof Interpolation) { const {strings, expressions} = value; - const {id, bindings} = this.i18n !; + const {id, bindings} = this.i18n!; const label = assembleI18nBoundString(strings, bindings.size, id); this.i18nAppendBindings(expressions); bound[key] = o.literal(label); @@ -424,7 +427,7 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver void { const index = this.allocateDataSlot(); if (this.i18nContext) { - this.i18n = this.i18nContext.forkChildContext(index, this.templateIndex !, meta); + this.i18n = this.i18nContext.forkChildContext(index, this.templateIndex!, meta); } else { const ref = o.variable(this.constantPool.uniqueName(TRANSLATION_PREFIX)); this.i18n = new I18nContext(index, ref, 0, this.templateIndex, meta); @@ -479,7 +482,7 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver const i18nAttrArgs: o.Expression[] = []; const bindings: ChainableBindingInstruction[] = []; attrs.forEach(attr => { - const message = attr.i18n !as i18n.Message; + const message = attr.i18n! as i18n.Message; if (attr instanceof t.TextAttribute) { i18nAttrArgs.push(o.literal(attr.name), this.i18nTranslate(message)); } else { @@ -559,7 +562,7 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver this.creationInstruction(ngContent.sourceSpan, R3.projection, parameters); if (this.i18n) { - this.i18n.appendProjection(ngContent.i18n !, slot); + this.i18n.appendProjection(ngContent.i18n!, slot); } } @@ -571,7 +574,7 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver const isI18nRootElement: boolean = isI18nRootNode(element.i18n) && !isSingleI18nIcu(element.i18n); - const i18nAttrs: (t.TextAttribute | t.BoundAttribute)[] = []; + const i18nAttrs: (t.TextAttribute|t.BoundAttribute)[] = []; const outputAttrs: t.TextAttribute[] = []; const [namespaceKey, elementName] = splitNsName(element.name); @@ -633,7 +636,7 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver } if (this.i18n) { - this.i18n.appendElement(element.i18n !, elementIndex); + this.i18n.appendElement(element.i18n!, elementIndex); } // Note that we do not append text node instructions and ICUs inside i18n section, @@ -676,7 +679,7 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver // Note: it's important to keep i18n/i18nStart instructions after i18nAttributes and // listeners, to make sure i18nAttributes instruction targets current element at runtime. if (isI18nRootElement) { - this.i18nStart(element.sourceSpan, element.i18n !, createSelfClosingI18nInstruction); + this.i18nStart(element.sourceSpan, element.i18n!, createSelfClosingI18nInstruction); } } @@ -757,7 +760,8 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver propertyBindings.push({ name: attrName, sourceSpan: input.sourceSpan, - value: () => this.convertPropertyBinding(value), params + value: () => this.convertPropertyBinding(value), + params }); } } else if (inputType === BindingType.Attribute) { @@ -773,7 +777,8 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver attributeBindings.push({ name: attrName, sourceSpan: input.sourceSpan, - value: () => this.convertPropertyBinding(boundValue), params + value: () => this.convertPropertyBinding(boundValue), + params }); } } else { @@ -801,7 +806,7 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver t.visitAll(this, element.children); if (!isI18nRootElement && this.i18n) { - this.i18n.appendElement(element.i18n !, elementIndex, true); + this.i18n.appendElement(element.i18n!, elementIndex, true); } if (!createSelfClosingInstruction) { @@ -823,7 +828,7 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver const templateIndex = this.allocateDataSlot(); if (this.i18n) { - this.i18n.appendTemplate(template.i18n !, templateIndex); + this.i18n.appendTemplate(template.i18n!, templateIndex); } const tagName = sanitizeIdentifier(template.tagName || ''); @@ -892,7 +897,7 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver // Only add normal input/output binding instructions on explicit elements. if (template.tagName === NG_TEMPLATE_TAG_NAME) { const inputs: t.BoundAttribute[] = []; - const i18nAttrs: (t.TextAttribute | t.BoundAttribute)[] = + const i18nAttrs: (t.TextAttribute|t.BoundAttribute)[] = template.attributes.filter(attr => !!attr.i18n); template.inputs.forEach( @@ -935,7 +940,7 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver const value = text.value.visit(this._valueConverter); this.allocateBindingSlots(value); if (value instanceof Interpolation) { - this.i18n.appendBoundText(text.i18n !); + this.i18n.appendBoundText(text.i18n!); this.i18nAppendBindings(value.expressions); } return; @@ -975,15 +980,15 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver // to generate i18n context and the necessary instructions if (!this.i18n) { initWasInvoked = true; - this.i18nStart(null, icu.i18n !, true); + this.i18nStart(null, icu.i18n!, true); } - const i18n = this.i18n !; + const i18n = this.i18n!; const vars = this.i18nBindProps(icu.vars); const placeholders = this.i18nBindProps(icu.placeholders); // output ICU directly and keep ICU reference in context - const message = icu.i18n !as i18n.Message; + const message = icu.i18n! as i18n.Message; // we always need post-processing function for ICUs, to make sure that: // - all placeholders in a form of {PLACEHOLDER} are replaced with actual values (note: @@ -1016,13 +1021,21 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver return null; } - private allocateDataSlot() { return this._dataIndex++; } + private allocateDataSlot() { + return this._dataIndex++; + } - getConstCount() { return this._dataIndex; } + getConstCount() { + return this._dataIndex; + } - getVarCount() { return this._pureFunctionSlots; } + getVarCount() { + return this._pureFunctionSlots; + } - getConsts() { return this._constants; } + getConsts() { + return this._constants; + } getNgContentSelectors(): o.Expression|null { return this._ngContentReservedSlots.length ? @@ -1030,7 +1043,9 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver null; } - private bindingContext() { return `${this._bindingContext++}`; } + private bindingContext() { + return `${this._bindingContext++}`; + } private templatePropertyBindings( templateIndex: number, attrs: (t.BoundAttribute|t.TextAttribute)[]) { @@ -1092,11 +1107,10 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver calls.push({ sourceSpan: call.sourceSpan, value: () => { - return call - .params( - value => (call.supportsInterpolation && value instanceof Interpolation) ? - this.getUpdateInstructionArguments(value) : - this.convertPropertyBinding(value)) as o.Expression[]; + return call.params( + value => (call.supportsInterpolation && value instanceof Interpolation) ? + this.getUpdateInstructionArguments(value) : + this.convertPropertyBinding(value)) as o.Expression[]; } }); }); @@ -1114,7 +1128,7 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver } private creationInstructionChain(reference: o.ExternalReference, calls: { - sourceSpan: ParseSourceSpan | null, + sourceSpan: ParseSourceSpan|null, params: () => o.Expression[] }[]) { const span = calls.length ? calls[0].sourceSpan : null; @@ -1228,8 +1242,9 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver private matchDirectives(elementName: string, elOrTpl: t.Element|t.Template) { if (this.directiveMatcher) { const selector = createCssSelector(elementName, getAttrsForDirectiveMatching(elOrTpl)); - this.directiveMatcher.match( - selector, (cssSelector, staticType) => { this.directives.add(staticType); }); + this.directiveMatcher.match(selector, (cssSelector, staticType) => { + this.directives.add(staticType); + }); } } @@ -1277,7 +1292,7 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver attrExprs.push(...getNgProjectAsLiteral(ngProjectAsAttr)); } - function addAttrExpr(key: string | number, value?: o.Expression): void { + function addAttrExpr(key: string|number, value?: o.Expression): void { if (typeof key === 'string') { if (!alreadySeen.has(key)) { attrExprs.push(...getAttributeNameLiterals(key)); @@ -1390,7 +1405,7 @@ export class TemplateDefinitionBuilder implements t.Visitor, LocalResolver const eventName: string = outputAst.name; const bindingFnName = outputAst.type === ParsedEventType.Animation ? // synthetic @listener.foo values are treated the exact same as are standard listeners - prepareSyntheticListenerFunctionName(eventName, outputAst.phase !) : + prepareSyntheticListenerFunctionName(eventName, outputAst.phase!) : sanitizeIdentifier(eventName); const handlerName = `${this.templateName}_${tagName}_${bindingFnName}_${index}_listener`; const scope = this._bindingScope.nestedScope(this._bindingScope.bindingLevel); @@ -1492,7 +1507,7 @@ function pureFunctionCallInfo(args: o.Expression[]) { } function instruction( - span: ParseSourceSpan | null, reference: o.ExternalReference, + span: ParseSourceSpan|null, reference: o.ExternalReference, params: o.Expression[]): o.Expression { return o.importExpr(reference, null, span).callFn(params, span); } @@ -1504,7 +1519,7 @@ function generateNextContextExpr(relativeLevelDiff: number): o.Expression { } function getLiteralFactory( - constantPool: ConstantPool, literal: o.LiteralArrayExpr | o.LiteralMapExpr, + constantPool: ConstantPool, literal: o.LiteralArrayExpr|o.LiteralMapExpr, allocateSlots: (numSlots: number) => number): o.Expression { const {literalFactory, literalFactoryArguments} = constantPool.getLiteralFactory(literal); // Allocate 1 slot for the result plus 1 per argument @@ -1570,9 +1585,8 @@ const SHARED_CONTEXT_KEY = '$$shared_ctx$$'; * declaration should always come before the local ref declaration. */ type BindingData = { - retrievalLevel: number; lhs: o.Expression; declareLocalCallback?: DeclareLocalVarCallback; - declare: boolean; - priority: number; + retrievalLevel: number; lhs: o.Expression; + declareLocalCallback?: DeclareLocalVarCallback; declare: boolean; priority: number; localRef: boolean; }; @@ -1580,7 +1594,11 @@ type BindingData = { * The sorting priority of a local variable declaration. Higher numbers * mean the declaration will appear first in the generated code. */ -const enum DeclarationPriority { DEFAULT = 0, CONTEXT = 1, SHARED_CONTEXT = 2 } +const enum DeclarationPriority { + DEFAULT = 0, + CONTEXT = 1, + SHARED_CONTEXT = 2 +} export class BindingScope implements LocalResolver { /** Keeps a map from local variables to their BindingData. */ @@ -1669,7 +1687,9 @@ export class BindingScope implements LocalResolver { } // Implemented as part of LocalResolver. - getLocal(name: string): (o.Expression|null) { return this.get(name); } + getLocal(name: string): (o.Expression|null) { + return this.get(name); + } // Implemented as part of LocalResolver. notifyImplicitReceiverUse(): void { @@ -1677,7 +1697,7 @@ export class BindingScope implements LocalResolver { // Since the implicit receiver is accessed in an embedded view, we need to // ensure that we declare a shared context variable for the current template // in the update variables. - this.map.get(SHARED_CONTEXT_KEY + 0) !.declare = true; + this.map.get(SHARED_CONTEXT_KEY + 0)!.declare = true; } } @@ -1698,7 +1718,7 @@ export class BindingScope implements LocalResolver { this.generateSharedContextVar(retrievalLevel); } // Shared context variables are always generated as "ReadVarExpr". - return this.map.get(bindingKey) !.lhs as o.ReadVarExpr; + return this.map.get(bindingKey)!.lhs as o.ReadVarExpr; } getSharedContextName(retrievalLevel: number): o.ReadVarExpr|null { @@ -1735,7 +1755,7 @@ export class BindingScope implements LocalResolver { } getComponentProperty(name: string): o.Expression { - const componentValue = this.map.get(SHARED_CONTEXT_KEY + 0) !; + const componentValue = this.map.get(SHARED_CONTEXT_KEY + 0)!; componentValue.declare = true; this.maybeRestoreView(0, false); return componentValue.lhs.prop(name); @@ -1748,11 +1768,11 @@ export class BindingScope implements LocalResolver { // 2 - we are looking up a local ref, which requires restoring the view where the local // ref is stored if (this.isListenerScope() && (retrievalLevel < this.bindingLevel || localRefLookup)) { - if (!this.parent !.restoreViewVariable) { + if (!this.parent!.restoreViewVariable) { // parent saves variable to generate a shared `const $s$ = getCurrentView();` instruction - this.parent !.restoreViewVariable = o.variable(this.parent !.freshReferenceName()); + this.parent!.restoreViewVariable = o.variable(this.parent!.freshReferenceName()); } - this.restoreViewVariable = this.parent !.restoreViewVariable; + this.restoreViewVariable = this.parent!.restoreViewVariable; } } @@ -1771,19 +1791,22 @@ export class BindingScope implements LocalResolver { []; } - isListenerScope() { return this.parent && this.parent.bindingLevel === this.bindingLevel; } + isListenerScope() { + return this.parent && this.parent.bindingLevel === this.bindingLevel; + } variableDeclarations(): o.Statement[] { let currentContextLevel = 0; return Array.from(this.map.values()) - .filter(value => value.declare) - .sort((a, b) => b.retrievalLevel - a.retrievalLevel || b.priority - a.priority) - .reduce((stmts: o.Statement[], value: BindingData) => { - const levelDiff = this.bindingLevel - value.retrievalLevel; - const currStmts = value.declareLocalCallback !(this, levelDiff - currentContextLevel); - currentContextLevel = levelDiff; - return stmts.concat(currStmts); - }, []) as o.Statement[]; + .filter(value => value.declare) + .sort((a, b) => b.retrievalLevel - a.retrievalLevel || b.priority - a.priority) + .reduce((stmts: o.Statement[], value: BindingData) => { + const levelDiff = this.bindingLevel - value.retrievalLevel; + const currStmts = + value.declareLocalCallback!(this, levelDiff - currentContextLevel); + currentContextLevel = levelDiff; + return stmts.concat(currStmts); + }, []) as o.Statement[]; } @@ -1985,10 +2008,10 @@ export interface ParseTemplateOptions { export function parseTemplate( template: string, templateUrl: string, options: ParseTemplateOptions = {}): { errors?: ParseError[], - nodes: t.Node[], - styleUrls: string[], - styles: string[], - ngContentSelectors: string[] + nodes: t.Node[], + styleUrls: string[], + styles: string[], + ngContentSelectors: string[] } { const {interpolationConfig, preserveWhitespaces, enableI18nLegacyMessageIdFormat} = options; const bindingParser = makeBindingParser(interpolationConfig); @@ -2126,9 +2149,10 @@ export function getTranslationDeclStmts( const statements: o.Statement[] = [ declareI18nVariable(variable), o.ifStmt( - createClosureModeGuard(), createGoogleGetMsgStatements( - variable, message, closureVar, - i18nFormatPlaceholderNames(params, /* useCamelCase */ true)), + createClosureModeGuard(), + createGoogleGetMsgStatements( + variable, message, closureVar, + i18nFormatPlaceholderNames(params, /* useCamelCase */ true)), createLocalizeStatements( variable, message, i18nFormatPlaceholderNames(params, /* useCamelCase */ false))), ]; diff --git a/packages/compiler/src/render3/view/util.ts b/packages/compiler/src/render3/view/util.ts index 84b9d323bc..c762194774 100644 --- a/packages/compiler/src/render3/view/util.ts +++ b/packages/compiler/src/render3/view/util.ts @@ -69,7 +69,7 @@ export function unsupported(this: void|Function, feature: string): never { throw new Error(`Feature ${feature} is not supported yet`); } -export function invalid(this: t.Visitor, arg: o.Expression | o.Statement | t.Node): never { +export function invalid(this: t.Visitor, arg: o.Expression|o.Statement|t.Node): never { throw new Error( `Invalid state: Visitor ${this.constructor.name} doesn't handle ${arg.constructor.name}`); } @@ -82,7 +82,7 @@ export function asLiteral(value: any): o.Expression { } export function conditionallyCreateMapObjectLiteral( - keys: {[key: string]: string | string[]}, keepDeclared?: boolean): o.Expression|null { + keys: {[key: string]: string|string[]}, keepDeclared?: boolean): o.Expression|null { if (Object.getOwnPropertyNames(keys).length > 0) { return mapToExpression(keys, keepDeclared); } @@ -90,7 +90,7 @@ export function conditionallyCreateMapObjectLiteral( } function mapToExpression( - map: {[key: string]: string | string[]}, keepDeclared?: boolean): o.Expression { + map: {[key: string]: string|string[]}, keepDeclared?: boolean): o.Expression { return o.literalMap(Object.getOwnPropertyNames(map).map(key => { // canonical syntax: `dirProp: publicProp` // if there is no `:`, use dirProp = elProp @@ -153,7 +153,9 @@ export class DefinitionMap { } } - toLiteralMap(): o.LiteralMapExpr { return o.literalMap(this.values); } + toLiteralMap(): o.LiteralMapExpr { + return o.literalMap(this.values); + } } /** @@ -165,8 +167,8 @@ export class DefinitionMap { * object maps a property name to its (static) value. For any bindings, this map simply maps the * property name to an empty string. */ -export function getAttrsForDirectiveMatching(elOrTpl: t.Element | t.Template): - {[name: string]: string} { +export function getAttrsForDirectiveMatching(elOrTpl: t.Element| + t.Template): {[name: string]: string} { const attributesMap: {[name: string]: string} = {}; @@ -179,8 +181,12 @@ export function getAttrsForDirectiveMatching(elOrTpl: t.Element | t.Template): } }); - elOrTpl.inputs.forEach(i => { attributesMap[i.name] = ''; }); - elOrTpl.outputs.forEach(o => { attributesMap[o.name] = ''; }); + elOrTpl.inputs.forEach(i => { + attributesMap[i.name] = ''; + }); + elOrTpl.outputs.forEach(o => { + attributesMap[o.name] = ''; + }); } return attributesMap; @@ -188,7 +194,7 @@ export function getAttrsForDirectiveMatching(elOrTpl: t.Element | t.Template): /** Returns a call expression to a chained instruction, e.g. `property(params[0])(params[1])`. */ export function chainedInstruction( - reference: o.ExternalReference, calls: o.Expression[][], span?: ParseSourceSpan | null) { + reference: o.ExternalReference, calls: o.Expression[][], span?: ParseSourceSpan|null) { let expression = o.importExpr(reference, null, span) as o.Expression; if (calls.length > 0) { diff --git a/packages/compiler/src/resource_loader.ts b/packages/compiler/src/resource_loader.ts index a0237cd17b..1f59cc981e 100644 --- a/packages/compiler/src/resource_loader.ts +++ b/packages/compiler/src/resource_loader.ts @@ -11,5 +11,7 @@ * to load templates. */ export class ResourceLoader { - get(url: string): Promise|string { return ''; } + get(url: string): Promise|string { + return ''; + } } diff --git a/packages/compiler/src/schema/dom_element_schema_registry.ts b/packages/compiler/src/schema/dom_element_schema_registry.ts index e74e90ae4f..8705c21957 100644 --- a/packages/compiler/src/schema/dom_element_schema_registry.ts +++ b/packages/compiler/src/schema/dom_element_schema_registry.ts @@ -253,7 +253,9 @@ export class DomElementSchemaRegistry extends ElementSchemaRegistry { typeNames.split(',').forEach(tag => this._schema[tag.toLowerCase()] = type); const superType = superName && this._schema[superName.toLowerCase()]; if (superType) { - Object.keys(superType).forEach((prop: string) => { type[prop] = superType[prop]; }); + Object.keys(superType).forEach((prop: string) => { + type[prop] = superType[prop]; + }); } properties.forEach((property: string) => { if (property.length > 0) { @@ -350,9 +352,13 @@ export class DomElementSchemaRegistry extends ElementSchemaRegistry { return ctx ? ctx : SecurityContext.NONE; } - getMappedPropName(propName: string): string { return _ATTR_TO_PROP[propName] || propName; } + getMappedPropName(propName: string): string { + return _ATTR_TO_PROP[propName] || propName; + } - getDefaultComponentElementName(): string { return 'ng-component'; } + getDefaultComponentElementName(): string { + return 'ng-component'; + } validateProperty(name: string): {error: boolean, msg?: string} { if (name.toLowerCase().startsWith('on')) { @@ -376,7 +382,9 @@ export class DomElementSchemaRegistry extends ElementSchemaRegistry { } } - allKnownElementNames(): string[] { return Object.keys(this._schema); } + allKnownElementNames(): string[] { + return Object.keys(this._schema); + } normalizeAnimationStyleProperty(propName: string): string { return dashCaseToCamelCase(propName); @@ -386,7 +394,7 @@ export class DomElementSchemaRegistry extends ElementSchemaRegistry { {error: string, value: string} { let unit: string = ''; const strVal = val.toString().trim(); - let errorMsg: string = null !; + let errorMsg: string = null!; if (_isPixelDimensionStyle(camelCaseProp) && val !== 0 && val !== '0') { if (typeof val === 'number') { diff --git a/packages/compiler/src/schema/dom_security_schema.ts b/packages/compiler/src/schema/dom_security_schema.ts index 73bff2233e..7768f29f19 100644 --- a/packages/compiler/src/schema/dom_security_schema.ts +++ b/packages/compiler/src/schema/dom_security_schema.ts @@ -20,7 +20,7 @@ import {SecurityContext} from '../core'; // ================================================================================================= /** Map from tagName|propertyName SecurityContext. Properties applying to all tags use '*'. */ -let _SECURITY_SCHEMA !: {[k: string]: SecurityContext}; +let _SECURITY_SCHEMA!: {[k: string]: SecurityContext}; export function SECURITY_SCHEMA(): {[k: string]: SecurityContext} { if (!_SECURITY_SCHEMA) { diff --git a/packages/compiler/src/selector.ts b/packages/compiler/src/selector.ts index b5e131de5d..a2d4aaaff6 100644 --- a/packages/compiler/src/selector.ts +++ b/packages/compiler/src/selector.ts @@ -118,9 +118,13 @@ export class CssSelector { this.notSelectors.length === 0; } - hasElementSelector(): boolean { return !!this.element; } + hasElementSelector(): boolean { + return !!this.element; + } - setElement(element: string|null = null) { this.element = element; } + setElement(element: string|null = null) { + this.element = element; + } /** Gets a template string for an element that matches the selector. */ getMatchingElementTemplate(): string { @@ -150,7 +154,9 @@ export class CssSelector { this.attrs.push(name, value && value.toLowerCase() || ''); } - addClassName(name: string) { this.classNames.push(name.toLowerCase()); } + addClassName(name: string) { + this.classNames.push(name.toLowerCase()); + } toString(): string { let res: string = this.element || ''; @@ -189,7 +195,7 @@ export class SelectorMatcher { private _listContexts: SelectorListContext[] = []; addSelectables(cssSelectors: CssSelector[], callbackCtxt?: T) { - let listContext: SelectorListContext = null !; + let listContext: SelectorListContext = null!; if (cssSelectors.length > 1) { listContext = new SelectorListContext(cssSelectors); this._listContexts.push(listContext); @@ -284,10 +290,10 @@ export class SelectorMatcher { * @param cssSelector A css selector * @param matchedCallback This callback will be called with the object handed into `addSelectable` * @return boolean true if a match was found - */ + */ match(cssSelector: CssSelector, matchedCallback: ((c: CssSelector, a: T) => void)|null): boolean { let result = false; - const element = cssSelector.element !; + const element = cssSelector.element!; const classNames = cssSelector.classNames; const attrs = cssSelector.attrs; @@ -315,7 +321,7 @@ export class SelectorMatcher { const name = attrs[i]; const value = attrs[i + 1]; - const terminalValuesMap = this._attrValueMap.get(name) !; + const terminalValuesMap = this._attrValueMap.get(name)!; if (value) { result = this._matchTerminal(terminalValuesMap, '', cssSelector, matchedCallback) || result; @@ -323,7 +329,7 @@ export class SelectorMatcher { result = this._matchTerminal(terminalValuesMap, value, cssSelector, matchedCallback) || result; - const partialValuesMap = this._attrValuePartialMap.get(name) !; + const partialValuesMap = this._attrValuePartialMap.get(name)!; if (value) { result = this._matchPartial(partialValuesMap, '', cssSelector, matchedCallback) || result; } @@ -343,7 +349,7 @@ export class SelectorMatcher { } let selectables: SelectorContext[] = map.get(name) || []; - const starSelectables: SelectorContext[] = map.get('*') !; + const starSelectables: SelectorContext[] = map.get('*')!; if (starSelectables) { selectables = selectables.concat(starSelectables); } diff --git a/packages/compiler/src/shadow_css.ts b/packages/compiler/src/shadow_css.ts index c65e6883bf..0ce9bd570c 100644 --- a/packages/compiler/src/shadow_css.ts +++ b/packages/compiler/src/shadow_css.ts @@ -138,13 +138,13 @@ export class ShadowCss { constructor() {} /* - * Shim some cssText with the given selector. Returns cssText that can - * be included in the document via WebComponents.ShadowCSS.addCssToDocument(css). - * - * When strictStyling is true: - * - selector is the attribute added to all elements inside the host, - * - hostSelector is the attribute added to the host itself. - */ + * Shim some cssText with the given selector. Returns cssText that can + * be included in the document via WebComponents.ShadowCSS.addCssToDocument(css). + * + * When strictStyling is true: + * - selector is the attribute added to all elements inside the host, + * - hostSelector is the attribute added to the host itself. + */ shimCssText(cssText: string, selector: string, hostSelector: string = ''): string { const commentsWithHash = extractCommentsWithHash(cssText); cssText = stripComments(cssText); @@ -172,11 +172,12 @@ export class ShadowCss { * * scopeName menu-item { * - **/ + **/ private _insertPolyfillDirectivesInCssText(cssText: string): string { // Difference with webcomponents.js: does not handle comments - return cssText.replace( - _cssContentNextSelectorRe, function(...m: string[]) { return m[2] + '{'; }); + return cssText.replace(_cssContentNextSelectorRe, function(...m: string[]) { + return m[2] + '{'; + }); } /* @@ -193,7 +194,7 @@ export class ShadowCss { * * scopeName menu-item {...} * - **/ + **/ private _insertPolyfillRulesInCssText(cssText: string): string { // Difference with webcomponents.js: does not handle comments return cssText.replace(_cssContentRuleRe, (...m: string[]) => { @@ -209,7 +210,7 @@ export class ShadowCss { * and converts this to * * scopeName .foo { ... } - */ + */ private _scopeCssText(cssText: string, scopeSelector: string, hostSelector: string): string { const unscopedRules = this._extractUnscopedRulesFromCssText(cssText); // replace :host and :host-context -shadowcsshost and -shadowcsshost respectively @@ -238,7 +239,7 @@ export class ShadowCss { * * menu-item {...} * - **/ + **/ private _extractUnscopedRulesFromCssText(cssText: string): string { // Difference with webcomponents.js: does not handle comments let r = ''; @@ -257,7 +258,7 @@ export class ShadowCss { * to * * .foo > .bar - */ + */ private _convertColonHost(cssText: string): string { return this._convertColonRule(cssText, _cssColonHostRe, this._colonHostPartReplacer); } @@ -276,7 +277,7 @@ export class ShadowCss { * to * * .foo .bar { ... } - */ + */ private _convertColonHostContext(cssText: string): string { return this._convertColonRule( cssText, _cssColonHostContextRe, this._colonHostContextPartReplacer); @@ -315,7 +316,7 @@ export class ShadowCss { /* * Convert combinators like ::shadow and pseudo-elements like ::content * by replacing with space. - */ + */ private _convertShadowDOMSelectors(cssText: string): string { return _shadowDOMSelectorsRe.reduce((result, pattern) => result.replace(pattern, ' '), cssText); } @@ -505,7 +506,9 @@ class SafeSelector { return content.replace(/__ph-(\d+)__/g, (ph, index) => this.placeholders[+index]); } - content(): string { return this._content; } + content(): string { + return this._content; + } } const _cssContentNextSelectorRe = diff --git a/packages/compiler/src/style_compiler.ts b/packages/compiler/src/style_compiler.ts index 751a7c3ca8..e484d8b746 100644 --- a/packages/compiler/src/style_compiler.ts +++ b/packages/compiler/src/style_compiler.ts @@ -66,7 +66,7 @@ export class StyleCompiler { stylesheet.styleUrls.forEach((styleUrl) => { const exprIndex = styleExpressions.length; // Note: This placeholder will be filled later. - styleExpressions.push(null !); + styleExpressions.push(null!); dependencies.push(new StylesCompileDependency( getStylesVarName(null), styleUrl, (value) => styleExpressions[exprIndex] = outputCtx.importExpr(value))); @@ -89,7 +89,7 @@ export class StyleCompiler { } } -function getStylesVarName(component: CompileDirectiveMetadata | null): string { +function getStylesVarName(component: CompileDirectiveMetadata|null): string { let result = `styles`; if (component) { result += `_${identifierName(component.type)}`; diff --git a/packages/compiler/src/summary_resolver.ts b/packages/compiler/src/summary_resolver.ts index 38d7dda3a0..8b53145ab9 100644 --- a/packages/compiler/src/summary_resolver.ts +++ b/packages/compiler/src/summary_resolver.ts @@ -28,14 +28,28 @@ export abstract class SummaryResolver { export class JitSummaryResolver implements SummaryResolver { private _summaries = new Map>(); - isLibraryFile(): boolean { return false; } - toSummaryFileName(fileName: string): string { return fileName; } - fromSummaryFileName(fileName: string): string { return fileName; } + isLibraryFile(): boolean { + return false; + } + toSummaryFileName(fileName: string): string { + return fileName; + } + fromSummaryFileName(fileName: string): string { + return fileName; + } resolveSummary(reference: Type): Summary|null { return this._summaries.get(reference) || null; } - getSymbolsOf(): Type[] { return []; } - getImportAs(reference: Type): Type { return reference; } - getKnownModuleName(fileName: string) { return null; } - addSummary(summary: Summary) { this._summaries.set(summary.symbol, summary); } + getSymbolsOf(): Type[] { + return []; + } + getImportAs(reference: Type): Type { + return reference; + } + getKnownModuleName(fileName: string) { + return null; + } + addSummary(summary: Summary) { + this._summaries.set(summary.symbol, summary); + } } diff --git a/packages/compiler/src/template_parser/binding_parser.ts b/packages/compiler/src/template_parser/binding_parser.ts index deec96edce..f6dde21f6d 100644 --- a/packages/compiler/src/template_parser/binding_parser.ts +++ b/packages/compiler/src/template_parser/binding_parser.ts @@ -8,7 +8,7 @@ import {CompileDirectiveSummary, CompilePipeSummary} from '../compile_metadata'; import {SecurityContext} from '../core'; -import {ASTWithSource, AbsoluteSourceSpan, BindingPipe, BindingType, BoundElementProperty, EmptyExpr, ParsedEvent, ParsedEventType, ParsedProperty, ParsedPropertyType, ParsedVariable, ParserError, RecursiveAstVisitor, TemplateBinding, VariableBinding} from '../expression_parser/ast'; +import {AbsoluteSourceSpan, ASTWithSource, BindingPipe, BindingType, BoundElementProperty, EmptyExpr, ParsedEvent, ParsedEventType, ParsedProperty, ParsedPropertyType, ParsedVariable, ParserError, RecursiveAstVisitor, TemplateBinding, VariableBinding} from '../expression_parser/ast'; import {Parser} from '../expression_parser/parser'; import {InterpolationConfig} from '../ml_parser/interpolation_config'; import {mergeNsAndName} from '../ml_parser/tags'; @@ -45,9 +45,13 @@ export class BindingParser { } } - get interpolationConfig(): InterpolationConfig { return this._interpolationConfig; } + get interpolationConfig(): InterpolationConfig { + return this._interpolationConfig; + } - getUsedPipes(): CompilePipeSummary[] { return Array.from(this._usedPipes.values()); } + getUsedPipes(): CompilePipeSummary[] { + return Array.from(this._usedPipes.values()); + } createBoundHostProperties(dirMeta: CompileDirectiveSummary, sourceSpan: ParseSourceSpan): ParsedProperty[]|null { @@ -61,7 +65,9 @@ export class BindingParser { boundProps); } else { this._reportError( - `Value of the host property binding "${propName}" needs to be a string representing an expression but got "${expression}" (${typeof expression})`, + `Value of the host property binding "${ + propName}" needs to be a string representing an expression but got "${ + expression}" (${typeof expression})`, sourceSpan); } }); @@ -89,7 +95,9 @@ export class BindingParser { this.parseEvent(propName, expression, sourceSpan, sourceSpan, [], targetEvents); } else { this._reportError( - `Value of the host listener "${propName}" needs to be a string representing an expression but got "${expression}" (${typeof expression})`, + `Value of the host listener "${ + propName}" needs to be a string representing an expression but got "${ + expression}" (${typeof expression})`, sourceSpan); } }); @@ -103,7 +111,7 @@ export class BindingParser { try { const ast = this._exprParser.parseInterpolation( - value, sourceInfo, sourceSpan.start.offset, this._interpolationConfig) !; + value, sourceInfo, sourceSpan.start.offset, this._interpolationConfig)!; if (ast) this._reportExpressionParserErrors(ast.errors, sourceSpan); this._checkPipes(ast, sourceSpan); return ast; @@ -183,8 +191,9 @@ export class BindingParser { this._checkPipes(binding.value, sourceSpan); } }); - bindingsResult.warnings.forEach( - (warning) => { this._reportError(warning, sourceSpan, ParseErrorLevel.WARNING); }); + bindingsResult.warnings.forEach((warning) => { + this._reportError(warning, sourceSpan, ParseErrorLevel.WARNING); + }); return bindingsResult.templateBindings; } catch (e) { this._reportError(`${e}`, sourceSpan); @@ -257,7 +266,7 @@ export class BindingParser { name: string, ast: ASTWithSource, sourceSpan: ParseSourceSpan, valueSpan: ParseSourceSpan|undefined, targetMatchableAttrs: string[][], targetProps: ParsedProperty[]) { - targetMatchableAttrs.push([name, ast.source !]); + targetMatchableAttrs.push([name, ast.source!]); targetProps.push( new ParsedProperty(name, ast, ParsedPropertyType.DEFAULT, sourceSpan, valueSpan)); } @@ -275,7 +284,7 @@ export class BindingParser { // states will be applied by angular when the element is attached/detached const ast = this._parseBinding( expression || 'undefined', false, valueSpan || sourceSpan, absoluteOffset); - targetMatchableAttrs.push([name, ast.source !]); + targetMatchableAttrs.push([name, ast.source!]); targetProps.push( new ParsedProperty(name, ast, ParsedPropertyType.ANIMATION, sourceSpan, valueSpan)); } @@ -310,10 +319,10 @@ export class BindingParser { } let unit: string|null = null; - let bindingType: BindingType = undefined !; + let bindingType: BindingType = undefined!; let boundPropertyName: string|null = null; const parts = boundProp.name.split(PROPERTY_PARTS_SEPARATOR); - let securityContexts: SecurityContext[] = undefined !; + let securityContexts: SecurityContext[] = undefined!; // Check for special cases (prefix style, attr, class) if (parts.length > 1) { @@ -401,13 +410,15 @@ export class BindingParser { default: this._reportError( - `The provided animation output phase value "${phase}" for "@${eventName}" is not supported (use start or done)`, + `The provided animation output phase value "${phase}" for "@${ + eventName}" is not supported (use start or done)`, sourceSpan); break; } } else { this._reportError( - `The animation trigger output event (@${eventName}) is missing its phase value name (start or done are currently supported)`, + `The animation trigger output event (@${ + eventName}) is missing its phase value name (start or done are currently supported)`, sourceSpan); } } @@ -416,9 +427,9 @@ export class BindingParser { name: string, expression: string, sourceSpan: ParseSourceSpan, handlerSpan: ParseSourceSpan, targetMatchableAttrs: string[][], targetEvents: ParsedEvent[]) { // long format: 'target: eventName' - const [target, eventName] = splitAtColon(name, [null !, name]); + const [target, eventName] = splitAtColon(name, [null!, name]); const ast = this._parseAction(expression, handlerSpan); - targetMatchableAttrs.push([name !, ast.source !]); + targetMatchableAttrs.push([name!, ast.source!]); targetEvents.push( new ParsedEvent(eventName, target, ParsedEventType.Regular, ast, sourceSpan, handlerSpan)); // Don't detect directives for event names for now, @@ -465,7 +476,7 @@ export class BindingParser { const collector = new PipeCollector(); ast.visit(collector); collector.pipes.forEach((ast, pipeName) => { - const pipeMeta = this.pipesByName !.get(pipeName); + const pipeMeta = this.pipesByName!.get(pipeName); if (!pipeMeta) { this._reportError( `The pipe '${pipeName}' could not be found`, @@ -488,7 +499,7 @@ export class BindingParser { const report = isAttr ? this._schemaRegistry.validateAttribute(propName) : this._schemaRegistry.validateProperty(propName); if (report.error) { - this._reportError(report.msg !, sourceSpan, ParseErrorLevel.ERROR); + this._reportError(report.msg!, sourceSpan, ParseErrorLevel.ERROR); } } } diff --git a/packages/compiler/src/template_parser/template_ast.ts b/packages/compiler/src/template_parser/template_ast.ts index 2a876bcc6e..62be9f756b 100644 --- a/packages/compiler/src/template_parser/template_ast.ts +++ b/packages/compiler/src/template_parser/template_ast.ts @@ -36,7 +36,9 @@ export interface TemplateAst { export class TextAst implements TemplateAst { constructor( public value: string, public ngContentIndex: number, public sourceSpan: ParseSourceSpan) {} - visit(visitor: TemplateAstVisitor, context: any): any { return visitor.visitText(this, context); } + visit(visitor: TemplateAstVisitor, context: any): any { + return visitor.visitText(this, context); + } } /** @@ -56,7 +58,9 @@ export class BoundTextAst implements TemplateAst { */ export class AttrAst implements TemplateAst { constructor(public name: string, public value: string, public sourceSpan: ParseSourceSpan) {} - visit(visitor: TemplateAstVisitor, context: any): any { return visitor.visitAttr(this, context); } + visit(visitor: TemplateAstVisitor, context: any): any { + return visitor.visitAttr(this, context); + } } export const enum PropertyBindingType { @@ -319,7 +323,9 @@ export class NullTemplateVisitor implements TemplateAstVisitor { * in an template ast recursively. */ export class RecursiveTemplateAstVisitor extends NullTemplateVisitor implements TemplateAstVisitor { - constructor() { super(); } + constructor() { + super(); + } // Nodes with children visitEmbeddedTemplate(ast: EmbeddedTemplateAst, context: any): any { @@ -358,7 +364,7 @@ export class RecursiveTemplateAstVisitor extends NullTemplateVisitor implements cb: (visit: ((children: V[]|undefined) => void)) => void) { let results: any[][] = []; let t = this; - function visit(children: T[] | undefined) { + function visit(children: T[]|undefined) { if (children && children.length) results.push(templateVisitAll(t, children, context)); } cb(visit); @@ -373,7 +379,7 @@ export function templateVisitAll( visitor: TemplateAstVisitor, asts: TemplateAst[], context: any = null): any[] { const result: any[] = []; const visit = visitor.visit ? - (ast: TemplateAst) => visitor.visit !(ast, context) || ast.visit(visitor, context) : + (ast: TemplateAst) => visitor.visit!(ast, context) || ast.visit(visitor, context) : (ast: TemplateAst) => ast.visit(visitor, context); asts.forEach(ast => { const astResult = visit(ast); diff --git a/packages/compiler/src/template_parser/template_parser.ts b/packages/compiler/src/template_parser/template_parser.ts index 453a32b29e..2323b8f683 100644 --- a/packages/compiler/src/template_parser/template_parser.ts +++ b/packages/compiler/src/template_parser/template_parser.ts @@ -12,7 +12,7 @@ import {CompilerConfig} from '../config'; import {SchemaMetadata} from '../core'; import {AST, ASTWithSource, EmptyExpr, ParsedEvent, ParsedProperty, ParsedVariable} from '../expression_parser/ast'; import {Parser} from '../expression_parser/parser'; -import {Identifiers, createTokenForExternalReference, createTokenForReference} from '../identifiers'; +import {createTokenForExternalReference, createTokenForReference, Identifiers} from '../identifiers'; import * as html from '../ml_parser/ast'; import {HtmlParser, ParseTreeResult} from '../ml_parser/html_parser'; import {removeWhitespaces, replaceNgsp} from '../ml_parser/html_whitespaces'; @@ -57,7 +57,7 @@ const IDENT_EVENT_IDX = 10; const TEMPLATE_ATTR_PREFIX = '*'; const CLASS_ATTR = 'class'; -let _TEXT_CSS_SELECTOR !: CssSelector; +let _TEXT_CSS_SELECTOR!: CssSelector; function TEXT_CSS_SELECTOR(): CssSelector { if (!_TEXT_CSS_SELECTOR) { _TEXT_CSS_SELECTOR = CssSelector.parse('*')[0]; @@ -84,7 +84,9 @@ export class TemplateParser { private _htmlParser: HtmlParser, private _console: Console, public transforms: t.TemplateAstVisitor[]) {} - public get expressionParser() { return this._exprParser; } + public get expressionParser() { + return this._exprParser; + } parse( component: CompileDirectiveMetadata, template: string|ParseTreeResult, @@ -93,9 +95,9 @@ export class TemplateParser { preserveWhitespaces: boolean): {template: t.TemplateAst[], pipes: CompilePipeSummary[]} { const result = this.tryParse( component, template, directives, pipes, schemas, templateUrl, preserveWhitespaces); - const warnings = result.errors !.filter(error => error.level === ParseErrorLevel.WARNING); + const warnings = result.errors!.filter(error => error.level === ParseErrorLevel.WARNING); - const errors = result.errors !.filter(error => error.level === ParseErrorLevel.ERROR); + const errors = result.errors!.filter(error => error.level === ParseErrorLevel.ERROR); if (warnings.length > 0) { this._console.warn(`Template parse warnings:\n${warnings.join('\n')}`); @@ -106,7 +108,7 @@ export class TemplateParser { throw syntaxError(`Template parse errors:\n${errorString}`, errors); } - return {template: result.templateAst !, pipes: result.usedPipes !}; + return {template: result.templateAst!, pipes: result.usedPipes!}; } tryParse( @@ -114,7 +116,7 @@ export class TemplateParser { directives: CompileDirectiveSummary[], pipes: CompilePipeSummary[], schemas: SchemaMetadata[], templateUrl: string, preserveWhitespaces: boolean): TemplateParseResult { let htmlParseResult = typeof template === 'string' ? - this._htmlParser !.parse(template, templateUrl, { + this._htmlParser!.parse(template, templateUrl, { tokenizeExpansionForms: true, interpolationConfig: this.getInterpolationConfig(component) }) : @@ -139,7 +141,7 @@ export class TemplateParser { const uniqDirectives = removeSummaryDuplicates(directives); const uniqPipes = removeSummaryDuplicates(pipes); const providerViewContext = new ProviderViewContext(this._reflector, component); - let interpolationConfig: InterpolationConfig = undefined !; + let interpolationConfig: InterpolationConfig = undefined!; if (component.template && component.template.interpolation) { interpolationConfig = { start: component.template.interpolation[0], @@ -147,7 +149,7 @@ export class TemplateParser { }; } const bindingParser = new BindingParser( - this._exprParser, interpolationConfig !, this._schemaRegistry, uniqPipes, errors); + this._exprParser, interpolationConfig!, this._schemaRegistry, uniqPipes, errors); const parseVisitor = new TemplateParseVisitor( this._reflector, this._config, providerViewContext, uniqDirectives, bindingParser, this._schemaRegistry, schemas, errors); @@ -164,8 +166,9 @@ export class TemplateParser { } if (this.transforms) { - this.transforms.forEach( - (transform: t.TemplateAstVisitor) => { result = t.templateVisitAll(transform, result); }); + this.transforms.forEach((transform: t.TemplateAstVisitor) => { + result = t.templateVisitAll(transform, result); + }); } return new TemplateParseResult(result, usedPipes, errors); @@ -224,29 +227,35 @@ class TemplateParseVisitor implements html.Visitor { // Note: queries start with id 1 so we can use the number in a Bloom filter! this.contentQueryStartId = providerViewContext.component.viewQueries.length + 1; directives.forEach((directive, index) => { - const selector = CssSelector.parse(directive.selector !); + const selector = CssSelector.parse(directive.selector!); this.selectorMatcher.addSelectables(selector, directive); this.directivesIndex.set(directive, index); }); } - visitExpansion(expansion: html.Expansion, context: any): any { return null; } + visitExpansion(expansion: html.Expansion, context: any): any { + return null; + } - visitExpansionCase(expansionCase: html.ExpansionCase, context: any): any { return null; } + visitExpansionCase(expansionCase: html.ExpansionCase, context: any): any { + return null; + } visitText(text: html.Text, parent: ElementContext): any { - const ngContentIndex = parent.findNgContentIndex(TEXT_CSS_SELECTOR()) !; + const ngContentIndex = parent.findNgContentIndex(TEXT_CSS_SELECTOR())!; const valueNoNgsp = replaceNgsp(text.value); - const expr = this._bindingParser.parseInterpolation(valueNoNgsp, text.sourceSpan !); - return expr ? new t.BoundTextAst(expr, ngContentIndex, text.sourceSpan !) : - new t.TextAst(valueNoNgsp, ngContentIndex, text.sourceSpan !); + const expr = this._bindingParser.parseInterpolation(valueNoNgsp, text.sourceSpan!); + return expr ? new t.BoundTextAst(expr, ngContentIndex, text.sourceSpan!) : + new t.TextAst(valueNoNgsp, ngContentIndex, text.sourceSpan!); } visitAttribute(attribute: html.Attribute, context: any): any { return new t.AttrAst(attribute.name, attribute.value, attribute.sourceSpan); } - visitComment(comment: html.Comment, context: any): any { return null; } + visitComment(comment: html.Comment, context: any): any { + return null; + } visitElement(element: html.Element, parent: ElementContext): any { const queryStartIndex = this.contentQueryStartId; @@ -307,7 +316,7 @@ class TemplateParseVisitor implements html.Visitor { const parsedVariables: ParsedVariable[] = []; const absoluteOffset = (attr.valueSpan || attr.sourceSpan).start.offset; this._bindingParser.parseInlineTemplateBinding( - templateKey !, templateValue !, attr.sourceSpan, absoluteOffset, templateMatchableAttrs, + templateKey!, templateValue!, attr.sourceSpan, absoluteOffset, templateMatchableAttrs, templateElementOrDirectiveProps, parsedVariables); templateElementVars.push(...parsedVariables.map(v => t.VariableAst.fromParsedVariable(v))); } @@ -326,52 +335,51 @@ class TemplateParseVisitor implements html.Visitor { const boundDirectivePropNames = new Set(); const directiveAsts = this._createDirectiveAsts( isTemplateElement, element.name, directiveMetas, elementOrDirectiveProps, - elementOrDirectiveRefs, element.sourceSpan !, references, boundDirectivePropNames); + elementOrDirectiveRefs, element.sourceSpan!, references, boundDirectivePropNames); const elementProps: t.BoundElementPropertyAst[] = this._createElementPropertyAsts( element.name, elementOrDirectiveProps, boundDirectivePropNames); const isViewRoot = parent.isTemplateElement || hasInlineTemplates; const providerContext = new ProviderElementContext( - this.providerViewContext, parent.providerContext !, isViewRoot, directiveAsts, attrs, - references, isTemplateElement, queryStartIndex, element.sourceSpan !); + this.providerViewContext, parent.providerContext!, isViewRoot, directiveAsts, attrs, + references, isTemplateElement, queryStartIndex, element.sourceSpan!); const children: t.TemplateAst[] = html.visitAll( preparsedElement.nonBindable ? NON_BINDABLE_VISITOR : this, element.children, ElementContext.create( isTemplateElement, directiveAsts, - isTemplateElement ? parent.providerContext ! : providerContext)); + isTemplateElement ? parent.providerContext! : providerContext)); providerContext.afterElement(); // Override the actual selector when the `ngProjectAs` attribute is provided const projectionSelector = preparsedElement.projectAs != '' ? CssSelector.parse(preparsedElement.projectAs)[0] : elementCssSelector; - const ngContentIndex = parent.findNgContentIndex(projectionSelector) !; + const ngContentIndex = parent.findNgContentIndex(projectionSelector)!; let parsedElement: t.TemplateAst; if (preparsedElement.type === PreparsedElementType.NG_CONTENT) { // `` element if (element.children && !element.children.every(_isEmptyTextNode)) { - this._reportError(` element cannot have content.`, element.sourceSpan !); + this._reportError(` element cannot have content.`, element.sourceSpan!); } parsedElement = new t.NgContentAst( - this.ngContentCount++, hasInlineTemplates ? null ! : ngContentIndex, - element.sourceSpan !); + this.ngContentCount++, hasInlineTemplates ? null! : ngContentIndex, element.sourceSpan!); } else if (isTemplateElement) { // `` element this._assertAllEventsPublishedByDirectives(directiveAsts, events); this._assertNoComponentsNorElementBindingsOnTemplate( - directiveAsts, elementProps, element.sourceSpan !); + directiveAsts, elementProps, element.sourceSpan!); parsedElement = new t.EmbeddedTemplateAst( attrs, events, references, elementVars, providerContext.transformedDirectiveAsts, providerContext.transformProviders, providerContext.transformedHasViewContainer, - providerContext.queryMatches, children, hasInlineTemplates ? null ! : ngContentIndex, - element.sourceSpan !); + providerContext.queryMatches, children, hasInlineTemplates ? null! : ngContentIndex, + element.sourceSpan!); } else { // element other than `` and `` this._assertElementExists(matchElement, element); - this._assertOnlyOneComponent(directiveAsts, element.sourceSpan !); + this._assertOnlyOneComponent(directiveAsts, element.sourceSpan!); const ngContentIndex = hasInlineTemplates ? null : parent.findNgContentIndex(projectionSelector); @@ -389,22 +397,22 @@ class TemplateParseVisitor implements html.Visitor { const {directives} = this._parseDirectives(this.selectorMatcher, templateSelector); const templateBoundDirectivePropNames = new Set(); const templateDirectiveAsts = this._createDirectiveAsts( - true, elName, directives, templateElementOrDirectiveProps, [], element.sourceSpan !, [], + true, elName, directives, templateElementOrDirectiveProps, [], element.sourceSpan!, [], templateBoundDirectivePropNames); const templateElementProps: t.BoundElementPropertyAst[] = this._createElementPropertyAsts( elName, templateElementOrDirectiveProps, templateBoundDirectivePropNames); this._assertNoComponentsNorElementBindingsOnTemplate( - templateDirectiveAsts, templateElementProps, element.sourceSpan !); + templateDirectiveAsts, templateElementProps, element.sourceSpan!); const templateProviderContext = new ProviderElementContext( - this.providerViewContext, parent.providerContext !, parent.isTemplateElement, - templateDirectiveAsts, [], [], true, templateQueryStartIndex, element.sourceSpan !); + this.providerViewContext, parent.providerContext!, parent.isTemplateElement, + templateDirectiveAsts, [], [], true, templateQueryStartIndex, element.sourceSpan!); templateProviderContext.afterElement(); parsedElement = new t.EmbeddedTemplateAst( [], [], [], templateElementVars, templateProviderContext.transformedDirectiveAsts, templateProviderContext.transformProviders, templateProviderContext.transformedHasViewContainer, templateProviderContext.queryMatches, - [parsedElement], ngContentIndex, element.sourceSpan !); + [parsedElement], ngContentIndex, element.sourceSpan!); } return parsedElement; @@ -538,7 +546,7 @@ class TemplateParseVisitor implements html.Visitor { let matchElement = false; selectorMatcher.match(elementCssSelector, (selector, directive) => { - directives[this.directivesIndex.get(directive) !] = directive; + directives[this.directivesIndex.get(directive)!] = directive; matchElement = matchElement || selector.hasElementSelector(); }); @@ -554,7 +562,7 @@ class TemplateParseVisitor implements html.Visitor { elementSourceSpan: ParseSourceSpan, targetReferences: t.ReferenceAst[], targetBoundDirectivePropNames: Set): t.DirectiveAst[] { const matchedReferences = new Set(); - let component: CompileDirectiveSummary = null !; + let component: CompileDirectiveSummary = null!; const directiveAsts = directives.map((directive) => { const sourceSpan = new ParseSourceSpan( @@ -566,15 +574,14 @@ class TemplateParseVisitor implements html.Visitor { } const directiveProperties: t.BoundDirectivePropertyAst[] = []; const boundProperties = - this._bindingParser.createDirectiveHostPropertyAsts(directive, elementName, sourceSpan) !; + this._bindingParser.createDirectiveHostPropertyAsts(directive, elementName, sourceSpan)!; let hostProperties = boundProperties.map(prop => t.BoundElementPropertyAst.fromBoundProperty(prop)); // Note: We need to check the host properties here as well, // as we don't know the element name in the DirectiveWrapperCompiler yet. hostProperties = this._checkPropertiesInSchema(elementName, hostProperties); - const parsedEvents = - this._bindingParser.createDirectiveHostEventAsts(directive, sourceSpan) !; + const parsedEvents = this._bindingParser.createDirectiveHostEventAsts(directive, sourceSpan)!; this._createDirectivePropertyAsts( directive.inputs, props, directiveProperties, targetBoundDirectivePropNames); elementOrDirectiveRefs.forEach((elOrDirRef) => { @@ -602,7 +609,7 @@ class TemplateParseVisitor implements html.Visitor { elOrDirRef.sourceSpan); } } else if (!component) { - let refToken: CompileTokenMetadata = null !; + let refToken: CompileTokenMetadata = null!; if (isTemplateElement) { refToken = createTokenForExternalReference(this.reflector, Identifiers.TemplateRef); } @@ -663,7 +670,7 @@ class TemplateParseVisitor implements html.Visitor { private _findComponentDirectiveNames(directives: t.DirectiveAst[]): string[] { return this._findComponentDirectives(directives) - .map(directive => identifierName(directive.directive.type) !); + .map(directive => identifierName(directive.directive.type)!); } private _assertOnlyOneComponent(directives: t.DirectiveAst[], sourceSpan: ParseSourceSpan) { @@ -691,16 +698,16 @@ class TemplateParseVisitor implements html.Visitor { if (!matchElement && !this._schemaRegistry.hasElement(elName, this._schemas)) { let errorMsg = `'${elName}' is not a known element:\n`; - errorMsg += - `1. If '${elName}' is an Angular component, then verify that it is part of this module.\n`; + errorMsg += `1. If '${ + elName}' is an Angular component, then verify that it is part of this module.\n`; if (elName.indexOf('-') > -1) { - errorMsg += - `2. If '${elName}' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.`; + errorMsg += `2. If '${ + elName}' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.`; } else { errorMsg += `2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.`; } - this._reportError(errorMsg, element.sourceSpan !); + this._reportError(errorMsg, element.sourceSpan!); } } @@ -714,7 +721,8 @@ class TemplateParseVisitor implements html.Visitor { } elementProps.forEach(prop => { this._reportError( - `Property binding ${prop.name} not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations".`, + `Property binding ${ + prop.name} not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations".`, sourceSpan); }); } @@ -733,7 +741,9 @@ class TemplateParseVisitor implements html.Visitor { events.forEach(event => { if (event.target != null || !allDirectiveEvents.has(event.name)) { this._reportError( - `Event binding ${event.fullName} not emitted by any directive on an embedded template. Make sure that the event name is spelled correctly and all directives are listed in the "@NgModule.declarations".`, + `Event binding ${ + event + .fullName} not emitted by any directive on an embedded template. Make sure that the event name is spelled correctly and all directives are listed in the "@NgModule.declarations".`, event.sourceSpan); } }); @@ -746,16 +756,20 @@ class TemplateParseVisitor implements html.Visitor { return boundProps.filter((boundProp) => { if (boundProp.type === t.PropertyBindingType.Property && !this._schemaRegistry.hasProperty(elementName, boundProp.name, this._schemas)) { - let errorMsg = - `Can't bind to '${boundProp.name}' since it isn't a known property of '${elementName}'.`; + let errorMsg = `Can't bind to '${boundProp.name}' since it isn't a known property of '${ + elementName}'.`; if (elementName.startsWith('ng-')) { errorMsg += - `\n1. If '${boundProp.name}' is an Angular directive, then add 'CommonModule' to the '@NgModule.imports' of this component.` + + `\n1. If '${ + boundProp + .name}' is an Angular directive, then add 'CommonModule' to the '@NgModule.imports' of this component.` + `\n2. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.`; } else if (elementName.indexOf('-') > -1) { errorMsg += - `\n1. If '${elementName}' is an Angular component and it has '${boundProp.name}' input, then verify that it is part of this module.` + - `\n2. If '${elementName}' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.` + + `\n1. If '${elementName}' is an Angular component and it has '${ + boundProp.name}' input, then verify that it is part of this module.` + + `\n2. If '${ + elementName}' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.` + `\n3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.`; } this._reportError(errorMsg, boundProp.sourceSpan); @@ -791,20 +805,26 @@ class NonBindableVisitor implements html.Visitor { ast.name, html.visitAll(this, ast.attrs), [], [], [], [], [], false, [], children, ngContentIndex, ast.sourceSpan, ast.endSourceSpan); } - visitComment(comment: html.Comment, context: any): any { return null; } + visitComment(comment: html.Comment, context: any): any { + return null; + } visitAttribute(attribute: html.Attribute, context: any): t.AttrAst { return new t.AttrAst(attribute.name, attribute.value, attribute.sourceSpan); } visitText(text: html.Text, parent: ElementContext): t.TextAst { - const ngContentIndex = parent.findNgContentIndex(TEXT_CSS_SELECTOR()) !; - return new t.TextAst(text.value, ngContentIndex, text.sourceSpan !); + const ngContentIndex = parent.findNgContentIndex(TEXT_CSS_SELECTOR())!; + return new t.TextAst(text.value, ngContentIndex, text.sourceSpan!); } - visitExpansion(expansion: html.Expansion, context: any): any { return expansion; } + visitExpansion(expansion: html.Expansion, context: any): any { + return expansion; + } - visitExpansionCase(expansionCase: html.ExpansionCase, context: any): any { return expansionCase; } + visitExpansionCase(expansionCase: html.ExpansionCase, context: any): any { + return expansionCase; + } } /** @@ -824,7 +844,7 @@ class ElementOrDirectiveRef { } /** Splits a raw, potentially comma-delimited `exportAs` value into an array of names. */ -function splitExportAs(exportAs: string | null): string[] { +function splitExportAs(exportAs: string|null): string[] { return exportAs ? exportAs.split(',').map(e => e.trim()) : []; } @@ -837,7 +857,7 @@ class ElementContext { isTemplateElement: boolean, directives: t.DirectiveAst[], providerContext: ProviderElementContext): ElementContext { const matcher = new SelectorMatcher(); - let wildcardNgContentIndex: number = null !; + let wildcardNgContentIndex: number = null!; const component = directives.find(directive => directive.directive.isComponent); if (component) { const ngContentSelectors = component.directive.template !.ngContentSelectors; @@ -859,8 +879,9 @@ class ElementContext { findNgContentIndex(selector: CssSelector): number|null { const ngContentIndices: number[] = []; - this._ngContentIndexMatcher.match( - selector, (selector, ngContentIndex) => { ngContentIndices.push(ngContentIndex); }); + this._ngContentIndexMatcher.match(selector, (selector, ngContentIndex) => { + ngContentIndices.push(ngContentIndex); + }); ngContentIndices.sort(); if (this._wildcardNgContentIndex != null) { ngContentIndices.push(this._wildcardNgContentIndex); @@ -897,7 +918,7 @@ function _isEmptyTextNode(node: html.Node): boolean { return node instanceof html.Text && node.value.trim().length == 0; } -export function removeSummaryDuplicates(items: T[]): T[] { +export function removeSummaryDuplicates(items: T[]): T[] { const map = new Map(); items.forEach((item) => { diff --git a/packages/compiler/src/template_parser/template_preparser.ts b/packages/compiler/src/template_parser/template_preparser.ts index d76d53b231..515806cceb 100644 --- a/packages/compiler/src/template_parser/template_preparser.ts +++ b/packages/compiler/src/template_parser/template_preparser.ts @@ -20,9 +20,9 @@ const NG_NON_BINDABLE_ATTR = 'ngNonBindable'; const NG_PROJECT_AS = 'ngProjectAs'; export function preparseElement(ast: html.Element): PreparsedElement { - let selectAttr: string = null !; - let hrefAttr: string = null !; - let relAttr: string = null !; + let selectAttr: string = null!; + let hrefAttr: string = null!; + let relAttr: string = null!; let nonBindable = false; let projectAs = ''; ast.attrs.forEach(attr => { diff --git a/packages/compiler/src/url_resolver.ts b/packages/compiler/src/url_resolver.ts index 3f819309c0..daf54b8916 100644 --- a/packages/compiler/src/url_resolver.ts +++ b/packages/compiler/src/url_resolver.ts @@ -33,9 +33,13 @@ export function createOfflineCompileUrlResolver(): UrlResolver { * Attacker-controlled data introduced by a template could expose your * application to XSS risks. For more detail, see the [Security Guide](http://g.co/ng/security). */ -export interface UrlResolver { resolve(baseUrl: string, url: string): string; } +export interface UrlResolver { + resolve(baseUrl: string, url: string): string; +} -export interface UrlResolverCtor { new (packagePrefix?: string|null): UrlResolver; } +export interface UrlResolverCtor { + new(packagePrefix?: string|null): UrlResolver; +} export const UrlResolver: UrlResolverCtor = class UrlResolverImpl { constructor(private _packagePrefix: string|null = null) {} @@ -242,16 +246,16 @@ enum _ComponentIndex { * arbitrary strings may still look like path names. */ function _split(uri: string): Array { - return uri.match(_splitRe) !; + return uri.match(_splitRe)!; } /** - * Removes dot segments in given path component, as described in - * RFC 3986, section 5.2.4. - * - * @param path A non-empty path component. - * @return Path component with removed dot segments. - */ + * Removes dot segments in given path component, as described in + * RFC 3986, section 5.2.4. + * + * @param path A non-empty path component. + * @return Path component with removed dot segments. + */ function _removeDotSegments(path: string): string { if (path == '/') return '/'; diff --git a/packages/compiler/src/util.ts b/packages/compiler/src/util.ts index 789219e115..1cd8db7061 100644 --- a/packages/compiler/src/util.ts +++ b/packages/compiler/src/util.ts @@ -52,8 +52,8 @@ export function isDefined(val: any): boolean { return val !== null && val !== undefined; } -export function noUndefined(val: T | undefined): T { - return val === undefined ? null ! : val; +export function noUndefined(val: T|undefined): T { + return val === undefined ? null! : val; } export interface ValueVisitor { @@ -69,14 +69,20 @@ export class ValueTransformer implements ValueVisitor { } visitStringMap(map: {[key: string]: any}, context: any): any { const result: {[key: string]: any} = {}; - Object.keys(map).forEach(key => { result[key] = visitValue(map[key], this, context); }); + Object.keys(map).forEach(key => { + result[key] = visitValue(map[key], this, context); + }); return result; } - visitPrimitive(value: any, context: any): any { return value; } - visitOther(value: any, context: any): any { return value; } + visitPrimitive(value: any, context: any): any { + return value; + } + visitOther(value: any, context: any): any { + return value; + } } -export type SyncAsync = T | Promise; +export type SyncAsync = T|Promise; export const SyncAsync = { assertSync: (value: SyncAsync): T => { @@ -86,7 +92,9 @@ export const SyncAsync = { return value; }, then: (value: SyncAsync, cb: (value: T) => R | Promise| SyncAsync): - SyncAsync => { return isPromise(value) ? value.then(cb) : cb(value);}, + SyncAsync => { + return isPromise(value) ? value.then(cb) : cb(value); + }, all: (syncAsyncValues: SyncAsync[]): SyncAsync => { return syncAsyncValues.some(isPromise) ? Promise.all(syncAsyncValues) : syncAsyncValues as T[]; } @@ -259,7 +267,7 @@ export function newArray(size: number, value: T): T[]; export function newArray(size: number, value?: T): T[] { const list: T[] = []; for (let i = 0; i < size; i++) { - list.push(value !); + list.push(value!); } return list; } \ No newline at end of file diff --git a/packages/compiler/src/view_compiler/provider_compiler.ts b/packages/compiler/src/view_compiler/provider_compiler.ts index baa833cd53..8dbc2b182f 100644 --- a/packages/compiler/src/view_compiler/provider_compiler.ts +++ b/packages/compiler/src/view_compiler/provider_compiler.ts @@ -9,7 +9,7 @@ import {CompileDiDependencyMetadata, CompileEntryComponentMetadata, CompileProviderMetadata, CompileTokenMetadata} from '../compile_metadata'; import {CompileReflector} from '../compile_reflector'; import {DepFlags, NodeFlags} from '../core'; -import {Identifiers, createTokenForExternalReference} from '../identifiers'; +import {createTokenForExternalReference, Identifiers} from '../identifiers'; import {LifecycleHooks} from '../lifecycle_reflector'; import * as o from '../output/output_ast'; import {convertValueToOutputAst} from '../output/value_util'; @@ -45,7 +45,8 @@ export function providerDef(ctx: OutputContext, providerAst: ProviderAst): { singleProviderDef(ctx, flags, providerAst.providerType, providerAst.providers[0]); return { providerExpr, - flags: providerFlags, depsExpr, + flags: providerFlags, + depsExpr, tokenExpr: tokenExpr(ctx, providerAst.token), }; } @@ -96,9 +97,9 @@ function singleProviderDef( let providerExpr: o.Expression; let deps: CompileDiDependencyMetadata[]; if (providerType === ProviderAstType.Directive || providerType === ProviderAstType.Component) { - providerExpr = ctx.importExpr(providerMeta.useClass !.reference); + providerExpr = ctx.importExpr(providerMeta.useClass!.reference); flags |= NodeFlags.TypeDirective; - deps = providerMeta.deps || providerMeta.useClass !.diDeps; + deps = providerMeta.deps || providerMeta.useClass!.diDeps; } else { if (providerMeta.useClass) { providerExpr = ctx.importExpr(providerMeta.useClass.reference); @@ -130,7 +131,7 @@ function tokenExpr(ctx: OutputContext, tokenMeta: CompileTokenMetadata): o.Expre export function depDef(ctx: OutputContext, dep: CompileDiDependencyMetadata): o.Expression { // Note: the following fields have already been normalized out by provider_analyzer: // - isAttribute, isHost - const expr = dep.isValue ? convertValueToOutputAst(ctx, dep.value) : tokenExpr(ctx, dep.token !); + const expr = dep.isValue ? convertValueToOutputAst(ctx, dep.value) : tokenExpr(ctx, dep.token!); let flags = DepFlags.None; if (dep.isSkipSelf) { flags |= DepFlags.SkipSelf; diff --git a/packages/compiler/src/view_compiler/type_check_compiler.ts b/packages/compiler/src/view_compiler/type_check_compiler.ts index 02a3b9ec1a..7d06d5ee20 100644 --- a/packages/compiler/src/view_compiler/type_check_compiler.ts +++ b/packages/compiler/src/view_compiler/type_check_compiler.ts @@ -10,11 +10,11 @@ import {AotCompilerOptions} from '../aot/compiler_options'; import {StaticReflector} from '../aot/static_reflector'; import {StaticSymbol} from '../aot/static_symbol'; import {CompileDirectiveMetadata, CompilePipeSummary} from '../compile_metadata'; -import {BindingForm, EventHandlerVars, LocalResolver, convertActionBinding, convertPropertyBinding, convertPropertyBindingBuiltins} from '../compiler_util/expression_converter'; +import {BindingForm, convertActionBinding, convertPropertyBinding, convertPropertyBindingBuiltins, EventHandlerVars, LocalResolver} from '../compiler_util/expression_converter'; 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, TextAst, VariableAst, templateVisitAll} from '../template_parser/template_ast'; +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'; @@ -40,7 +40,7 @@ export class TypeCheckCompiler { usedPipes.forEach(p => pipes.set(p.name, p.type.reference)); let embeddedViewCount = 0; const viewBuilderFactory = - (parent: ViewBuilder | null, guards: GuardExpression[]): ViewBuilder => { + (parent: ViewBuilder|null, guards: GuardExpression[]): ViewBuilder => { const embeddedViewIndex = embeddedViewCount++; return new ViewBuilder( this.options, this.reflector, externalReferenceVars, parent, component.type.reference, @@ -66,7 +66,7 @@ interface ViewBuilderFactory { // Note: This is used as key in Map and should therefore be // unique per value. -type OutputVarType = o.BuiltinTypeName | StaticSymbol; +type OutputVarType = o.BuiltinTypeName|StaticSymbol; interface Expression { context: OutputVarType; @@ -247,10 +247,12 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { directives: DirectiveAst[], references: ReferenceAst[], }) { - ast.directives.forEach((dirAst) => { this.visitDirective(dirAst); }); + ast.directives.forEach((dirAst) => { + this.visitDirective(dirAst); + }); ast.references.forEach((ref) => { - let outputVarType: OutputVarType = null !; + let outputVarType: OutputVarType = null!; // Note: The old view compiler used to use an `any` type // for directives exposed via `exportAs`. // We keep this behaivor behind a flag for now. @@ -331,8 +333,8 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { // for arrays. return this.options.fullTemplateTypeCheck ? arr : arr.cast(o.DYNAMIC_TYPE); }, - createLiteralMapConverter: - (keys: {key: string, quoted: boolean}[]) => (values: o.Expression[]) => { + createLiteralMapConverter: (keys: {key: string, quoted: boolean}[]) => + (values: o.Expression[]) => { const entries = keys.map((k, i) => ({ key: k.key, value: values[i], diff --git a/packages/compiler/src/view_compiler/view_compiler.ts b/packages/compiler/src/view_compiler/view_compiler.ts index a0ede7a73e..8a52d633ea 100644 --- a/packages/compiler/src/view_compiler/view_compiler.ts +++ b/packages/compiler/src/view_compiler/view_compiler.ts @@ -8,7 +8,7 @@ import {CompileDirectiveMetadata, CompilePipeSummary, CompileQueryMetadata, rendererTypeName, tokenReference, viewClassName} from '../compile_metadata'; import {CompileReflector} from '../compile_reflector'; -import {BindingForm, BuiltinConverter, EventHandlerVars, LocalResolver, convertActionBinding, convertPropertyBinding, convertPropertyBindingBuiltins} from '../compiler_util/expression_converter'; +import {BindingForm, BuiltinConverter, convertActionBinding, convertPropertyBinding, convertPropertyBindingBuiltins, EventHandlerVars, LocalResolver} from '../compiler_util/expression_converter'; import {ArgumentType, BindingFlags, ChangeDetectionStrategy, NodeFlags, QueryBindingType, QueryValueType, ViewFlags} from '../core'; import {AST, ASTWithSource, Interpolation} from '../expression_parser/ast'; import {Identifiers} from '../identifiers'; @@ -17,7 +17,7 @@ import {isNgContainer} from '../ml_parser/tags'; 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, TextAst, VariableAst, templateVisitAll} from '../template_parser/template_ast'; +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'; @@ -38,7 +38,7 @@ export class ViewCompiler { styles: o.Expression, usedPipes: CompilePipeSummary[]): ViewCompileResult { let embeddedViewCount = 0; - let renderComponentVarName: string = undefined !; + let renderComponentVarName: string = undefined!; if (!component.isHost) { const template = component.template !; const customRenderData: o.LiteralMapEntry[] = []; @@ -48,7 +48,7 @@ export class ViewCompiler { } const renderComponentVar = o.variable(rendererTypeName(component.type.reference)); - renderComponentVarName = renderComponentVar.name !; + renderComponentVarName = renderComponentVar.name!; outputCtx.statements.push( renderComponentVar .set(o.importExpr(Identifiers.createRendererType2).callFn([new o.LiteralMapExpr([ @@ -61,7 +61,7 @@ export class ViewCompiler { [o.StmtModifier.Final, o.StmtModifier.Exported])); } - const viewBuilderFactory = (parent: ViewBuilder | null): ViewBuilder => { + const viewBuilderFactory = (parent: ViewBuilder|null): ViewBuilder => { const embeddedViewIndex = embeddedViewCount++; return new ViewBuilder( this._reflector, outputCtx, parent, component, embeddedViewIndex, usedPipes, @@ -101,7 +101,9 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { private nodes: (() => { sourceSpan: ParseSourceSpan | null, nodeDef: o.Expression, - nodeFlags: NodeFlags, updateDirectives?: UpdateExpression[], updateRenderer?: UpdateExpression[] + nodeFlags: NodeFlags, + updateDirectives?: UpdateExpression[], + updateRenderer?: UpdateExpression[] })[] = []; private purePipeNodeIndices: {[pipeName: string]: number} = Object.create(null); // Need Object.create so that we don't have builtin values... @@ -121,7 +123,7 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { // to be able to introduce the new view compiler without too many errors. this.compType = this.embeddedViewIndex > 0 ? o.DYNAMIC_TYPE : - o.expressionType(outputCtx.importExpr(this.component.type.reference)) !; + o.expressionType(outputCtx.importExpr(this.component.type.reference))!; this.viewName = viewClassName(this.component.type.reference, this.embeddedViewIndex); } @@ -181,7 +183,7 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { viewFlags |= ViewFlags.OnPush; } const viewFactory = new o.DeclareFunctionStmt( - this.viewName, [new o.FnParam(LOG_VAR.name !)], + this.viewName, [new o.FnParam(LOG_VAR.name!)], [new o.ReturnStatement(o.importExpr(Identifiers.viewDef).callFn([ o.literal(viewFlags), o.literalArr(nodeDefExprs), @@ -199,13 +201,13 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { let updateFn: o.Expression; if (updateStmts.length > 0) { const preStmts: o.Statement[] = []; - if (!this.component.isHost && o.findReadVarNames(updateStmts).has(COMP_VAR.name !)) { + if (!this.component.isHost && o.findReadVarNames(updateStmts).has(COMP_VAR.name!)) { preStmts.push(COMP_VAR.set(VIEW_VAR.prop('component')).toDeclStmt(this.compType)); } updateFn = o.fn( [ - new o.FnParam(CHECK_VAR.name !, o.INFERRED_TYPE), - new o.FnParam(VIEW_VAR.name !, o.INFERRED_TYPE) + new o.FnParam(CHECK_VAR.name!, o.INFERRED_TYPE), + new o.FnParam(VIEW_VAR.name!, o.INFERRED_TYPE) ], [...preStmts, ...updateStmts], o.INFERRED_TYPE); } else { @@ -219,9 +221,8 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { this.nodes.push(() => ({ sourceSpan: ast.sourceSpan, nodeFlags: NodeFlags.TypeNgContent, - nodeDef: o.importExpr(Identifiers.ngContentDef).callFn([ - o.literal(ast.ngContentIndex), o.literal(ast.index) - ]) + nodeDef: o.importExpr(Identifiers.ngContentDef) + .callFn([o.literal(ast.ngContentIndex), o.literal(ast.index)]) })); } @@ -242,7 +243,7 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { visitBoundText(ast: BoundTextAst, context: any): any { const nodeIndex = this.nodes.length; // reserve the space in the nodeDefs array - this.nodes.push(null !); + this.nodes.push(null!); const astWithSource = ast.value; const inter = astWithSource.ast; @@ -270,7 +271,7 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { visitEmbeddedTemplate(ast: EmbeddedTemplateAst, context: any): any { const nodeIndex = this.nodes.length; // reserve the space in the nodeDefs array - this.nodes.push(null !); + this.nodes.push(null!); const {flags, queryMatchesExpr, hostEvents} = this._visitElementOrTemplate(nodeIndex, ast); @@ -301,7 +302,7 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { visitElement(ast: ElementAst, context: any): any { const nodeIndex = this.nodes.length; // reserve the space in the nodeDefs array so we can add children - this.nodes.push(null !); + this.nodes.push(null!); // Using a null element name creates an anchor. const elName: string|null = isNgContainer(ast.name) ? null : ast.name; @@ -382,7 +383,7 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { queryMatches: QueryMatch[] }): { flags: NodeFlags, - usedEvents: [string | null, string][], + usedEvents: [string|null, string][], queryMatchesExpr: o.Expression, hostBindings: {context: o.Expression, inputAst: BoundElementPropertyAst, dirAst: DirectiveAst}[], @@ -409,7 +410,7 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { this._visitComponentFactoryResolverProvider(ast.directives); ast.providers.forEach(providerAst => { - let dirAst: DirectiveAst = undefined !; + let dirAst: DirectiveAst = undefined!; ast.directives.forEach(localDirAst => { if (localDirAst.directive.type.reference === tokenReference(providerAst.token)) { dirAst = localDirAst; @@ -427,7 +428,7 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { let queryMatchExprs: o.Expression[] = []; ast.queryMatches.forEach((match) => { - let valueType: QueryValueType = undefined !; + let valueType: QueryValueType = undefined!; if (tokenReference(match.value) === this.reflector.resolveExternalReference(Identifiers.ElementRef)) { valueType = QueryValueType.ElementRef; @@ -445,7 +446,7 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { } }); ast.references.forEach((ref) => { - let valueType: QueryValueType = undefined !; + let valueType: QueryValueType = undefined!; if (!ref.value) { valueType = QueryValueType.RenderElement; } else if ( @@ -459,7 +460,7 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { } }); ast.outputs.forEach((outputAst) => { - hostEvents.push({context: COMP_VAR, eventAst: outputAst, dirAst: null !}); + hostEvents.push({context: COMP_VAR, eventAst: outputAst, dirAst: null!}); }); return { @@ -480,7 +481,7 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { } { const nodeIndex = this.nodes.length; // reserve the space in the nodeDefs array so we can add children - this.nodes.push(null !); + this.nodes.push(null!); dirAst.directive.queries.forEach((query, queryIndex) => { const queryId = dirAst.contentQueryStartId + queryIndex; @@ -554,7 +555,8 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { })); const hostEvents = dirAst.hostEvents.map((hostEventAst) => ({ context: dirContextExpr, - eventAst: hostEventAst, dirAst, + eventAst: hostEventAst, + dirAst, })); // Check index is the same as the node index during compilation @@ -727,7 +729,7 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { private _createPipeConverter(expression: UpdateExpression, name: string, argCount: number): BuiltinConverter { - const pipe = this.usedPipes.find((pipeSummary) => pipeSummary.name === name) !; + const pipe = this.usedPipes.find((pipeSummary) => pipeSummary.name === name)!; if (pipe.pure) { const checkIndex = this.nodes.length; this.nodes.push(() => ({ @@ -803,13 +805,12 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { context: expression.context, value: convertPropertyBindingBuiltins( { - createLiteralArrayConverter: (argCount: number) => this._createLiteralArrayConverter( - expression.sourceSpan, argCount), - createLiteralMapConverter: - (keys: {key: string, quoted: boolean}[]) => - this._createLiteralMapConverter(expression.sourceSpan, keys), + createLiteralArrayConverter: (argCount: number) => + this._createLiteralArrayConverter(expression.sourceSpan, argCount), + createLiteralMapConverter: (keys: {key: string, quoted: boolean}[]) => + this._createLiteralMapConverter(expression.sourceSpan, keys), createPipeConverter: (name: string, argCount: number) => - this._createPipeConverter(expression, name, argCount) + this._createPipeConverter(expression, name, argCount) }, expression.value) }; @@ -848,7 +849,7 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { return {updateRendererStmts, updateDirectivesStmts, nodeDefExprs}; function createUpdateStatements( - nodeIndex: number, sourceSpan: ParseSourceSpan | null, expressions: UpdateExpression[], + nodeIndex: number, sourceSpan: ParseSourceSpan|null, expressions: UpdateExpression[], allowEmptyExprs: boolean): o.Statement[] { const updateStmts: o.Statement[] = []; const exprs = expressions.map(({sourceSpan, context, value}) => { @@ -892,14 +893,14 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver { if (handleEventStmts.length > 0) { const preStmts: o.Statement[] = [ALLOW_DEFAULT_VAR.set(o.literal(true)).toDeclStmt(o.BOOL_TYPE)]; - if (!this.component.isHost && o.findReadVarNames(handleEventStmts).has(COMP_VAR.name !)) { + if (!this.component.isHost && o.findReadVarNames(handleEventStmts).has(COMP_VAR.name!)) { preStmts.push(COMP_VAR.set(VIEW_VAR.prop('component')).toDeclStmt(this.compType)); } handleEventFn = o.fn( [ - new o.FnParam(VIEW_VAR.name !, o.INFERRED_TYPE), - new o.FnParam(EVENT_NAME_VAR.name !, o.INFERRED_TYPE), - new o.FnParam(EventHandlerVars.event.name !, o.INFERRED_TYPE) + new o.FnParam(VIEW_VAR.name!, o.INFERRED_TYPE), + new o.FnParam(EVENT_NAME_VAR.name!, o.INFERRED_TYPE), + new o.FnParam(EventHandlerVars.event.name!, o.INFERRED_TYPE) ], [...preStmts, ...handleEventStmts, new o.ReturnStatement(ALLOW_DEFAULT_VAR)], o.INFERRED_TYPE); @@ -975,7 +976,9 @@ function elementBindingDef(inputAst: BoundElementPropertyAst, dirAst: DirectiveA function fixedAttrsDef(elementAst: ElementAst): o.Expression { const mapResult: {[key: string]: string} = Object.create(null); - elementAst.attrs.forEach(attrAst => { mapResult[attrAst.name] = attrAst.value; }); + elementAst.attrs.forEach(attrAst => { + mapResult[attrAst.name] = attrAst.value; + }); elementAst.directives.forEach(dirAst => { Object.keys(dirAst.directive.hostAttributes).forEach(name => { const value = dirAst.directive.hostAttributes[name]; @@ -1014,7 +1017,7 @@ function callUnwrapValue(nodeIndex: number, bindingIdx: number, expr: o.Expressi } function elementEventNameAndTarget( - eventAst: BoundEventAst, dirAst: DirectiveAst | null): {name: string, target: string | null} { + eventAst: BoundEventAst, dirAst: DirectiveAst|null): {name: string, target: string|null} { if (eventAst.isAnimation) { return { name: `@${eventAst.name}.${eventAst.phase}`, @@ -1037,6 +1040,6 @@ function calcStaticDynamicQueryFlags(query: CompileQueryMetadata) { return flags; } -export function elementEventFullName(target: string | null, name: string): string { +export function elementEventFullName(target: string|null, name: string): string { return target ? `${target}:${name}` : name; } diff --git a/packages/compiler/test/aot/compiler_spec.ts b/packages/compiler/test/aot/compiler_spec.ts index fd30a6d1a9..3084b9d390 100644 --- a/packages/compiler/test/aot/compiler_spec.ts +++ b/packages/compiler/test/aot/compiler_spec.ts @@ -13,7 +13,7 @@ import {extractSourceMap, originalPositionFor} from '@angular/compiler/testing/s import {NodeFlags} from '@angular/core/src/view/index'; import * as ts from 'typescript'; -import {EmittingCompilerHost, MockAotCompilerHost, MockCompilerHost, MockDirectory, MockMetadataBundlerHost, arrayToMockDir, compile, expectNoDiagnostics, isInBazel, settings, setup, toMockFileArray} from './test_util'; +import {arrayToMockDir, compile, EmittingCompilerHost, expectNoDiagnostics, isInBazel, MockAotCompilerHost, MockCompilerHost, MockDirectory, MockMetadataBundlerHost, settings, setup, toMockFileArray} from './test_util'; describe('compiler (unbundled Angular)', () => { let angularFiles = setup(); @@ -53,11 +53,11 @@ describe('compiler (unbundled Angular)', () => { function compileApp(): GeneratedFile { const {genFiles} = compile([rootDir, angularFiles]); return genFiles.find( - genFile => genFile.srcFileUrl === componentPath && genFile.genFileUrl.endsWith('.ts')) !; + genFile => genFile.srcFileUrl === componentPath && genFile.genFileUrl.endsWith('.ts'))!; } function findLineAndColumn( - file: string, token: string): {line: number | null, column: number | null} { + file: string, token: string): {line: number|null, column: number|null} { const index = file.indexOf(token); if (index === -1) { return {line: null, column: null}; @@ -84,7 +84,9 @@ describe('compiler (unbundled Angular)', () => { describe('inline templates', () => { const ngUrl = `${componentPath}.AppComponent.html`; - function templateDecorator(template: string) { return `template: \`${template}\`,`; } + function templateDecorator(template: string) { + return `template: \`${template}\`,`; + } declareTests({ngUrl, templateDecorator}); }); @@ -125,7 +127,7 @@ describe('compiler (unbundled Angular)', () => { const genFile = compileApp(); const genSource = toTypeScript(genFile); - const sourceMap = extractSourceMap(genSource) !; + const sourceMap = extractSourceMap(genSource)!; expect(sourceMap.file).toEqual(genFile.genFileUrl); // Note: the generated file also contains code that is not mapped to @@ -146,7 +148,7 @@ describe('compiler (unbundled Angular)', () => { const genFile = compileApp(); const genSource = toTypeScript(genFile); - const sourceMap = extractSourceMap(genSource) !; + const sourceMap = extractSourceMap(genSource)!; expect(originalPositionFor(sourceMap, findLineAndColumn(genSource, `'span'`))) .toEqual({line: 2, column: 3, source: ngUrl}); }); @@ -158,7 +160,7 @@ describe('compiler (unbundled Angular)', () => { const genFile = compileApp(); const genSource = toTypeScript(genFile); - const sourceMap = extractSourceMap(genSource) !; + const sourceMap = extractSourceMap(genSource)!; expect(originalPositionFor(sourceMap, findLineAndColumn(genSource, `someMethod()`))) .toEqual({line: 2, column: 9, source: ngUrl}); }); @@ -170,7 +172,7 @@ describe('compiler (unbundled Angular)', () => { const genFile = compileApp(); const genSource = toTypeScript(genFile); - const sourceMap = extractSourceMap(genSource) !; + const sourceMap = extractSourceMap(genSource)!; expect(originalPositionFor(sourceMap, findLineAndColumn(genSource, `someMethod()`))) .toEqual({line: 2, column: 9, source: ngUrl}); }); @@ -180,7 +182,7 @@ describe('compiler (unbundled Angular)', () => { const genFile = compileApp(); const genSource = toTypeScript(genFile); - const sourceMap = extractSourceMap(genSource) !; + const sourceMap = extractSourceMap(genSource)!; expect(originalPositionFor(sourceMap, {line: 1, column: 0})) .toEqual({line: 1, column: 0, source: ngFactoryPath}); }); @@ -205,7 +207,6 @@ describe('compiler (unbundled Angular)', () => { compile([FILES, angularFiles]); expect(warnSpy).toHaveBeenCalledWith( `Warning: Can't resolve all parameters for MyService in /app/app.ts: (?). This will become an error in Angular v6.x`); - }); it('should error if not all arguments of an @Injectable class can be resolved if strictInjectionParameters is true', @@ -279,7 +280,7 @@ describe('compiler (unbundled Angular)', () => { }; compile([FILES, angularFiles], { postCompile: program => { - const factorySource = program.getSourceFile('/app/app.ngfactory.ts') !; + const factorySource = program.getSourceFile('/app/app.ngfactory.ts')!; expect(factorySource.text).not.toContain('\'/app/app.ngfactory\''); } }); @@ -321,7 +322,7 @@ describe('compiler (unbundled Angular)', () => { const genFilePreamble = '/* Hello world! */'; const {genFiles} = compile([FILES, angularFiles]); const genFile = - genFiles.find(gf => gf.srcFileUrl === '/app/app.ts' && gf.genFileUrl.endsWith('.ts')) !; + genFiles.find(gf => gf.srcFileUrl === '/app/app.ts' && gf.genFileUrl.endsWith('.ts'))!; const genSource = toTypeScript(genFile, genFilePreamble); expect(genSource.startsWith(genFilePreamble)).toBe(true); }); @@ -445,9 +446,9 @@ describe('compiler (unbundled Angular)', () => { } }; const {genFiles} = compile([FILES, angularFiles]); - const genFile = genFiles.find(genFile => genFile.srcFileUrl === '/app/app.ts') !; + const genFile = genFiles.find(genFile => genFile.srcFileUrl === '/app/app.ts')!; const genSource = toTypeScript(genFile); - const createComponentFactoryCall = /ɵccf\([^)]*\)/m.exec(genSource) ![0].replace(/\s*/g, ''); + const createComponentFactoryCall = /ɵccf\([^)]*\)/m.exec(genSource)![0].replace(/\s*/g, ''); // selector expect(createComponentFactoryCall).toContain('my-comp'); // inputs @@ -476,10 +477,9 @@ describe('compiler (unbundled Angular)', () => { }; const {genFiles} = compile([FILES, angularFiles]); const genFile = - genFiles.find(gf => gf.srcFileUrl === '/app/app.ts' && gf.genFileUrl.endsWith('.ts')) !; + genFiles.find(gf => gf.srcFileUrl === '/app/app.ts' && gf.genFileUrl.endsWith('.ts'))!; const genSource = toTypeScript(genFile); expect(genSource).not.toContain('check('); - }); }); @@ -492,7 +492,6 @@ describe('compiler (unbundled Angular)', () => { inheritanceWithSummariesSpecs(() => angularSummaryFiles); describe('external symbol re-exports enabled', () => { - it('should not reexport type symbols mentioned in constructors', () => { const libInput: MockDirectory = { 'lib': { @@ -520,7 +519,7 @@ describe('compiler (unbundled Angular)', () => { const {genFiles: appGenFiles} = compile( [appInput, libOutDir, angularSummaryFiles], {useSummaries: true, createExternalSymbolFactoryReexports: true}); - const appNgFactory = appGenFiles.find((f) => f.genFileUrl === '/app/main.ngfactory.ts') !; + const appNgFactory = appGenFiles.find((f) => f.genFileUrl === '/app/main.ngfactory.ts')!; const appNgFactoryTs = toTypeScript(appNgFactory); expect(appNgFactoryTs).not.toContain('AType'); expect(appNgFactoryTs).toContain('AValue'); @@ -570,7 +569,7 @@ describe('compiler (unbundled Angular)', () => { const {genFiles: appGenFiles} = compile( [appInput, libOutDir, angularSummaryFiles], {useSummaries: true, createExternalSymbolFactoryReexports: true}); - const appNgFactory = appGenFiles.find((f) => f.genFileUrl === '/app/main.ngfactory.ts') !; + const appNgFactory = appGenFiles.find((f) => f.genFileUrl === '/app/main.ngfactory.ts')!; const appNgFactoryTs = toTypeScript(appNgFactory); // metadata of ctor calls is preserved, so we reexport the argument @@ -614,7 +613,7 @@ describe('compiler (unbundled Angular)', () => { const {genFiles: appGenFiles} = compile( [appInput, libOutDir, angularSummaryFiles], {useSummaries: true, createExternalSymbolFactoryReexports: true}); - const appNgFactory = appGenFiles.find((f) => f.genFileUrl === '/app/main.ngfactory.ts') !; + const appNgFactory = appGenFiles.find((f) => f.genFileUrl === '/app/main.ngfactory.ts')!; const appNgFactoryTs = toTypeScript(appNgFactory); // we don't need to reexport exported symbols via the .ngfactory @@ -707,7 +706,7 @@ describe('compiler (unbundled Angular)', () => { compile([libInput, getAngularSummaryFiles()], {useSummaries: true}); const {genFiles} = compile([libOutDir, appInput, getAngularSummaryFiles()], {useSummaries: true}); - const mainNgFactory = genFiles.find(gf => gf.srcFileUrl === '/app/main.ts') !; + const mainNgFactory = genFiles.find(gf => gf.srcFileUrl === '/app/main.ts')!; const flags = NodeFlags.TypeDirective | NodeFlags.Component | NodeFlags.OnDestroy; expect(toTypeScript(mainNgFactory)) .toContain(`${flags},(null as any),0,i1.Extends,[i2.AParam]`); @@ -761,7 +760,7 @@ describe('compiler (unbundled Angular)', () => { const {genFiles} = compile( [lib1OutDir, lib2OutDir, appInput, getAngularSummaryFiles()], {useSummaries: true}); - const mainNgFactory = genFiles.find(gf => gf.srcFileUrl === '/app/main.ts') !; + const mainNgFactory = genFiles.find(gf => gf.srcFileUrl === '/app/main.ts')!; const flags = NodeFlags.TypeDirective | NodeFlags.Component | NodeFlags.OnDestroy; const mainNgFactorySource = toTypeScript(mainNgFactory); expect(mainNgFactorySource).toContain(`import * as i2 from '/lib1/base';`); diff --git a/packages/compiler/test/aot/jit_summaries_spec.ts b/packages/compiler/test/aot/jit_summaries_spec.ts index 08bbdd27df..52ca5d290d 100644 --- a/packages/compiler/test/aot/jit_summaries_spec.ts +++ b/packages/compiler/test/aot/jit_summaries_spec.ts @@ -8,7 +8,7 @@ import {AotCompiler, AotCompilerHost, AotCompilerOptions, CompileSummaryKind, GeneratedFile, toTypeScript} from '@angular/compiler'; -import {MockDirectory, compile, setup} from './test_util'; +import {compile, MockDirectory, setup} from './test_util'; describe('aot summaries for jit', () => { let angularFiles = setup(); @@ -19,7 +19,7 @@ describe('aot summaries for jit', () => { }); function compileApp( - rootDir: MockDirectory, options: {useSummaries?: boolean}& AotCompilerOptions = {}): + rootDir: MockDirectory, options: {useSummaries?: boolean}&AotCompilerOptions = {}): {genFiles: GeneratedFile[], outDir: MockDirectory} { return compile( [rootDir, options.useSummaries ? angularSummaryFiles : angularFiles], @@ -42,7 +42,7 @@ describe('aot summaries for jit', () => { const rootDir = {'app': appDir}; const genFile = - compileApp(rootDir).genFiles.find(f => f.genFileUrl === '/app/app.module.ngsummary.ts') !; + compileApp(rootDir).genFiles.find(f => f.genFileUrl === '/app/app.module.ngsummary.ts')!; const genSource = toTypeScript(genFile); expect(genSource).toContain(`import * as i0 from '/app/app.module'`); @@ -71,7 +71,7 @@ describe('aot summaries for jit', () => { const rootDir = {'app': appDir}; const genFile = - compileApp(rootDir).genFiles.find(f => f.genFileUrl === '/app/app.module.ngsummary.ts') !; + compileApp(rootDir).genFiles.find(f => f.genFileUrl === '/app/app.module.ngsummary.ts')!; const genSource = toTypeScript(genFile); expect(genSource).toContain(`import * as i0 from '/app/app.module'`); @@ -100,7 +100,7 @@ describe('aot summaries for jit', () => { const rootDir = {'app': appDir}; const genFile = - compileApp(rootDir).genFiles.find(f => f.genFileUrl === '/app/app.module.ngsummary.ts') !; + compileApp(rootDir).genFiles.find(f => f.genFileUrl === '/app/app.module.ngsummary.ts')!; const genSource = toTypeScript(genFile); expect(genSource).toContain(`import * as i0 from '/app/app.module'`); @@ -126,7 +126,7 @@ describe('aot summaries for jit', () => { const rootDir = {'app': appDir}; const genFile = - compileApp(rootDir).genFiles.find(f => f.genFileUrl === '/app/app.module.ngsummary.ts') !; + compileApp(rootDir).genFiles.find(f => f.genFileUrl === '/app/app.module.ngsummary.ts')!; const genSource = toTypeScript(genFile); expect(genSource).toContain(`import * as i0 from '/app/app.module'`); @@ -165,7 +165,7 @@ describe('aot summaries for jit', () => { const rootDir = {'app': appDir}; const genFile = - compileApp(rootDir).genFiles.find(f => f.genFileUrl === '/app/app.module.ngsummary.ts') !; + compileApp(rootDir).genFiles.find(f => f.genFileUrl === '/app/app.module.ngsummary.ts')!; const genSource = toTypeScript(genFile); expect(genSource).toMatch(/useClass:\{\s*reference:i1.MyService/); @@ -199,7 +199,7 @@ describe('aot summaries for jit', () => { const rootDir = {'app': appDir}; const genFile = - compileApp(rootDir).genFiles.find(f => f.genFileUrl === '/app/app.module.ngsummary.ts') !; + compileApp(rootDir).genFiles.find(f => f.genFileUrl === '/app/app.module.ngsummary.ts')!; const genSource = toTypeScript(genFile); expect(genSource).toMatch(/useClass:\{\s*reference:i1.MyService/); @@ -226,7 +226,7 @@ describe('aot summaries for jit', () => { const rootDir = {'app': appDir}; const genFile = - compileApp(rootDir).genFiles.find(f => f.genFileUrl === '/app/app.module.ngsummary.ts') !; + compileApp(rootDir).genFiles.find(f => f.genFileUrl === '/app/app.module.ngsummary.ts')!; const genSource = toTypeScript(genFile); expect(genSource).toMatch( @@ -248,7 +248,7 @@ describe('aot summaries for jit', () => { const rootDir = {'app': appDir}; const genFile = - compileApp(rootDir).genFiles.find(f => f.genFileUrl === '/app/app.module.ngsummary.ts') !; + compileApp(rootDir).genFiles.find(f => f.genFileUrl === '/app/app.module.ngsummary.ts')!; const genSource = toTypeScript(genFile); expect(genSource).toMatch( @@ -301,10 +301,9 @@ describe('aot summaries for jit', () => { createExternalSymbolFactoryReexports: true, }); - const lib2ModuleNgSummary = - lib2Gen.find(f => f.genFileUrl === '/lib2/module.ngsummary.ts') !; + const lib2ModuleNgSummary = lib2Gen.find(f => f.genFileUrl === '/lib2/module.ngsummary.ts')!; const lib2ReexportNgSummary = - lib2Gen.find(f => f.genFileUrl === '/lib2/reexport.ngsummary.ts') !; + lib2Gen.find(f => f.genFileUrl === '/lib2/reexport.ngsummary.ts')!; // ngsummaries should add reexports for imported NgModules from a direct dependency expect(toTypeScript(lib2ModuleNgSummary)) @@ -336,10 +335,9 @@ describe('aot summaries for jit', () => { useSummaries: true, createExternalSymbolFactoryReexports: true }).genFiles; - const lib3ModuleNgSummary = - lib3Gen.find(f => f.genFileUrl === '/lib3/module.ngsummary.ts') !; + const lib3ModuleNgSummary = lib3Gen.find(f => f.genFileUrl === '/lib3/module.ngsummary.ts')!; const lib3ReexportNgSummary = - lib3Gen.find(f => f.genFileUrl === '/lib3/reexport.ngsummary.ts') !; + lib3Gen.find(f => f.genFileUrl === '/lib3/reexport.ngsummary.ts')!; // ngsummary.ts files should use the reexported values from direct and deep deps const lib3ModuleNgSummarySource = toTypeScript(lib3ModuleNgSummary); @@ -398,9 +396,9 @@ describe('aot summaries for jit', () => { const {outDir: lib2Out, genFiles: lib2Gen} = compileApp(lib2In, {useSummaries: true}); - const lib2ModuleNgSummary = lib2Gen.find(f => f.genFileUrl === '/lib2/module.ngsummary.ts') !; + const lib2ModuleNgSummary = lib2Gen.find(f => f.genFileUrl === '/lib2/module.ngsummary.ts')!; const lib2ReexportNgSummary = - lib2Gen.find(f => f.genFileUrl === '/lib2/reexport.ngsummary.ts') !; + lib2Gen.find(f => f.genFileUrl === '/lib2/reexport.ngsummary.ts')!; // ngsummaries should not add reexports by default for imported NgModules from a direct // dependency @@ -435,9 +433,9 @@ describe('aot summaries for jit', () => { }; const lib3Gen = compileApp(lib3In, {useSummaries: true}).genFiles; - const lib3ModuleNgSummary = lib3Gen.find(f => f.genFileUrl === '/lib3/module.ngsummary.ts') !; + const lib3ModuleNgSummary = lib3Gen.find(f => f.genFileUrl === '/lib3/module.ngsummary.ts')!; const lib3ReexportNgSummary = - lib3Gen.find(f => f.genFileUrl === '/lib3/reexport.ngsummary.ts') !; + lib3Gen.find(f => f.genFileUrl === '/lib3/reexport.ngsummary.ts')!; // ngsummary.ts files should use the external symbols which are manually re-exported from // "lib2" from their original symbol location. With re-exported external symbols this would diff --git a/packages/compiler/test/aot/regression_spec.ts b/packages/compiler/test/aot/regression_spec.ts index 8c768559a1..d7e74946db 100644 --- a/packages/compiler/test/aot/regression_spec.ts +++ b/packages/compiler/test/aot/regression_spec.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {MockDirectory, compile, expectNoDiagnostics, setup} from './test_util'; +import {compile, expectNoDiagnostics, MockDirectory, setup} from './test_util'; describe('regressions', () => { let angularFiles = setup(); diff --git a/packages/compiler/test/aot/static_reflector_spec.ts b/packages/compiler/test/aot/static_reflector_spec.ts index ced6e99bf1..5f382c0e17 100644 --- a/packages/compiler/test/aot/static_reflector_spec.ts +++ b/packages/compiler/test/aot/static_reflector_spec.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {StaticReflector, StaticSymbol, StaticSymbolCache, StaticSymbolResolver, StaticSymbolResolverHost, core as compilerCore} from '@angular/compiler'; +import {core as compilerCore, StaticReflector, StaticSymbol, StaticSymbolCache, StaticSymbolResolver, StaticSymbolResolverHost} from '@angular/compiler'; import {CollectorOptions, METADATA_VERSION} from '@angular/compiler-cli'; import {MockStaticSymbolResolverHost, MockSummaryResolver} from './static_symbol_resolver_spec'; @@ -358,7 +358,7 @@ describe('StaticReflector', () => { it('should record data about the error in the exception', () => { let threw = false; try { - const metadata = host.getMetadataFor('/tmp/src/invalid-metadata.ts') !; + const metadata = host.getMetadataFor('/tmp/src/invalid-metadata.ts')!; expect(metadata).toBeDefined(); const moduleMetadata: any = metadata[0]['metadata']; expect(moduleMetadata).toBeDefined(); @@ -1334,10 +1334,9 @@ const DEFAULT_TEST_DATA: {[key: string]: any} = { 'decorators': [{ '__symbolic': 'call', 'expression': {'__symbolic': 'reference', 'name': 'Directive', 'module': '@angular/core'}, - 'arguments': [{ - 'selector': '[ngFor][ngForOf]', - 'inputs': ['ngForTrackBy', 'ngForOf', 'ngForTemplate'] - }] + 'arguments': [ + {'selector': '[ngFor][ngForOf]', 'inputs': ['ngForTrackBy', 'ngForOf', 'ngForTemplate']} + ] }], 'members': { '__ctor__': [{ @@ -1345,11 +1344,8 @@ const DEFAULT_TEST_DATA: {[key: string]: any} = { 'parameters': [ {'__symbolic': 'reference', 'module': '@angular/core', 'name': 'ViewContainerRef'}, {'__symbolic': 'reference', 'module': '@angular/core', 'name': 'TemplateRef'}, - {'__symbolic': 'reference', 'module': '@angular/core', 'name': 'IterableDiffers'}, { - '__symbolic': 'reference', - 'module': '@angular/core', - 'name': 'ChangeDetectorRef' - } + {'__symbolic': 'reference', 'module': '@angular/core', 'name': 'IterableDiffers'}, + {'__symbolic': 'reference', 'module': '@angular/core', 'name': 'ChangeDetectorRef'} ] }] } @@ -1387,8 +1383,7 @@ const DEFAULT_TEST_DATA: {[key: string]: any} = { '__symbolic': 'property', 'decorators': [{ '__symbolic': 'call', - 'expression': - {'__symbolic': 'reference', 'name': 'Input', 'module': '@angular/core'} + 'expression': {'__symbolic': 'reference', 'name': 'Input', 'module': '@angular/core'} }] }], 'onMouseOver': [{ diff --git a/packages/compiler/test/aot/static_symbol_resolver_spec.ts b/packages/compiler/test/aot/static_symbol_resolver_spec.ts index 112adcc680..3e0edd7d70 100644 --- a/packages/compiler/test/aot/static_symbol_resolver_spec.ts +++ b/packages/compiler/test/aot/static_symbol_resolver_spec.ts @@ -19,7 +19,9 @@ describe('StaticSymbolResolver', () => { let symbolResolver: StaticSymbolResolver; let symbolCache: StaticSymbolCache; - beforeEach(() => { symbolCache = new StaticSymbolCache(); }); + beforeEach(() => { + symbolCache = new StaticSymbolCache(); + }); function init( testData: {[key: string]: any} = DEFAULT_TEST_DATA, summaries: Summary[] = [], @@ -36,7 +38,8 @@ describe('StaticSymbolResolver', () => { () => symbolResolver.resolveSymbol( symbolResolver.getSymbolByModule('src/version-error', 'e'))) .toThrow(new Error( - `Metadata version mismatch for module /tmp/src/version-error.d.ts, found version 100, expected ${METADATA_VERSION}`)); + `Metadata version mismatch for module /tmp/src/version-error.d.ts, found version 100, expected ${ + METADATA_VERSION}`)); }); it('should throw an exception for version 2 metadata', () => { @@ -159,7 +162,6 @@ describe('StaticSymbolResolver', () => { }); describe('importAs', () => { - it('should calculate importAs relationship for non source files without summaries', () => { init( { @@ -241,7 +243,6 @@ describe('StaticSymbolResolver', () => { expect(symbolResolver.getImportAs(symbolCache.get('/test2.d.ts', 'a', ['someMember']))) .toBe(symbolCache.get('/test3.d.ts', 'b', ['someMember'])); }); - }); it('should replace references by StaticSymbols', () => { @@ -345,10 +346,9 @@ describe('StaticSymbolResolver', () => { __symbolic: 'class', arity: 1, members: { - __ctor__: [{ - __symbolic: 'constructor', - parameters: [symbolCache.get('/test.d.ts', 'AParam')] - }] + __ctor__: [ + {__symbolic: 'constructor', parameters: [symbolCache.get('/test.d.ts', 'AParam')]} + ] } } } @@ -423,7 +423,6 @@ describe('StaticSymbolResolver', () => { expect(symbol.name).toEqual('One'); expect(symbol.filePath).toEqual('/tmp/src/reexport/src/origin1.d.ts'); }); - }); export class MockSummaryResolver implements SummaryResolver { @@ -431,9 +430,11 @@ export class MockSummaryResolver implements SummaryResolver { symbol: StaticSymbol, importAs: StaticSymbol }[] = []) {} - addSummary(summary: Summary) { this.summaries.push(summary); } + addSummary(summary: Summary) { + this.summaries.push(summary); + } resolveSummary(reference: StaticSymbol): Summary { - return this.summaries.find(summary => summary.symbol === reference) !; + return this.summaries.find(summary => summary.symbol === reference)!; } getSymbolsOf(filePath: string): StaticSymbol[]|null { const symbols = this.summaries.filter(summary => summary.symbol.filePath === filePath) @@ -442,12 +443,20 @@ export class MockSummaryResolver implements SummaryResolver { } getImportAs(symbol: StaticSymbol): StaticSymbol { const entry = this.importAs.find(entry => entry.symbol === symbol); - return entry ? entry.importAs : undefined !; + return entry ? entry.importAs : undefined!; + } + getKnownModuleName(fileName: string): string|null { + return null; + } + isLibraryFile(filePath: string): boolean { + return filePath.endsWith('.d.ts'); + } + toSummaryFileName(filePath: string): string { + return filePath.replace(/(\.d)?\.ts$/, '.d.ts'); + } + fromSummaryFileName(filePath: string): string { + return filePath; } - getKnownModuleName(fileName: string): string|null { return null; } - isLibraryFile(filePath: string): boolean { return filePath.endsWith('.d.ts'); } - toSummaryFileName(filePath: string): string { return filePath.replace(/(\.d)?\.ts$/, '.d.ts'); } - fromSummaryFileName(filePath: string): string { return filePath; } } export class MockStaticSymbolResolverHost implements StaticSymbolResolverHost { @@ -459,7 +468,9 @@ export class MockStaticSymbolResolverHost implements StaticSymbolResolverHost { // In tests, assume that symbols are not re-exported moduleNameToFileName(modulePath: string, containingFile?: string): string { - function splitPath(path: string): string[] { return path.split(/\/|\\/g); } + function splitPath(path: string): string[] { + return path.split(/\/|\\/g); + } function resolvePath(pathParts: string[]): string { const result: string[] = []; @@ -490,7 +501,7 @@ export class MockStaticSymbolResolverHost implements StaticSymbolResolverHost { } if (modulePath.indexOf('.') === 0) { - const baseName = pathTo(containingFile !, modulePath); + const baseName = pathTo(containingFile!, modulePath); const tsName = baseName + '.ts'; if (this._getMetadataFor(tsName)) { return tsName; @@ -498,14 +509,18 @@ export class MockStaticSymbolResolverHost implements StaticSymbolResolverHost { return baseName + '.d.ts'; } if (modulePath == 'unresolved') { - return undefined !; + return undefined!; } return '/tmp/' + modulePath + '.d.ts'; } - getMetadataFor(moduleId: string): any { return this._getMetadataFor(moduleId); } + getMetadataFor(moduleId: string): any { + return this._getMetadataFor(moduleId); + } - getOutputName(filePath: string): string { return filePath; } + getOutputName(filePath: string): string { + return filePath; + } private _getMetadataFor(filePath: string): any { if (this.data[filePath] && filePath.match(TS_EXT)) { @@ -515,13 +530,13 @@ export class MockStaticSymbolResolverHost implements StaticSymbolResolverHost { filePath, this.data[filePath], ts.ScriptTarget.ES5, /* setParentNodes */ true); const diagnostics: ts.Diagnostic[] = (sf).parseDiagnostics; if (diagnostics && diagnostics.length) { - const errors = - diagnostics - .map(d => { - const {line, character} = ts.getLineAndCharacterOfPosition(d.file !, d.start !); - return `(${line}:${character}): ${d.messageText}`; - }) - .join('\n'); + const errors = diagnostics + .map(d => { + const {line, character} = + ts.getLineAndCharacterOfPosition(d.file!, d.start!); + return `(${line}:${character}): ${d.messageText}`; + }) + .join('\n'); throw Error(`Error encountered during parse of file ${filePath}\n${errors}`); } return [this.collector.getMetadata(sf)]; diff --git a/packages/compiler/test/aot/summary_resolver_spec.ts b/packages/compiler/test/aot/summary_resolver_spec.ts index d518319c2d..b4e416b7e2 100644 --- a/packages/compiler/test/aot/summary_resolver_spec.ts +++ b/packages/compiler/test/aot/summary_resolver_spec.ts @@ -23,7 +23,9 @@ const EXT = /(\.d)?\.ts$/; let symbolCache: StaticSymbolCache; let host: MockAotSummaryResolverHost; - beforeEach(() => { symbolCache = new StaticSymbolCache(); }); + beforeEach(() => { + symbolCache = new StaticSymbolCache(); + }); function init(summaries: {[filePath: string]: string} = {}) { host = new MockAotSummaryResolverHost(summaries); @@ -121,11 +123,17 @@ export class MockAotSummaryResolverHost implements AotSummaryResolverHost { return sourceFileName.replace(EXT, '') + '.d.ts'; } - fromSummaryFileName(filePath: string): string { return filePath; } + fromSummaryFileName(filePath: string): string { + return filePath; + } - isSourceFile(filePath: string) { return !filePath.endsWith('.d.ts'); } + isSourceFile(filePath: string) { + return !filePath.endsWith('.d.ts'); + } - loadSummary(filePath: string): string { return this.summaries[filePath]; } + loadSummary(filePath: string): string { + return this.summaries[filePath]; + } } export function createMockOutputContext(): OutputContext { diff --git a/packages/compiler/test/aot/summary_serializer_spec.ts b/packages/compiler/test/aot/summary_serializer_spec.ts index 3410c60921..fb9bc57111 100644 --- a/packages/compiler/test/aot/summary_serializer_spec.ts +++ b/packages/compiler/test/aot/summary_serializer_spec.ts @@ -12,7 +12,7 @@ import {deserializeSummaries, serializeSummaries} from '@angular/compiler/src/ao import {summaryFileName} from '@angular/compiler/src/aot/util'; import {MockStaticSymbolResolverHost} from './static_symbol_resolver_spec'; -import {MockAotSummaryResolverHost, createMockOutputContext} from './summary_resolver_spec'; +import {createMockOutputContext, MockAotSummaryResolverHost} from './summary_resolver_spec'; { @@ -22,7 +22,9 @@ import {MockAotSummaryResolverHost, createMockOutputContext} from './summary_res let symbolCache: StaticSymbolCache; let host: MockAotSummaryResolverHost; - beforeEach(() => { symbolCache = new StaticSymbolCache(); }); + beforeEach(() => { + symbolCache = new StaticSymbolCache(); + }); function init( summaries: {[filePath: string]: string} = {}, metadata: {[key: string]: any} = {}) { @@ -101,7 +103,7 @@ import {MockAotSummaryResolverHost, createMockOutputContext} from './summary_res members: {aMethod: {__symbolic: 'function'}}, statics: {aStatic: true} }); - expect(summaries[1].type !.type.reference) + expect(summaries[1].type!.type.reference) .toBe(symbolCache.get('/tmp/some_service.d.ts', 'SomeService')); }); @@ -274,7 +276,7 @@ import {MockAotSummaryResolverHost, createMockOutputContext} from './summary_res '/tmp/external_svc.d.ts', 'SomeService')]); // SomService is a transitive dep, but should have been serialized as well. expect(summaries[2].symbol).toBe(symbolCache.get('/tmp/external_svc.d.ts', 'SomeService')); - expect(summaries[2].type !.type.reference) + expect(summaries[2].type!.type.reference) .toBe(symbolCache.get('/tmp/external_svc.d.ts', 'SomeService')); // there was no summary for non_summary, but it should have // been serialized as well. @@ -387,7 +389,6 @@ import {MockAotSummaryResolverHost, createMockOutputContext} from './summary_res describe('symbol re-exports enabled', () => { - it('should not create "importAs" names for ctor arguments which are types of reexported classes in libraries', () => { init(); diff --git a/packages/compiler/test/aot/test_util.ts b/packages/compiler/test/aot/test_util.ts index f516d6f300..112deff7e0 100644 --- a/packages/compiler/test/aot/test_util.ts +++ b/packages/compiler/test/aot/test_util.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {AotCompilerHost, AotCompilerOptions, GeneratedFile, createAotCompiler, toTypeScript} from '@angular/compiler'; +import {AotCompilerHost, AotCompilerOptions, createAotCompiler, GeneratedFile, toTypeScript} from '@angular/compiler'; import {MetadataBundlerHost} from '@angular/compiler-cli/src/metadata/bundler'; import {MetadataCollector} from '@angular/compiler-cli/src/metadata/collector'; import {ModuleMetadata} from '@angular/compiler-cli/src/metadata/index'; @@ -15,7 +15,9 @@ import * as fs from 'fs'; import * as path from 'path'; import * as ts from 'typescript'; -export interface MetadataProvider { getMetadata(source: ts.SourceFile): ModuleMetadata|undefined; } +export interface MetadataProvider { + getMetadata(source: ts.SourceFile): ModuleMetadata|undefined; +} let nodeModulesPath: string; let angularSourcePath: string; @@ -23,13 +25,13 @@ let rootPath: string; calcPathsOnDisc(); -export type MockFileOrDirectory = string | MockDirectory; +export type MockFileOrDirectory = string|MockDirectory; export type MockDirectory = { - [name: string]: MockFileOrDirectory | undefined; + [name: string]: MockFileOrDirectory|undefined; }; -export function isDirectory(data: MockFileOrDirectory | undefined): data is MockDirectory { +export function isDirectory(data: MockFileOrDirectory|undefined): data is MockDirectory { return typeof data !== 'string'; } @@ -119,9 +121,13 @@ export class EmittingCompilerHost implements ts.CompilerHost { return Array.from(this.writtenFiles).map(f => ({name: f[0], content: f[1]})); } - public get scripts(): string[] { return this.scriptNames; } + public get scripts(): string[] { + return this.scriptNames; + } - public get written(): Map { return this.writtenFiles; } + public get written(): Map { + return this.writtenFiles; + } public effectiveName(fileName: string): string { const prefix = '@angular/'; @@ -154,7 +160,9 @@ export class EmittingCompilerHost implements ts.CompilerHost { (fs.existsSync(directoryName) && fs.statSync(directoryName).isDirectory()); } - getCurrentDirectory(): string { return this.root; } + getCurrentDirectory(): string { + return this.root; + } getDirectories(dir: string): string[] { const result = open(dir, this.options.mockData); @@ -179,7 +187,9 @@ export class EmittingCompilerHost implements ts.CompilerHost { throw new Error(`File not found '${fileName}'.`); } - getDefaultLibFileName(options: ts.CompilerOptions): string { return 'lib.d.ts'; } + getDefaultLibFileName(options: ts.CompilerOptions): string { + return 'lib.d.ts'; + } writeFile: ts.WriteFileCallback = (fileName: string, data: string, writeByteOrderMark: boolean, @@ -197,8 +207,12 @@ export class EmittingCompilerHost implements ts.CompilerHost { getCanonicalFileName(fileName: string): string { return fileName; } - useCaseSensitiveFileNames(): boolean { return false; } - getNewLine(): string { return '\n'; } + useCaseSensitiveFileNames(): boolean { + return false; + } + getNewLine(): string { + return '\n'; + } private getAddedDirectories(): Set { let result = this.cachedAddedDirectories; @@ -247,7 +261,9 @@ export class MockCompilerHost implements ts.CompilerHost { this.sourceFiles.delete(fileName); } - assumeFileExists(fileName: string) { this.assumeExists.add(fileName); } + assumeFileExists(fileName: string) { + this.assumeExists.add(fileName); + } remove(files: string[]) { // Remove the files from the list of scripts. @@ -274,11 +290,17 @@ export class MockCompilerHost implements ts.CompilerHost { return false; } - readFile(fileName: string): string { return this.getFileContent(fileName) !; } + readFile(fileName: string): string { + return this.getFileContent(fileName)!; + } - trace(s: string): void { this.traces.push(s); } + trace(s: string): void { + this.traces.push(s); + } - getCurrentDirectory(): string { return '/'; } + getCurrentDirectory(): string { + return '/'; + } getDirectories(dir: string): string[] { const effectiveName = this.getEffectiveName(dir); @@ -303,10 +325,12 @@ export class MockCompilerHost implements ts.CompilerHost { this.sourceFiles.set(fileName, result); } } - return result !; + return result!; } - getDefaultLibFileName(options: ts.CompilerOptions): string { return 'lib.d.ts'; } + getDefaultLibFileName(options: ts.CompilerOptions): string { + return 'lib.d.ts'; + } writeFile: ts.WriteFileCallback = (fileName: string, data: string, writeByteOrderMark: boolean) => { @@ -317,8 +341,12 @@ export class MockCompilerHost implements ts.CompilerHost { getCanonicalFileName(fileName: string): string { return fileName; } - useCaseSensitiveFileNames(): boolean { return false; } - getNewLine(): string { return '\n'; } + useCaseSensitiveFileNames(): boolean { + return false; + } + getNewLine(): string { + return '\n'; + } // Private methods private getFileContent(fileName: string): string|undefined { @@ -373,9 +401,13 @@ export class MockAotCompilerHost implements AotCompilerHost { }; } - hideMetadata() { this.metadataVisible = false; } + hideMetadata() { + this.metadataVisible = false; + } - tsFilesOnly() { this.dtsAreSource = false; } + tsFilesOnly() { + this.dtsAreSource = false; + } // StaticSymbolResolverHost getMetadataFor(modulePath: string): {[key: string]: any}[]|undefined { @@ -414,7 +446,9 @@ export class MockAotCompilerHost implements AotCompilerHost { return resolved ? resolved.resolvedFileName : null; } - getOutputName(filePath: string) { return filePath; } + getOutputName(filePath: string) { + return filePath; + } resourceNameToFileName(resourceName: string, containingFile: string) { // Note: we convert package paths into relative paths to be compatible with the the @@ -428,16 +462,22 @@ export class MockAotCompilerHost implements AotCompilerHost { } // AotSummaryResolverHost - loadSummary(filePath: string): string|null { return this.tsHost.readFile(filePath); } + loadSummary(filePath: string): string|null { + return this.tsHost.readFile(filePath); + } isSourceFile(sourceFilePath: string): boolean { return !GENERATED_FILES.test(sourceFilePath) && (this.dtsAreSource || !DTS.test(sourceFilePath)); } - toSummaryFileName(filePath: string): string { return filePath.replace(EXT, '') + '.d.ts'; } + toSummaryFileName(filePath: string): string { + return filePath.replace(EXT, '') + '.d.ts'; + } - fromSummaryFileName(filePath: string): string { return filePath; } + fromSummaryFileName(filePath: string): string { + return filePath; + } // AotCompilerHost fileNameToModuleName(importedFile: string, containingFile: string): string { @@ -464,7 +504,7 @@ export class MockMetadataBundlerHost implements MetadataBundlerHost { } } -function find(fileName: string, data: MockFileOrDirectory | undefined): MockFileOrDirectory| +function find(fileName: string, data: MockFileOrDirectory|undefined): MockFileOrDirectory| undefined { if (!data) return undefined; const names = fileName.split('/'); @@ -479,7 +519,7 @@ function find(fileName: string, data: MockFileOrDirectory | undefined): MockFile return current; } -function open(fileName: string, data: MockFileOrDirectory | undefined): string|undefined { +function open(fileName: string, data: MockFileOrDirectory|undefined): string|undefined { let result = find(fileName, data); if (typeof result === 'string') { return result; @@ -487,7 +527,7 @@ function open(fileName: string, data: MockFileOrDirectory | undefined): string|u return undefined; } -function directoryExists(dirname: string, data: MockFileOrDirectory | undefined): boolean { +function directoryExists(dirname: string, data: MockFileOrDirectory|undefined): boolean { let result = find(dirname, data); return !!result && typeof result !== 'string'; } @@ -497,7 +537,7 @@ export type MockFileArray = { content: string }[]; -export type MockData = MockDirectory | Map| (MockDirectory | Map)[]; +export type MockData = MockDirectory|Map|(MockDirectory|Map)[]; export function toMockFileArray(data: MockData, target: MockFileArray = []): MockFileArray { if (data instanceof Map) { @@ -512,7 +552,7 @@ export function toMockFileArray(data: MockData, target: MockFileArray = []): Moc function mockDirToFileArray(dir: MockDirectory, path: string, target: MockFileArray) { Object.keys(dir).forEach((localFileName) => { - const value = dir[localFileName] !; + const value = dir[localFileName]!; const fileName = `${path}/${localFileName}`; if (typeof value === 'string') { target.push({fileName, content: value}); @@ -523,12 +563,16 @@ function mockDirToFileArray(dir: MockDirectory, path: string, target: MockFileAr } function mapToMockFileArray(files: Map, target: MockFileArray) { - files.forEach((content, fileName) => { target.push({fileName, content}); }); + files.forEach((content, fileName) => { + target.push({fileName, content}); + }); } export function arrayToMockMap(arr: MockFileArray): Map { const map = new Map(); - arr.forEach(({fileName, content}) => { map.set(fileName, content); }); + arr.forEach(({fileName, content}) => { + map.set(fileName, content); + }); return map; } @@ -594,8 +638,8 @@ function readBazelWrittenFilesFrom( map.set(path.posix.join('/node_modules/@angular', packageName, 'index.d.ts'), content); } } catch (e) { - console.error( - `Consider adding //packages/${packageName} as a data dependency in the BUILD.bazel rule for the failing test`); + console.error(`Consider adding //packages/${ + packageName} as a data dependency in the BUILD.bazel rule for the failing test`); throw e; } } @@ -606,8 +650,8 @@ export function isInBazel(): boolean { export function setup(options: { compileAngular: boolean, - compileFakeCore?: boolean, - compileAnimations: boolean, compileCommon?: boolean + compileFakeCore?: boolean, compileAnimations: boolean, + compileCommon?: boolean } = { compileAngular: true, compileAnimations: true, @@ -687,7 +731,9 @@ export function expectNoDiagnostics(program: ts.Program) { return ''; } - function chars(len: number, ch: string): string { return newArray(len, ch).join(''); } + function chars(len: number, ch: string): string { + return newArray(len, ch).join(''); + } function lineNoOf(offset: number, text: string): number { let result = 1; @@ -699,8 +745,8 @@ export function expectNoDiagnostics(program: ts.Program) { function lineInfo(diagnostic: ts.Diagnostic): string { if (diagnostic.file) { - const start = diagnostic.start !; - let end = diagnostic.start ! + diagnostic.length !; + const start = diagnostic.start!; + let end = diagnostic.start! + diagnostic.length!; const source = diagnostic.file.text; let lineStart = start; let lineEnd = end; @@ -726,8 +772,8 @@ export function expectNoDiagnostics(program: ts.Program) { 'Errors from TypeScript:\n' + diagnostics .map( - d => - `${fileInfo(d)}${ts.flattenDiagnosticMessageText(d.messageText, '\n')}${lineInfo(d)}`) + d => `${fileInfo(d)}${ts.flattenDiagnosticMessageText(d.messageText, '\n')}${ + lineInfo(d)}`) .join(' \n')); } } @@ -758,7 +804,7 @@ export function compile( useSummaries?: boolean, preCompile?: (program: ts.Program) => void, postCompile?: (program: ts.Program) => void, - }& AotCompilerOptions = {}, + }&AotCompilerOptions = {}, tsOptions: ts.CompilerOptions = {}): {genFiles: GeneratedFile[], outDir: MockDirectory} { // when using summaries, always emit so the next step can use the results. const emit = options.emit || options.useSummaries; @@ -777,7 +823,9 @@ export function compile( const tsSettings = {...settings, ...tsOptions}; const program = ts.createProgram([...host.scriptNames], tsSettings, host); preCompile(program); - const {compiler, reflector} = createAotCompiler(aotHost, options, (err) => { throw err; }); + const {compiler, reflector} = createAotCompiler(aotHost, options, (err) => { + throw err; + }); const analyzedModules = compiler.analyzeModulesSync(program.getSourceFiles().map(sf => sf.fileName)); const genFiles = compiler.emitAllImpls(analyzedModules); diff --git a/packages/compiler/test/compiler_facade_interface_spec.ts b/packages/compiler/test/compiler_facade_interface_spec.ts index dcb97afd31..fd58bc9d91 100644 --- a/packages/compiler/test/compiler_facade_interface_spec.ts +++ b/packages/compiler/test/compiler_facade_interface_spec.ts @@ -26,87 +26,87 @@ import * as compiler from '../src/compiler_facade_interface'; */ const coreExportedCompilerFacade1: core.ExportedCompilerFacade = - null !as compiler.ExportedCompilerFacade; + null! as compiler.ExportedCompilerFacade; const compilerExportedCompilerFacade2: compiler.ExportedCompilerFacade = - null !as core.ExportedCompilerFacade; + null! as core.ExportedCompilerFacade; -const coreCompilerFacade: core.CompilerFacade = null !as compiler.CompilerFacade; -const compilerCompilerFacade: compiler.CompilerFacade = null !as core.CompilerFacade; +const coreCompilerFacade: core.CompilerFacade = null! as compiler.CompilerFacade; +const compilerCompilerFacade: compiler.CompilerFacade = null! as core.CompilerFacade; -const coreCoreEnvironment: core.CoreEnvironment = null !as compiler.CoreEnvironment; -const compilerCoreEnvironment: compiler.CoreEnvironment = null !as core.CoreEnvironment; +const coreCoreEnvironment: core.CoreEnvironment = null! as compiler.CoreEnvironment; +const compilerCoreEnvironment: compiler.CoreEnvironment = null! as core.CoreEnvironment; -const coreResourceLoader: core.ResourceLoader = null !as compiler.ResourceLoader; -const compilerResourceLoader: compiler.ResourceLoader = null !as core.ResourceLoader; +const coreResourceLoader: core.ResourceLoader = null! as compiler.ResourceLoader; +const compilerResourceLoader: compiler.ResourceLoader = null! as core.ResourceLoader; -const coreStringMap: core.StringMap = null !as compiler.StringMap; -const compilerStringMap: compiler.StringMap = null !as core.StringMap; +const coreStringMap: core.StringMap = null! as compiler.StringMap; +const compilerStringMap: compiler.StringMap = null! as core.StringMap; -const coreProvider: core.Provider = null !as compiler.Provider; -const compilerProvider: compiler.Provider = null !as core.Provider; +const coreProvider: core.Provider = null! as compiler.Provider; +const compilerProvider: compiler.Provider = null! as core.Provider; const coreR3ResolvedDependencyType: core.R3ResolvedDependencyType = - null !as compiler.R3ResolvedDependencyType; + null! as compiler.R3ResolvedDependencyType; const compilerR3ResolvedDependencyType: compiler.R3ResolvedDependencyType = - null !as core.R3ResolvedDependencyType; + null! as core.R3ResolvedDependencyType; const coreR3ResolvedDependencyType2: R3ResolvedDependencyType = - null !as core.R3ResolvedDependencyType; + null! as core.R3ResolvedDependencyType; const compilerR3ResolvedDependencyType2: R3ResolvedDependencyType = - null !as core.R3ResolvedDependencyType; + null! as core.R3ResolvedDependencyType; const coreR3ResolvedDependencyType3: core.R3ResolvedDependencyType = - null !as R3ResolvedDependencyType; + null! as R3ResolvedDependencyType; const compilerR3ResolvedDependencyType3: compiler.R3ResolvedDependencyType = - null !as R3ResolvedDependencyType; + null! as R3ResolvedDependencyType; -const coreR3FactoryTarget: core.R3FactoryTarget = null !as compiler.R3FactoryTarget; -const compilerR3FactoryTarget: compiler.R3FactoryTarget = null !as core.R3FactoryTarget; +const coreR3FactoryTarget: core.R3FactoryTarget = null! as compiler.R3FactoryTarget; +const compilerR3FactoryTarget: compiler.R3FactoryTarget = null! as core.R3FactoryTarget; -const coreR3FactoryTarget2: R3FactoryTarget = null !as core.R3FactoryTarget; -const compilerR3FactoryTarget2: R3FactoryTarget = null !as core.R3FactoryTarget; +const coreR3FactoryTarget2: R3FactoryTarget = null! as core.R3FactoryTarget; +const compilerR3FactoryTarget2: R3FactoryTarget = null! as core.R3FactoryTarget; -const coreR3FactoryTarget3: core.R3FactoryTarget = null !as R3FactoryTarget; -const compilerR3FactoryTarget3: compiler.R3FactoryTarget = null !as R3FactoryTarget; +const coreR3FactoryTarget3: core.R3FactoryTarget = null! as R3FactoryTarget; +const compilerR3FactoryTarget3: compiler.R3FactoryTarget = null! as R3FactoryTarget; const coreR3DependencyMetadataFacade: core.R3DependencyMetadataFacade = - null !as compiler.R3DependencyMetadataFacade; + null! as compiler.R3DependencyMetadataFacade; const compilerR3DependencyMetadataFacade: compiler.R3DependencyMetadataFacade = - null !as core.R3DependencyMetadataFacade; + null! as core.R3DependencyMetadataFacade; -const coreR3PipeMetadataFacade: core.R3PipeMetadataFacade = null !as compiler.R3PipeMetadataFacade; +const coreR3PipeMetadataFacade: core.R3PipeMetadataFacade = null! as compiler.R3PipeMetadataFacade; const compilerR3PipeMetadataFacade: compiler.R3PipeMetadataFacade = - null !as core.R3PipeMetadataFacade; + null! as core.R3PipeMetadataFacade; const coreR3InjectableMetadataFacade: core.R3InjectableMetadataFacade = - null !as compiler.R3InjectableMetadataFacade; + null! as compiler.R3InjectableMetadataFacade; const compilerR3InjectableMetadataFacade: compiler.R3InjectableMetadataFacade = - null !as core.R3InjectableMetadataFacade; + null! as core.R3InjectableMetadataFacade; const coreR3NgModuleMetadataFacade: core.R3NgModuleMetadataFacade = - null !as compiler.R3NgModuleMetadataFacade; + null! as compiler.R3NgModuleMetadataFacade; const compilerR3NgModuleMetadataFacade: compiler.R3NgModuleMetadataFacade = - null !as core.R3NgModuleMetadataFacade; + null! as core.R3NgModuleMetadataFacade; const coreR3InjectorMetadataFacade: core.R3InjectorMetadataFacade = - null !as compiler.R3InjectorMetadataFacade; + null! as compiler.R3InjectorMetadataFacade; const compilerR3InjectorMetadataFacade: compiler.R3InjectorMetadataFacade = - null !as core.R3InjectorMetadataFacade; + null! as core.R3InjectorMetadataFacade; const coreR3DirectiveMetadataFacade: core.R3DirectiveMetadataFacade = - null !as compiler.R3DirectiveMetadataFacade; + null! as compiler.R3DirectiveMetadataFacade; const compilerR3DirectiveMetadataFacade: compiler.R3DirectiveMetadataFacade = - null !as core.R3DirectiveMetadataFacade; + null! as core.R3DirectiveMetadataFacade; const coreR3ComponentMetadataFacade: core.R3ComponentMetadataFacade = - null !as compiler.R3ComponentMetadataFacade; + null! as compiler.R3ComponentMetadataFacade; const compilerR3ComponentMetadataFacade: compiler.R3ComponentMetadataFacade = - null !as core.R3ComponentMetadataFacade; + null! as core.R3ComponentMetadataFacade; -const coreViewEncapsulation: core.ViewEncapsulation = null !as compiler.ViewEncapsulation; -const compilerViewEncapsulation: compiler.ViewEncapsulation = null !as core.ViewEncapsulation; +const coreViewEncapsulation: core.ViewEncapsulation = null! as compiler.ViewEncapsulation; +const compilerViewEncapsulation: compiler.ViewEncapsulation = null! as core.ViewEncapsulation; const coreR3QueryMetadataFacade: core.R3QueryMetadataFacade = - null !as compiler.R3QueryMetadataFacade; + null! as compiler.R3QueryMetadataFacade; const compilerR3QueryMetadataFacade: compiler.R3QueryMetadataFacade = - null !as core.R3QueryMetadataFacade; + null! as core.R3QueryMetadataFacade; diff --git a/packages/compiler/test/config_spec.ts b/packages/compiler/test/config_spec.ts index 459e80cf60..5126941759 100644 --- a/packages/compiler/test/config_spec.ts +++ b/packages/compiler/test/config_spec.ts @@ -19,7 +19,9 @@ import {CompilerConfig, preserveWhitespacesDefault} from '../src/config'; describe('preserveWhitespacesDefault', () => { it('should return the default `false` setting when no preserveWhitespacesOption are provided', - () => { expect(preserveWhitespacesDefault(null)).toEqual(false); }); + () => { + expect(preserveWhitespacesDefault(null)).toEqual(false); + }); it('should return the preserveWhitespacesOption when provided as a parameter', () => { expect(preserveWhitespacesDefault(true)).toEqual(true); expect(preserveWhitespacesDefault(false)).toEqual(false); diff --git a/packages/compiler/test/core_spec.ts b/packages/compiler/test/core_spec.ts index f5864584cc..e198db2e8d 100644 --- a/packages/compiler/test/core_spec.ts +++ b/packages/compiler/test/core_spec.ts @@ -190,7 +190,9 @@ import * as core from '@angular/core'; function compareRuntimeShape(a: any, b: any) { const keys = metadataKeys(a); expect(keys).toEqual(metadataKeys(b)); - keys.forEach(key => { expect(a[key]).toBe(b[key]); }); + keys.forEach(key => { + expect(a[key]).toBe(b[key]); + }); // Need to check 'ngMetadataName' separately, as this is // on the prototype in @angular/core, but a regular property in @angular/compiler. expect(a.ngMetadataName).toBe(b.ngMetadataName); diff --git a/packages/compiler/test/css_parser/css_lexer_spec.ts b/packages/compiler/test/css_parser/css_lexer_spec.ts index 64a44d6c64..96835c809a 100644 --- a/packages/compiler/test/css_parser/css_lexer_spec.ts +++ b/packages/compiler/test/css_parser/css_lexer_spec.ts @@ -7,370 +7,381 @@ */ import {describe, expect, it} from '../../../core/testing/src/testing_internal'; -import {CssLexer, CssLexerMode, CssToken, CssTokenType, cssScannerError, getRawMessage, getToken} from '../../src/css_parser/css_lexer'; +import {CssLexer, CssLexerMode, cssScannerError, CssToken, CssTokenType, getRawMessage, getToken} from '../../src/css_parser/css_lexer'; (function() { - function tokenize( - code: string, trackComments: boolean = false, - mode: CssLexerMode = CssLexerMode.ALL): CssToken[] { - const scanner = new CssLexer().scan(code, trackComments); - scanner.setMode(mode); +function tokenize( + code: string, trackComments: boolean = false, + mode: CssLexerMode = CssLexerMode.ALL): CssToken[] { + const scanner = new CssLexer().scan(code, trackComments); + scanner.setMode(mode); - const tokens: CssToken[] = []; - let output = scanner.scan(); - while (output != null) { - const error = output.error; - if (error != null) { - throw cssScannerError(getToken(error), getRawMessage(error)); - } - tokens.push(output.token); - output = scanner.scan(); + const tokens: CssToken[] = []; + let output = scanner.scan(); + while (output != null) { + const error = output.error; + if (error != null) { + throw cssScannerError(getToken(error), getRawMessage(error)); } - - return tokens; + tokens.push(output.token); + output = scanner.scan(); } - describe('CssLexer', () => { - it('should lex newline characters as whitespace when whitespace mode is on', () => { - const newlines = ['\n', '\r\n', '\r', '\f']; - newlines.forEach((line) => { - const token = tokenize(line, false, CssLexerMode.ALL_TRACK_WS)[0]; - expect(token.type).toEqual(CssTokenType.Whitespace); - }); - }); + return tokens; +} - it('should combined newline characters as one newline token when whitespace mode is on', () => { - const newlines = ['\n', '\r\n', '\r', '\f'].join(''); - const tokens = tokenize(newlines, false, CssLexerMode.ALL_TRACK_WS); - expect(tokens.length).toEqual(1); - expect(tokens[0].type).toEqual(CssTokenType.Whitespace); +describe('CssLexer', () => { + it('should lex newline characters as whitespace when whitespace mode is on', () => { + const newlines = ['\n', '\r\n', '\r', '\f']; + newlines.forEach((line) => { + const token = tokenize(line, false, CssLexerMode.ALL_TRACK_WS)[0]; + expect(token.type).toEqual(CssTokenType.Whitespace); }); + }); - it('should not consider whitespace or newline values at all when whitespace mode is off', + it('should combined newline characters as one newline token when whitespace mode is on', () => { + const newlines = ['\n', '\r\n', '\r', '\f'].join(''); + const tokens = tokenize(newlines, false, CssLexerMode.ALL_TRACK_WS); + expect(tokens.length).toEqual(1); + expect(tokens[0].type).toEqual(CssTokenType.Whitespace); + }); + + it('should not consider whitespace or newline values at all when whitespace mode is off', () => { + const newlines = ['\n', '\r\n', '\r', '\f'].join(''); + const tokens = tokenize(newlines); + expect(tokens.length).toEqual(0); + }); + + it('should lex simple selectors and their inner properties', () => { + const cssCode = '\n' + + ' .selector { my-prop: my-value; }\n'; + const tokens = tokenize(cssCode); + + expect(tokens[0].type).toEqual(CssTokenType.Character); + expect(tokens[0].strValue).toEqual('.'); + + expect(tokens[1].type).toEqual(CssTokenType.Identifier); + expect(tokens[1].strValue).toEqual('selector'); + + expect(tokens[2].type).toEqual(CssTokenType.Character); + expect(tokens[2].strValue).toEqual('{'); + + expect(tokens[3].type).toEqual(CssTokenType.Identifier); + expect(tokens[3].strValue).toEqual('my-prop'); + + expect(tokens[4].type).toEqual(CssTokenType.Character); + expect(tokens[4].strValue).toEqual(':'); + + expect(tokens[5].type).toEqual(CssTokenType.Identifier); + expect(tokens[5].strValue).toEqual('my-value'); + + expect(tokens[6].type).toEqual(CssTokenType.Character); + expect(tokens[6].strValue).toEqual(';'); + + expect(tokens[7].type).toEqual(CssTokenType.Character); + expect(tokens[7].strValue).toEqual('}'); + }); + + it('should capture the column and line values for each token', () => { + const cssCode = '#id {\n' + + ' prop:value;\n' + + '}'; + + const tokens = tokenize(cssCode); + + // # + expect(tokens[0].type).toEqual(CssTokenType.Character); + expect(tokens[0].column).toEqual(0); + expect(tokens[0].line).toEqual(0); + + // id + expect(tokens[1].type).toEqual(CssTokenType.Identifier); + expect(tokens[1].column).toEqual(1); + expect(tokens[1].line).toEqual(0); + + // { + expect(tokens[2].type).toEqual(CssTokenType.Character); + expect(tokens[2].column).toEqual(4); + expect(tokens[2].line).toEqual(0); + + // prop + expect(tokens[3].type).toEqual(CssTokenType.Identifier); + expect(tokens[3].column).toEqual(2); + expect(tokens[3].line).toEqual(1); + + // : + expect(tokens[4].type).toEqual(CssTokenType.Character); + expect(tokens[4].column).toEqual(6); + expect(tokens[4].line).toEqual(1); + + // value + expect(tokens[5].type).toEqual(CssTokenType.Identifier); + expect(tokens[5].column).toEqual(7); + expect(tokens[5].line).toEqual(1); + + // ; + expect(tokens[6].type).toEqual(CssTokenType.Character); + expect(tokens[6].column).toEqual(12); + expect(tokens[6].line).toEqual(1); + + // } + expect(tokens[7].type).toEqual(CssTokenType.Character); + expect(tokens[7].column).toEqual(0); + expect(tokens[7].line).toEqual(2); + }); + + it('should lex quoted strings and escape accordingly', () => { + const cssCode = 'prop: \'some { value } \\\' that is quoted\''; + const tokens = tokenize(cssCode); + + expect(tokens[0].type).toEqual(CssTokenType.Identifier); + expect(tokens[1].type).toEqual(CssTokenType.Character); + expect(tokens[2].type).toEqual(CssTokenType.String); + expect(tokens[2].strValue).toEqual('\'some { value } \\\' that is quoted\''); + }); + + it('should treat attribute operators as regular characters', () => { + tokenize('^|~+*').forEach((token) => { + expect(token.type).toEqual(CssTokenType.Character); + }); + }); + + it('should lex numbers properly and set them as numbers', () => { + const cssCode = '0 1 -2 3.0 -4.001'; + const tokens = tokenize(cssCode); + + expect(tokens[0].type).toEqual(CssTokenType.Number); + expect(tokens[0].strValue).toEqual('0'); + + expect(tokens[1].type).toEqual(CssTokenType.Number); + expect(tokens[1].strValue).toEqual('1'); + + expect(tokens[2].type).toEqual(CssTokenType.Number); + expect(tokens[2].strValue).toEqual('-2'); + + expect(tokens[3].type).toEqual(CssTokenType.Number); + expect(tokens[3].strValue).toEqual('3.0'); + + expect(tokens[4].type).toEqual(CssTokenType.Number); + expect(tokens[4].strValue).toEqual('-4.001'); + }); + + it('should lex @keywords', () => { + const cssCode = '@import()@something'; + const tokens = tokenize(cssCode); + + expect(tokens[0].type).toEqual(CssTokenType.AtKeyword); + expect(tokens[0].strValue).toEqual('@import'); + + expect(tokens[1].type).toEqual(CssTokenType.Character); + expect(tokens[1].strValue).toEqual('('); + + expect(tokens[2].type).toEqual(CssTokenType.Character); + expect(tokens[2].strValue).toEqual(')'); + + expect(tokens[3].type).toEqual(CssTokenType.AtKeyword); + expect(tokens[3].strValue).toEqual('@something'); + }); + + it('should still lex a number even if it has a dimension suffix', () => { + const cssCode = '40% is 40 percent'; + const tokens = tokenize(cssCode); + + expect(tokens[0].type).toEqual(CssTokenType.Number); + expect(tokens[0].strValue).toEqual('40'); + + expect(tokens[1].type).toEqual(CssTokenType.Character); + expect(tokens[1].strValue).toEqual('%'); + + expect(tokens[2].type).toEqual(CssTokenType.Identifier); + expect(tokens[2].strValue).toEqual('is'); + + expect(tokens[3].type).toEqual(CssTokenType.Number); + expect(tokens[3].strValue).toEqual('40'); + }); + + it('should allow escaped character and unicode character-strings in CSS selectors', () => { + const cssCode = '\\123456 .some\\thing \{\}'; + const tokens = tokenize(cssCode); + + expect(tokens[0].type).toEqual(CssTokenType.Identifier); + expect(tokens[0].strValue).toEqual('\\123456'); + + expect(tokens[1].type).toEqual(CssTokenType.Character); + expect(tokens[2].type).toEqual(CssTokenType.Identifier); + expect(tokens[2].strValue).toEqual('some\\thing'); + }); + + it('should distinguish identifiers and numbers from special characters', () => { + const cssCode = 'one*two=-4+three-4-equals_value$'; + const tokens = tokenize(cssCode); + + expect(tokens[0].type).toEqual(CssTokenType.Identifier); + expect(tokens[0].strValue).toEqual('one'); + + expect(tokens[1].type).toEqual(CssTokenType.Character); + expect(tokens[1].strValue).toEqual('*'); + + expect(tokens[2].type).toEqual(CssTokenType.Identifier); + expect(tokens[2].strValue).toEqual('two'); + + expect(tokens[3].type).toEqual(CssTokenType.Character); + expect(tokens[3].strValue).toEqual('='); + + expect(tokens[4].type).toEqual(CssTokenType.Number); + expect(tokens[4].strValue).toEqual('-4'); + + expect(tokens[5].type).toEqual(CssTokenType.Character); + expect(tokens[5].strValue).toEqual('+'); + + expect(tokens[6].type).toEqual(CssTokenType.Identifier); + expect(tokens[6].strValue).toEqual('three-4-equals_value'); + + expect(tokens[7].type).toEqual(CssTokenType.Character); + expect(tokens[7].strValue).toEqual('$'); + }); + + it('should filter out comments and whitespace by default', () => { + const cssCode = '.selector /* comment */ { /* value */ }'; + const tokens = tokenize(cssCode); + + expect(tokens[0].strValue).toEqual('.'); + expect(tokens[1].strValue).toEqual('selector'); + expect(tokens[2].strValue).toEqual('{'); + expect(tokens[3].strValue).toEqual('}'); + }); + + it('should track comments when the flag is set to true', () => { + const cssCode = '.selector /* comment */ { /* value */ }'; + const trackComments = true; + const tokens = tokenize(cssCode, trackComments, CssLexerMode.ALL_TRACK_WS); + + expect(tokens[0].strValue).toEqual('.'); + expect(tokens[1].strValue).toEqual('selector'); + expect(tokens[2].strValue).toEqual(' '); + + expect(tokens[3].type).toEqual(CssTokenType.Comment); + expect(tokens[3].strValue).toEqual('/* comment */'); + + expect(tokens[4].strValue).toEqual(' '); + expect(tokens[5].strValue).toEqual('{'); + expect(tokens[6].strValue).toEqual(' '); + + expect(tokens[7].type).toEqual(CssTokenType.Comment); + expect(tokens[7].strValue).toEqual('/* value */'); + }); + + describe('Selector Mode', () => { + it('should throw an error if a selector is being parsed while in the wrong mode', () => { + const cssCode = '.class > tag'; + + let capturedMessage: string|null = null; + try { + tokenize(cssCode, false, CssLexerMode.STYLE_BLOCK); + } catch (e) { + capturedMessage = getRawMessage(e); + } + + expect(capturedMessage).toMatch(/Unexpected character \[\>\] at column 0:7 in expression/g); + + capturedMessage = null; + try { + tokenize(cssCode, false, CssLexerMode.SELECTOR); + } catch (e) { + capturedMessage = getRawMessage(e); + } + + expect(capturedMessage).toEqual(null); + }); + }); + + describe('Attribute Mode', () => { + it('should consider attribute selectors as valid input and throw when an invalid modifier is used', () => { - const newlines = ['\n', '\r\n', '\r', '\f'].join(''); - const tokens = tokenize(newlines); - expect(tokens.length).toEqual(0); + function tokenizeAttr(modifier: string) { + const cssCode = 'value' + modifier + '=\'something\''; + return tokenize(cssCode, false, CssLexerMode.ATTRIBUTE_SELECTOR); + } + + expect(tokenizeAttr('*').length).toEqual(4); + expect(tokenizeAttr('|').length).toEqual(4); + expect(tokenizeAttr('^').length).toEqual(4); + expect(tokenizeAttr('$').length).toEqual(4); + expect(tokenizeAttr('~').length).toEqual(4); + expect(tokenizeAttr('').length).toEqual(3); + + expect(() => { + tokenizeAttr('+'); + }).toThrow(); }); + }); - it('should lex simple selectors and their inner properties', () => { - const cssCode = '\n' + - ' .selector { my-prop: my-value; }\n'; - const tokens = tokenize(cssCode); + describe('Media Query Mode', () => { + it('should validate media queries with a reduced subset of valid characters', () => { + function tokenizeQuery(code: string) { + return tokenize(code, false, CssLexerMode.MEDIA_QUERY); + } - expect(tokens[0].type).toEqual(CssTokenType.Character); - expect(tokens[0].strValue).toEqual('.'); + // the reason why the numbers are so high is because MediaQueries keep + // track of the whitespace values + expect(tokenizeQuery('(prop: value)').length).toEqual(5); + expect(tokenizeQuery('(prop: value) and (prop2: value2)').length).toEqual(11); + expect(tokenizeQuery('tv and (prop: value)').length).toEqual(7); + expect(tokenizeQuery('print and ((prop: value) or (prop2: value2))').length).toEqual(15); + expect(tokenizeQuery('(content: \'something $ crazy inside &\')').length).toEqual(5); - expect(tokens[1].type).toEqual(CssTokenType.Identifier); - expect(tokens[1].strValue).toEqual('selector'); + expect(() => { + tokenizeQuery('(max-height: 10 + 20)'); + }).toThrow(); - expect(tokens[2].type).toEqual(CssTokenType.Character); - expect(tokens[2].strValue).toEqual('{'); - - expect(tokens[3].type).toEqual(CssTokenType.Identifier); - expect(tokens[3].strValue).toEqual('my-prop'); - - expect(tokens[4].type).toEqual(CssTokenType.Character); - expect(tokens[4].strValue).toEqual(':'); - - expect(tokens[5].type).toEqual(CssTokenType.Identifier); - expect(tokens[5].strValue).toEqual('my-value'); - - expect(tokens[6].type).toEqual(CssTokenType.Character); - expect(tokens[6].strValue).toEqual(';'); - - expect(tokens[7].type).toEqual(CssTokenType.Character); - expect(tokens[7].strValue).toEqual('}'); + expect(() => { + tokenizeQuery('(max-height: fifty < 100)'); + }).toThrow(); }); - - it('should capture the column and line values for each token', () => { - const cssCode = '#id {\n' + - ' prop:value;\n' + - '}'; - - const tokens = tokenize(cssCode); - - // # - expect(tokens[0].type).toEqual(CssTokenType.Character); - expect(tokens[0].column).toEqual(0); - expect(tokens[0].line).toEqual(0); - - // id - expect(tokens[1].type).toEqual(CssTokenType.Identifier); - expect(tokens[1].column).toEqual(1); - expect(tokens[1].line).toEqual(0); - - // { - expect(tokens[2].type).toEqual(CssTokenType.Character); - expect(tokens[2].column).toEqual(4); - expect(tokens[2].line).toEqual(0); - - // prop - expect(tokens[3].type).toEqual(CssTokenType.Identifier); - expect(tokens[3].column).toEqual(2); - expect(tokens[3].line).toEqual(1); - - // : - expect(tokens[4].type).toEqual(CssTokenType.Character); - expect(tokens[4].column).toEqual(6); - expect(tokens[4].line).toEqual(1); - - // value - expect(tokens[5].type).toEqual(CssTokenType.Identifier); - expect(tokens[5].column).toEqual(7); - expect(tokens[5].line).toEqual(1); - - // ; - expect(tokens[6].type).toEqual(CssTokenType.Character); - expect(tokens[6].column).toEqual(12); - expect(tokens[6].line).toEqual(1); - - // } - expect(tokens[7].type).toEqual(CssTokenType.Character); - expect(tokens[7].column).toEqual(0); - expect(tokens[7].line).toEqual(2); - }); - - it('should lex quoted strings and escape accordingly', () => { - const cssCode = 'prop: \'some { value } \\\' that is quoted\''; - const tokens = tokenize(cssCode); - - expect(tokens[0].type).toEqual(CssTokenType.Identifier); - expect(tokens[1].type).toEqual(CssTokenType.Character); - expect(tokens[2].type).toEqual(CssTokenType.String); - expect(tokens[2].strValue).toEqual('\'some { value } \\\' that is quoted\''); - }); - - it('should treat attribute operators as regular characters', () => { - tokenize('^|~+*').forEach((token) => { expect(token.type).toEqual(CssTokenType.Character); }); - }); - - it('should lex numbers properly and set them as numbers', () => { - const cssCode = '0 1 -2 3.0 -4.001'; - const tokens = tokenize(cssCode); - - expect(tokens[0].type).toEqual(CssTokenType.Number); - expect(tokens[0].strValue).toEqual('0'); - - expect(tokens[1].type).toEqual(CssTokenType.Number); - expect(tokens[1].strValue).toEqual('1'); - - expect(tokens[2].type).toEqual(CssTokenType.Number); - expect(tokens[2].strValue).toEqual('-2'); - - expect(tokens[3].type).toEqual(CssTokenType.Number); - expect(tokens[3].strValue).toEqual('3.0'); - - expect(tokens[4].type).toEqual(CssTokenType.Number); - expect(tokens[4].strValue).toEqual('-4.001'); - }); - - it('should lex @keywords', () => { - const cssCode = '@import()@something'; - const tokens = tokenize(cssCode); - - expect(tokens[0].type).toEqual(CssTokenType.AtKeyword); - expect(tokens[0].strValue).toEqual('@import'); - - expect(tokens[1].type).toEqual(CssTokenType.Character); - expect(tokens[1].strValue).toEqual('('); - - expect(tokens[2].type).toEqual(CssTokenType.Character); - expect(tokens[2].strValue).toEqual(')'); - - expect(tokens[3].type).toEqual(CssTokenType.AtKeyword); - expect(tokens[3].strValue).toEqual('@something'); - }); - - it('should still lex a number even if it has a dimension suffix', () => { - const cssCode = '40% is 40 percent'; - const tokens = tokenize(cssCode); - - expect(tokens[0].type).toEqual(CssTokenType.Number); - expect(tokens[0].strValue).toEqual('40'); - - expect(tokens[1].type).toEqual(CssTokenType.Character); - expect(tokens[1].strValue).toEqual('%'); - - expect(tokens[2].type).toEqual(CssTokenType.Identifier); - expect(tokens[2].strValue).toEqual('is'); - - expect(tokens[3].type).toEqual(CssTokenType.Number); - expect(tokens[3].strValue).toEqual('40'); - }); - - it('should allow escaped character and unicode character-strings in CSS selectors', () => { - const cssCode = '\\123456 .some\\thing \{\}'; - const tokens = tokenize(cssCode); - - expect(tokens[0].type).toEqual(CssTokenType.Identifier); - expect(tokens[0].strValue).toEqual('\\123456'); - - expect(tokens[1].type).toEqual(CssTokenType.Character); - expect(tokens[2].type).toEqual(CssTokenType.Identifier); - expect(tokens[2].strValue).toEqual('some\\thing'); - }); - - it('should distinguish identifiers and numbers from special characters', () => { - const cssCode = 'one*two=-4+three-4-equals_value$'; - const tokens = tokenize(cssCode); - - expect(tokens[0].type).toEqual(CssTokenType.Identifier); - expect(tokens[0].strValue).toEqual('one'); - - expect(tokens[1].type).toEqual(CssTokenType.Character); - expect(tokens[1].strValue).toEqual('*'); - - expect(tokens[2].type).toEqual(CssTokenType.Identifier); - expect(tokens[2].strValue).toEqual('two'); - - expect(tokens[3].type).toEqual(CssTokenType.Character); - expect(tokens[3].strValue).toEqual('='); - - expect(tokens[4].type).toEqual(CssTokenType.Number); - expect(tokens[4].strValue).toEqual('-4'); - - expect(tokens[5].type).toEqual(CssTokenType.Character); - expect(tokens[5].strValue).toEqual('+'); - - expect(tokens[6].type).toEqual(CssTokenType.Identifier); - expect(tokens[6].strValue).toEqual('three-4-equals_value'); - - expect(tokens[7].type).toEqual(CssTokenType.Character); - expect(tokens[7].strValue).toEqual('$'); - }); - - it('should filter out comments and whitespace by default', () => { - const cssCode = '.selector /* comment */ { /* value */ }'; - const tokens = tokenize(cssCode); - - expect(tokens[0].strValue).toEqual('.'); - expect(tokens[1].strValue).toEqual('selector'); - expect(tokens[2].strValue).toEqual('{'); - expect(tokens[3].strValue).toEqual('}'); - }); - - it('should track comments when the flag is set to true', () => { - const cssCode = '.selector /* comment */ { /* value */ }'; - const trackComments = true; - const tokens = tokenize(cssCode, trackComments, CssLexerMode.ALL_TRACK_WS); - - expect(tokens[0].strValue).toEqual('.'); - expect(tokens[1].strValue).toEqual('selector'); - expect(tokens[2].strValue).toEqual(' '); - - expect(tokens[3].type).toEqual(CssTokenType.Comment); - expect(tokens[3].strValue).toEqual('/* comment */'); - - expect(tokens[4].strValue).toEqual(' '); - expect(tokens[5].strValue).toEqual('{'); - expect(tokens[6].strValue).toEqual(' '); - - expect(tokens[7].type).toEqual(CssTokenType.Comment); - expect(tokens[7].strValue).toEqual('/* value */'); - }); - - describe('Selector Mode', () => { - it('should throw an error if a selector is being parsed while in the wrong mode', () => { - const cssCode = '.class > tag'; - - let capturedMessage: string|null = null; - try { - tokenize(cssCode, false, CssLexerMode.STYLE_BLOCK); - } catch (e) { - capturedMessage = getRawMessage(e); - } - - expect(capturedMessage).toMatch(/Unexpected character \[\>\] at column 0:7 in expression/g); - - capturedMessage = null; - try { - tokenize(cssCode, false, CssLexerMode.SELECTOR); - } catch (e) { - capturedMessage = getRawMessage(e); - } - - expect(capturedMessage).toEqual(null); - }); - }); - - describe('Attribute Mode', () => { - it('should consider attribute selectors as valid input and throw when an invalid modifier is used', - () => { - function tokenizeAttr(modifier: string) { - const cssCode = 'value' + modifier + '=\'something\''; - return tokenize(cssCode, false, CssLexerMode.ATTRIBUTE_SELECTOR); - } - - expect(tokenizeAttr('*').length).toEqual(4); - expect(tokenizeAttr('|').length).toEqual(4); - expect(tokenizeAttr('^').length).toEqual(4); - expect(tokenizeAttr('$').length).toEqual(4); - expect(tokenizeAttr('~').length).toEqual(4); - expect(tokenizeAttr('').length).toEqual(3); - - expect(() => { tokenizeAttr('+'); }).toThrow(); - }); - }); - - describe('Media Query Mode', () => { - it('should validate media queries with a reduced subset of valid characters', () => { - function tokenizeQuery(code: string) { - return tokenize(code, false, CssLexerMode.MEDIA_QUERY); - } - - // the reason why the numbers are so high is because MediaQueries keep - // track of the whitespace values - expect(tokenizeQuery('(prop: value)').length).toEqual(5); - expect(tokenizeQuery('(prop: value) and (prop2: value2)').length).toEqual(11); - expect(tokenizeQuery('tv and (prop: value)').length).toEqual(7); - expect(tokenizeQuery('print and ((prop: value) or (prop2: value2))').length).toEqual(15); - expect(tokenizeQuery('(content: \'something $ crazy inside &\')').length).toEqual(5); - - expect(() => { tokenizeQuery('(max-height: 10 + 20)'); }).toThrow(); - - expect(() => { tokenizeQuery('(max-height: fifty < 100)'); }).toThrow(); - }); - }); - - describe('Pseudo Selector Mode', () => { - it('should validate pseudo selector identifiers with a reduced subset of valid characters', - () => { - function tokenizePseudo(code: string, withArgs = false): CssToken[] { - const mode = withArgs ? CssLexerMode.PSEUDO_SELECTOR_WITH_ARGUMENTS : - CssLexerMode.PSEUDO_SELECTOR; - return tokenize(code, false, mode); - } - - expect(tokenizePseudo('hover').length).toEqual(1); - expect(tokenizePseudo('focus').length).toEqual(1); - expect(tokenizePseudo('lang(en-us)', true).length).toEqual(4); - - expect(() => { tokenizePseudo('lang(something:broken)', true); }).toThrow(); - - expect(() => { tokenizePseudo('not(.selector)', true); }).toThrow(); - }); - }); - - describe( - 'Style Block Mode', () => { - it('should style blocks with a reduced subset of valid characters', - () => { - function tokenizeStyles(code: string) { - return tokenize(code, false, CssLexerMode.STYLE_BLOCK); - } - - expect(tokenizeStyles(` + }); + + describe('Pseudo Selector Mode', () => { + it('should validate pseudo selector identifiers with a reduced subset of valid characters', + () => { + function tokenizePseudo(code: string, withArgs = false): CssToken[] { + const mode = withArgs ? CssLexerMode.PSEUDO_SELECTOR_WITH_ARGUMENTS : + CssLexerMode.PSEUDO_SELECTOR; + return tokenize(code, false, mode); + } + + expect(tokenizePseudo('hover').length).toEqual(1); + expect(tokenizePseudo('focus').length).toEqual(1); + expect(tokenizePseudo('lang(en-us)', true).length).toEqual(4); + + expect(() => { + tokenizePseudo('lang(something:broken)', true); + }).toThrow(); + + expect(() => { + tokenizePseudo('not(.selector)', true); + }).toThrow(); + }); + }); + + describe( + 'Style Block Mode', () => { + it( + 'should style blocks with a reduced subset of valid characters', () => { + function tokenizeStyles(code: string) { + return tokenize(code, false, CssLexerMode.STYLE_BLOCK); + } + + expect(tokenizeStyles(` key: value; prop: 100; style: value3!important; `).length).toEqual(14); - expect(() => tokenizeStyles(` key$: value; `)).toThrow(); - expect(() => tokenizeStyles(` key: value$; `)).toThrow(); - expect(() => tokenizeStyles(` key: value + 10; `)).toThrow(); - expect(() => tokenizeStyles(` key: &value; `)).toThrow(); - }); - }); - }); + expect(() => tokenizeStyles(` key$: value; `)).toThrow(); + expect(() => tokenizeStyles(` key: value$; `)).toThrow(); + expect(() => tokenizeStyles(` key: value + 10; `)).toThrow(); + expect(() => tokenizeStyles(` key: &value; `)).toThrow(); + }); + }); +}); })(); diff --git a/packages/compiler/test/css_parser/css_parser_spec.ts b/packages/compiler/test/css_parser/css_parser_spec.ts index 670a48264b..ad8c211ece 100644 --- a/packages/compiler/test/css_parser/css_parser_spec.ts +++ b/packages/compiler/test/css_parser/css_parser_spec.ts @@ -111,26 +111,26 @@ export function assertTokens(tokens: CssToken[], valuesArr: string[]) { expect(ast.rules.length).toEqual(1); const rule = ast.rules[0]; - expect(rule.name !.strValue).toEqual('rotateMe'); + expect(rule.name!.strValue).toEqual('rotateMe'); const block = rule.block; const fromRule = block.entries[0]; - expect(fromRule.name !.strValue).toEqual('from'); + expect(fromRule.name!.strValue).toEqual('from'); const fromStyle = (fromRule.block).entries[0]; expect(fromStyle.property.strValue).toEqual('transform'); assertTokens(fromStyle.value.tokens, ['rotate', '(', '-360', 'deg', ')']); const midRule = block.entries[1]; - expect(midRule.name !.strValue).toEqual('50%'); + expect(midRule.name!.strValue).toEqual('50%'); const midStyle = (midRule.block).entries[0]; expect(midStyle.property.strValue).toEqual('transform'); assertTokens(midStyle.value.tokens, ['rotate', '(', '0', 'deg', ')']); const toRule = block.entries[2]; - expect(toRule.name !.strValue).toEqual('to'); + expect(toRule.name!.strValue).toEqual('to'); const toStyle = (toRule.block).entries[0]; expect(toStyle.property.strValue).toEqual('transform'); assertTokens(toStyle.value.tokens, ['rotate', '(', '360', 'deg', ')']); @@ -695,7 +695,7 @@ export function assertTokens(tokens: CssToken[], valuesArr: string[]) { const ast = output.ast; assertMatchesOffsetAndChar(ast.location.start, 0, '#'); - assertMatchesOffsetAndChar(ast.location.end, 22, undefined !); + assertMatchesOffsetAndChar(ast.location.end, 22, undefined!); }); }); diff --git a/packages/compiler/test/css_parser/css_visitor_spec.ts b/packages/compiler/test/css_parser/css_visitor_spec.ts index 5a36459264..f7d05a5e5b 100644 --- a/packages/compiler/test/css_parser/css_visitor_spec.ts +++ b/packages/compiler/test/css_parser/css_visitor_spec.ts @@ -8,7 +8,7 @@ import {beforeEach, describe, expect, it} from '../../../core/testing/src/testing_internal'; -import {CssAst, CssAstVisitor, CssAtRulePredicateAst, CssBlockAst, CssDefinitionAst, CssInlineRuleAst, CssKeyframeDefinitionAst, CssKeyframeRuleAst, CssMediaQueryRuleAst, CssPseudoSelectorAst, CssRuleAst, CssSelectorAst, CssSelectorRuleAst, CssSimpleSelectorAst, CssStyleSheetAst, CssStyleValueAst, CssStylesBlockAst, CssUnknownRuleAst, CssUnknownTokenListAst} from '../../src/css_parser/css_ast'; +import {CssAst, CssAstVisitor, CssAtRulePredicateAst, CssBlockAst, CssDefinitionAst, CssInlineRuleAst, CssKeyframeDefinitionAst, CssKeyframeRuleAst, CssMediaQueryRuleAst, CssPseudoSelectorAst, CssRuleAst, CssSelectorAst, CssSelectorRuleAst, CssSimpleSelectorAst, CssStylesBlockAst, CssStyleSheetAst, CssStyleValueAst, CssUnknownRuleAst, CssUnknownTokenListAst} from '../../src/css_parser/css_ast'; import {BlockType, CssParseError, CssParser, CssToken} from '../../src/css_parser/css_parser'; function _assertTokens(tokens: CssToken[], valuesArr: string[]): void { @@ -29,7 +29,9 @@ class MyVisitor implements CssAstVisitor { this.captures[method].push([ast, context]); } - constructor(ast: CssStyleSheetAst, context: any) { ast.visit(this, context); } + constructor(ast: CssStyleSheetAst, context: any) { + ast.visit(this, context); + } visitCssValue(ast: CssStyleValueAst, context: any): void { this._capture('visitCssValue', ast, context); @@ -61,20 +63,24 @@ class MyVisitor implements CssAstVisitor { visitCssSelectorRule(ast: CssSelectorRuleAst, context: any): void { this._capture('visitCssSelectorRule', ast, context); - ast.selectors.forEach((selAst: CssSelectorAst) => { selAst.visit(this, context); }); + ast.selectors.forEach((selAst: CssSelectorAst) => { + selAst.visit(this, context); + }); ast.block.visit(this, context); } visitCssSelector(ast: CssSelectorAst, context: any): void { this._capture('visitCssSelector', ast, context); - ast.selectorParts.forEach( - (simpleAst: CssSimpleSelectorAst) => { simpleAst.visit(this, context); }); + ast.selectorParts.forEach((simpleAst: CssSimpleSelectorAst) => { + simpleAst.visit(this, context); + }); } visitCssSimpleSelector(ast: CssSimpleSelectorAst, context: any): void { this._capture('visitCssSimpleSelector', ast, context); - ast.pseudoSelectors.forEach( - (pseudoAst: CssPseudoSelectorAst) => { pseudoAst.visit(this, context); }); + ast.pseudoSelectors.forEach((pseudoAst: CssPseudoSelectorAst) => { + pseudoAst.visit(this, context); + }); } visitCssDefinition(ast: CssDefinitionAst, context: any): void { @@ -84,18 +90,23 @@ class MyVisitor implements CssAstVisitor { visitCssBlock(ast: CssBlockAst, context: any): void { this._capture('visitCssBlock', ast, context); - ast.entries.forEach((entryAst: CssAst) => { entryAst.visit(this, context); }); + ast.entries.forEach((entryAst: CssAst) => { + entryAst.visit(this, context); + }); } visitCssStylesBlock(ast: CssStylesBlockAst, context: any): void { this._capture('visitCssStylesBlock', ast, context); - ast.definitions.forEach( - (definitionAst: CssDefinitionAst) => { definitionAst.visit(this, context); }); + ast.definitions.forEach((definitionAst: CssDefinitionAst) => { + definitionAst.visit(this, context); + }); } visitCssStyleSheet(ast: CssStyleSheetAst, context: any): void { this._capture('visitCssStyleSheet', ast, context); - ast.rules.forEach((ruleAst: CssRuleAst) => { ruleAst.visit(this, context); }); + ast.rules.forEach((ruleAst: CssRuleAst) => { + ruleAst.visit(this, context); + }); } visitCssUnknownRule(ast: CssUnknownRuleAst, context: any): void { @@ -116,21 +127,21 @@ function _getCaptureAst(capture: any[], index = 0): CssAst { } (function() { - function parse(cssCode: string, ignoreErrors: boolean = false) { - const output = new CssParser().parse(cssCode, 'some-fake-css-file.css'); - const errors = output.errors; - if (errors.length > 0 && !ignoreErrors) { - throw new Error(errors.map((error: CssParseError) => error.msg).join(', ')); - } - return output.ast; +function parse(cssCode: string, ignoreErrors: boolean = false) { + const output = new CssParser().parse(cssCode, 'some-fake-css-file.css'); + const errors = output.errors; + if (errors.length > 0 && !ignoreErrors) { + throw new Error(errors.map((error: CssParseError) => error.msg).join(', ')); } + return output.ast; +} - describe('CSS parsing and visiting', () => { - let ast: CssStyleSheetAst; - const context = {}; +describe('CSS parsing and visiting', () => { + let ast: CssStyleSheetAst; + const context = {}; - beforeEach(() => { - const cssCode = ` + beforeEach(() => { + const cssCode = ` .rule1 { prop1: value1 } .rule2 { prop2: value2 } @@ -149,174 +160,174 @@ function _getCaptureAst(capture: any[], index = 0): CssAst { } } `; - ast = parse(cssCode); - }); + ast = parse(cssCode); + }); - it('should parse and visit a stylesheet', () => { - const visitor = new MyVisitor(ast, context); - const captures = visitor.captures['visitCssStyleSheet']; + it('should parse and visit a stylesheet', () => { + const visitor = new MyVisitor(ast, context); + const captures = visitor.captures['visitCssStyleSheet']; - expect(captures.length).toEqual(1); + expect(captures.length).toEqual(1); - const capture = captures[0]; - expect(capture[0]).toEqual(ast); - expect(capture[1]).toEqual(context); - }); + const capture = captures[0]; + expect(capture[0]).toEqual(ast); + expect(capture[1]).toEqual(context); + }); - it('should parse and visit each of the stylesheet selectors', () => { - const visitor = new MyVisitor(ast, context); - const captures = visitor.captures['visitCssSelectorRule']; + it('should parse and visit each of the stylesheet selectors', () => { + const visitor = new MyVisitor(ast, context); + const captures = visitor.captures['visitCssSelectorRule']; - expect(captures.length).toEqual(3); + expect(captures.length).toEqual(3); - const rule1 = _getCaptureAst(captures, 0); - expect(rule1).toEqual(ast.rules[0] as CssSelectorRuleAst); + const rule1 = _getCaptureAst(captures, 0); + expect(rule1).toEqual(ast.rules[0] as CssSelectorRuleAst); - const firstSelector = rule1.selectors[0]; - const firstSimpleSelector = firstSelector.selectorParts[0]; - _assertTokens(firstSimpleSelector.tokens, ['.', 'rule1']); + const firstSelector = rule1.selectors[0]; + const firstSimpleSelector = firstSelector.selectorParts[0]; + _assertTokens(firstSimpleSelector.tokens, ['.', 'rule1']); - const rule2 = _getCaptureAst(captures, 1); - expect(rule2).toEqual(ast.rules[1] as CssSelectorRuleAst); + const rule2 = _getCaptureAst(captures, 1); + expect(rule2).toEqual(ast.rules[1] as CssSelectorRuleAst); - const secondSelector = rule2.selectors[0]; - const secondSimpleSelector = secondSelector.selectorParts[0]; - _assertTokens(secondSimpleSelector.tokens, ['.', 'rule2']); + const secondSelector = rule2.selectors[0]; + const secondSimpleSelector = secondSelector.selectorParts[0]; + _assertTokens(secondSimpleSelector.tokens, ['.', 'rule2']); - const rule3 = _getCaptureAst(captures, 2); - expect(rule3).toEqual( - (ast.rules[2] as CssSelectorRuleAst).block.entries[0] as CssSelectorRuleAst); + const rule3 = _getCaptureAst(captures, 2); + expect(rule3).toEqual( + (ast.rules[2] as CssSelectorRuleAst).block.entries[0] as CssSelectorRuleAst); - const thirdSelector = rule3.selectors[0]; - const thirdSimpleSelector = thirdSelector.selectorParts[0]; - _assertTokens(thirdSimpleSelector.tokens, ['#', 'rule3']); - }); + const thirdSelector = rule3.selectors[0]; + const thirdSimpleSelector = thirdSelector.selectorParts[0]; + _assertTokens(thirdSimpleSelector.tokens, ['#', 'rule3']); + }); - it('should parse and visit each of the stylesheet style key/value definitions', () => { - const visitor = new MyVisitor(ast, context); - const captures = visitor.captures['visitCssDefinition']; + it('should parse and visit each of the stylesheet style key/value definitions', () => { + const visitor = new MyVisitor(ast, context); + const captures = visitor.captures['visitCssDefinition']; - expect(captures.length).toEqual(5); + expect(captures.length).toEqual(5); - const def1 = _getCaptureAst(captures, 0); - expect(def1.property.strValue).toEqual('prop1'); - expect(def1.value.tokens[0].strValue).toEqual('value1'); + const def1 = _getCaptureAst(captures, 0); + expect(def1.property.strValue).toEqual('prop1'); + expect(def1.value.tokens[0].strValue).toEqual('value1'); - const def2 = _getCaptureAst(captures, 1); - expect(def2.property.strValue).toEqual('prop2'); - expect(def2.value.tokens[0].strValue).toEqual('value2'); + const def2 = _getCaptureAst(captures, 1); + expect(def2.property.strValue).toEqual('prop2'); + expect(def2.value.tokens[0].strValue).toEqual('value2'); - const def3 = _getCaptureAst(captures, 2); - expect(def3.property.strValue).toEqual('prop3'); - expect(def3.value.tokens[0].strValue).toEqual('value3'); + const def3 = _getCaptureAst(captures, 2); + expect(def3.property.strValue).toEqual('prop3'); + expect(def3.value.tokens[0].strValue).toEqual('value3'); - const def4 = _getCaptureAst(captures, 3); - expect(def4.property.strValue).toEqual('prop4'); - expect(def4.value.tokens[0].strValue).toEqual('value4'); + const def4 = _getCaptureAst(captures, 3); + expect(def4.property.strValue).toEqual('prop4'); + expect(def4.value.tokens[0].strValue).toEqual('value4'); - const def5 = _getCaptureAst(captures, 4); - expect(def5.property.strValue).toEqual('prop5'); - expect(def5.value.tokens[0].strValue).toEqual('value5'); - }); + const def5 = _getCaptureAst(captures, 4); + expect(def5.property.strValue).toEqual('prop5'); + expect(def5.value.tokens[0].strValue).toEqual('value5'); + }); - it('should parse and visit the associated media query values', () => { - const visitor = new MyVisitor(ast, context); - const captures = visitor.captures['visitCssMediaQueryRule']; + it('should parse and visit the associated media query values', () => { + const visitor = new MyVisitor(ast, context); + const captures = visitor.captures['visitCssMediaQueryRule']; - expect(captures.length).toEqual(1); + expect(captures.length).toEqual(1); - const query1 = _getCaptureAst(captures, 0); - _assertTokens(query1.query.tokens, ['all', 'and', '(', 'max-width', '100', 'px', ')']); - expect(query1.block.entries.length).toEqual(1); - }); + const query1 = _getCaptureAst(captures, 0); + _assertTokens(query1.query.tokens, ['all', 'and', '(', 'max-width', '100', 'px', ')']); + expect(query1.block.entries.length).toEqual(1); + }); - it('should capture the media query predicate', () => { - const visitor = new MyVisitor(ast, context); - const captures = visitor.captures['visitCssAtRulePredicate']; + it('should capture the media query predicate', () => { + const visitor = new MyVisitor(ast, context); + const captures = visitor.captures['visitCssAtRulePredicate']; - expect(captures.length).toEqual(1); + expect(captures.length).toEqual(1); - const predicate = _getCaptureAst(captures, 0); - expect(predicate.strValue).toEqual('@media all (max-width: 100px)'); - }); + const predicate = _getCaptureAst(captures, 0); + expect(predicate.strValue).toEqual('@media all (max-width: 100px)'); + }); - it('should parse and visit the associated "@inline" rule values', () => { - const visitor = new MyVisitor(ast, context); - const captures = visitor.captures['visitCssInlineRule']; + it('should parse and visit the associated "@inline" rule values', () => { + const visitor = new MyVisitor(ast, context); + const captures = visitor.captures['visitCssInlineRule']; - expect(captures.length).toEqual(1); + expect(captures.length).toEqual(1); - const inline1 = _getCaptureAst(captures, 0); - expect(inline1.type).toEqual(BlockType.Import); - _assertTokens(inline1.value.tokens, ['url', '(', 'file.css', ')']); - }); + const inline1 = _getCaptureAst(captures, 0); + expect(inline1.type).toEqual(BlockType.Import); + _assertTokens(inline1.value.tokens, ['url', '(', 'file.css', ')']); + }); - it('should parse and visit the keyframe blocks', () => { - const visitor = new MyVisitor(ast, context); - const captures = visitor.captures['visitCssKeyframeRule']; + it('should parse and visit the keyframe blocks', () => { + const visitor = new MyVisitor(ast, context); + const captures = visitor.captures['visitCssKeyframeRule']; - expect(captures.length).toEqual(1); + expect(captures.length).toEqual(1); - const keyframe1 = _getCaptureAst(captures, 0); - expect(keyframe1.name !.strValue).toEqual('rotate'); - expect(keyframe1.block.entries.length).toEqual(2); - }); + const keyframe1 = _getCaptureAst(captures, 0); + expect(keyframe1.name!.strValue).toEqual('rotate'); + expect(keyframe1.block.entries.length).toEqual(2); + }); - it('should parse and visit the associated keyframe rules', () => { - const visitor = new MyVisitor(ast, context); - const captures = visitor.captures['visitCssKeyframeDefinition']; + it('should parse and visit the associated keyframe rules', () => { + const visitor = new MyVisitor(ast, context); + const captures = visitor.captures['visitCssKeyframeDefinition']; - expect(captures.length).toEqual(2); + expect(captures.length).toEqual(2); - const def1 = _getCaptureAst(captures, 0); - _assertTokens(def1.steps, ['from']); - expect(def1.block.entries.length).toEqual(1); + const def1 = _getCaptureAst(captures, 0); + _assertTokens(def1.steps, ['from']); + expect(def1.block.entries.length).toEqual(1); - const def2 = _getCaptureAst(captures, 1); - _assertTokens(def2.steps, ['50%', '100%']); - expect(def2.block.entries.length).toEqual(1); - }); + const def2 = _getCaptureAst(captures, 1); + _assertTokens(def2.steps, ['50%', '100%']); + expect(def2.block.entries.length).toEqual(1); + }); - it('should visit an unknown `@` rule', () => { - const cssCode = ` + it('should visit an unknown `@` rule', () => { + const cssCode = ` @someUnknownRule param { one two three } `; - ast = parse(cssCode, true); - const visitor = new MyVisitor(ast, context); - const captures = visitor.captures['visitCssUnknownRule']; + ast = parse(cssCode, true); + const visitor = new MyVisitor(ast, context); + const captures = visitor.captures['visitCssUnknownRule']; - expect(captures.length).toEqual(1); + expect(captures.length).toEqual(1); - const rule = _getCaptureAst(captures, 0); - expect(rule.ruleName).toEqual('@someUnknownRule'); + const rule = _getCaptureAst(captures, 0); + expect(rule.ruleName).toEqual('@someUnknownRule'); - _assertTokens(rule.tokens, ['param', '{', 'one', 'two', 'three', '}']); - }); - - it('should collect an invalid list of tokens before a valid selector', () => { - const cssCode = 'one two three four five; selector { }'; - ast = parse(cssCode, true); - const visitor = new MyVisitor(ast, context); - const captures = visitor.captures['visitCssUnknownTokenList']; - - expect(captures.length).toEqual(1); - - const rule = _getCaptureAst(captures, 0); - _assertTokens(rule.tokens, ['one', 'two', 'three', 'four', 'five']); - }); - - it('should collect an invalid list of tokens after a valid selector', () => { - const cssCode = 'selector { } six seven eight'; - ast = parse(cssCode, true); - const visitor = new MyVisitor(ast, context); - const captures = visitor.captures['visitCssUnknownTokenList']; - - expect(captures.length).toEqual(1); - - const rule = _getCaptureAst(captures, 0); - _assertTokens(rule.tokens, ['six', 'seven', 'eight']); - }); + _assertTokens(rule.tokens, ['param', '{', 'one', 'two', 'three', '}']); }); + + it('should collect an invalid list of tokens before a valid selector', () => { + const cssCode = 'one two three four five; selector { }'; + ast = parse(cssCode, true); + const visitor = new MyVisitor(ast, context); + const captures = visitor.captures['visitCssUnknownTokenList']; + + expect(captures.length).toEqual(1); + + const rule = _getCaptureAst(captures, 0); + _assertTokens(rule.tokens, ['one', 'two', 'three', 'four', 'five']); + }); + + it('should collect an invalid list of tokens after a valid selector', () => { + const cssCode = 'selector { } six seven eight'; + ast = parse(cssCode, true); + const visitor = new MyVisitor(ast, context); + const captures = visitor.captures['visitCssUnknownTokenList']; + + expect(captures.length).toEqual(1); + + const rule = _getCaptureAst(captures, 0); + _assertTokens(rule.tokens, ['six', 'seven', 'eight']); + }); +}); })(); diff --git a/packages/compiler/test/directive_lifecycle_spec.ts b/packages/compiler/test/directive_lifecycle_spec.ts index a4bd85928f..4e77bec806 100644 --- a/packages/compiler/test/directive_lifecycle_spec.ts +++ b/packages/compiler/test/directive_lifecycle_spec.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {LifecycleHooks as Hooks, hasLifecycleHook as hasLifecycleHookImpl} from '@angular/compiler/src/lifecycle_reflector'; +import {hasLifecycleHook as hasLifecycleHookImpl, LifecycleHooks as Hooks} from '@angular/compiler/src/lifecycle_reflector'; import {SimpleChanges} from '@angular/core'; import {JitReflector} from '@angular/platform-browser-dynamic/src/compiler_reflector'; @@ -17,14 +17,14 @@ function hasLifecycleHook(hook: Hooks, directive: any): boolean { { describe('Create Directive', () => { describe('lifecycle', () => { - describe('ngOnChanges', () => { it('should be true when the directive has the ngOnChanges method', () => { expect(hasLifecycleHook(Hooks.OnChanges, DirectiveWithOnChangesMethod)).toBe(true); }); - it('should be false otherwise', - () => { expect(hasLifecycleHook(Hooks.OnChanges, DirectiveNoHooks)).toBe(false); }); + it('should be false otherwise', () => { + expect(hasLifecycleHook(Hooks.OnChanges, DirectiveNoHooks)).toBe(false); + }); }); describe('ngOnDestroy', () => { @@ -32,16 +32,19 @@ function hasLifecycleHook(hook: Hooks, directive: any): boolean { expect(hasLifecycleHook(Hooks.OnDestroy, DirectiveWithOnDestroyMethod)).toBe(true); }); - it('should be false otherwise', - () => { expect(hasLifecycleHook(Hooks.OnDestroy, DirectiveNoHooks)).toBe(false); }); + it('should be false otherwise', () => { + expect(hasLifecycleHook(Hooks.OnDestroy, DirectiveNoHooks)).toBe(false); + }); }); describe('ngOnInit', () => { - it('should be true when the directive has the ngOnInit method', - () => { expect(hasLifecycleHook(Hooks.OnInit, DirectiveWithOnInitMethod)).toBe(true); }); + it('should be true when the directive has the ngOnInit method', () => { + expect(hasLifecycleHook(Hooks.OnInit, DirectiveWithOnInitMethod)).toBe(true); + }); - it('should be false otherwise', - () => { expect(hasLifecycleHook(Hooks.OnInit, DirectiveNoHooks)).toBe(false); }); + it('should be false otherwise', () => { + expect(hasLifecycleHook(Hooks.OnInit, DirectiveNoHooks)).toBe(false); + }); }); describe('ngDoCheck', () => { @@ -49,8 +52,9 @@ function hasLifecycleHook(hook: Hooks, directive: any): boolean { expect(hasLifecycleHook(Hooks.DoCheck, DirectiveWithOnCheckMethod)).toBe(true); }); - it('should be false otherwise', - () => { expect(hasLifecycleHook(Hooks.DoCheck, DirectiveNoHooks)).toBe(false); }); + it('should be false otherwise', () => { + expect(hasLifecycleHook(Hooks.DoCheck, DirectiveNoHooks)).toBe(false); + }); }); describe('ngAfterContentInit', () => { @@ -83,8 +87,9 @@ function hasLifecycleHook(hook: Hooks, directive: any): boolean { .toBe(true); }); - it('should be false otherwise', - () => { expect(hasLifecycleHook(Hooks.AfterViewInit, DirectiveNoHooks)).toBe(false); }); + it('should be false otherwise', () => { + expect(hasLifecycleHook(Hooks.AfterViewInit, DirectiveNoHooks)).toBe(false); + }); }); describe('ngAfterViewChecked', () => { diff --git a/packages/compiler/test/directive_normalizer_spec.ts b/packages/compiler/test/directive_normalizer_spec.ts index 34bd29d809..4e2c64cc20 100644 --- a/packages/compiler/test/directive_normalizer_spec.ts +++ b/packages/compiler/test/directive_normalizer_spec.ts @@ -10,7 +10,7 @@ import {CompilerConfig, preserveWhitespacesDefault} from '@angular/compiler/src/ import {DirectiveNormalizer} from '@angular/compiler/src/directive_normalizer'; import {ResourceLoader} from '@angular/compiler/src/resource_loader'; import {ViewEncapsulation} from '@angular/core/src/metadata/view'; -import {TestBed, inject} from '@angular/core/testing'; +import {inject, TestBed} from '@angular/core/testing'; import {noUndefined} from '../src/util'; @@ -20,7 +20,10 @@ const SOME_MODULE_URL = 'package:some/module/a.js'; const SOME_HTTP_MODULE_URL = 'http://some/module/a.js'; function normalizeTemplate(normalizer: DirectiveNormalizer, o: { - moduleUrl?: string; template?: string | null; templateUrl?: string | null; styles?: string[]; + moduleUrl?: string; + template?: string | null; + templateUrl?: string | null; + styles?: string[]; styleUrls?: string[]; interpolation?: [string, string] | null; encapsulation?: ViewEncapsulation | null; @@ -51,8 +54,7 @@ function normalizeTemplate(normalizer: DirectiveNormalizer, o: { jasmine.createSpy('get').and.callFake((url: string) => `resource(${url})`); const resourceLoader = {get: resourceLoaderSpy}; TestBed.configureCompiler({ - providers: - [...TEST_COMPILER_PROVIDERS, {provide: ResourceLoader, useValue: resourceLoader}] + providers: [...TEST_COMPILER_PROVIDERS, {provide: ResourceLoader, useValue: resourceLoader}] }); }); @@ -64,12 +66,12 @@ function normalizeTemplate(normalizer: DirectiveNormalizer, o: { })); it('should throw if template is not a string', inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => { - expect(() => normalizeTemplate(normalizer, {template: {}})) + expect(() => normalizeTemplate(normalizer, {template: {}})) .toThrowError('The template specified for component SomeComp is not a string'); })); it('should throw if templateUrl is not a string', inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => { - expect(() => normalizeTemplate(normalizer, {templateUrl: {}})) + expect(() => normalizeTemplate(normalizer, {templateUrl: {}})) .toThrowError('The templateUrl specified for component SomeComp is not a string'); })); it('should throw if both template and templateUrl are defined', @@ -89,11 +91,9 @@ function normalizeTemplate(normalizer: DirectiveNormalizer, o: { .toThrowError( 'The preserveWhitespaces option for component SomeComp must be a boolean'); })); - }); describe('inline template', () => { - it('should store the template', inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => { const template = normalizeTemplate(normalizer, { @@ -174,7 +174,6 @@ function normalizeTemplate(normalizer: DirectiveNormalizer, o: { })); describe('externalStylesheets', () => { - it('should load an external stylesheet', inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => { const template = normalizeTemplate( @@ -222,7 +221,6 @@ function normalizeTemplate(normalizer: DirectiveNormalizer, o: { expect(resourceLoaderSpy).toHaveBeenCalledTimes(1); })); - }); describe('normalizeLoadedTemplate', () => { diff --git a/packages/compiler/test/directive_resolver_mock_spec.ts b/packages/compiler/test/directive_resolver_mock_spec.ts index dc1a82cff9..c81dff025a 100644 --- a/packages/compiler/test/directive_resolver_mock_spec.ts +++ b/packages/compiler/test/directive_resolver_mock_spec.ts @@ -7,7 +7,7 @@ */ import {Component, Directive, Injector} from '@angular/core'; -import {TestBed, inject} from '@angular/core/testing'; +import {inject, TestBed} from '@angular/core/testing'; import {JitReflector} from '@angular/platform-browser-dynamic/src/compiler_reflector'; import {MockDirectiveResolver} from '../testing'; diff --git a/packages/compiler/test/directive_resolver_spec.ts b/packages/compiler/test/directive_resolver_spec.ts index 4d211643f9..c0cf0fa394 100644 --- a/packages/compiler/test/directive_resolver_spec.ts +++ b/packages/compiler/test/directive_resolver_spec.ts @@ -32,13 +32,16 @@ class SomeDirectiveWithOutputs { @Directive({selector: 'someDirective'}) class SomeDirectiveWithSetterProps { @Input('renamed') - set a(value: any) {} + set a(value: any) { + } } @Directive({selector: 'someDirective'}) class SomeDirectiveWithGetterOutputs { @Output('renamed') - get a(): any { return null; } + get a(): any { + return null; + } } @Directive({selector: 'someDirective', host: {'[c]': 'c'}}) @@ -51,9 +54,11 @@ class SomeDirectiveWithHostBindings { @Directive({selector: 'someDirective', host: {'(c)': 'onC()'}}) class SomeDirectiveWithHostListeners { @HostListener('a') - onA() {} + onA() { + } @HostListener('b', ['$event.value']) - onB(value: any) {} + onB(value: any) { + } } @Directive({selector: 'someDirective', queries: {'cs': new ContentChildren('c')}}) @@ -101,13 +106,15 @@ class SomeDirectiveWithSameHostBindingAndInput { @Directive({selector: 'someDirective'}) class SomeDirectiveWithMalformedHostBinding1 { @HostBinding('(a)') - onA() {} + onA() { + } } @Directive({selector: 'someDirective'}) class SomeDirectiveWithMalformedHostBinding2 { @HostBinding('[a]') - onA() {} + onA() { + } } class SomeDirectiveWithoutMetadata {} @@ -116,7 +123,9 @@ class SomeDirectiveWithoutMetadata {} describe('DirectiveResolver', () => { let resolver: DirectiveResolver; - beforeEach(() => { resolver = new DirectiveResolver(new JitReflector()); }); + beforeEach(() => { + resolver = new DirectiveResolver(new JitReflector()); + }); it('should read out the Directive metadata', () => { const directiveMetadata = resolver.resolve(SomeDirective); @@ -204,8 +213,7 @@ class SomeDirectiveWithoutMetadata {} it('should prefer @Input over @Directive.inputs', () => { @Directive({selector: 'someDirective', inputs: ['a']}) class SomeDirectiveWithDuplicateInputs { - @Input('a') - propA: any; + @Input('a') propA: any; } const directiveMetadata = resolver.resolve(SomeDirectiveWithDuplicateInputs); expect(directiveMetadata.inputs).toEqual(['propA: a']); @@ -214,17 +222,13 @@ class SomeDirectiveWithoutMetadata {} it('should support inheriting inputs', () => { @Directive({selector: 'p'}) class Parent { - @Input() - p1: any; - @Input('p21') - p2: any; + @Input() p1: any; + @Input('p21') p2: any; } class Child extends Parent { - @Input('p22') - p2: any; - @Input() - p3: any; + @Input('p22') p2: any; + @Input() p3: any; } const directiveMetadata = resolver.resolve(Child); @@ -264,8 +268,7 @@ class SomeDirectiveWithoutMetadata {} it('should prefer @Output over @Directive.outputs', () => { @Directive({selector: 'someDirective', outputs: ['a']}) class SomeDirectiveWithDuplicateOutputs { - @Output('a') - propA: any; + @Output('a') propA: any; } const directiveMetadata = resolver.resolve(SomeDirectiveWithDuplicateOutputs); expect(directiveMetadata.outputs).toEqual(['propA: a']); @@ -274,17 +277,13 @@ class SomeDirectiveWithoutMetadata {} it('should support inheriting outputs', () => { @Directive({selector: 'p'}) class Parent { - @Output() - p1: any; - @Output('p21') - p2: any; + @Output() p1: any; + @Output('p21') p2: any; } class Child extends Parent { - @Output('p22') - p2: any; - @Output() - p3: any; + @Output('p22') p2: any; + @Output() p3: any; } const directiveMetadata = resolver.resolve(Child); @@ -324,17 +323,13 @@ class SomeDirectiveWithoutMetadata {} it('should support inheriting host bindings', () => { @Directive({selector: 'p'}) class Parent { - @HostBinding() - p1: any; - @HostBinding('p21') - p2: any; + @HostBinding() p1: any; + @HostBinding('p21') p2: any; } class Child extends Parent { - @HostBinding('p22') - p2: any; - @HostBinding() - p3: any; + @HostBinding('p22') p2: any; + @HostBinding() p3: any; } const directiveMetadata = resolver.resolve(Child); @@ -346,16 +341,20 @@ class SomeDirectiveWithoutMetadata {} @Directive({selector: 'p'}) class Parent { @HostListener('p1') - p1() {} + p1() { + } @HostListener('p21') - p2() {} + p2() { + } } class Child extends Parent { @HostListener('p22') - p2() {} + p2() { + } @HostListener('p3') - p3() {} + p3() { + } } const directiveMetadata = resolver.resolve(Child); @@ -366,19 +365,20 @@ class SomeDirectiveWithoutMetadata {} it('should combine host bindings and listeners during inheritance', () => { @Directive({selector: 'p'}) class Parent { - @HostListener('p11') @HostListener('p12') - p1() {} + @HostListener('p11') + @HostListener('p12') + p1() { + } - @HostBinding('p21') @HostBinding('p22') - p2: any; + @HostBinding('p21') @HostBinding('p22') p2: any; } class Child extends Parent { @HostListener('c1') - p1() {} + p1() { + } - @HostBinding('c2') - p2: any; + @HostBinding('c2') p2: any; } const directiveMetadata = resolver.resolve(Child); @@ -421,17 +421,13 @@ class SomeDirectiveWithoutMetadata {} it('should support inheriting queries', () => { @Directive({selector: 'p'}) class Parent { - @ContentChild('p1') - p1: any; - @ContentChild('p21') - p2: any; + @ContentChild('p1') p1: any; + @ContentChild('p21') p2: any; } class Child extends Parent { - @ContentChild('p22') - p2: any; - @ContentChild('p3') - p3: any; + @ContentChild('p22') p2: any; + @ContentChild('p3') p3: any; } const directiveMetadata = resolver.resolve(Child); diff --git a/packages/compiler/test/expression_parser/lexer_spec.ts b/packages/compiler/test/expression_parser/lexer_spec.ts index 3fda05b0a4..d09d261cf7 100644 --- a/packages/compiler/test/expression_parser/lexer_spec.ts +++ b/packages/compiler/test/expression_parser/lexer_spec.ts @@ -101,14 +101,17 @@ function expectErrorToken(token: Token, index: any, end: number, message: string expectNumberToken(tokens[0], 0, 2, 88); }); - it('should tokenize numbers within index ops', - () => { expectNumberToken(lex('a[22]')[2], 2, 4, 22); }); + it('should tokenize numbers within index ops', () => { + expectNumberToken(lex('a[22]')[2], 2, 4, 22); + }); - it('should tokenize simple quoted strings', - () => { expectStringToken(lex('"a"')[0], 0, 3, 'a'); }); + it('should tokenize simple quoted strings', () => { + expectStringToken(lex('"a"')[0], 0, 3, 'a'); + }); - it('should tokenize quoted strings with escaped quotes', - () => { expectStringToken(lex('"a\\""')[0], 0, 5, 'a"'); }); + it('should tokenize quoted strings with escaped quotes', () => { + expectStringToken(lex('"a\\""')[0], 0, 5, 'a"'); + }); it('should tokenize a string', () => { const tokens: Token[] = lex('j-a.bc[22]+1.3|f:\'a\\\'c\':"d\\"e"'); @@ -213,7 +216,9 @@ function expectErrorToken(token: Token, index: any, end: number, message: string expectCharacterToken(tokens[13], 16, 17, ')'); }); - it('should tokenize number', () => { expectNumberToken(lex('0.5')[0], 0, 3, 0.5); }); + it('should tokenize number', () => { + expectNumberToken(lex('0.5')[0], 0, 3, 0.5); + }); it('should tokenize number with exponent', () => { let tokens: Token[] = lex('0.5E-10'); @@ -233,8 +238,9 @@ function expectErrorToken(token: Token, index: any, end: number, message: string 'Lexer Error: Invalid exponent at column 4 in expression [0.5E-A]'); }); - it('should tokenize number starting with a dot', - () => { expectNumberToken(lex('.5')[0], 0, 2, 0.5); }); + it('should tokenize number starting with a dot', () => { + expectNumberToken(lex('.5')[0], 0, 2, 0.5); + }); it('should throw error on invalid unicode', () => { expectErrorToken( @@ -242,11 +248,13 @@ function expectErrorToken(token: Token, index: any, end: number, message: string 'Lexer Error: Invalid unicode escape [\\u1\'\'b] at column 2 in expression [\'\\u1\'\'bla\']'); }); - it('should tokenize hash as operator', - () => { expectOperatorToken(lex('#')[0], 0, 1, '#'); }); + it('should tokenize hash as operator', () => { + expectOperatorToken(lex('#')[0], 0, 1, '#'); + }); - it('should tokenize ?. as operator', - () => { expectOperatorToken(lex('?.')[0], 0, 2, '?.'); }); + it('should tokenize ?. as operator', () => { + expectOperatorToken(lex('?.')[0], 0, 2, '?.'); + }); }); }); } diff --git a/packages/compiler/test/expression_parser/parser_spec.ts b/packages/compiler/test/expression_parser/parser_spec.ts index 69fb2a358c..f25aab0e40 100644 --- a/packages/compiler/test/expression_parser/parser_spec.ts +++ b/packages/compiler/test/expression_parser/parser_spec.ts @@ -17,16 +17,22 @@ import {validate} from './utils/validator'; describe('parser', () => { describe('parseAction', () => { - it('should parse numbers', () => { checkAction('1'); }); + it('should parse numbers', () => { + checkAction('1'); + }); it('should parse strings', () => { checkAction('\'1\'', '"1"'); checkAction('"1"'); }); - it('should parse null', () => { checkAction('null'); }); + it('should parse null', () => { + checkAction('null'); + }); - it('should parse undefined', () => { checkAction('undefined'); }); + it('should parse undefined', () => { + checkAction('undefined'); + }); it('should parse unary - expressions', () => { checkAction('-1', '0 - 1'); @@ -47,10 +53,13 @@ describe('parser', () => { checkAction('a!!!!.b'); }); - it('should parse multiplicative expressions', - () => { checkAction('3*4/2%5', '3 * 4 / 2 % 5'); }); + it('should parse multiplicative expressions', () => { + checkAction('3*4/2%5', '3 * 4 / 2 % 5'); + }); - it('should parse additive expressions', () => { checkAction('3 + 6 - 2'); }); + it('should parse additive expressions', () => { + checkAction('3 + 6 - 2'); + }); it('should parse relational expressions', () => { checkAction('2 < 3'); @@ -74,14 +83,21 @@ describe('parser', () => { checkAction('true || false'); }); - it('should parse grouped expressions', () => { checkAction('(1 + 2) * 3', '1 + 2 * 3'); }); + it('should parse grouped expressions', () => { + checkAction('(1 + 2) * 3', '1 + 2 * 3'); + }); - it('should ignore comments in expressions', () => { checkAction('a //comment', 'a'); }); + it('should ignore comments in expressions', () => { + checkAction('a //comment', 'a'); + }); - it('should retain // in string literals', - () => { checkAction(`"http://www.google.com"`, `"http://www.google.com"`); }); + it('should retain // in string literals', () => { + checkAction(`"http://www.google.com"`, `"http://www.google.com"`); + }); - it('should parse an empty string', () => { checkAction(''); }); + it('should parse an empty string', () => { + checkAction(''); + }); describe('literals', () => { it('should parse array', () => { @@ -133,7 +149,9 @@ describe('parser', () => { }); describe('functional calls', () => { - it('should parse function calls', () => { checkAction('fn()(1, 2)'); }); + it('should parse function calls', () => { + checkAction('fn()(1, 2)'); + }); }); describe('conditional', () => { @@ -154,20 +172,26 @@ describe('parser', () => { checkAction('a = 123; b = 234;'); }); - it('should report on safe field assignments', - () => { expectActionError('a?.a = 123', 'cannot be used in the assignment'); }); + it('should report on safe field assignments', () => { + expectActionError('a?.a = 123', 'cannot be used in the assignment'); + }); - it('should support array updates', () => { checkAction('a[0] = 200'); }); + it('should support array updates', () => { + checkAction('a[0] = 200'); + }); }); - it('should error when using pipes', - () => { expectActionError('x|blah', 'Cannot have a pipe'); }); + it('should error when using pipes', () => { + expectActionError('x|blah', 'Cannot have a pipe'); + }); - it('should store the source in the result', - () => { expect(parseAction('someExpr', 'someExpr')); }); + it('should store the source in the result', () => { + expect(parseAction('someExpr', 'someExpr')); + }); - it('should store the passed-in location', - () => { expect(parseAction('someExpr', 'location').location).toBe('location'); }); + it('should store the passed-in location', () => { + expect(parseAction('someExpr', 'location').location).toBe('location'); + }); it('should report when encountering interpolation', () => { expectActionError('{{a()}}', 'Got interpolation ({{}}) where expression was expected'); @@ -175,11 +199,13 @@ describe('parser', () => { }); describe('general error handling', () => { - it('should report an unexpected token', - () => { expectActionError('[1,2] trac', 'Unexpected token \'trac\''); }); + it('should report an unexpected token', () => { + expectActionError('[1,2] trac', 'Unexpected token \'trac\''); + }); - it('should report reasonable error for unconsumed tokens', - () => { expectActionError(')', 'Unexpected token ) at column 1 in [)]'); }); + it('should report reasonable error for unconsumed tokens', () => { + expectActionError(')', 'Unexpected token ) at column 1 in [)]'); + }); it('should report a missing expected token', () => { expectActionError('a(b', 'Missing expected ) at the end of the expression [a(b]'); @@ -206,12 +232,17 @@ describe('parser', () => { expectBindingError('"Foo"|"uppercase"', 'identifier or keyword'); }); - it('should parse quoted expressions', () => { checkBinding('a:b', 'a:b'); }); + it('should parse quoted expressions', () => { + checkBinding('a:b', 'a:b'); + }); - it('should not crash when prefix part is not tokenizable', - () => { checkBinding('"a:b"', '"a:b"'); }); + it('should not crash when prefix part is not tokenizable', () => { + checkBinding('"a:b"', '"a:b"'); + }); - it('should ignore whitespace around quote prefix', () => { checkBinding(' a :b', 'a:b'); }); + it('should ignore whitespace around quote prefix', () => { + checkBinding(' a :b', 'a:b'); + }); it('should refuse prefixes that are not single identifiers', () => { expectBindingError('a + b:c', ''); @@ -219,31 +250,41 @@ describe('parser', () => { }); }); - it('should store the source in the result', - () => { expect(parseBinding('someExpr').source).toBe('someExpr'); }); + it('should store the source in the result', () => { + expect(parseBinding('someExpr').source).toBe('someExpr'); + }); - it('should store the passed-in location', - () => { expect(parseBinding('someExpr', 'location').location).toBe('location'); }); + it('should store the passed-in location', () => { + expect(parseBinding('someExpr', 'location').location).toBe('location'); + }); - it('should report chain expressions', - () => { expectError(parseBinding('1;2'), 'contain chained expression'); }); + it('should report chain expressions', () => { + expectError(parseBinding('1;2'), 'contain chained expression'); + }); - it('should report assignment', - () => { expectError(parseBinding('a=2'), 'contain assignments'); }); + it('should report assignment', () => { + expectError(parseBinding('a=2'), 'contain assignments'); + }); it('should report when encountering interpolation', () => { expectBindingError('{{a.b}}', 'Got interpolation ({{}}) where expression was expected'); }); - it('should parse conditional expression', () => { checkBinding('a < b ? a : b'); }); + it('should parse conditional expression', () => { + checkBinding('a < b ? a : b'); + }); - it('should ignore comments in bindings', () => { checkBinding('a //comment', 'a'); }); + it('should ignore comments in bindings', () => { + checkBinding('a //comment', 'a'); + }); - it('should retain // in string literals', - () => { checkBinding(`"http://www.google.com"`, `"http://www.google.com"`); }); - - it('should retain // in : microsyntax', () => { checkBinding('one:a//b', 'one:a//b'); }); + it('should retain // in string literals', () => { + checkBinding(`"http://www.google.com"`, `"http://www.google.com"`); + }); + it('should retain // in : microsyntax', () => { + checkBinding('one:a//b', 'one:a//b'); + }); }); describe('parseTemplateBindings', () => { @@ -555,11 +596,12 @@ describe('parser', () => { }); describe('parseInterpolation', () => { - it('should return null if no interpolation', - () => { expect(parseInterpolation('nothing')).toBe(null); }); + it('should return null if no interpolation', () => { + expect(parseInterpolation('nothing')).toBe(null); + }); it('should parse no prefix/suffix interpolation', () => { - const ast = parseInterpolation('{{a}}') !.ast as Interpolation; + const ast = parseInterpolation('{{a}}')!.ast as Interpolation; expect(ast.strings).toEqual(['', '']); expect(ast.expressions.length).toEqual(1); expect(ast.expressions[0].name).toEqual('a'); @@ -567,22 +609,23 @@ describe('parser', () => { it('should parse prefix/suffix with multiple interpolation', () => { const originalExp = 'before {{ a }} middle {{ b }} after'; - const ast = parseInterpolation(originalExp) !.ast; + const ast = parseInterpolation(originalExp)!.ast; expect(unparse(ast)).toEqual(originalExp); validate(ast); }); it('should report empty interpolation expressions', () => { expectError( - parseInterpolation('{{}}') !, - 'Blank expressions are not allowed in interpolated strings'); + parseInterpolation('{{}}')!, 'Blank expressions are not allowed in interpolated strings'); expectError( - parseInterpolation('foo {{ }}') !, + parseInterpolation('foo {{ }}')!, 'Parser Error: Blank expressions are not allowed in interpolated strings'); }); - it('should parse conditional expression', () => { checkInterpolation('{{ a < b ? a : b }}'); }); + it('should parse conditional expression', () => { + checkInterpolation('{{ a < b ? a : b }}'); + }); it('should parse expression with newline characters', () => { checkInterpolation(`{{ 'foo' +\n 'bar' +\r 'baz' }}`, `{{ "foo" + "bar" + "baz" }}`); @@ -591,15 +634,16 @@ describe('parser', () => { it('should support custom interpolation', () => { const parser = new Parser(new Lexer()); const ast = - parser.parseInterpolation('{% a %}', null, 0, {start: '{%', end: '%}'}) !.ast as any; + parser.parseInterpolation('{% a %}', null, 0, {start: '{%', end: '%}'})!.ast as any; expect(ast.strings).toEqual(['', '']); expect(ast.expressions.length).toEqual(1); expect(ast.expressions[0].name).toEqual('a'); }); describe('comments', () => { - it('should ignore comments in interpolation expressions', - () => { checkInterpolation('{{a //comment}}', '{{ a }}'); }); + it('should ignore comments in interpolation expressions', () => { + checkInterpolation('{{a //comment}}', '{{ a }}'); + }); it('should retain // in single quote strings', () => { checkInterpolation(`{{ 'http://www.google.com' }}`, `{{ "http://www.google.com" }}`); @@ -609,18 +653,19 @@ describe('parser', () => { checkInterpolation(`{{ "http://www.google.com" }}`, `{{ "http://www.google.com" }}`); }); - it('should ignore comments after string literals', - () => { checkInterpolation(`{{ "a//b" //comment }}`, `{{ "a//b" }}`); }); + it('should ignore comments after string literals', () => { + checkInterpolation(`{{ "a//b" //comment }}`, `{{ "a//b" }}`); + }); it('should retain // in complex strings', () => { checkInterpolation( `{{"//a\'//b\`//c\`//d\'//e" //comment}}`, `{{ "//a\'//b\`//c\`//d\'//e" }}`); }); - it('should retain // in nested, unterminated strings', - () => { checkInterpolation(`{{ "a\'b\`" //comment}}`, `{{ "a\'b\`" }}`); }); + it('should retain // in nested, unterminated strings', () => { + checkInterpolation(`{{ "a\'b\`" //comment}}`, `{{ "a\'b\`" }}`); + }); }); - }); describe('parseSimpleBinding', () => { @@ -670,12 +715,12 @@ describe('parser', () => { describe('offsets', () => { it('should retain the offsets of an interpolation', () => { - const interpolations = splitInterpolation('{{a}} {{b}} {{c}}') !; + const interpolations = splitInterpolation('{{a}} {{b}} {{c}}')!; expect(interpolations.offsets).toEqual([2, 9, 16]); }); it('should retain the offsets into the expression AST of interpolations', () => { - const source = parseInterpolation('{{a}} {{b}} {{c}}') !; + const source = parseInterpolation('{{a}} {{b}} {{c}}')!; const interpolation = source.ast as Interpolation; expect(interpolation.expressions.map(e => e.span.start)).toEqual([2, 9, 16]); }); @@ -722,7 +767,7 @@ function parseSimpleBinding(text: string, location: any = null, offset: number = } function checkInterpolation(exp: string, expected?: string) { - const ast = parseInterpolation(exp) !; + const ast = parseInterpolation(exp)!; if (expected == null) expected = exp; expect(unparse(ast)).toEqual(expected); validate(ast); diff --git a/packages/compiler/test/expression_parser/utils/unparser.ts b/packages/compiler/test/expression_parser/utils/unparser.ts index e5ea2f9f89..fd2574e388 100644 --- a/packages/compiler/test/expression_parser/utils/unparser.ts +++ b/packages/compiler/test/expression_parser/utils/unparser.ts @@ -12,9 +12,9 @@ import {DEFAULT_INTERPOLATION_CONFIG, InterpolationConfig} from '../../../src/ml class Unparser implements AstVisitor { private static _quoteRegExp = /"/g; // TODO(issue/24571): remove '!'. - private _expression !: string; + private _expression!: string; // TODO(issue/24571): remove '!'. - private _interpolationConfig !: InterpolationConfig; + private _interpolationConfig!: InterpolationConfig; unparse(ast: AST, interpolationConfig: InterpolationConfig) { this._expression = ''; @@ -69,7 +69,7 @@ class Unparser implements AstVisitor { } visitFunctionCall(ast: FunctionCall, context: any) { - this._visit(ast.target !); + this._visit(ast.target!); this._expression += '('; let isFirst = true; ast.args.forEach(arg => { @@ -137,7 +137,7 @@ class Unparser implements AstVisitor { visitLiteralPrimitive(ast: LiteralPrimitive, context: any) { if (typeof ast.value === 'string') { - this._expression += `"${ast.value.replace( Unparser._quoteRegExp, '\"')}"`; + this._expression += `"${ast.value.replace(Unparser._quoteRegExp, '\"')}"`; } else { this._expression += `${ast.value}`; } @@ -186,7 +186,9 @@ class Unparser implements AstVisitor { this._expression += `${ast.prefix}:${ast.uninterpretedExpression}`; } - private _visit(ast: AST) { ast.visit(this); } + private _visit(ast: AST) { + ast.visit(this); + } } const sharedUnparser = new Unparser(); diff --git a/packages/compiler/test/expression_parser/utils/validator.ts b/packages/compiler/test/expression_parser/utils/validator.ts index cfdc851da5..2ddbbcd9c0 100644 --- a/packages/compiler/test/expression_parser/utils/validator.ts +++ b/packages/compiler/test/expression_parser/utils/validator.ts @@ -22,8 +22,8 @@ class ASTValidator extends RecursiveAstVisitor { if (!inSpan(ast.span, this.parentSpan)) { if (this.parentSpan) { const parentSpan = this.parentSpan as ParseSpan; - throw Error( - `Invalid AST span [expected (${ast.span.start}, ${ast.span.end}) to be in (${parentSpan.start}, ${parentSpan.end}) for ${unparse(ast)}`); + throw Error(`Invalid AST span [expected (${ast.span.start}, ${ast.span.end}) to be in (${ + parentSpan.start}, ${parentSpan.end}) for ${unparse(ast)}`); } else { throw Error(`Invalid root AST span for ${unparse(ast)}`); } @@ -111,7 +111,7 @@ class ASTValidator extends RecursiveAstVisitor { } } -function inSpan(span: ParseSpan, parentSpan: ParseSpan | undefined): parentSpan is ParseSpan { +function inSpan(span: ParseSpan, parentSpan: ParseSpan|undefined): parentSpan is ParseSpan { return !parentSpan || (span.start >= parentSpan.start && span.end <= parentSpan.end); } diff --git a/packages/compiler/test/i18n/digest_spec.ts b/packages/compiler/test/i18n/digest_spec.ts index e77e49735f..b37988d8ba 100644 --- a/packages/compiler/test/i18n/digest_spec.ts +++ b/packages/compiler/test/i18n/digest_spec.ts @@ -27,14 +27,17 @@ import {computeMsgId, digest, sha1} from '../../src/i18n/digest'; }); describe('sha1', () => { - it('should work on empty strings', - () => { expect(sha1('')).toEqual('da39a3ee5e6b4b0d3255bfef95601890afd80709'); }); + it('should work on empty strings', () => { + expect(sha1('')).toEqual('da39a3ee5e6b4b0d3255bfef95601890afd80709'); + }); - it('should returns the sha1 of "hello world"', - () => { expect(sha1('abc')).toEqual('a9993e364706816aba3e25717850c26c9cd0d89d'); }); + it('should returns the sha1 of "hello world"', () => { + expect(sha1('abc')).toEqual('a9993e364706816aba3e25717850c26c9cd0d89d'); + }); - it('should returns the sha1 of unicode strings', - () => { expect(sha1('你好,世界')).toEqual('3becb03b015ed48050611c8d7afe4b88f70d5a20'); }); + it('should returns the sha1 of unicode strings', () => { + expect(sha1('你好,世界')).toEqual('3becb03b015ed48050611c8d7afe4b88f70d5a20'); + }); it('should support arbitrary string size', () => { // node.js reference code: @@ -89,8 +92,9 @@ import {computeMsgId, digest, sha1} from '../../src/i18n/digest'; '': '4416290763660062288', }; - Object.keys(fixtures).forEach( - msg => { expect(computeMsgId(msg, '')).toEqual(fixtures[msg]); }); + Object.keys(fixtures).forEach(msg => { + expect(computeMsgId(msg, '')).toEqual(fixtures[msg]); + }); }); it('should work on well known inputs with meaning', () => { @@ -100,8 +104,9 @@ import {computeMsgId, digest, sha1} from '../../src/i18n/digest'; '3993998469942805487': ['View', 'Gmail UI'], }; - Object.keys(fixtures).forEach( - id => { expect(computeMsgId(fixtures[id][0], fixtures[id][1])).toEqual(id); }); + Object.keys(fixtures).forEach(id => { + expect(computeMsgId(fixtures[id][0], fixtures[id][1])).toEqual(id); + }); }); it('should support arbitrary string size', () => { @@ -116,7 +121,6 @@ import {computeMsgId, digest, sha1} from '../../src/i18n/digest'; } expect(computeMsgId(result, '')).toEqual('2122606631351252558'); }); - }); }); } diff --git a/packages/compiler/test/i18n/extractor_merger_spec.ts b/packages/compiler/test/i18n/extractor_merger_spec.ts index 52eec7c548..7ad468fb1a 100644 --- a/packages/compiler/test/i18n/extractor_merger_spec.ts +++ b/packages/compiler/test/i18n/extractor_merger_spec.ts @@ -94,8 +94,9 @@ import {serializeNodes as serializeHtmlNodes} from '../ml_parser/util/util'; ]); }); - it('should not create a message for empty elements', - () => { expect(extract('
')).toEqual([]); }); + it('should not create a message for empty elements', () => { + expect(extract('
')).toEqual([]); + }); it('should ignore implicit elements in translatable elements', () => { expect(extract('

', ['p'])).toEqual([ @@ -138,7 +139,8 @@ import {serializeNodes as serializeHtmlNodes} from '../ml_parser/util/util'; ], [ [ - 'text', 'html, html, nested', '{count, plural, =0 {[html]}}', @@ -156,8 +158,9 @@ import {serializeNodes as serializeHtmlNodes} from '../ml_parser/util/util'; ]); }); - it('should not create a message for empty blocks', - () => { expect(extract(``)).toEqual([]); }); + it('should not create a message for empty blocks', () => { + expect(extract(``)).toEqual([]); + }); }); describe('ICU messages', () => { @@ -199,8 +202,9 @@ import {serializeNodes as serializeHtmlNodes} from '../ml_parser/util/util'; ]); }); - it('should not extract ICU messages outside of i18n sections', - () => { expect(extract('{count, plural, =0 {text}}')).toEqual([]); }); + it('should not extract ICU messages outside of i18n sections', () => { + expect(extract('{count, plural, =0 {text}}')).toEqual([]); + }); it('should ignore nested ICU messages', () => { expect(extract('
{count, plural, =0 { {sex, select, male {m}} }}
')) @@ -280,8 +284,9 @@ import {serializeNodes as serializeHtmlNodes} from '../ml_parser/util/util'; ]); }); - it('should not create a message for empty attributes', - () => { expect(extract('
')).toEqual([]); }); + it('should not create a message for empty attributes', () => { + expect(extract('
')).toEqual([]); + }); }); describe('implicit elements', () => { @@ -292,7 +297,7 @@ import {serializeNodes as serializeHtmlNodes} from '../ml_parser/util/util'; }); it('should allow nested implicit elements', () => { - let result: any[] = undefined !; + let result: any[] = undefined!; expect(() => { result = extract('
outer
inner
', ['div']); @@ -302,7 +307,6 @@ import {serializeNodes as serializeHtmlNodes} from '../ml_parser/util/util'; [['outer', 'inner'], '', '', ''], ]); }); - }); describe('implicit attributes', () => { @@ -341,7 +345,6 @@ import {serializeNodes as serializeHtmlNodes} from '../ml_parser/util/util'; ['Could not start a block inside a translatable section', '`; } + visitComment(comment: html.Comment, context: any): any { + return ``; + } visitExpansion(expansion: html.Expansion, context: any): any { return `{${expansion.switchValue}, ${expansion.type},${this._visitAll(expansion.cases)}}`; diff --git a/packages/compiler/test/ng_module_resolver_spec.ts b/packages/compiler/test/ng_module_resolver_spec.ts index d9b019c520..787f5ca93e 100644 --- a/packages/compiler/test/ng_module_resolver_spec.ts +++ b/packages/compiler/test/ng_module_resolver_spec.ts @@ -33,7 +33,9 @@ class SimpleClass {} describe('NgModuleResolver', () => { let resolver: NgModuleResolver; - beforeEach(() => { resolver = new NgModuleResolver(new JitReflector()); }); + beforeEach(() => { + resolver = new NgModuleResolver(new JitReflector()); + }); it('should read out the metadata from the class', () => { const moduleMetadata = resolver.resolve(SomeModule); @@ -66,6 +68,5 @@ class SimpleClass {} expect(resolver.resolve(ChildWithDecorator)).toEqual(new NgModule({id: 'c'})); }); - }); } diff --git a/packages/compiler/test/output/abstract_emitter_node_only_spec.ts b/packages/compiler/test/output/abstract_emitter_node_only_spec.ts index d70410d491..a95031527d 100644 --- a/packages/compiler/test/output/abstract_emitter_node_only_spec.ts +++ b/packages/compiler/test/output/abstract_emitter_node_only_spec.ts @@ -18,14 +18,16 @@ import {extractSourceMap, originalPositionFor} from '@angular/compiler/testing/s const fileB = new ParseSourceFile('b0b1b2b3b4b5b6b7b8b9', 'b.js'); let ctx: EmitterVisitorContext; - beforeEach(() => { ctx = EmitterVisitorContext.createRoot(); }); + beforeEach(() => { + ctx = EmitterVisitorContext.createRoot(); + }); it('should add source files to the source map', () => { ctx.print(createSourceSpan(fileA, 0), 'o0'); ctx.print(createSourceSpan(fileA, 1), 'o1'); ctx.print(createSourceSpan(fileB, 0), 'o2'); ctx.print(createSourceSpan(fileB, 1), 'o3'); - const sm = ctx.toSourceMapGenerator('o.ts').toJSON() !; + const sm = ctx.toSourceMapGenerator('o.ts').toJSON()!; expect(sm.sources).toEqual([fileA.url, fileB.url]); expect(sm.sourcesContent).toEqual([fileA.content, fileB.content]); }); @@ -43,7 +45,7 @@ import {extractSourceMap, originalPositionFor} from '@angular/compiler/testing/s it('should be able to shift the content', () => { ctx.print(createSourceSpan(fileA, 0), 'fileA-0'); - const sm = ctx.toSourceMapGenerator('o.ts', 10).toJSON() !; + const sm = ctx.toSourceMapGenerator('o.ts', 10).toJSON()!; expect(originalPositionFor(sm, {line: 11, column: 0})).toEqual({ line: 1, column: 0, @@ -111,9 +113,9 @@ import {extractSourceMap, originalPositionFor} from '@angular/compiler/testing/s // All lines / columns indexes are 0-based // Note: source-map line indexes are 1-based, column 0-based function expectMap( - ctx: EmitterVisitorContext, genLine: number, genCol: number, source: string | null = null, - srcLine: number | null = null, srcCol: number | null = null) { - const sm = ctx.toSourceMapGenerator('o.ts').toJSON() !; + ctx: EmitterVisitorContext, genLine: number, genCol: number, source: string|null = null, + srcLine: number|null = null, srcCol: number|null = null) { + const sm = ctx.toSourceMapGenerator('o.ts').toJSON()!; const genPosition = {line: genLine + 1, column: genCol}; const origPosition = originalPositionFor(sm, genPosition); // TODO: Review use of `any` here (#19904) @@ -124,7 +126,7 @@ function expectMap( // returns the number of segments per line function nbSegmentsPerLine(ctx: EmitterVisitorContext) { - const sm = ctx.toSourceMapGenerator('o.ts').toJSON() !; + const sm = ctx.toSourceMapGenerator('o.ts').toJSON()!; const lines = sm.mappings.split(';'); return lines.map(l => { const m = l.match(/,/g); diff --git a/packages/compiler/test/output/abstract_emitter_spec.ts b/packages/compiler/test/output/abstract_emitter_spec.ts index d28b439ff1..6f777c9b6b 100644 --- a/packages/compiler/test/output/abstract_emitter_spec.ts +++ b/packages/compiler/test/output/abstract_emitter_spec.ts @@ -11,24 +11,34 @@ import {escapeIdentifier} from '@angular/compiler/src/output/abstract_emitter'; { describe('AbstractEmitter', () => { describe('escapeIdentifier', () => { - it('should escape single quotes', - () => { expect(escapeIdentifier(`'`, false)).toEqual(`'\\''`); }); + it('should escape single quotes', () => { + expect(escapeIdentifier(`'`, false)).toEqual(`'\\''`); + }); - it('should escape backslash', - () => { expect(escapeIdentifier('\\', false)).toEqual(`'\\\\'`); }); + it('should escape backslash', () => { + expect(escapeIdentifier('\\', false)).toEqual(`'\\\\'`); + }); - it('should escape newlines', - () => { expect(escapeIdentifier('\n', false)).toEqual(`'\\n'`); }); + it('should escape newlines', () => { + expect(escapeIdentifier('\n', false)).toEqual(`'\\n'`); + }); - it('should escape carriage returns', - () => { expect(escapeIdentifier('\r', false)).toEqual(`'\\r'`); }); + it('should escape carriage returns', () => { + expect(escapeIdentifier('\r', false)).toEqual(`'\\r'`); + }); - it('should escape $', () => { expect(escapeIdentifier('$', true)).toEqual(`'\\$'`); }); - it('should not escape $', () => { expect(escapeIdentifier('$', false)).toEqual(`'$'`); }); - it('should add quotes for non-identifiers', - () => { expect(escapeIdentifier('==', false, false)).toEqual(`'=='`); }); - it('does not escape class (but it probably should)', - () => { expect(escapeIdentifier('class', false, false)).toEqual('class'); }); + it('should escape $', () => { + expect(escapeIdentifier('$', true)).toEqual(`'\\$'`); + }); + it('should not escape $', () => { + expect(escapeIdentifier('$', false)).toEqual(`'$'`); + }); + it('should add quotes for non-identifiers', () => { + expect(escapeIdentifier('==', false, false)).toEqual(`'=='`); + }); + it('does not escape class (but it probably should)', () => { + expect(escapeIdentifier('class', false, false)).toEqual('class'); + }); }); }); } 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 1ec2bb4e3c..c2b5aac10a 100644 --- a/packages/compiler/test/output/js_emitter_node_only_spec.ts +++ b/packages/compiler/test/output/js_emitter_node_only_spec.ts @@ -22,12 +22,14 @@ const someGenFilePath = 'somePackage/someGenFile'; let emitter: JavaScriptEmitter; let someVar: o.ReadVarExpr; - beforeEach(() => { emitter = new JavaScriptEmitter(); }); + beforeEach(() => { + emitter = new JavaScriptEmitter(); + }); - function emitSourceMap(stmt: o.Statement | o.Statement[], preamble?: string): SourceMap { + function emitSourceMap(stmt: o.Statement|o.Statement[], preamble?: string): SourceMap { const stmts = Array.isArray(stmt) ? stmt : [stmt]; const source = emitter.emitStatements(someGenFilePath, stmts, preamble); - return extractSourceMap(source) !; + return extractSourceMap(source)!; } describe('source maps', () => { diff --git a/packages/compiler/test/output/js_emitter_spec.ts b/packages/compiler/test/output/js_emitter_spec.ts index a60c15c706..a2f7d11999 100644 --- a/packages/compiler/test/output/js_emitter_spec.ts +++ b/packages/compiler/test/output/js_emitter_spec.ts @@ -171,8 +171,9 @@ const externalModuleIdentifier = new o.ExternalReference(anotherModuleUrl, 'some }); it('should support function statements', () => { - expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], [ - ]))).toEqual(['function someFn() {', '}'].join('\n')); + expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], []))).toEqual([ + 'function someFn() {', '}' + ].join('\n')); expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], [], null, [o.StmtModifier.Exported]))) .toEqual([ 'function someFn() {', '}', @@ -181,8 +182,9 @@ const externalModuleIdentifier = new o.ExternalReference(anotherModuleUrl, 'some expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], [ new o.ReturnStatement(o.literal(1)) ]))).toEqual(['function someFn() {', ' return 1;', '}'].join('\n')); - expect(emitStmt(new o.DeclareFunctionStmt('someFn', [new o.FnParam('param1')], [ - ]))).toEqual(['function someFn(param1) {', '}'].join('\n')); + expect(emitStmt(new o.DeclareFunctionStmt('someFn', [new o.FnParam('param1')], []))).toEqual([ + 'function someFn(param1) {', '}' + ].join('\n')); }); it('should support comments', () => { @@ -219,25 +221,29 @@ const externalModuleIdentifier = new o.ExternalReference(anotherModuleUrl, 'some ].join('\n')); }); - it('should support support throwing', - () => { expect(emitStmt(new o.ThrowStmt(someVar))).toEqual('throw someVar;'); }); + it('should support support throwing', () => { + expect(emitStmt(new o.ThrowStmt(someVar))).toEqual('throw someVar;'); + }); describe('classes', () => { let callSomeMethod: o.Statement; - beforeEach(() => { callSomeMethod = o.THIS_EXPR.callMethod('someMethod', []).toStmt(); }); + beforeEach(() => { + callSomeMethod = o.THIS_EXPR.callMethod('someMethod', []).toStmt(); + }); it('should support declaring classes', () => { - expect(emitStmt(new o.ClassStmt('SomeClass', null !, [], [], null !, [ - ]))).toEqual(['function SomeClass() {', '}'].join('\n')); + expect(emitStmt(new o.ClassStmt('SomeClass', null!, [], [], null!, []))).toEqual([ + 'function SomeClass() {', '}' + ].join('\n')); expect(emitStmt(new o.ClassStmt( - 'SomeClass', null !, [], [], null !, [], [o.StmtModifier.Exported]))) + 'SomeClass', null!, [], [], null!, [], [o.StmtModifier.Exported]))) .toEqual([ 'function SomeClass() {', '}', `Object.defineProperty(exports, 'SomeClass', { get: function() { return SomeClass; }});` ].join('\n')); - expect(emitStmt( - new o.ClassStmt('SomeClass', o.variable('SomeSuperClass'), [], [], null !, []))) + expect( + emitStmt(new o.ClassStmt('SomeClass', o.variable('SomeSuperClass'), [], [], null!, []))) .toEqual([ 'function SomeClass() {', '}', 'SomeClass.prototype = Object.create(SomeSuperClass.prototype);' @@ -247,23 +253,22 @@ const externalModuleIdentifier = new o.ExternalReference(anotherModuleUrl, 'some it('should support declaring constructors', () => { const superCall = o.SUPER_EXPR.callFn([o.variable('someParam')]).toStmt(); expect(emitStmt(new o.ClassStmt( - 'SomeClass', null !, [], [], new o.ClassMethod(null !, [], []), []))) + 'SomeClass', null!, [], [], new o.ClassMethod(null!, [], []), []))) .toEqual(['function SomeClass() {', '}'].join('\n')); expect(emitStmt(new o.ClassStmt( - 'SomeClass', null !, [], [], - new o.ClassMethod(null !, [new o.FnParam('someParam')], []), []))) + 'SomeClass', null!, [], [], + new o.ClassMethod(null!, [new o.FnParam('someParam')], []), []))) .toEqual(['function SomeClass(someParam) {', '}'].join('\n')); expect(emitStmt(new o.ClassStmt( 'SomeClass', o.variable('SomeSuperClass'), [], [], - new o.ClassMethod(null !, [], [superCall]), []))) + new o.ClassMethod(null!, [], [superCall]), []))) .toEqual([ 'function SomeClass() {', ' var self = this;', ' SomeSuperClass.call(this, someParam);', '}', 'SomeClass.prototype = Object.create(SomeSuperClass.prototype);' ].join('\n')); - expect( - emitStmt(new o.ClassStmt( - 'SomeClass', null !, [], [], new o.ClassMethod(null !, [], [callSomeMethod]), []))) + expect(emitStmt(new o.ClassStmt( + 'SomeClass', null!, [], [], new o.ClassMethod(null!, [], [callSomeMethod]), []))) .toEqual([ 'function SomeClass() {', ' var self = this;', ' self.someMethod();', '}' ].join('\n')); @@ -271,14 +276,14 @@ const externalModuleIdentifier = new o.ExternalReference(anotherModuleUrl, 'some it('should support declaring getters', () => { expect(emitStmt(new o.ClassStmt( - 'SomeClass', null !, [], [new o.ClassGetter('someGetter', [])], null !, []))) + 'SomeClass', null!, [], [new o.ClassGetter('someGetter', [])], null!, []))) .toEqual([ 'function SomeClass() {', '}', `Object.defineProperty(SomeClass.prototype, 'someGetter', { get: function() {`, `}});` ].join('\n')); expect(emitStmt(new o.ClassStmt( - 'SomeClass', null !, [], [new o.ClassGetter('someGetter', [callSomeMethod])], - null !, []))) + 'SomeClass', null!, [], [new o.ClassGetter('someGetter', [callSomeMethod])], + null!, []))) .toEqual([ 'function SomeClass() {', '}', `Object.defineProperty(SomeClass.prototype, 'someGetter', { get: function() {`, @@ -288,19 +293,19 @@ const externalModuleIdentifier = new o.ExternalReference(anotherModuleUrl, 'some it('should support methods', () => { expect(emitStmt(new o.ClassStmt( - 'SomeClass', null !, [], [], null !, [new o.ClassMethod('someMethod', [], [])]))) + 'SomeClass', null!, [], [], null!, [new o.ClassMethod('someMethod', [], [])]))) .toEqual([ 'function SomeClass() {', '}', 'SomeClass.prototype.someMethod = function() {', '};' ].join('\n')); expect(emitStmt(new o.ClassStmt( - 'SomeClass', null !, [], [], null !, + 'SomeClass', null!, [], [], null!, [new o.ClassMethod('someMethod', [new o.FnParam('someParam')], [])]))) .toEqual([ 'function SomeClass() {', '}', 'SomeClass.prototype.someMethod = function(someParam) {', '};' ].join('\n')); expect(emitStmt(new o.ClassStmt( - 'SomeClass', null !, [], [], null !, + 'SomeClass', null!, [], [], null!, [new o.ClassMethod('someMethod', [], [callSomeMethod])]))) .toEqual([ 'function SomeClass() {', '}', 'SomeClass.prototype.someMethod = function() {', diff --git a/packages/compiler/test/output/output_ast_spec.ts b/packages/compiler/test/output/output_ast_spec.ts index 2af2adaa99..41c7fbaca1 100644 --- a/packages/compiler/test/output/output_ast_spec.ts +++ b/packages/compiler/test/output/output_ast_spec.ts @@ -15,8 +15,8 @@ import * as o from '../../src/output/output_ast'; const ref1 = new o.ExternalReference('aModule', 'name1'); const ref2 = new o.ExternalReference('aModule', 'name2'); const stmt = - o.variable('test').set(o.NULL_EXPR).toDeclStmt(o.importType(ref1, [o.importType( - ref2) !])); + o.variable('test').set(o.NULL_EXPR).toDeclStmt(o.importType(ref1, [o.importType(ref2)! + ])); expect(o.collectExternalReferences([stmt])).toEqual([ref1, ref2]); }); diff --git a/packages/compiler/test/output/output_jit_spec.ts b/packages/compiler/test/output/output_jit_spec.ts index eadc9292c5..d978ecda22 100644 --- a/packages/compiler/test/output/output_jit_spec.ts +++ b/packages/compiler/test/output/output_jit_spec.ts @@ -57,7 +57,7 @@ const anotherModuleUrl = 'somePackage/someOtherPath'; o.literal('use strict').toStmt(), ], ctx); - const matches = ctx.toSource().match(/'use strict';/g) !; + const matches = ctx.toSource().match(/'use strict';/g)!; expect(matches.length).toBe(1); }); }); diff --git a/packages/compiler/test/output/source_map_spec.ts b/packages/compiler/test/output/source_map_spec.ts index ea76893e11..2d80d01e09 100644 --- a/packages/compiler/test/output/source_map_spec.ts +++ b/packages/compiler/test/output/source_map_spec.ts @@ -52,7 +52,7 @@ import {SourceMapGenerator, toBase64String} from '@angular/compiler/src/output/s .addMapping(3, 'a.js', 5, 2); // Generated with https://sokra.github.io/source-map-visualization using a TS source map - expect(map.toJSON() !.mappings) + expect(map.toJSON()!.mappings) .toEqual( 'AAAA,IAAM,CAAC,GAAe,CAAC,CAAC;AACxB,IAAM,CAAC,GAAG,CAAC,CAAC;AAEZ,EAAE,CAAC,OAAO,CAAC,UAAA,CAAC;IACR,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;AACvB,CAAC,CAAC,CAAA'); }); @@ -64,7 +64,7 @@ import {SourceMapGenerator, toBase64String} from '@angular/compiler/src/output/s .addSource('url.ts', null) .addLine() .addMapping(0, 'inline.ts', 0, 0) - .toJSON() !; + .toJSON()!; expect(map.file).toEqual('out.js'); expect(map.sources).toEqual(['inline.ts', 'url.ts']); @@ -81,7 +81,11 @@ import {SourceMapGenerator, toBase64String} from '@angular/compiler/src/output/s describe('encodeB64String', () => { it('should return the b64 encoded value', () => { - [['', ''], ['a', 'YQ=='], ['Foo', 'Rm9v'], ['Foo1', 'Rm9vMQ=='], ['Foo12', 'Rm9vMTI='], + [['', ''], + ['a', 'YQ=='], + ['Foo', 'Rm9v'], + ['Foo1', 'Rm9vMQ=='], + ['Foo12', 'Rm9vMTI='], ['Foo123', 'Rm9vMTIz'], ].forEach(([src, b64]) => expect(toBase64String(src)).toEqual(b64)); }); @@ -113,7 +117,7 @@ import {SourceMapGenerator, toBase64String} from '@angular/compiler/src/output/s it('should throw when adding segments without column', () => { expect(() => { - new SourceMapGenerator('out.js').addSource('in.js').addLine().addMapping(null !); + new SourceMapGenerator('out.js').addSource('in.js').addLine().addMapping(null!); }).toThrowError('The column in the generated code must be provided'); }); diff --git a/packages/compiler/test/output/ts_emitter_node_only_spec.ts b/packages/compiler/test/output/ts_emitter_node_only_spec.ts index b1ce420096..0e6c75e457 100644 --- a/packages/compiler/test/output/ts_emitter_node_only_spec.ts +++ b/packages/compiler/test/output/ts_emitter_node_only_spec.ts @@ -31,10 +31,10 @@ const someGenFilePath = 'somePackage/someGenFile'; someVar = o.variable('someVar'); }); - function emitSourceMap(stmt: o.Statement | o.Statement[], preamble?: string): SourceMap { + function emitSourceMap(stmt: o.Statement|o.Statement[], preamble?: string): SourceMap { const stmts = Array.isArray(stmt) ? stmt : [stmt]; const source = emitter.emitStatements(someGenFilePath, stmts, preamble); - return extractSourceMap(source) !; + return extractSourceMap(source)!; } describe('source maps', () => { diff --git a/packages/compiler/test/output/ts_emitter_spec.ts b/packages/compiler/test/output/ts_emitter_spec.ts index cc287c22a8..e499de649b 100644 --- a/packages/compiler/test/output/ts_emitter_spec.ts +++ b/packages/compiler/test/output/ts_emitter_spec.ts @@ -35,7 +35,7 @@ const externalModuleIdentifier = new o.ExternalReference(anotherModuleUrl, 'some someVar = o.variable('someVar', null, null); }); - function emitStmt(stmt: o.Statement | o.Statement[], preamble?: string): string { + function emitStmt(stmt: o.Statement|o.Statement[], preamble?: string): string { const stmts = Array.isArray(stmt) ? stmt : [stmt]; const source = emitter.emitStatements(someGenFilePath, stmts, preamble); return stripSourceMapAndNewLine(source); @@ -223,15 +223,17 @@ const externalModuleIdentifier = new o.ExternalReference(anotherModuleUrl, 'some }); it('should support function statements', () => { - expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], [ - ]))).toEqual(['function someFn():void {', '}'].join('\n')); + expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], []))).toEqual([ + 'function someFn():void {', '}' + ].join('\n')); expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], [], null, [o.StmtModifier.Exported]))) .toEqual(['export function someFn():void {', '}'].join('\n')); expect(emitStmt(new o.DeclareFunctionStmt( 'someFn', [], [new o.ReturnStatement(o.literal(1))], o.INT_TYPE))) .toEqual(['function someFn():number {', ' return 1;', '}'].join('\n')); - expect(emitStmt(new o.DeclareFunctionStmt('someFn', [new o.FnParam('param1', o.INT_TYPE)], [ - ]))).toEqual(['function someFn(param1:number):void {', '}'].join('\n')); + expect( + emitStmt(new o.DeclareFunctionStmt('someFn', [new o.FnParam('param1', o.INT_TYPE)], []))) + .toEqual(['function someFn(param1:number):void {', '}'].join('\n')); }); it('should support comments', () => { @@ -266,43 +268,47 @@ const externalModuleIdentifier = new o.ExternalReference(anotherModuleUrl, 'some ].join('\n')); }); - it('should support support throwing', - () => { expect(emitStmt(new o.ThrowStmt(someVar))).toEqual('throw someVar;'); }); + it('should support support throwing', () => { + expect(emitStmt(new o.ThrowStmt(someVar))).toEqual('throw someVar;'); + }); describe('classes', () => { let callSomeMethod: o.Statement; - beforeEach(() => { callSomeMethod = o.THIS_EXPR.callMethod('someMethod', []).toStmt(); }); + beforeEach(() => { + callSomeMethod = o.THIS_EXPR.callMethod('someMethod', []).toStmt(); + }); it('should support declaring classes', () => { - expect(emitStmt(new o.ClassStmt('SomeClass', null !, [], [], null !, [ - ]))).toEqual(['class SomeClass {', '}'].join('\n')); - expect(emitStmt(new o.ClassStmt('SomeClass', null !, [], [], null !, [], [ + expect(emitStmt(new o.ClassStmt('SomeClass', null!, [], [], null!, []))).toEqual([ + 'class SomeClass {', '}' + ].join('\n')); + expect(emitStmt(new o.ClassStmt('SomeClass', null!, [], [], null!, [], [ o.StmtModifier.Exported ]))).toEqual(['export class SomeClass {', '}'].join('\n')); - expect(emitStmt(new o.ClassStmt('SomeClass', o.variable('SomeSuperClass'), [], [], null !, [ - ]))).toEqual(['class SomeClass extends SomeSuperClass {', '}'].join('\n')); + expect( + emitStmt(new o.ClassStmt('SomeClass', o.variable('SomeSuperClass'), [], [], null!, []))) + .toEqual(['class SomeClass extends SomeSuperClass {', '}'].join('\n')); }); it('should support declaring constructors', () => { const superCall = o.SUPER_EXPR.callFn([o.variable('someParam')]).toStmt(); expect(emitStmt(new o.ClassStmt( - 'SomeClass', null !, [], [], new o.ClassMethod(null !, [], []), []))) + 'SomeClass', null!, [], [], new o.ClassMethod(null!, [], []), []))) .toEqual(['class SomeClass {', ' constructor() {', ' }', '}'].join('\n')); expect(emitStmt(new o.ClassStmt( - 'SomeClass', null !, [], [], - new o.ClassMethod(null !, [new o.FnParam('someParam', o.INT_TYPE)], []), []))) + 'SomeClass', null!, [], [], + new o.ClassMethod(null!, [new o.FnParam('someParam', o.INT_TYPE)], []), []))) .toEqual( ['class SomeClass {', ' constructor(someParam:number) {', ' }', '}'].join('\n')); expect(emitStmt(new o.ClassStmt( - 'SomeClass', null !, [], [], new o.ClassMethod(null !, [], [superCall]), []))) + 'SomeClass', null!, [], [], new o.ClassMethod(null!, [], [superCall]), []))) .toEqual([ 'class SomeClass {', ' constructor() {', ' super(someParam);', ' }', '}' ].join('\n')); - expect( - emitStmt(new o.ClassStmt( - 'SomeClass', null !, [], [], new o.ClassMethod(null !, [], [callSomeMethod]), []))) + expect(emitStmt(new o.ClassStmt( + 'SomeClass', null!, [], [], new o.ClassMethod(null!, [], [callSomeMethod]), []))) .toEqual([ 'class SomeClass {', ' constructor() {', ' this.someMethod();', ' }', '}' ].join('\n')); @@ -310,57 +316,56 @@ const externalModuleIdentifier = new o.ExternalReference(anotherModuleUrl, 'some it('should support declaring fields', () => { expect(emitStmt(new o.ClassStmt( - 'SomeClass', null !, [new o.ClassField('someField')], [], null !, []))) + 'SomeClass', null!, [new o.ClassField('someField')], [], null!, []))) .toEqual(['class SomeClass {', ' someField:any;', '}'].join('\n')); - expect( - emitStmt(new o.ClassStmt( - 'SomeClass', null !, [new o.ClassField('someField', o.INT_TYPE)], [], null !, []))) + expect(emitStmt(new o.ClassStmt( + 'SomeClass', null!, [new o.ClassField('someField', o.INT_TYPE)], [], null!, []))) .toEqual(['class SomeClass {', ' someField:number;', '}'].join('\n')); expect(emitStmt(new o.ClassStmt( - 'SomeClass', null !, - [new o.ClassField('someField', o.INT_TYPE, [o.StmtModifier.Private])], [], - null !, []))) + 'SomeClass', null!, + [new o.ClassField('someField', o.INT_TYPE, [o.StmtModifier.Private])], [], null!, + []))) .toEqual(['class SomeClass {', ' /*private*/ someField:number;', '}'].join('\n')); }); it('should support declaring getters', () => { expect(emitStmt(new o.ClassStmt( - 'SomeClass', null !, [], [new o.ClassGetter('someGetter', [])], null !, []))) + 'SomeClass', null!, [], [new o.ClassGetter('someGetter', [])], null!, []))) .toEqual(['class SomeClass {', ' get someGetter():any {', ' }', '}'].join('\n')); expect(emitStmt(new o.ClassStmt( - 'SomeClass', null !, [], [new o.ClassGetter('someGetter', [], o.INT_TYPE)], - null !, []))) + 'SomeClass', null!, [], [new o.ClassGetter('someGetter', [], o.INT_TYPE)], null!, + []))) .toEqual(['class SomeClass {', ' get someGetter():number {', ' }', '}'].join('\n')); expect(emitStmt(new o.ClassStmt( - 'SomeClass', null !, [], [new o.ClassGetter('someGetter', [callSomeMethod])], - null !, []))) + 'SomeClass', null!, [], [new o.ClassGetter('someGetter', [callSomeMethod])], + null!, []))) .toEqual([ 'class SomeClass {', ' get someGetter():any {', ' this.someMethod();', ' }', '}' ].join('\n')); - expect(emitStmt(new o.ClassStmt( - 'SomeClass', null !, [], - [new o.ClassGetter('someGetter', [], null !, [o.StmtModifier.Private])], null !, - []))) + expect( + emitStmt(new o.ClassStmt( + 'SomeClass', null!, [], + [new o.ClassGetter('someGetter', [], null!, [o.StmtModifier.Private])], null!, []))) .toEqual( ['class SomeClass {', ' private get someGetter():any {', ' }', '}'].join('\n')); }); it('should support methods', () => { - expect(emitStmt(new o.ClassStmt('SomeClass', null !, [], [], null !, [ + expect(emitStmt(new o.ClassStmt('SomeClass', null!, [], [], null!, [ new o.ClassMethod('someMethod', [], []) ]))).toEqual(['class SomeClass {', ' someMethod():void {', ' }', '}'].join('\n')); - expect(emitStmt(new o.ClassStmt('SomeClass', null !, [], [], null !, [ + expect(emitStmt(new o.ClassStmt('SomeClass', null!, [], [], null!, [ new o.ClassMethod('someMethod', [], [], o.INT_TYPE) ]))).toEqual(['class SomeClass {', ' someMethod():number {', ' }', '}'].join('\n')); expect( emitStmt(new o.ClassStmt( - 'SomeClass', null !, [], [], null !, + 'SomeClass', null!, [], [], null!, [new o.ClassMethod('someMethod', [new o.FnParam('someParam', o.INT_TYPE)], [])]))) .toEqual([ 'class SomeClass {', ' someMethod(someParam:number):void {', ' }', '}' ].join('\n')); expect(emitStmt(new o.ClassStmt( - 'SomeClass', null !, [], [], null !, + 'SomeClass', null!, [], [], null!, [new o.ClassMethod('someMethod', [], [callSomeMethod])]))) .toEqual([ 'class SomeClass {', ' someMethod():void {', ' this.someMethod();', ' }', '}' @@ -412,7 +417,7 @@ const externalModuleIdentifier = new o.ExternalReference(anotherModuleUrl, 'some it('should support combined types', () => { const writeVarExpr = o.variable('a').set(o.NULL_EXPR); - expect(emitStmt(writeVarExpr.toDeclStmt(new o.ArrayType(null !)))) + expect(emitStmt(writeVarExpr.toDeclStmt(new o.ArrayType(null!)))) .toEqual('var a:any[] = (null as any);'); expect(emitStmt(writeVarExpr.toDeclStmt(new o.ArrayType(o.INT_TYPE)))) .toEqual('var a:number[] = (null as any);'); diff --git a/packages/compiler/test/pipe_resolver_spec.ts b/packages/compiler/test/pipe_resolver_spec.ts index e338973107..bff6ce1263 100644 --- a/packages/compiler/test/pipe_resolver_spec.ts +++ b/packages/compiler/test/pipe_resolver_spec.ts @@ -21,7 +21,9 @@ class SimpleClass {} describe('PipeResolver', () => { let resolver: PipeResolver; - beforeEach(() => { resolver = new PipeResolver(new JitReflector()); }); + beforeEach(() => { + resolver = new PipeResolver(new JitReflector()); + }); it('should read out the metadata from the class', () => { const moduleMetadata = resolver.resolve(SomePipe); @@ -48,6 +50,5 @@ class SimpleClass {} expect(resolver.resolve(ChildWithDecorator)).toEqual(new Pipe({name: 'c'})); }); - }); } diff --git a/packages/compiler/test/render3/r3_ast_spans_spec.ts b/packages/compiler/test/render3/r3_ast_spans_spec.ts index e8e6931c49..f0d65ce50d 100644 --- a/packages/compiler/test/render3/r3_ast_spans_spec.ts +++ b/packages/compiler/test/render3/r3_ast_spans_spec.ts @@ -74,18 +74,24 @@ class R3AstSourceSpans implements t.Visitor { ['BoundEvent', humanizeSpan(event.sourceSpan), humanizeSpan(event.handlerSpan)]); } - visitText(text: t.Text) { this.result.push(['Text', humanizeSpan(text.sourceSpan)]); } + visitText(text: t.Text) { + this.result.push(['Text', humanizeSpan(text.sourceSpan)]); + } visitBoundText(text: t.BoundText) { this.result.push(['BoundText', humanizeSpan(text.sourceSpan)]); } - visitIcu(icu: t.Icu) { return null; } + visitIcu(icu: t.Icu) { + return null; + } - private visitAll(nodes: t.Node[][]) { nodes.forEach(node => t.visitAll(this, node)); } + private visitAll(nodes: t.Node[][]) { + nodes.forEach(node => t.visitAll(this, node)); + } } -function humanizeSpan(span: ParseSourceSpan | null | undefined): string { +function humanizeSpan(span: ParseSourceSpan|null|undefined): string { if (span === null || span === undefined) { return ``; } diff --git a/packages/compiler/test/render3/r3_template_transform_spec.ts b/packages/compiler/test/render3/r3_template_transform_spec.ts index 13f5e1b41c..f43177fdcb 100644 --- a/packages/compiler/test/render3/r3_template_transform_spec.ts +++ b/packages/compiler/test/render3/r3_template_transform_spec.ts @@ -75,13 +75,21 @@ class R3AstHumanizer implements t.Visitor { ]); } - visitText(text: t.Text) { this.result.push(['Text', text.value]); } + visitText(text: t.Text) { + this.result.push(['Text', text.value]); + } - visitBoundText(text: t.BoundText) { this.result.push(['BoundText', unparse(text.value)]); } + visitBoundText(text: t.BoundText) { + this.result.push(['BoundText', unparse(text.value)]); + } - visitIcu(icu: t.Icu) { return null; } + visitIcu(icu: t.Icu) { + return null; + } - private visitAll(nodes: t.Node[][]) { nodes.forEach(node => t.visitAll(this, node)); } + private visitAll(nodes: t.Node[][]) { + nodes.forEach(node => t.visitAll(this, node)); + } } function expectFromHtml(html: string) { @@ -97,13 +105,14 @@ function expectFromR3Nodes(nodes: t.Node[]) { function expectSpanFromHtml(html: string) { const {nodes} = parse(html); - return expect(nodes[0] !.sourceSpan.toString()); + return expect(nodes[0]!.sourceSpan.toString()); } describe('R3 template transform', () => { describe('ParseSpan on nodes toString', () => { - it('should create valid text span on Element with adjacent start and end tags', - () => { expectSpanFromHtml('
').toBe('
'); }); + it('should create valid text span on Element with adjacent start and end tags', () => { + expectSpanFromHtml('
').toBe('
'); + }); }); describe('Nodes without binding', () => { diff --git a/packages/compiler/test/render3/style_parser_spec.ts b/packages/compiler/test/render3/style_parser_spec.ts index 4f44f6aebb..e0d9533bec 100644 --- a/packages/compiler/test/render3/style_parser_spec.ts +++ b/packages/compiler/test/render3/style_parser_spec.ts @@ -90,7 +90,8 @@ describe('style parsing', () => { expect(hyphenate('-fooBar-man')).toEqual('-foo-bar-man'); }); - it('should make everything lowercase', - () => { expect(hyphenate('-WebkitAnimation')).toEqual('-webkit-animation'); }); + it('should make everything lowercase', () => { + expect(hyphenate('-WebkitAnimation')).toEqual('-webkit-animation'); + }); }); }); diff --git a/packages/compiler/test/render3/util/expression.ts b/packages/compiler/test/render3/util/expression.ts index 65de317e1f..30d7dafa05 100644 --- a/packages/compiler/test/render3/util/expression.ts +++ b/packages/compiler/test/render3/util/expression.ts @@ -15,7 +15,9 @@ type HumanizedExpressionSource = [string, AbsoluteSourceSpan]; class ExpressionSourceHumanizer extends e.RecursiveAstVisitor implements t.Visitor { result: HumanizedExpressionSource[] = []; - private recordAst(ast: e.AST) { this.result.push([unparse(ast), ast.sourceSpan]); } + private recordAst(ast: e.AST) { + this.result.push([unparse(ast), ast.sourceSpan]); + } // This method is defined to reconcile the type of ExpressionSourceHumanizer // since both RecursiveAstVisitor and Visitor define the visit() method in @@ -124,11 +126,19 @@ class ExpressionSourceHumanizer extends e.RecursiveAstVisitor implements t.Visit } visitReference(ast: t.Reference) {} visitVariable(ast: t.Variable) {} - visitEvent(ast: t.BoundEvent) { ast.handler.visit(this); } + visitEvent(ast: t.BoundEvent) { + ast.handler.visit(this); + } visitTextAttribute(ast: t.TextAttribute) {} - visitBoundAttribute(ast: t.BoundAttribute) { ast.value.visit(this); } - visitBoundEvent(ast: t.BoundEvent) { ast.handler.visit(this); } - visitBoundText(ast: t.BoundText) { ast.value.visit(this); } + visitBoundAttribute(ast: t.BoundAttribute) { + ast.value.visit(this); + } + visitBoundEvent(ast: t.BoundEvent) { + ast.handler.visit(this); + } + visitBoundText(ast: t.BoundText) { + ast.value.visit(this); + } visitContent(ast: t.Content) {} visitText(ast: t.Text) {} visitIcu(ast: t.Icu) {} diff --git a/packages/compiler/test/render3/view/binding_spec.ts b/packages/compiler/test/render3/view/binding_spec.ts index e47c57d18b..15cad85c71 100644 --- a/packages/compiler/test/render3/view/binding_spec.ts +++ b/packages/compiler/test/render3/view/binding_spec.ts @@ -54,8 +54,9 @@ describe('t2 binding', () => { const binder = new R3TargetBinder(new SelectorMatcher()); const res = binder.bind({template: template.nodes}); - const itemBinding = (findExpression(template.nodes, '{{item.name}}') !as e.Interpolation) - .expressions[0] as e.PropertyRead; + const itemBinding = + (findExpression(template.nodes, '{{item.name}}')! as e.Interpolation).expressions[0] as + e.PropertyRead; const item = itemBinding.receiver; const itemTarget = res.getExpressionTarget(item); if (!(itemTarget instanceof a.Variable)) { @@ -64,7 +65,7 @@ describe('t2 binding', () => { expect(itemTarget.value).toBe('$implicit'); const itemTemplate = res.getTemplateOfSymbol(itemTarget); expect(itemTemplate).not.toBeNull(); - expect(res.getNestingLevel(itemTemplate !)).toBe(1); + expect(res.getNestingLevel(itemTemplate!)).toBe(1); }); it('should match directives when binding a simple template', () => { @@ -72,7 +73,7 @@ describe('t2 binding', () => { const binder = new R3TargetBinder(makeSelectorMatcher()); const res = binder.bind({template: template.nodes}); const tmpl = template.nodes[0] as a.Template; - const directives = res.getDirectivesOfNode(tmpl) !; + const directives = res.getDirectivesOfNode(tmpl)!; expect(directives).not.toBeNull(); expect(directives.length).toBe(1); expect(directives[0].name).toBe('NgFor'); @@ -92,7 +93,7 @@ describe('t2 binding', () => { const res = binder.bind({template: template.nodes}); const svgNode = template.nodes[0] as a.Element; const textNode = svgNode.children[0] as a.Element; - const directives = res.getDirectivesOfNode(textNode) !; + const directives = res.getDirectivesOfNode(textNode)!; expect(directives).not.toBeNull(); expect(directives.length).toBe(1); expect(directives[0].name).toBe('Dir'); @@ -103,11 +104,11 @@ describe('t2 binding', () => { const binder = new R3TargetBinder(makeSelectorMatcher()); const res = binder.bind({template: template.nodes}); const tmpl = template.nodes[0] as a.Template; - const tmplDirectives = res.getDirectivesOfNode(tmpl) !; + const tmplDirectives = res.getDirectivesOfNode(tmpl)!; expect(tmplDirectives).not.toBeNull(); expect(tmplDirectives.length).toBe(1); expect(tmplDirectives[0].name).toBe('NgFor'); - const elDirectives = res.getDirectivesOfNode(tmpl.children[0] as a.Element) !; + const elDirectives = res.getDirectivesOfNode(tmpl.children[0] as a.Element)!; expect(elDirectives).not.toBeNull(); expect(elDirectives.length).toBe(1); expect(elDirectives[0].name).toBe('Dir'); diff --git a/packages/compiler/test/render3/view/i18n_spec.ts b/packages/compiler/test/render3/view/i18n_spec.ts index afa3778075..ffdb57fee1 100644 --- a/packages/compiler/test/render3/view/i18n_spec.ts +++ b/packages/compiler/test/render3/view/i18n_spec.ts @@ -21,7 +21,7 @@ import {formatI18nPlaceholderName} from '../../../src/render3/view/i18n/util'; import {parseR3 as parse} from './util'; const expressionParser = new Parser(new Lexer()); -const i18nOf = (element: t.Node & {i18n?: i18n.I18nMeta}) => element.i18n !; +const i18nOf = (element: t.Node&{i18n?: i18n.I18nMeta}) => element.i18n!; describe('I18nContext', () => { it('should support i18n content collection', () => { @@ -74,7 +74,7 @@ describe('I18nContext', () => { const [boundTextB, elementC, boundTextC] = (elementB as t.Element).children; // simulate I18nContext for a given template - const ctx = new I18nContext(1, o.variable('ctx'), 0, null, root.i18n !); + const ctx = new I18nContext(1, o.variable('ctx'), 0, null, root.i18n!); // set data for root ctx ctx.appendBoundText(i18nOf(boundTextA)); @@ -87,7 +87,7 @@ describe('I18nContext', () => { expect(ctx.isResolved).toBe(false); // create child context - const childCtx = ctx.forkChildContext(2, 1, (templateA as t.Template).i18n !); + const childCtx = ctx.forkChildContext(2, 1, (templateA as t.Template).i18n!); expect(childCtx.bindings.size).toBe(0); expect(childCtx.isRoot).toBe(false); @@ -116,11 +116,12 @@ describe('I18nContext', () => { const expected = new Map([ ['INTERPOLATION', '�0�'], ['START_TAG_DIV', '�#0�|�#1:1�'], ['START_BOLD_TEXT', '�*1:1��#0:1�'], ['CLOSE_BOLD_TEXT', '�/#0:1��/*1:1�'], - ['CLOSE_TAG_DIV', '�/#0�|�/#1:1�'], ['INTERPOLATION_1', '�0:1�'], - ['INTERPOLATION_2', '�1:1�'] + ['CLOSE_TAG_DIV', '�/#0�|�/#1:1�'], ['INTERPOLATION_1', '�0:1�'], ['INTERPOLATION_2', '�1:1�'] ]); const phs = ctx.getSerializedPlaceholders(); - expected.forEach((value, key) => { expect(phs.get(key) !.join('|')).toEqual(value); }); + expected.forEach((value, key) => { + expect(phs.get(key)!.join('|')).toEqual(value); + }); // placeholders are added into the root ctx expect(phs.size).toBe(expected.size); @@ -152,7 +153,7 @@ describe('I18nContext', () => { const [textC] = (templateB as t.Template).children; // simulate I18nContext for a given template - const ctxLevelA = new I18nContext(0, o.variable('ctx'), 0, null, root.i18n !); + const ctxLevelA = new I18nContext(0, o.variable('ctx'), 0, null, root.i18n!); // create Level A context ctxLevelA.appendTemplate(i18nOf(templateA), 1); @@ -160,12 +161,12 @@ describe('I18nContext', () => { expect(ctxLevelA.isResolved).toBe(false); // create Level B context - const ctxLevelB = ctxLevelA.forkChildContext(0, 1, (templateA as t.Template).i18n !); + const ctxLevelB = ctxLevelA.forkChildContext(0, 1, (templateA as t.Template).i18n!); ctxLevelB.appendTemplate(i18nOf(templateB), 1); expect(ctxLevelB.isRoot).toBe(false); // create Level 2 context - const ctxLevelC = ctxLevelB.forkChildContext(0, 1, (templateB as t.Template).i18n !); + const ctxLevelC = ctxLevelB.forkChildContext(0, 1, (templateB as t.Template).i18n!); expect(ctxLevelC.isRoot).toBe(false); // reconcile @@ -176,7 +177,9 @@ describe('I18nContext', () => { const expected = new Map( [['START_TAG_NG-TEMPLATE', '�*1:1�|�*1:2�'], ['CLOSE_TAG_NG-TEMPLATE', '�/*1:2�|�/*1:1�']]); const phs = ctxLevelA.getSerializedPlaceholders(); - expected.forEach((value, key) => { expect(phs.get(key) !.join('|')).toEqual(value); }); + expected.forEach((value, key) => { + expect(phs.get(key)!.join('|')).toEqual(value); + }); // placeholders are added into the root ctx expect(phs.size).toBe(expected.size); @@ -195,8 +198,9 @@ describe('Utils', () => { ['START_TAG_NG-CONTAINER_1', 'startTagNgContainer_1'], ['CLOSE_TAG_ITALIC', 'closeTagItalic'], ['CLOSE_TAG_BOLD_1', 'closeTagBold_1'] ]; - cases.forEach( - ([input, output]) => { expect(formatI18nPlaceholderName(input)).toEqual(output); }); + cases.forEach(([input, output]) => { + expect(formatI18nPlaceholderName(input)).toEqual(output); + }); }); describe('metadata serialization', () => { @@ -292,8 +296,9 @@ describe('serializeI18nMessageForGetMsg', () => { return serializeI18nMessageForGetMsg(root.i18n as i18n.Message); }; - it('should serialize plain text for `GetMsg()`', - () => { expect(serialize('Some text')).toEqual('Some text'); }); + it('should serialize plain text for `GetMsg()`', () => { + expect(serialize('Some text')).toEqual('Some text'); + }); it('should serialize text with interpolation for `GetMsg()`', () => { expect(serialize('Some text {{ valueA }} and {{ valueB + valueC }}')) diff --git a/packages/compiler/test/render3/view/util.ts b/packages/compiler/test/render3/view/util.ts index df1268fed9..0ecf3ab42d 100644 --- a/packages/compiler/test/render3/view/util.ts +++ b/packages/compiler/test/render3/view/util.ts @@ -14,7 +14,7 @@ import {HtmlParser, ParseTreeResult} from '../../../src/ml_parser/html_parser'; import {WhitespaceVisitor} from '../../../src/ml_parser/html_whitespaces'; import {DEFAULT_INTERPOLATION_CONFIG, InterpolationConfig} from '../../../src/ml_parser/interpolation_config'; import * as a from '../../../src/render3/r3_ast'; -import {Render3ParseResult, htmlAstToRender3Ast} from '../../../src/render3/r3_template_transform'; +import {htmlAstToRender3Ast, Render3ParseResult} from '../../../src/render3/r3_template_transform'; import {I18nMetaVisitor} from '../../../src/render3/view/i18n/meta'; import {BindingParser} from '../../../src/template_parser/binding_parser'; import {MockSchemaRegistry} from '../../../testing'; diff --git a/packages/compiler/test/resource_loader_mock_spec.ts b/packages/compiler/test/resource_loader_mock_spec.ts index 760ff5fce0..8df73f398f 100644 --- a/packages/compiler/test/resource_loader_mock_spec.ts +++ b/packages/compiler/test/resource_loader_mock_spec.ts @@ -13,10 +13,12 @@ import {AsyncTestCompleter, beforeEach, describe, expect, inject, it} from '@ang describe('MockResourceLoader', () => { let resourceLoader: MockResourceLoader; - beforeEach(() => { resourceLoader = new MockResourceLoader(); }); + beforeEach(() => { + resourceLoader = new MockResourceLoader(); + }); function expectResponse( - request: Promise, url: string, response: string, done: () => void = null !) { + request: Promise, url: string, response: string, done: () => void = null!) { function onResponse(text: string): string { if (response === null) { throw `Unexpected response ${url} -> ${text}`; @@ -52,7 +54,7 @@ import {AsyncTestCompleter, beforeEach, describe, expect, inject, it} from '@ang it('should return an error from the definitions', inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { const url = '/foo'; - const response: string = null !; + const response: string = null!; resourceLoader.when(url, response); expectResponse(resourceLoader.get(url), url, response, () => async.done()); resourceLoader.flush(); @@ -70,7 +72,7 @@ import {AsyncTestCompleter, beforeEach, describe, expect, inject, it} from '@ang it('should return an error from the expectations', inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { const url = '/foo'; - const response: string = null !; + const response: string = null!; resourceLoader.expect(url, response); expectResponse(resourceLoader.get(url), url, response, () => async.done()); resourceLoader.flush(); @@ -82,7 +84,9 @@ import {AsyncTestCompleter, beforeEach, describe, expect, inject, it} from '@ang resourceLoader.expect(url, response); resourceLoader.get(url); resourceLoader.get(url); - expect(() => { resourceLoader.flush(); }).toThrowError('Unexpected request /foo'); + expect(() => { + resourceLoader.flush(); + }).toThrowError('Unexpected request /foo'); }); it('should return expectations before definitions', @@ -97,18 +101,24 @@ import {AsyncTestCompleter, beforeEach, describe, expect, inject, it} from '@ang it('should throw when there is no definitions or expectations', () => { resourceLoader.get('/foo'); - expect(() => { resourceLoader.flush(); }).toThrowError('Unexpected request /foo'); + expect(() => { + resourceLoader.flush(); + }).toThrowError('Unexpected request /foo'); }); it('should throw when flush is called without any pending requests', () => { - expect(() => { resourceLoader.flush(); }).toThrowError('No pending requests to flush'); + expect(() => { + resourceLoader.flush(); + }).toThrowError('No pending requests to flush'); }); it('should throw on unsatisfied expectations', () => { resourceLoader.expect('/foo', 'bar'); resourceLoader.when('/bar', 'foo'); resourceLoader.get('/bar'); - expect(() => { resourceLoader.flush(); }).toThrowError('Unsatisfied requests: /foo'); + expect(() => { + resourceLoader.flush(); + }).toThrowError('Unsatisfied requests: /foo'); }); }); } diff --git a/packages/compiler/test/runtime_compiler_spec.ts b/packages/compiler/test/runtime_compiler_spec.ts index 47423395bd..a66dfb3ccd 100644 --- a/packages/compiler/test/runtime_compiler_spec.ts +++ b/packages/compiler/test/runtime_compiler_spec.ts @@ -8,9 +8,11 @@ import {DirectiveResolver, ResourceLoader} from '@angular/compiler'; import {Compiler, Component, Injector, NgModule, NgModuleFactory, ɵstringify as stringify} from '@angular/core'; -import {TestBed, async, fakeAsync, inject, tick} from '@angular/core/testing'; +import {async, fakeAsync, inject, TestBed, tick} from '@angular/core/testing'; import {expect} from '@angular/platform-browser/testing/src/matchers'; + import {MockDirectiveResolver} from '../testing'; + import {SpyResourceLoader} from './spies'; @Component({selector: 'child-cmp'}) @@ -27,11 +29,12 @@ class SomeCompWithUrlTemplate { { describe('RuntimeCompiler', () => { - describe('compilerComponentSync', () => { describe('never resolving loader', () => { class StubResourceLoader { - get(url: string) { return new Promise(() => {}); } + get(url: string) { + return new Promise(() => {}); + } } beforeEach(() => { @@ -43,8 +46,8 @@ class SomeCompWithUrlTemplate { TestBed.configureTestingModule({declarations: [SomeCompWithUrlTemplate]}); TestBed.compileComponents().then(() => { expect(() => TestBed.createComponent(SomeCompWithUrlTemplate)) - .toThrowError( - `Can't compile synchronously as ${stringify(SomeCompWithUrlTemplate)} is still being loaded!`); + .toThrowError(`Can't compile synchronously as ${ + stringify(SomeCompWithUrlTemplate)} is still being loaded!`); }); })); @@ -55,15 +58,17 @@ class SomeCompWithUrlTemplate { TestBed.overrideComponent(SomeComp, {set: {template: ''}}); TestBed.compileComponents().then(() => { expect(() => TestBed.createComponent(SomeComp)) - .toThrowError( - `Can't compile synchronously as ${stringify(ChildComp)} is still being loaded!`); + .toThrowError(`Can't compile synchronously as ${ + stringify(ChildComp)} is still being loaded!`); }); }); }); describe('resolving loader', () => { class StubResourceLoader { - get(url: string) { return Promise.resolve('hello'); } + get(url: string) { + return Promise.resolve('hello'); + } } beforeEach(() => { @@ -88,7 +93,9 @@ class SomeCompWithUrlTemplate { let dirResolver: MockDirectiveResolver; let injector: Injector; - beforeEach(() => { TestBed.configureCompiler({providers: [SpyResourceLoader.PROVIDE]}); }); + beforeEach(() => { + TestBed.configureCompiler({providers: [SpyResourceLoader.PROVIDE]}); + }); beforeEach(fakeAsync(inject( [Compiler, ResourceLoader, DirectiveResolver, Injector], @@ -110,7 +117,7 @@ class SomeCompWithUrlTemplate { } resourceLoader.spy('get').and.callFake(() => Promise.resolve('hello')); - let ngModuleFactory: NgModuleFactory = undefined !; + let ngModuleFactory: NgModuleFactory = undefined!; compiler.compileModuleAsync(SomeModule).then((f) => ngModuleFactory = f); tick(); expect(ngModuleFactory.moduleType).toBe(SomeModule); @@ -126,8 +133,8 @@ class SomeCompWithUrlTemplate { resourceLoader.spy('get').and.callFake(() => Promise.resolve('')); expect(() => compiler.compileModuleSync(SomeModule)) - .toThrowError( - `Can't compile synchronously as ${stringify(SomeCompWithUrlTemplate)} is still being loaded!`); + .toThrowError(`Can't compile synchronously as ${ + stringify(SomeCompWithUrlTemplate)} is still being loaded!`); }); it('should throw when using a templateUrl in a nested component that has not been compiled before', diff --git a/packages/compiler/test/schema/dom_element_schema_registry_spec.ts b/packages/compiler/test/schema/dom_element_schema_registry_spec.ts index 7c6b98c67c..ef2a312633 100644 --- a/packages/compiler/test/schema/dom_element_schema_registry_spec.ts +++ b/packages/compiler/test/schema/dom_element_schema_registry_spec.ts @@ -19,7 +19,9 @@ import {extractSchema} from './schema_extractor'; { describe('DOMElementSchema', () => { let registry: DomElementSchemaRegistry; - beforeEach(() => { registry = new DomElementSchemaRegistry(); }); + beforeEach(() => { + registry = new DomElementSchemaRegistry(); + }); it('should detect elements', () => { expect(registry.hasElement('div', [])).toBeTruthy(); @@ -97,8 +99,9 @@ import {extractSchema} from './schema_extractor'; expect(registry.hasElement('unknown', [NO_ERRORS_SCHEMA])).toBeTruthy(); }); - it('should re-map property names that are specified in DOM facade', - () => { expect(registry.getMappedPropName('readonly')).toEqual('readOnly'); }); + it('should re-map property names that are specified in DOM facade', () => { + expect(registry.getMappedPropName('readonly')).toEqual('readOnly'); + }); it('should not re-map property names that are not specified in DOM facade', () => { expect(registry.getMappedPropName('title')).toEqual('title'); @@ -173,8 +176,9 @@ If 'onAnything' is a directive input, make sure the directive is imported by the }); describe('Angular custom elements', () => { - it('should support ', - () => { expect(registry.hasProperty('ng-container', 'id', [])).toBeFalsy(); }); + it('should support ', () => { + expect(registry.hasProperty('ng-container', 'id', [])).toBeFalsy(); + }); it('should support ', () => { expect(registry.hasProperty('ng-content', 'id', [])).toBeFalsy(); @@ -185,8 +189,9 @@ If 'onAnything' is a directive input, make sure the directive is imported by the if (browserDetection.isChromeDesktop) { it('generate a new schema', () => { let schema = '\n'; - extractSchema() !.forEach( - (props, name) => { schema += `'${name}|${props.join(',')}',\n`; }); + extractSchema()!.forEach((props, name) => { + schema += `'${name}|${props.join(',')}',\n`; + }); // Uncomment this line to see: // the generated schema which can then be pasted to the DomElementSchemaRegistry // console.log(schema); diff --git a/packages/compiler/test/schema/schema_extractor.ts b/packages/compiler/test/schema/schema_extractor.ts index ec25bda11b..27a9650d91 100644 --- a/packages/compiler/test/schema/schema_extractor.ts +++ b/packages/compiler/test/schema/schema_extractor.ts @@ -97,7 +97,9 @@ export function extractSchema(): Map|null { types.sort(); - types.forEach(type => { extractRecursiveProperties(visited, descMap, (window as any)[type]); }); + types.forEach(type => { + extractRecursiveProperties(visited, descMap, (window as any)[type]); + }); // Add elements missed by Chrome auto-detection Object.keys(MISSING_FROM_CHROME).forEach(elHierarchy => { @@ -125,7 +127,7 @@ function assertNoMissingTags(descMap: Map): void { function extractRecursiveProperties( visited: {[name: string]: boolean}, descMap: Map, type: Function): string { - const name = extractName(type) !; + const name = extractName(type)!; if (visited[name]) { return name; @@ -181,7 +183,7 @@ function extractProperties( const fullName = name + (superName ? '^' + superName : ''); - const props: string[] = descMap.has(fullName) ? descMap.get(fullName) ! : []; + const props: string[] = descMap.has(fullName) ? descMap.get(fullName)! : []; const prototype = type.prototype; const keys = Object.getOwnPropertyNames(prototype); diff --git a/packages/compiler/test/selector/selector_spec.ts b/packages/compiler/test/selector/selector_spec.ts index b61bc7ad44..33ecbe686a 100644 --- a/packages/compiler/test/selector/selector_spec.ts +++ b/packages/compiler/test/selector/selector_spec.ts @@ -17,13 +17,16 @@ import {el} from '@angular/platform-browser/testing/src/browser_util'; let s1: any[], s2: any[], s3: any[], s4: any[]; let matched: any[]; - function reset() { matched = []; } + function reset() { + matched = []; + } beforeEach(() => { reset(); - s1 = s2 = s3 = s4 = null !; - selectableCollector = - (selector: CssSelector, context: any) => { matched.push(selector, context); }; + s1 = s2 = s3 = s4 = null!; + selectableCollector = (selector: CssSelector, context: any) => { + matched.push(selector, context); + }; matcher = new SelectorMatcher(); }); @@ -128,7 +131,7 @@ import {el} from '@angular/platform-browser/testing/src/browser_util'; const elementSelector = new CssSelector(); const element = el('
'); - const empty = element.getAttribute('attr') !; + const empty = element.getAttribute('attr')!; elementSelector.addAttribute('some-decor', empty); matcher.match(elementSelector, selectableCollector); expect(matched).toEqual([s1[0], 1]); @@ -458,9 +461,13 @@ function getSelectorFor( const selector = new CssSelector(); selector.setElement(tag); - attrs.forEach(nameValue => { selector.addAttribute(nameValue[0], nameValue[1]); }); + attrs.forEach(nameValue => { + selector.addAttribute(nameValue[0], nameValue[1]); + }); - classes.trim().split(/\s+/g).forEach(cName => { selector.addClassName(cName); }); + classes.trim().split(/\s+/g).forEach(cName => { + selector.addClassName(cName); + }); return selector; } diff --git a/packages/compiler/test/shadow_css_spec.ts b/packages/compiler/test/shadow_css_spec.ts index 8bf0cd85e2..e23e7e28aa 100644 --- a/packages/compiler/test/shadow_css_spec.ts +++ b/packages/compiler/test/shadow_css_spec.ts @@ -6,12 +6,11 @@ * found in the LICENSE file at https://angular.io/license */ -import {CssRule, ShadowCss, processRules} from '@angular/compiler/src/shadow_css'; +import {CssRule, processRules, ShadowCss} from '@angular/compiler/src/shadow_css'; import {normalizeCSS} from '@angular/platform-browser/testing/src/browser_util'; { describe('ShadowCss', function() { - function s(css: string, contentAttr: string, hostAttr: string = '') { const shadowCss = new ShadowCss(); const shim = shadowCss.shimCssText(css, contentAttr, hostAttr); @@ -19,7 +18,9 @@ import {normalizeCSS} from '@angular/platform-browser/testing/src/browser_util'; return normalizeCSS(shim.replace(nlRegexp, '')); } - it('should handle empty string', () => { expect(s('', 'contenta')).toEqual(''); }); + it('should handle empty string', () => { + expect(s('', 'contenta')).toEqual(''); + }); it('should add an attribute to every rule', () => { const css = 'one {color: red;}two {color: red;}'; @@ -112,14 +113,17 @@ import {normalizeCSS} from '@angular/platform-browser/testing/src/browser_util'; }); describe((':host'), () => { - it('should handle no context', - () => { expect(s(':host {}', 'contenta', 'a-host')).toEqual('[a-host] {}'); }); + it('should handle no context', () => { + expect(s(':host {}', 'contenta', 'a-host')).toEqual('[a-host] {}'); + }); - it('should handle tag selector', - () => { expect(s(':host(ul) {}', 'contenta', 'a-host')).toEqual('ul[a-host] {}'); }); + it('should handle tag selector', () => { + expect(s(':host(ul) {}', 'contenta', 'a-host')).toEqual('ul[a-host] {}'); + }); - it('should handle class selector', - () => { expect(s(':host(.x) {}', 'contenta', 'a-host')).toEqual('.x[a-host] {}'); }); + it('should handle class selector', () => { + expect(s(':host(.x) {}', 'contenta', 'a-host')).toEqual('.x[a-host] {}'); + }); it('should handle attribute selector', () => { expect(s(':host([a="b"]) {}', 'contenta', 'a-host')).toEqual('[a="b"][a-host] {}'); @@ -285,14 +289,17 @@ import {normalizeCSS} from '@angular/platform-browser/testing/src/browser_util'; expect(css).toEqual('div[contenta] {height:calc(100% - 55px);}'); }); - it('should strip comments', - () => { expect(s('/* x */b {c}', 'contenta')).toEqual('b[contenta] {c}'); }); + it('should strip comments', () => { + expect(s('/* x */b {c}', 'contenta')).toEqual('b[contenta] {c}'); + }); - it('should ignore special characters in comments', - () => { expect(s('/* {;, */b {c}', 'contenta')).toEqual('b[contenta] {c}'); }); + it('should ignore special characters in comments', () => { + expect(s('/* {;, */b {c}', 'contenta')).toEqual('b[contenta] {c}'); + }); - it('should support multiline comments', - () => { expect(s('/* \n */b {c}', 'contenta')).toEqual('b[contenta] {c}'); }); + it('should support multiline comments', () => { + expect(s('/* \n */b {c}', 'contenta')).toEqual('b[contenta] {c}'); + }); it('should keep sourceMappingURL comments', () => { expect(s('b {c}/*# sourceMappingURL=data:x */', 'contenta')) @@ -318,13 +325,17 @@ import {normalizeCSS} from '@angular/platform-browser/testing/src/browser_util'; return result; } - it('should work with empty css', () => { expect(captureRules('')).toEqual([]); }); + it('should work with empty css', () => { + expect(captureRules('')).toEqual([]); + }); - it('should capture a rule without body', - () => { expect(captureRules('a;')).toEqual([new CssRule('a', '')]); }); + it('should capture a rule without body', () => { + expect(captureRules('a;')).toEqual([new CssRule('a', '')]); + }); - it('should capture css rules with body', - () => { expect(captureRules('a {b}')).toEqual([new CssRule('a', 'b')]); }); + it('should capture css rules with body', () => { + expect(captureRules('a {b}')).toEqual([new CssRule('a', 'b')]); + }); it('should capture css rules with nested rules', () => { expect(captureRules('a {b {c}} d {e}')).toEqual([ diff --git a/packages/compiler/test/spies.ts b/packages/compiler/test/spies.ts index 54c2263c4a..a5575dffb2 100644 --- a/packages/compiler/test/spies.ts +++ b/packages/compiler/test/spies.ts @@ -12,5 +12,7 @@ import {SpyObject} from '@angular/core/testing/src/testing_internal'; export class SpyResourceLoader extends SpyObject { public static PROVIDE = {provide: ResourceLoader, useClass: SpyResourceLoader, deps: []}; - constructor() { super(ResourceLoader); } + constructor() { + super(ResourceLoader); + } } diff --git a/packages/compiler/test/style_url_resolver_spec.ts b/packages/compiler/test/style_url_resolver_spec.ts index ef8aebdb5c..c91f3cc687 100644 --- a/packages/compiler/test/style_url_resolver_spec.ts +++ b/packages/compiler/test/style_url_resolver_spec.ts @@ -13,7 +13,9 @@ import {UrlResolver} from '@angular/compiler/src/url_resolver'; describe('extractStyleUrls', () => { let urlResolver: UrlResolver; - beforeEach(() => { urlResolver = new UrlResolver(); }); + beforeEach(() => { + urlResolver = new UrlResolver(); + }); it('should not resolve "url()" urls', () => { const css = ` @@ -104,23 +106,25 @@ import {UrlResolver} from '@angular/compiler/src/url_resolver'; expect(styleWithImports.style.trim()).toEqual(``); expect(styleWithImports.styleUrls).toEqual(['fake_resolved_url']); }); - }); describe('isStyleUrlResolvable', () => { - it('should resolve relative urls', - () => { expect(isStyleUrlResolvable('someUrl.css')).toBe(true); }); + it('should resolve relative urls', () => { + expect(isStyleUrlResolvable('someUrl.css')).toBe(true); + }); - it('should resolve package: urls', - () => { expect(isStyleUrlResolvable('package:someUrl.css')).toBe(true); }); + it('should resolve package: urls', () => { + expect(isStyleUrlResolvable('package:someUrl.css')).toBe(true); + }); it('should not resolve empty urls', () => { - expect(isStyleUrlResolvable(null !)).toBe(false); + expect(isStyleUrlResolvable(null!)).toBe(false); expect(isStyleUrlResolvable('')).toBe(false); }); - it('should not resolve urls with other schema', - () => { expect(isStyleUrlResolvable('http://otherurl')).toBe(false); }); + it('should not resolve urls with other schema', () => { + expect(isStyleUrlResolvable('http://otherurl')).toBe(false); + }); it('should not resolve urls with absolute paths', () => { expect(isStyleUrlResolvable('/otherurl')).toBe(false); @@ -130,7 +134,11 @@ import {UrlResolver} from '@angular/compiler/src/url_resolver'; } class FakeUrlResolver extends UrlResolver { - constructor() { super(); } + constructor() { + super(); + } - resolve(baseUrl: string, url: string): string { return 'fake_resolved_url'; } + resolve(baseUrl: string, url: string): string { + return 'fake_resolved_url'; + } } diff --git a/packages/compiler/test/template_parser/binding_parser_spec.ts b/packages/compiler/test/template_parser/binding_parser_spec.ts index 77656c6bc4..0d4f737203 100644 --- a/packages/compiler/test/template_parser/binding_parser_spec.ts +++ b/packages/compiler/test/template_parser/binding_parser_spec.ts @@ -16,16 +16,18 @@ import {calcPossibleSecurityContexts} from '../../src/template_parser/binding_pa describe('BindingParser', () => { let registry: ElementSchemaRegistry; - beforeEach(inject( - [ElementSchemaRegistry], (_registry: ElementSchemaRegistry) => { registry = _registry; })); + beforeEach(inject([ElementSchemaRegistry], (_registry: ElementSchemaRegistry) => { + registry = _registry; + })); describe('possibleSecurityContexts', () => { function hrefSecurityContexts(selector: string) { return calcPossibleSecurityContexts(registry, selector, 'href', false); } - it('should return a single security context if the selector as an element name', - () => { expect(hrefSecurityContexts('a')).toEqual([SecurityContext.URL]); }); + it('should return a single security context if the selector as an element name', () => { + expect(hrefSecurityContexts('a')).toEqual([SecurityContext.URL]); + }); it('should return the possible security contexts if the selector has no element name', () => { expect(hrefSecurityContexts('[myDir]')).toEqual([ @@ -45,8 +47,9 @@ import {calcPossibleSecurityContexts} from '../../src/template_parser/binding_pa ]); }); - it('should return SecurityContext.NONE if there are no possible elements', - () => { expect(hrefSecurityContexts('img:not(img)')).toEqual([SecurityContext.NONE]); }); + it('should return SecurityContext.NONE if there are no possible elements', () => { + expect(hrefSecurityContexts('img:not(img)')).toEqual([SecurityContext.NONE]); + }); it('should return the union of the possible security contexts if multiple selectors are specified', () => { diff --git a/packages/compiler/test/template_parser/template_parser_absolute_span_spec.ts b/packages/compiler/test/template_parser/template_parser_absolute_span_spec.ts index ca2b1b3ad8..7547490e5b 100644 --- a/packages/compiler/test/template_parser/template_parser_absolute_span_spec.ts +++ b/packages/compiler/test/template_parser/template_parser_absolute_span_spec.ts @@ -36,7 +36,7 @@ describe('expression AST absolute source spans', () => { beforeEach(inject([TemplateParser], (parser: TemplateParser) => { parse = (template: string, directives: CompileDirectiveSummary[] = [], - pipes: CompilePipeSummary[] | null = null, schemas: SchemaMetadata[] = [], + pipes: CompilePipeSummary[]|null = null, schemas: SchemaMetadata[] = [], preserveWhitespaces = true): TemplateAst[] => { if (pipes === null) { pipes = []; diff --git a/packages/compiler/test/template_parser/template_parser_spec.ts b/packages/compiler/test/template_parser/template_parser_spec.ts index af03ee7ca7..2b5c8498e1 100644 --- a/packages/compiler/test/template_parser/template_parser_spec.ts +++ b/packages/compiler/test/template_parser/template_parser_spec.ts @@ -9,19 +9,20 @@ import {preserveWhitespacesDefault} from '@angular/compiler'; import {CompileDiDependencyMetadata, CompileDirectiveMetadata, CompileDirectiveSummary, CompilePipeMetadata, CompilePipeSummary, CompileProviderMetadata, CompileTemplateMetadata, CompileTokenMetadata, tokenReference} from '@angular/compiler/src/compile_metadata'; import {DomElementSchemaRegistry} from '@angular/compiler/src/schema/dom_element_schema_registry'; import {ElementSchemaRegistry} from '@angular/compiler/src/schema/element_schema_registry'; -import {AttrAst, BoundDirectivePropertyAst, BoundElementPropertyAst, BoundEventAst, BoundTextAst, DirectiveAst, ElementAst, EmbeddedTemplateAst, NgContentAst, PropertyBindingType, ProviderAstType, ReferenceAst, TemplateAst, TemplateAstVisitor, TextAst, VariableAst, templateVisitAll} from '@angular/compiler/src/template_parser/template_ast'; -import {TemplateParser, splitClasses} from '@angular/compiler/src/template_parser/template_parser'; +import {AttrAst, BoundDirectivePropertyAst, BoundElementPropertyAst, BoundEventAst, BoundTextAst, DirectiveAst, ElementAst, EmbeddedTemplateAst, NgContentAst, PropertyBindingType, ProviderAstType, ReferenceAst, TemplateAst, TemplateAstVisitor, templateVisitAll, TextAst, VariableAst} from '@angular/compiler/src/template_parser/template_ast'; +import {splitClasses, TemplateParser} from '@angular/compiler/src/template_parser/template_parser'; import {SchemaMetadata, SecurityContext} from '@angular/core'; import {Console} from '@angular/core/src/console'; -import {TestBed, inject} from '@angular/core/testing'; +import {inject, TestBed} from '@angular/core/testing'; import {JitReflector} from '@angular/platform-browser-dynamic/src/compiler_reflector'; -import {Identifiers, createTokenForExternalReference, createTokenForReference} from '../../src/identifiers'; +import {createTokenForExternalReference, createTokenForReference, Identifiers} from '../../src/identifiers'; import {DEFAULT_INTERPOLATION_CONFIG, InterpolationConfig} from '../../src/ml_parser/interpolation_config'; import {newArray} from '../../src/util'; import {MockSchemaRegistry} from '../../testing'; import {unparse} from '../expression_parser/utils/unparser'; import {TEST_COMPILER_PROVIDERS} from '../test_bindings'; + import {compileDirectiveMetadataCreate, compileTemplateMetadata, createTypeMeta} from './util/metadata'; const someModuleUrl = 'package:someModule'; @@ -139,7 +140,7 @@ class TemplateHumanizer implements TemplateAstVisitor { private _appendSourceSpan(ast: TemplateAst, input: any[]): any[] { if (!this.includeSourceSpan) return input; - input.push(ast.sourceSpan !.toString()); + input.push(ast.sourceSpan!.toString()); return input; } } @@ -166,11 +167,21 @@ class TemplateContentProjectionHumanizer implements TemplateAstVisitor { templateVisitAll(this, ast.children); return null; } - visitReference(ast: ReferenceAst, context: any): any { return null; } - visitVariable(ast: VariableAst, context: any): any { return null; } - visitEvent(ast: BoundEventAst, context: any): any { return null; } - visitElementProperty(ast: BoundElementPropertyAst, context: any): any { return null; } - visitAttr(ast: AttrAst, context: any): any { return null; } + visitReference(ast: ReferenceAst, context: any): any { + return null; + } + visitVariable(ast: VariableAst, context: any): any { + return null; + } + visitEvent(ast: BoundEventAst, context: any): any { + return null; + } + visitElementProperty(ast: BoundElementPropertyAst, context: any): any { + return null; + } + visitAttr(ast: AttrAst, context: any): any { + return null; + } visitBoundText(ast: BoundTextAst, context: any): any { this.result.push([`#text(${unparse(ast.value)})`, ast.ngContentIndex]); return null; @@ -179,22 +190,48 @@ class TemplateContentProjectionHumanizer implements TemplateAstVisitor { this.result.push([`#text(${ast.value})`, ast.ngContentIndex]); return null; } - visitDirective(ast: DirectiveAst, context: any): any { return null; } - visitDirectiveProperty(ast: BoundDirectivePropertyAst, context: any): any { return null; } + visitDirective(ast: DirectiveAst, context: any): any { + return null; + } + visitDirectiveProperty(ast: BoundDirectivePropertyAst, context: any): any { + return null; + } } class ThrowingVisitor implements TemplateAstVisitor { - visitNgContent(ast: NgContentAst, context: any): any { throw 'not implemented'; } - visitEmbeddedTemplate(ast: EmbeddedTemplateAst, context: any): any { throw 'not implemented'; } - visitElement(ast: ElementAst, context: any): any { throw 'not implemented'; } - visitReference(ast: ReferenceAst, context: any): any { throw 'not implemented'; } - visitVariable(ast: VariableAst, context: any): any { throw 'not implemented'; } - visitEvent(ast: BoundEventAst, context: any): any { throw 'not implemented'; } - visitElementProperty(ast: BoundElementPropertyAst, context: any): any { throw 'not implemented'; } - visitAttr(ast: AttrAst, context: any): any { throw 'not implemented'; } - visitBoundText(ast: BoundTextAst, context: any): any { throw 'not implemented'; } - visitText(ast: TextAst, context: any): any { throw 'not implemented'; } - visitDirective(ast: DirectiveAst, context: any): any { throw 'not implemented'; } + visitNgContent(ast: NgContentAst, context: any): any { + throw 'not implemented'; + } + visitEmbeddedTemplate(ast: EmbeddedTemplateAst, context: any): any { + throw 'not implemented'; + } + visitElement(ast: ElementAst, context: any): any { + throw 'not implemented'; + } + visitReference(ast: ReferenceAst, context: any): any { + throw 'not implemented'; + } + visitVariable(ast: VariableAst, context: any): any { + throw 'not implemented'; + } + visitEvent(ast: BoundEventAst, context: any): any { + throw 'not implemented'; + } + visitElementProperty(ast: BoundElementPropertyAst, context: any): any { + throw 'not implemented'; + } + visitAttr(ast: AttrAst, context: any): any { + throw 'not implemented'; + } + visitBoundText(ast: BoundTextAst, context: any): any { + throw 'not implemented'; + } + visitText(ast: TextAst, context: any): any { + throw 'not implemented'; + } + visitDirective(ast: DirectiveAst, context: any): any { + throw 'not implemented'; + } visitDirectiveProperty(ast: BoundDirectivePropertyAst, context: any): any { throw 'not implemented'; } @@ -236,2014 +273,2012 @@ class NullVisitor implements TemplateAstVisitor { class ArrayConsole implements Console { logs: string[] = []; warnings: string[] = []; - log(msg: string) { this.logs.push(msg); } - warn(msg: string) { this.warnings.push(msg); } + log(msg: string) { + this.logs.push(msg); + } + warn(msg: string) { + this.warnings.push(msg); + } } (function() { - let ngIf: CompileDirectiveSummary; - let parse: ( - template: string, directives: CompileDirectiveSummary[], pipes?: CompilePipeSummary[], - schemas?: SchemaMetadata[], preserveWhitespaces?: boolean) => TemplateAst[]; - let console: ArrayConsole; +let ngIf: CompileDirectiveSummary; +let parse: ( + template: string, directives: CompileDirectiveSummary[], pipes?: CompilePipeSummary[], + schemas?: SchemaMetadata[], preserveWhitespaces?: boolean) => TemplateAst[]; +let console: ArrayConsole; - function configureCompiler() { - console = new ArrayConsole(); - beforeEach(() => { - TestBed.configureCompiler({ - providers: [ - {provide: Console, useValue: console}, - ], - }); +function configureCompiler() { + console = new ArrayConsole(); + beforeEach(() => { + TestBed.configureCompiler({ + providers: [ + {provide: Console, useValue: console}, + ], }); + }); +} + +function commonBeforeEach() { + beforeEach(inject([TemplateParser], (parser: TemplateParser) => { + const someAnimation = ['someAnimation']; + const someTemplate = compileTemplateMetadata({animations: [someAnimation]}); + const component = compileDirectiveMetadataCreate({ + isHost: false, + selector: 'root', + template: someTemplate, + type: createTypeMeta({reference: {filePath: someModuleUrl, name: 'Root'}}), + isComponent: true + }); + ngIf = compileDirectiveMetadataCreate({ + selector: '[ngIf]', + template: someTemplate, + type: createTypeMeta({reference: {filePath: someModuleUrl, name: 'NgIf'}}), + inputs: ['ngIf'] + }).toSummary(); + + parse = + (template: string, directives: CompileDirectiveSummary[], + pipes: CompilePipeSummary[]|null = null, schemas: SchemaMetadata[] = [], + preserveWhitespaces = true): TemplateAst[] => { + if (pipes === null) { + pipes = []; + } + return parser + .parse( + component, template, directives, pipes, schemas, 'TestComp', preserveWhitespaces) + .template; + }; + })); +} + +describe('TemplateAstVisitor', () => { + function expectVisitedNode(visitor: TemplateAstVisitor, node: TemplateAst) { + expect(node.visit(visitor, null)).toEqual(node); } - function commonBeforeEach() { - beforeEach(inject([TemplateParser], (parser: TemplateParser) => { - const someAnimation = ['someAnimation']; - const someTemplate = compileTemplateMetadata({animations: [someAnimation]}); - const component = compileDirectiveMetadataCreate({ - isHost: false, - selector: 'root', - template: someTemplate, - type: createTypeMeta({reference: {filePath: someModuleUrl, name: 'Root'}}), - isComponent: true - }); - ngIf = compileDirectiveMetadataCreate({ - selector: '[ngIf]', - template: someTemplate, - type: createTypeMeta({reference: {filePath: someModuleUrl, name: 'NgIf'}}), - inputs: ['ngIf'] - }).toSummary(); + it('should visit NgContentAst', () => { + expectVisitedNode(new class extends NullVisitor { + visitNgContent(ast: NgContentAst, context: any): any { + return ast; + } + }, new NgContentAst(0, 0, null!)); + }); - parse = - (template: string, directives: CompileDirectiveSummary[], - pipes: CompilePipeSummary[] | null = null, schemas: SchemaMetadata[] = [], - preserveWhitespaces = true): TemplateAst[] => { - if (pipes === null) { - pipes = []; - } - return parser - .parse( - component, template, directives, pipes, schemas, 'TestComp', - preserveWhitespaces) - .template; - }; - })); - } + it('should visit EmbeddedTemplateAst', () => { + expectVisitedNode(new class extends NullVisitor { + visitEmbeddedTemplate(ast: EmbeddedTemplateAst, context: any) { + return ast; + } + }, new EmbeddedTemplateAst([], [], [], [], [], [], false, [], [], 0, null!)); + }); - describe('TemplateAstVisitor', () => { - function expectVisitedNode(visitor: TemplateAstVisitor, node: TemplateAst) { - expect(node.visit(visitor, null)).toEqual(node); + it('should visit ElementAst', () => { + expectVisitedNode(new class extends NullVisitor { + visitElement(ast: ElementAst, context: any) { + return ast; + } + }, new ElementAst('foo', [], [], [], [], [], [], false, [], [], 0, null!, null!)); + }); + + it('should visit RefererenceAst', () => { + expectVisitedNode(new class extends NullVisitor { + visitReference(ast: ReferenceAst, context: any): any { + return ast; + } + }, new ReferenceAst('foo', null!, null!, null!)); + }); + + it('should visit VariableAst', () => { + expectVisitedNode(new class extends NullVisitor { + visitVariable(ast: VariableAst, context: any): any { + return ast; + } + }, new VariableAst('foo', 'bar', null!)); + }); + + it('should visit BoundEventAst', () => { + expectVisitedNode(new class extends NullVisitor { + visitEvent(ast: BoundEventAst, context: any): any { + return ast; + } + }, new BoundEventAst('foo', 'bar', 'goo', null!, null!, null!)); + }); + + it('should visit BoundElementPropertyAst', () => { + expectVisitedNode(new class extends NullVisitor { + visitElementProperty(ast: BoundElementPropertyAst, context: any): any { + return ast; + } + }, new BoundElementPropertyAst('foo', null!, null!, null!, 'bar', null!)); + }); + + it('should visit AttrAst', () => { + expectVisitedNode(new class extends NullVisitor { + visitAttr(ast: AttrAst, context: any): any { + return ast; + } + }, new AttrAst('foo', 'bar', null!)); + }); + + it('should visit BoundTextAst', () => { + expectVisitedNode(new class extends NullVisitor { + visitBoundText(ast: BoundTextAst, context: any): any { + return ast; + } + }, new BoundTextAst(null!, 0, null!)); + }); + + it('should visit TextAst', () => { + expectVisitedNode(new class extends NullVisitor { + visitText(ast: TextAst, context: any): any { + return ast; + } + }, new TextAst('foo', 0, null!)); + }); + + it('should visit DirectiveAst', () => { + expectVisitedNode(new class extends NullVisitor { + visitDirective(ast: DirectiveAst, context: any): any { + return ast; + } + }, new DirectiveAst(null!, [], [], [], 0, null!)); + }); + + it('should visit DirectiveAst', () => { + expectVisitedNode(new class extends NullVisitor { + visitDirectiveProperty(ast: BoundDirectivePropertyAst, context: any): any { + return ast; + } + }, new BoundDirectivePropertyAst('foo', 'bar', null!, null!)); + }); + + it('should skip the typed call of a visitor if visit() returns a truthy value', () => { + const visitor = new class extends ThrowingVisitor { + visit(ast: TemplateAst, context: any): any { + return true; + } + }; + const nodes: TemplateAst[] = [ + new NgContentAst(0, 0, null!), + new EmbeddedTemplateAst([], [], [], [], [], [], false, [], [], 0, null!), + new ElementAst('foo', [], [], [], [], [], [], false, [], [], 0, null!, null!), + new ReferenceAst('foo', null!, 'bar', null!), new VariableAst('foo', 'bar', null!), + new BoundEventAst('foo', 'bar', 'goo', null!, null!, null!), + new BoundElementPropertyAst('foo', null!, null!, null!, 'bar', null!), + new AttrAst('foo', 'bar', null!), new BoundTextAst(null!, 0, null!), + new TextAst('foo', 0, null!), new DirectiveAst(null!, [], [], [], 0, null!), + new BoundDirectivePropertyAst('foo', 'bar', null!, null!) + ]; + const result = templateVisitAll(visitor, nodes, null); + expect(result).toEqual(newArray(nodes.length).fill(true)); + }); +}); + +describe('TemplateParser Security', () => { + // Semi-integration test to make sure TemplateParser properly sets the security context. + // Uses the actual DomElementSchemaRegistry. + beforeEach(() => { + TestBed.configureCompiler({ + providers: [ + TEST_COMPILER_PROVIDERS, + {provide: ElementSchemaRegistry, useClass: DomElementSchemaRegistry, deps: []} + ] + }); + }); + + configureCompiler(); + commonBeforeEach(); + + describe('security context', () => { + function secContext(tpl: string): SecurityContext { + const ast = parse(tpl, []); + const propBinding = (ast[0]).inputs[0]; + return propBinding.securityContext; } - it('should visit NgContentAst', () => { - expectVisitedNode( - new class extends - NullVisitor{visitNgContent(ast: NgContentAst, context: any): any{return ast;}}, - new NgContentAst(0, 0, null !)); + it('should set for properties', () => { + expect(secContext('
')).toBe(SecurityContext.NONE); + expect(secContext('
')).toBe(SecurityContext.HTML); }); - - it('should visit EmbeddedTemplateAst', () => { - expectVisitedNode( - new class extends NullVisitor{ - visitEmbeddedTemplate(ast: EmbeddedTemplateAst, context: any) { return ast; } - }, - new EmbeddedTemplateAst([], [], [], [], [], [], false, [], [], 0, null !)); + it('should set for property value bindings', () => { + expect(secContext('
')).toBe(SecurityContext.HTML); }); - - it('should visit ElementAst', () => { - expectVisitedNode( - new class extends - NullVisitor{visitElement(ast: ElementAst, context: any) { return ast; }}, - new ElementAst('foo', [], [], [], [], [], [], false, [], [], 0, null !, null !)); + it('should set for attributes', () => { + expect(secContext('')).toBe(SecurityContext.URL); + // NB: attributes below need to change case. + expect(secContext('')).toBe(SecurityContext.HTML); + expect(secContext('')).toBe(SecurityContext.URL); }); - - it('should visit RefererenceAst', () => { - expectVisitedNode( - new class extends - NullVisitor{visitReference(ast: ReferenceAst, context: any): any{return ast;}}, - new ReferenceAst('foo', null !, null !, null !)); - }); - - it('should visit VariableAst', () => { - expectVisitedNode( - new class extends - NullVisitor{visitVariable(ast: VariableAst, context: any): any{return ast;}}, - new VariableAst('foo', 'bar', null !)); - }); - - it('should visit BoundEventAst', () => { - expectVisitedNode( - new class extends - NullVisitor{visitEvent(ast: BoundEventAst, context: any): any{return ast;}}, - new BoundEventAst('foo', 'bar', 'goo', null !, null !, null !)); - }); - - it('should visit BoundElementPropertyAst', () => { - expectVisitedNode( - new class extends NullVisitor{ - visitElementProperty(ast: BoundElementPropertyAst, context: any): any{return ast;} - }, - new BoundElementPropertyAst('foo', null !, null !, null !, 'bar', null !)); - }); - - it('should visit AttrAst', () => { - expectVisitedNode( - new class extends NullVisitor{visitAttr(ast: AttrAst, context: any): any{return ast;}}, - new AttrAst('foo', 'bar', null !)); - }); - - it('should visit BoundTextAst', () => { - expectVisitedNode( - new class extends - NullVisitor{visitBoundText(ast: BoundTextAst, context: any): any{return ast;}}, - new BoundTextAst(null !, 0, null !)); - }); - - it('should visit TextAst', () => { - expectVisitedNode( - new class extends NullVisitor{visitText(ast: TextAst, context: any): any{return ast;}}, - new TextAst('foo', 0, null !)); - }); - - it('should visit DirectiveAst', () => { - expectVisitedNode( - new class extends - NullVisitor{visitDirective(ast: DirectiveAst, context: any): any{return ast;}}, - new DirectiveAst(null !, [], [], [], 0, null !)); - }); - - it('should visit DirectiveAst', () => { - expectVisitedNode( - new class extends NullVisitor{ - visitDirectiveProperty(ast: BoundDirectivePropertyAst, context: any): any{return ast;} - }, - new BoundDirectivePropertyAst('foo', 'bar', null !, null !)); - }); - - it('should skip the typed call of a visitor if visit() returns a truthy value', () => { - const visitor = new class extends ThrowingVisitor { - visit(ast: TemplateAst, context: any): any { return true; } - }; - const nodes: TemplateAst[] = [ - new NgContentAst(0, 0, null !), - new EmbeddedTemplateAst([], [], [], [], [], [], false, [], [], 0, null !), - new ElementAst('foo', [], [], [], [], [], [], false, [], [], 0, null !, null !), - new ReferenceAst('foo', null !, 'bar', null !), new VariableAst('foo', 'bar', null !), - new BoundEventAst('foo', 'bar', 'goo', null !, null !, null !), - new BoundElementPropertyAst('foo', null !, null !, null !, 'bar', null !), - new AttrAst('foo', 'bar', null !), new BoundTextAst(null !, 0, null !), - new TextAst('foo', 0, null !), new DirectiveAst(null !, [], [], [], 0, null !), - new BoundDirectivePropertyAst('foo', 'bar', null !, null !) - ]; - const result = templateVisitAll(visitor, nodes, null); - expect(result).toEqual(newArray(nodes.length).fill(true)); + it('should set for style', () => { + expect(secContext('')).toBe(SecurityContext.STYLE); }); }); +}); - describe('TemplateParser Security', () => { - // Semi-integration test to make sure TemplateParser properly sets the security context. - // Uses the actual DomElementSchemaRegistry. - beforeEach(() => { - TestBed.configureCompiler({ - providers: [ - TEST_COMPILER_PROVIDERS, - {provide: ElementSchemaRegistry, useClass: DomElementSchemaRegistry, deps: []} - ] - }); - }); - - configureCompiler(); - commonBeforeEach(); - - describe('security context', () => { - function secContext(tpl: string): SecurityContext { - const ast = parse(tpl, []); - const propBinding = (ast[0]).inputs[0]; - return propBinding.securityContext; - } - - it('should set for properties', () => { - expect(secContext('
')).toBe(SecurityContext.NONE); - expect(secContext('
')).toBe(SecurityContext.HTML); - }); - it('should set for property value bindings', - () => { expect(secContext('
')).toBe(SecurityContext.HTML); }); - it('should set for attributes', () => { - expect(secContext('')).toBe(SecurityContext.URL); - // NB: attributes below need to change case. - expect(secContext('')).toBe(SecurityContext.HTML); - expect(secContext('')).toBe(SecurityContext.URL); - }); - it('should set for style', () => { - expect(secContext('')).toBe(SecurityContext.STYLE); - }); - }); +describe('TemplateParser', () => { + beforeEach(() => { + TestBed.configureCompiler({providers: [TEST_COMPILER_PROVIDERS, MOCK_SCHEMA_REGISTRY]}); }); - describe('TemplateParser', () => { - beforeEach(() => { - TestBed.configureCompiler({providers: [TEST_COMPILER_PROVIDERS, MOCK_SCHEMA_REGISTRY]}); + configureCompiler(); + commonBeforeEach(); + + describe('parse', () => { + describe('nodes without bindings', () => { + it('should parse text nodes', () => { + expect(humanizeTplAst(parse('a', []))).toEqual([[TextAst, 'a']]); + }); + + it('should parse elements with attributes', () => { + expect(humanizeTplAst(parse('
', []))).toEqual([ + [ElementAst, 'div'], [AttrAst, 'a', 'b'] + ]); + }); }); - configureCompiler(); - commonBeforeEach(); + it('should parse ngContent', () => { + const parsed = parse('', []); + expect(humanizeTplAst(parsed)).toEqual([[NgContentAst]]); + }); - describe('parse', () => { - describe('nodes without bindings', () => { + it('should parse ngContent when it contains WS only', () => { + const parsed = parse(' \n ', []); + expect(humanizeTplAst(parsed)).toEqual([[NgContentAst]]); + }); - it('should parse text nodes', () => { - expect(humanizeTplAst(parse('a', []))).toEqual([[TextAst, 'a']]); - }); + it('should parse ngContent regardless the namespace', () => { + const parsed = parse('', []); + expect(humanizeTplAst(parsed)).toEqual([ + [ElementAst, ':svg:svg'], + [NgContentAst], + ]); + }); - it('should parse elements with attributes', () => { - expect(humanizeTplAst(parse('
', [ - ]))).toEqual([[ElementAst, 'div'], [AttrAst, 'a', 'b']]); - }); - }); + it('should parse bound text nodes', () => { + expect(humanizeTplAst(parse('{{a}}', []))).toEqual([[BoundTextAst, '{{ a }}']]); + }); - it('should parse ngContent', () => { - const parsed = parse('', []); - expect(humanizeTplAst(parsed)).toEqual([[NgContentAst]]); - }); + it('should parse with custom interpolation config', + inject([TemplateParser], (parser: TemplateParser) => { + const component = CompileDirectiveMetadata.create({ + selector: 'test', + type: createTypeMeta({reference: {filePath: someModuleUrl, name: 'Test'}}), + isComponent: true, + template: new CompileTemplateMetadata({ + interpolation: ['{%', '%}'], + isInline: false, + animations: [], + template: null, + templateUrl: null, + htmlAst: null, + ngContentSelectors: [], + externalStylesheets: [], + styleUrls: [], + styles: [], + encapsulation: null, + preserveWhitespaces: preserveWhitespacesDefault(null), + }), + isHost: false, + exportAs: null, + changeDetection: null, + inputs: [], + outputs: [], + host: {}, + providers: [], + viewProviders: [], + queries: [], + guards: {}, + viewQueries: [], + entryComponents: [], + componentViewType: null, + rendererType: null, + componentFactory: null - it('should parse ngContent when it contains WS only', () => { - const parsed = parse(' \n ', []); - expect(humanizeTplAst(parsed)).toEqual([[NgContentAst]]); - }); + }); + expect(humanizeTplAst( + parser.parse(component, '{%a%}', [], [], [], 'TestComp', true).template, + {start: '{%', end: '%}'})) + .toEqual([[BoundTextAst, '{% a %}']]); + })); - it('should parse ngContent regardless the namespace', () => { - const parsed = parse('', []); - expect(humanizeTplAst(parsed)).toEqual([ - [ElementAst, ':svg:svg'], - [NgContentAst], + describe('bound properties', () => { + it('should parse mixed case bound properties', () => { + expect(humanizeTplAst(parse('
', []))).toEqual([ + [ElementAst, 'div'], + [BoundElementPropertyAst, PropertyBindingType.Property, 'someProp', 'v', null] ]); }); - it('should parse bound text nodes', () => { - expect(humanizeTplAst(parse('{{a}}', []))).toEqual([[BoundTextAst, '{{ a }}']]); + it('should parse dash case bound properties', () => { + expect(humanizeTplAst(parse('
', []))).toEqual([ + [ElementAst, 'div'], + [BoundElementPropertyAst, PropertyBindingType.Property, 'some-prop', 'v', null] + ]); }); - it('should parse with custom interpolation config', - inject([TemplateParser], (parser: TemplateParser) => { - const component = CompileDirectiveMetadata.create({ - selector: 'test', - type: createTypeMeta({reference: {filePath: someModuleUrl, name: 'Test'}}), - isComponent: true, - template: new CompileTemplateMetadata({ - interpolation: ['{%', '%}'], - isInline: false, - animations: [], - template: null, - templateUrl: null, - htmlAst: null, - ngContentSelectors: [], - externalStylesheets: [], - styleUrls: [], - styles: [], - encapsulation: null, - preserveWhitespaces: preserveWhitespacesDefault(null), - }), - isHost: false, - exportAs: null, - changeDetection: null, - inputs: [], - outputs: [], - host: {}, - providers: [], - viewProviders: [], - queries: [], - guards: {}, - viewQueries: [], - entryComponents: [], - componentViewType: null, - rendererType: null, - componentFactory: null + it('should parse dotted name bound properties', () => { + expect(humanizeTplAst(parse('
', []))).toEqual([ + [ElementAst, 'div'], + [BoundElementPropertyAst, PropertyBindingType.Property, 'dot.name', 'v', null] + ]); + }); - }); - expect(humanizeTplAst( - parser.parse(component, '{%a%}', [], [], [], 'TestComp', true).template, - {start: '{%', end: '%}'})) - .toEqual([[BoundTextAst, '{% a %}']]); - })); + it('should normalize property names via the element schema', () => { + expect(humanizeTplAst(parse('
', []))).toEqual([ + [ElementAst, 'div'], + [BoundElementPropertyAst, PropertyBindingType.Property, 'mappedProp', 'v', null] + ]); + }); - describe('bound properties', () => { + it('should parse mixed case bound attributes', () => { + expect(humanizeTplAst(parse('
', []))).toEqual([ + [ElementAst, 'div'], + [BoundElementPropertyAst, PropertyBindingType.Attribute, 'someAttr', 'v', null] + ]); + }); - it('should parse mixed case bound properties', () => { - expect(humanizeTplAst(parse('
', []))).toEqual([ - [ElementAst, 'div'], - [BoundElementPropertyAst, PropertyBindingType.Property, 'someProp', 'v', null] - ]); - }); + it('should parse mixed case bound attributes with dot in the attribute name', () => { + expect(humanizeTplAst(parse('
', []))).toEqual([ + [ElementAst, 'div'], + [ + BoundElementPropertyAst, PropertyBindingType.Attribute, 'someAttr.someAttrSuffix', 'v', + null + ] + ]); + }); - it('should parse dash case bound properties', () => { - expect(humanizeTplAst(parse('
', []))).toEqual([ - [ElementAst, 'div'], - [BoundElementPropertyAst, PropertyBindingType.Property, 'some-prop', 'v', null] - ]); - }); + it('should parse and dash case bound classes', () => { + expect(humanizeTplAst(parse('
', []))).toEqual([ + [ElementAst, 'div'], + [BoundElementPropertyAst, PropertyBindingType.Class, 'some-class', 'v', null] + ]); + }); - it('should parse dotted name bound properties', () => { - expect(humanizeTplAst(parse('
', []))).toEqual([ - [ElementAst, 'div'], - [BoundElementPropertyAst, PropertyBindingType.Property, 'dot.name', 'v', null] - ]); - }); + it('should parse mixed case bound classes', () => { + expect(humanizeTplAst(parse('
', []))).toEqual([ + [ElementAst, 'div'], + [BoundElementPropertyAst, PropertyBindingType.Class, 'someClass', 'v', null] + ]); + }); - it('should normalize property names via the element schema', () => { - expect(humanizeTplAst(parse('
', []))).toEqual([ - [ElementAst, 'div'], - [BoundElementPropertyAst, PropertyBindingType.Property, 'mappedProp', 'v', null] - ]); - }); + it('should parse mixed case bound styles', () => { + expect(humanizeTplAst(parse('
', []))).toEqual([ + [ElementAst, 'div'], + [BoundElementPropertyAst, PropertyBindingType.Style, 'someStyle', 'v', null] + ]); + }); - it('should parse mixed case bound attributes', () => { - expect(humanizeTplAst(parse('
', []))).toEqual([ - [ElementAst, 'div'], - [BoundElementPropertyAst, PropertyBindingType.Attribute, 'someAttr', 'v', null] - ]); - }); - - it('should parse mixed case bound attributes with dot in the attribute name', () => { - expect(humanizeTplAst(parse('
', []))).toEqual([ - [ElementAst, 'div'], - [ - BoundElementPropertyAst, PropertyBindingType.Attribute, 'someAttr.someAttrSuffix', - 'v', null - ] - ]); - }); - - it('should parse and dash case bound classes', () => { - expect(humanizeTplAst(parse('
', []))).toEqual([ - [ElementAst, 'div'], - [BoundElementPropertyAst, PropertyBindingType.Class, 'some-class', 'v', null] - ]); - }); - - it('should parse mixed case bound classes', () => { - expect(humanizeTplAst(parse('
', []))).toEqual([ - [ElementAst, 'div'], - [BoundElementPropertyAst, PropertyBindingType.Class, 'someClass', 'v', null] - ]); - }); - - it('should parse mixed case bound styles', () => { - expect(humanizeTplAst(parse('
', []))).toEqual([ - [ElementAst, 'div'], - [BoundElementPropertyAst, PropertyBindingType.Style, 'someStyle', 'v', null] - ]); - }); - - describe('errors', () => { - it('should throw error when binding to an unknown property', () => { - expect(() => parse('', [])) - .toThrowError(`Template parse errors: + describe('errors', () => { + it('should throw error when binding to an unknown property', () => { + expect(() => parse('', [])) + .toThrowError(`Template parse errors: Can't bind to 'invalidProp' since it isn't a known property of 'my-component'. 1. If 'my-component' is an Angular component and it has 'invalidProp' input, then verify that it is part of this module. 2. If 'my-component' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. 3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("][invalidProp]="bar">"): TestComp@0:14`); - }); + }); - it('should throw error when binding to an unknown property of ng-container', () => { - expect(() => parse('', [])) - .toThrowError( - `Template parse errors: + it('should throw error when binding to an unknown property of ng-container', () => { + expect(() => parse('', [])) + .toThrowError( + `Template parse errors: Can't bind to 'invalidProp' since it isn't a known property of 'ng-container'. 1. If 'invalidProp' is an Angular directive, then add 'CommonModule' to the '@NgModule.imports' of this component. 2. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.` + - ` ("][invalidProp]="bar">"): TestComp@0:14`); - }); + ` ("][invalidProp]="bar">"): TestComp@0:14`); + }); - it('should throw error when binding to an unknown element w/o bindings', () => { - expect(() => parse('', [])).toThrowError(`Template parse errors: + it('should throw error when binding to an unknown element w/o bindings', () => { + expect(() => parse('', [])).toThrowError(`Template parse errors: 'unknown' is not a known element: 1. If 'unknown' is an Angular component, then verify that it is part of this module. 2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("[ERROR ->]"): TestComp@0:0`); - }); + }); - it('should throw error when binding to an unknown custom element w/o bindings', () => { - expect(() => parse('', [])).toThrowError(`Template parse errors: + it('should throw error when binding to an unknown custom element w/o bindings', () => { + expect(() => parse('', [])).toThrowError(`Template parse errors: 'un-known' is not a known element: 1. If 'un-known' is an Angular component, then verify that it is part of this module. 2. If 'un-known' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("[ERROR ->]"): TestComp@0:0`); - }); + }); - it('should throw error when binding to an invalid property', () => { - expect(() => parse('', [])) - .toThrowError(`Template parse errors: + it('should throw error when binding to an invalid property', () => { + expect(() => parse('', [])) + .toThrowError(`Template parse errors: Binding to property 'onEvent' is disallowed for security reasons ("][onEvent]="bar">"): TestComp@0:14`); - }); + }); - it('should throw error when binding to an invalid attribute', () => { - expect(() => parse('', [])) - .toThrowError(`Template parse errors: + it('should throw error when binding to an invalid attribute', () => { + expect(() => parse('', [])) + .toThrowError(`Template parse errors: Binding to attribute 'onEvent' is disallowed for security reasons ("][attr.onEvent]="bar">"): TestComp@0:14`); - }); }); + }); - it('should parse bound properties via [...] and not report them as attributes', () => { - expect(humanizeTplAst(parse('
', []))).toEqual([ - [ElementAst, 'div'], - [BoundElementPropertyAst, PropertyBindingType.Property, 'prop', 'v', null] - ]); - }); + it('should parse bound properties via [...] and not report them as attributes', () => { + expect(humanizeTplAst(parse('
', []))).toEqual([ + [ElementAst, 'div'], + [BoundElementPropertyAst, PropertyBindingType.Property, 'prop', 'v', null] + ]); + }); - it('should parse bound properties via bind- and not report them as attributes', () => { - expect(humanizeTplAst(parse('
', []))).toEqual([ - [ElementAst, 'div'], - [BoundElementPropertyAst, PropertyBindingType.Property, 'prop', 'v', null] - ]); - }); + it('should parse bound properties via bind- and not report them as attributes', () => { + expect(humanizeTplAst(parse('
', []))).toEqual([ + [ElementAst, 'div'], + [BoundElementPropertyAst, PropertyBindingType.Property, 'prop', 'v', null] + ]); + }); - it('should report missing property names in bind- syntax', () => { - expect(() => parse('
', [])).toThrowError(`Template parse errors: + it('should report missing property names in bind- syntax', () => { + expect(() => parse('
', [])).toThrowError(`Template parse errors: Property name is missing in binding ("
]bind->
"): TestComp@0:5`); - }); + }); - it('should parse bound properties via {{...}} and not report them as attributes', () => { - expect(humanizeTplAst(parse('
', []))).toEqual([ - [ElementAst, 'div'], - [BoundElementPropertyAst, PropertyBindingType.Property, 'prop', '{{ v }}', null] - ]); - }); + it('should parse bound properties via {{...}} and not report them as attributes', () => { + expect(humanizeTplAst(parse('
', []))).toEqual([ + [ElementAst, 'div'], + [BoundElementPropertyAst, PropertyBindingType.Property, 'prop', '{{ v }}', null] + ]); + }); - it('should parse bound properties via bind-animate- and not report them as attributes', - () => { - expect(humanizeTplAst(parse('
', [], [], []))) - .toEqual([ - [ElementAst, 'div'], - [ - BoundElementPropertyAst, PropertyBindingType.Animation, 'someAnimation', - 'value2', null - ] - ]); - }); + it('should parse bound properties via bind-animate- and not report them as attributes', + () => { + expect(humanizeTplAst(parse('
', [], [], []))) + .toEqual([ + [ElementAst, 'div'], + [ + BoundElementPropertyAst, PropertyBindingType.Animation, 'someAnimation', + 'value2', null + ] + ]); + }); - it('should throw an error when parsing detects non-bound properties via @ that contain a value', - () => { - expect(() => { parse('
', [], [], []); }) - .toThrowError( - /Assigning animation triggers via @prop="exp" attributes with an expression is invalid. Use property bindings \(e.g. \[@prop\]="exp"\) or use an attribute without a value \(e.g. @prop\) instead. \("
\]@someAnimation="value2">"\): TestComp@0:5/); - }); + it('should throw an error when parsing detects non-bound properties via @ that contain a value', + () => { + expect(() => { + parse('
', [], [], []); + }) + .toThrowError( + /Assigning animation triggers via @prop="exp" attributes with an expression is invalid. Use property bindings \(e.g. \[@prop\]="exp"\) or use an attribute without a value \(e.g. @prop\) instead. \("
\]@someAnimation="value2">"\): TestComp@0:5/); + }); - it('should report missing animation trigger in @ syntax', () => { - expect(() => parse('
', [])).toThrowError(`Template parse errors: + it('should report missing animation trigger in @ syntax', () => { + expect(() => parse('
', [])).toThrowError(`Template parse errors: Animation trigger is missing ("
]@>
"): TestComp@0:5`); - }); - - it('should not issue a warning when host attributes contain a valid property-bound animation trigger', - () => { - const animationEntries = ['prop']; - const dirA = compileDirectiveMetadataCreate({ - selector: 'div', - template: compileTemplateMetadata({animations: animationEntries}), - type: createTypeMeta({ - reference: {filePath: someModuleUrl, name: 'DirA'}, - }), - host: {'[@prop]': 'expr'} - }).toSummary(); - - humanizeTplAst(parse('
', [dirA])); - expect(console.warnings.length).toEqual(0); - }); - - it('should throw descriptive error when a host binding is not a string expression', () => { - const dirA = compileDirectiveMetadataCreate({ - selector: 'broken', - type: createTypeMeta({reference: {filePath: someModuleUrl, name: 'DirA'}}), - host: {'[class.foo]': null !} - }).toSummary(); - - expect(() => { parse('', [dirA]); }) - .toThrowError( - `Template parse errors:\nValue of the host property binding "class.foo" needs to be a string representing an expression but got "null" (object) ("[ERROR ->]"): TestComp@0:0, Directive DirA`); - }); - - it('should throw descriptive error when a host event is not a string expression', () => { - const dirA = compileDirectiveMetadataCreate({ - selector: 'broken', - type: createTypeMeta({reference: {filePath: someModuleUrl, name: 'DirA'}}), - host: {'(click)': null !} - }).toSummary(); - - expect(() => { parse('', [dirA]); }) - .toThrowError( - `Template parse errors:\nValue of the host listener "click" needs to be a string representing an expression but got "null" (object) ("[ERROR ->]"): TestComp@0:0, Directive DirA`); - }); - - it('should not issue a warning when an animation property is bound without an expression', - () => { - humanizeTplAst(parse('
', [], [], [])); - expect(console.warnings.length).toEqual(0); - }); - - it('should parse bound properties via [@] and not report them as attributes', () => { - expect(humanizeTplAst(parse('
', [], [], []))).toEqual([ - [ElementAst, 'div'], - [ - BoundElementPropertyAst, PropertyBindingType.Animation, 'someAnimation', 'value2', - null - ] - ]); - }); - - it('should support * directives', () => { - expect(humanizeTplAst(parse('
', [ngIf]))).toEqual([ - [EmbeddedTemplateAst], - [DirectiveAst, ngIf], - [BoundDirectivePropertyAst, 'ngIf', 'null'], - [ElementAst, 'div'], - ]); - }); - - it('should support ', () => { - expect(humanizeTplAst(parse('', []))).toEqual([ - [EmbeddedTemplateAst], - ]); - }); - - it('should treat