From 3ddfa57db3f55fa9e48e1fb0a63d556928c93f92 Mon Sep 17 00:00:00 2001 From: JoostK Date: Mon, 7 Oct 2019 20:29:51 +0200 Subject: [PATCH] fixup! fix(ivy): allow abstract directives to have an invalid constructor --- .../src/ngtsc/annotations/src/component.ts | 4 +- .../src/ngtsc/annotations/src/directive.ts | 6 ++- .../src/ngtsc/annotations/src/injectable.ts | 4 +- .../src/ngtsc/annotations/src/pipe.ts | 3 +- .../compiler-cli/test/ngtsc/ngtsc_spec.ts | 6 +++ .../compiler/src/compiler_facade_interface.ts | 11 +++- .../compiler/src/injectable_compiler_2.ts | 52 +++++++++++-------- packages/compiler/src/jit_compiler_facade.ts | 5 +- packages/compiler/src/render3/r3_factory.ts | 39 +++++++++----- .../src/render3/r3_module_compiler.ts | 18 ++++--- .../compiler/src/render3/r3_pipe_compiler.ts | 6 +-- .../compiler/src/render3/view/compiler.ts | 12 +++-- .../test/compiler_facade_interface_spec.ts | 10 ++++ .../src/compiler/compiler_facade_interface.ts | 11 +++- packages/core/src/di/jit/injectable.ts | 7 +-- packages/core/src/render3/jit/directive.ts | 5 +- packages/core/src/render3/jit/pipe.ts | 5 +- 17 files changed, 137 insertions(+), 67 deletions(-) diff --git a/packages/compiler-cli/src/ngtsc/annotations/src/component.ts b/packages/compiler-cli/src/ngtsc/annotations/src/component.ts index a0205bd27d50ef..5892b7cd864512 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/src/component.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/src/component.ts @@ -7,6 +7,7 @@ */ import {ConstantPool, CssSelector, DEFAULT_INTERPOLATION_CONFIG, DomElementSchemaRegistry, Expression, ExternalExpr, Identifiers, InterpolationConfig, LexerRange, ParseError, ParseSourceFile, ParseTemplateOptions, R3ComponentMetadata, R3TargetBinder, SchemaMetadata, SelectorMatcher, Statement, TmplAstNode, WrappedNodeExpr, compileComponentFromMetadata, makeBindingParser, parseTemplate} from '@angular/compiler'; +import {R3FactoryTarget} from '@angular/compiler/src/render3/r3_factory'; import * as ts from 'typescript'; import {CycleAnalyzer} from '../../cycles'; @@ -487,7 +488,8 @@ export class ComponentDecoratorHandler implements CompileResult[] { const meta = analysis.meta; const res = compileComponentFromMetadata(meta, pool, makeBindingParser()); - const factoryRes = compileNgFactoryDefField({...meta, injectFn: Identifiers.directiveInject}); + const factoryRes = compileNgFactoryDefField( + {...meta, injectFn: Identifiers.directiveInject, target: R3FactoryTarget.Component}); if (analysis.metadataStmt !== null) { factoryRes.statements.push(analysis.metadataStmt); } diff --git a/packages/compiler-cli/src/ngtsc/annotations/src/directive.ts b/packages/compiler-cli/src/ngtsc/annotations/src/directive.ts index b5adec34a227d7..c25e8942bbf567 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/src/directive.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/src/directive.ts @@ -7,6 +7,7 @@ */ import {ConstantPool, EMPTY_SOURCE_SPAN, Expression, Identifiers, ParseError, ParsedHostBindings, R3DependencyMetadata, R3DirectiveMetadata, R3QueryMetadata, Statement, WrappedNodeExpr, compileDirectiveFromMetadata, makeBindingParser, parseHostBindings, verifyHostBindings} from '@angular/compiler'; +import {R3FactoryTarget} from '@angular/compiler/src/render3/r3_factory'; import * as ts from 'typescript'; import {ErrorCode, FatalDiagnosticError} from '../../diagnostics'; @@ -94,7 +95,8 @@ export class DirectiveDecoratorHandler implements CompileResult[] { const meta = analysis.meta; const res = compileDirectiveFromMetadata(meta, pool, makeBindingParser()); - const factoryRes = compileNgFactoryDefField({...meta, injectFn: Identifiers.directiveInject}); + const factoryRes = compileNgFactoryDefField( + {...meta, injectFn: Identifiers.directiveInject, target: R3FactoryTarget.Directive}); if (analysis.metadataStmt !== null) { factoryRes.statements.push(analysis.metadataStmt); } @@ -234,7 +236,7 @@ export function extractDirectiveMetadata( } const rawCtorDeps = getConstructorDependencies(clazz, reflector, defaultImportRecorder, isCore); - let ctorDeps: R3DependencyMetadata[]|'invalid'|null = null; + let ctorDeps: R3DependencyMetadata[]|'invalid'|null; // Non-abstract directives (those with a selector) require valid constructor dependencies, whereas // abstract directives are allowed to have invalid dependencies, given that a subclass may call diff --git a/packages/compiler-cli/src/ngtsc/annotations/src/injectable.ts b/packages/compiler-cli/src/ngtsc/annotations/src/injectable.ts index c0501c7c509e97..0b453ce5909668 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/src/injectable.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/src/injectable.ts @@ -7,6 +7,7 @@ */ import {Expression, Identifiers, LiteralExpr, R3DependencyMetadata, R3InjectableMetadata, R3ResolvedDependencyType, Statement, WrappedNodeExpr, compileInjectable as compileIvyInjectable} from '@angular/compiler'; +import {R3FactoryTarget} from '@angular/compiler/src/render3/r3_factory'; import * as ts from 'typescript'; import {ErrorCode, FatalDiagnosticError} from '../../diagnostics'; @@ -83,7 +84,8 @@ export class InjectableDecoratorHandler implements type: meta.type, typeArgumentCount: meta.typeArgumentCount, deps: analysis.ctorDeps, - injectFn: Identifiers.inject + injectFn: Identifiers.inject, + target: R3FactoryTarget.Injectable, }); if (analysis.metadataStmt !== null) { factoryRes.statements.push(analysis.metadataStmt); diff --git a/packages/compiler-cli/src/ngtsc/annotations/src/pipe.ts b/packages/compiler-cli/src/ngtsc/annotations/src/pipe.ts index 4bbfbab24fe35a..07ad89dfc89a0a 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/src/pipe.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/src/pipe.ts @@ -7,6 +7,7 @@ */ import {Identifiers, R3PipeMetadata, Statement, WrappedNodeExpr, compilePipeFromMetadata} from '@angular/compiler'; +import {R3FactoryTarget} from '@angular/compiler/src/render3/r3_factory'; import * as ts from 'typescript'; import {ErrorCode, FatalDiagnosticError} from '../../diagnostics'; @@ -112,7 +113,7 @@ export class PipeDecoratorHandler implements DecoratorHandler { const jsContents = env.getContents('test.js'); expect(jsContents) .toContain('Test.ngFactoryDef = function Test_Factory(t) { throw new Error('); + expect(jsContents) + .toContain( + '("Test has a constructor which is not compatible with Dependency Injection. It should probably not be @Injectable().")'); }); it('should compile an @Injectable provided in the root on a class with a non-injectable constructor', @@ -1432,6 +1435,9 @@ runInEachFileSystem(os => { const jsContents = env.getContents('test.js'); expect(jsContents) .toContain('Test.ngFactoryDef = function Test_Factory(t) { throw new Error('); + expect(jsContents) + .toContain( + '("Test has a constructor which is not compatible with Dependency Injection.")'); }); }); }); diff --git a/packages/compiler/src/compiler_facade_interface.ts b/packages/compiler/src/compiler_facade_interface.ts index b30398b9e58584..f8231fccf46c2e 100644 --- a/packages/compiler/src/compiler_facade_interface.ts +++ b/packages/compiler/src/compiler_facade_interface.ts @@ -45,6 +45,7 @@ export interface CompilerFacade { createParseSourceSpan(kind: string, typeName: string, sourceUrl: string): ParseSourceSpan; R3ResolvedDependencyType: typeof R3ResolvedDependencyType; + R3FactoryTarget: typeof R3FactoryTarget; ResourceLoader: {new (): ResourceLoader}; } @@ -70,6 +71,14 @@ export enum R3ResolvedDependencyType { ChangeDetectorRef = 2, } +export enum R3FactoryTarget { + Directive = 0, + Component = 1, + Injectable = 2, + Pipe = 3, + NgModule = 4, +} + export interface R3DependencyMetadataFacade { token: any; resolved: R3ResolvedDependencyType; @@ -167,7 +176,7 @@ export interface R3FactoryDefMetadataFacade { typeArgumentCount: number; deps: R3DependencyMetadataFacade[]|null; injectFn: 'directiveInject'|'inject'; - isPipe: boolean; + target: R3FactoryTarget; } export type ViewEncapsulation = number; diff --git a/packages/compiler/src/injectable_compiler_2.ts b/packages/compiler/src/injectable_compiler_2.ts index cb9bab774a3fd5..4dffba3793de02 100644 --- a/packages/compiler/src/injectable_compiler_2.ts +++ b/packages/compiler/src/injectable_compiler_2.ts @@ -8,7 +8,7 @@ import {Identifiers} from './identifiers'; import * as o from './output/output_ast'; -import {R3DependencyMetadata, R3FactoryDelegateType, compileFactoryFunction} from './render3/r3_factory'; +import {R3DependencyMetadata, R3FactoryDelegateType, R3FactoryTarget, compileFactoryFunction} from './render3/r3_factory'; import {mapToMapExpression, typeWithParameters} from './render3/util'; export interface InjectableDef { @@ -56,25 +56,29 @@ export function compileInjectable(meta: R3InjectableMetadata): InjectableDef { if (deps !== undefined) { // factory: () => new meta.useClass(...deps) - result = compileFactoryFunction({ - ...factoryMeta, - delegate: meta.useClass, - delegateDeps: deps, - delegateType: R3FactoryDelegateType.Class, - }); + result = compileFactoryFunction( + { + ...factoryMeta, + delegate: meta.useClass, + delegateDeps: deps, + delegateType: R3FactoryDelegateType.Class, + }, + R3FactoryTarget.Injectable); } else if (useClassOnSelf) { - result = compileFactoryFunction(factoryMeta); + result = compileFactoryFunction(factoryMeta, R3FactoryTarget.Injectable); } else { result = delegateToFactory(meta.useClass); } } else if (meta.useFactory !== undefined) { if (meta.userDeps !== undefined) { - result = compileFactoryFunction({ - ...factoryMeta, - delegate: meta.useFactory, - delegateDeps: meta.userDeps || [], - delegateType: R3FactoryDelegateType.Function, - }); + result = compileFactoryFunction( + { + ...factoryMeta, + delegate: meta.useFactory, + delegateDeps: meta.userDeps || [], + delegateType: R3FactoryDelegateType.Function, + }, + R3FactoryTarget.Injectable); } else { result = { statements: [], @@ -85,16 +89,20 @@ export function compileInjectable(meta: R3InjectableMetadata): InjectableDef { // Note: it's safe to use `meta.useValue` instead of the `USE_VALUE in meta` check used for // client code because meta.useValue is an Expression which will be defined even if the actual // value is undefined. - result = compileFactoryFunction({ - ...factoryMeta, - expression: meta.useValue, - }); + result = compileFactoryFunction( + { + ...factoryMeta, + expression: meta.useValue, + }, + R3FactoryTarget.Injectable); } else if (meta.useExisting !== undefined) { // useExisting is an `inject` call on the existing token. - result = compileFactoryFunction({ - ...factoryMeta, - expression: o.importExpr(Identifiers.inject).callFn([meta.useExisting]), - }); + result = compileFactoryFunction( + { + ...factoryMeta, + expression: o.importExpr(Identifiers.inject).callFn([meta.useExisting]), + }, + R3FactoryTarget.Injectable); } else { result = delegateToFactory(meta.type); } diff --git a/packages/compiler/src/jit_compiler_facade.ts b/packages/compiler/src/jit_compiler_facade.ts index 8be8159abd013b..dde766a54cd8cd 100644 --- a/packages/compiler/src/jit_compiler_facade.ts +++ b/packages/compiler/src/jit_compiler_facade.ts @@ -16,7 +16,7 @@ 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, R3ResolvedDependencyType, compileFactoryFromMetadata} from './render3/r3_factory'; +import {R3DependencyMetadata, R3FactoryTarget, R3ResolvedDependencyType, compileFactoryFromMetadata} from './render3/r3_factory'; import {R3JitReflector} from './render3/r3_jit'; import {R3InjectorMetadata, R3NgModuleMetadata, compileInjector, compileNgModule} from './render3/r3_module_compiler'; import {compilePipeFromMetadata} from './render3/r3_pipe_compiler'; @@ -29,6 +29,7 @@ import {DomElementSchemaRegistry} from './schema/dom_element_schema_registry'; export class CompilerFacadeImpl implements CompilerFacade { R3ResolvedDependencyType = R3ResolvedDependencyType as any; + R3FactoryTarget = R3FactoryTarget as any; ResourceLoader = ResourceLoader; private elementSchemaRegistry = new DomElementSchemaRegistry(); @@ -162,7 +163,7 @@ export class CompilerFacadeImpl implements CompilerFacade { deps: convertR3DependencyMetadataArray(meta.deps), injectFn: meta.injectFn === 'directiveInject' ? Identifiers.directiveInject : Identifiers.inject, - isPipe: meta.isPipe + target: meta.target, }); return this.jitExpression( factoryRes.factory, angularCoreEnv, sourceMapUrl, factoryRes.statements); diff --git a/packages/compiler/src/render3/r3_factory.ts b/packages/compiler/src/render3/r3_factory.ts index 5c8a7eee04d775..b78bd73b5c2a8c 100644 --- a/packages/compiler/src/render3/r3_factory.ts +++ b/packages/compiler/src/render3/r3_factory.ts @@ -82,13 +82,21 @@ export interface R3ExpressionFactoryMetadata extends R3ConstructorFactoryMetadat export type R3FactoryMetadata = R3ConstructorFactoryMetadata | R3DelegatedFactoryMetadata | R3DelegatedFnOrClassMetadata | R3ExpressionFactoryMetadata; +export enum R3FactoryTarget { + Directive = 0, + Component = 1, + Injectable = 2, + Pipe = 3, + NgModule = 4, +} + export interface R3FactoryDefMetadata { name: string; type: o.Expression; typeArgumentCount: number; deps: R3DependencyMetadata[]|'invalid'|null; injectFn: o.ExternalReference; - isPipe?: boolean; + target: R3FactoryTarget; } /** @@ -163,7 +171,8 @@ export interface R3FactoryFn { /** * Construct a factory function expression for the given `R3FactoryMetadata`. */ -export function compileFactoryFunction(meta: R3FactoryMetadata, isPipe = false): R3FactoryFn { +export function compileFactoryFunction( + meta: R3FactoryMetadata, target: R3FactoryTarget): R3FactoryFn { const t = o.variable('t'); const statements: o.Statement[] = []; @@ -179,8 +188,9 @@ export function compileFactoryFunction(meta: R3FactoryMetadata, isPipe = false): if (meta.deps !== null) { // There is a constructor (either explicitly or implicitly defined). if (meta.deps !== 'invalid') { - ctorExpr = - new o.InstantiateExpr(typeForCtor, injectDependencies(meta.deps, meta.injectFn, isPipe)); + ctorExpr = new o.InstantiateExpr( + typeForCtor, + injectDependencies(meta.deps, meta.injectFn, target === R3FactoryTarget.Pipe)); } } else { const baseFactory = o.variable(`ɵ${meta.name}_BaseFactory`); @@ -206,7 +216,7 @@ export function compileFactoryFunction(meta: R3FactoryMetadata, isPipe = false): if (ctorExprFinal !== null) { ctorStmt = r.set(ctorExprFinal).toStmt(); } else { - ctorStmt = makeErrorStmt(meta.name); + ctorStmt = makeErrorStmt(meta.name, target); } body.push(o.ifStmt(t, [ctorStmt], [r.set(nonCtorExpr).toStmt()])); return r; @@ -228,7 +238,8 @@ export function compileFactoryFunction(meta: R3FactoryMetadata, isPipe = false): } else if (isDelegatedMetadata(meta)) { // This type is created with a delegated factory. If a type parameter is not specified, call // the factory instead. - const delegateArgs = injectDependencies(meta.delegateDeps, meta.injectFn, isPipe); + const delegateArgs = + injectDependencies(meta.delegateDeps, meta.injectFn, target === R3FactoryTarget.Pipe); // Either call `new delegate(...)` or `delegate(...)` depending on meta.useNewForDelegate. const factoryExpr = new ( meta.delegateType === R3FactoryDelegateType.Class ? @@ -245,7 +256,7 @@ export function compileFactoryFunction(meta: R3FactoryMetadata, isPipe = false): if (retExpr !== null) { body.push(new o.ReturnStatement(retExpr)); } else { - body.push(makeErrorStmt(meta.name)); + body.push(makeErrorStmt(meta.name, target)); } return { @@ -270,7 +281,7 @@ export function compileFactoryFromMetadata(meta: R3FactoryDefMetadata): R3Factor typeArgumentCount: meta.typeArgumentCount, injectFn: meta.injectFn, }, - meta.isPipe); + meta.target); } function injectDependencies( @@ -358,11 +369,13 @@ export function dependenciesFromGlobalMetadata( return deps; } -function makeErrorStmt(name: string): o.Statement { - return new o.ThrowStmt(new o.InstantiateExpr(new o.ReadVarExpr('Error'), [ - o.literal( - `${name} has a constructor which is not compatible with Dependency Injection. It should probably not be @Injectable().`) - ])); +function makeErrorStmt(name: string, target: R3FactoryTarget): o.Statement { + let message = `${name} has a constructor which is not compatible with Dependency Injection.`; + if (target === R3FactoryTarget.Injectable) { + message += ' It should probably not be @Injectable().'; + } + + return new o.ThrowStmt(new o.InstantiateExpr(new o.ReadVarExpr('Error'), [o.literal(message)])); } function isDelegatedMetadata(meta: R3FactoryMetadata): meta is R3DelegatedFactoryMetadata| diff --git a/packages/compiler/src/render3/r3_module_compiler.ts b/packages/compiler/src/render3/r3_module_compiler.ts index 2e3444bcbed742..db4df78b47c554 100644 --- a/packages/compiler/src/render3/r3_module_compiler.ts +++ b/packages/compiler/src/render3/r3_module_compiler.ts @@ -12,7 +12,7 @@ import {mapLiteral} from '../output/map_util'; import * as o from '../output/output_ast'; import {OutputContext} from '../util'; -import {R3DependencyMetadata, compileFactoryFunction} from './r3_factory'; +import {R3DependencyMetadata, R3FactoryTarget, compileFactoryFunction} from './r3_factory'; import {Identifiers as R3} from './r3_identifiers'; import {R3Reference, convertMetaToOutput, mapToMapExpression} from './util'; @@ -204,13 +204,15 @@ export interface R3InjectorMetadata { } export function compileInjector(meta: R3InjectorMetadata): R3InjectorDef { - const result = compileFactoryFunction({ - name: meta.name, - type: meta.type, - typeArgumentCount: 0, - deps: meta.deps, - injectFn: R3.inject, - }); + const result = compileFactoryFunction( + { + name: meta.name, + type: meta.type, + typeArgumentCount: 0, + deps: meta.deps, + injectFn: R3.inject, + }, + R3FactoryTarget.NgModule); const definitionMap = { factory: result.factory, } as{factory: o.Expression, providers: o.Expression, imports: o.Expression}; diff --git a/packages/compiler/src/render3/r3_pipe_compiler.ts b/packages/compiler/src/render3/r3_pipe_compiler.ts index 275d22ee19a2cb..1b7e03a1bf07e2 100644 --- a/packages/compiler/src/render3/r3_pipe_compiler.ts +++ b/packages/compiler/src/render3/r3_pipe_compiler.ts @@ -12,7 +12,7 @@ import {DefinitionKind} from '../constant_pool'; import * as o from '../output/output_ast'; import {OutputContext, error} from '../util'; -import {R3DependencyMetadata, compileFactoryFromMetadata, compileFactoryFunction, dependenciesFromGlobalMetadata} from './r3_factory'; +import {R3DependencyMetadata, R3FactoryTarget, compileFactoryFromMetadata, dependenciesFromGlobalMetadata} from './r3_factory'; import {Identifiers as R3} from './r3_identifiers'; import {typeWithParameters} from './util'; @@ -89,8 +89,8 @@ export function compilePipeFromRender2( pure: pipe.pure, }; const res = compilePipeFromMetadata(metadata); - const factoryRes = - compileFactoryFromMetadata({...metadata, injectFn: R3.directiveInject, isPipe: true}); + const factoryRes = compileFactoryFromMetadata( + {...metadata, injectFn: R3.directiveInject, target: R3FactoryTarget.Pipe}); const definitionField = outputCtx.constantPool.propertyNameOf(DefinitionKind.Pipe); const ngFactoryDefStatement = new o.ClassStmt( /* name */ name, diff --git a/packages/compiler/src/render3/view/compiler.ts b/packages/compiler/src/render3/view/compiler.ts index 8f27dd2944dbfb..d5739e66f74911 100644 --- a/packages/compiler/src/render3/view/compiler.ts +++ b/packages/compiler/src/render3/view/compiler.ts @@ -12,17 +12,17 @@ import {CompileReflector} from '../../compile_reflector'; import {BindingForm, convertPropertyBinding} from '../../compiler_util/expression_converter'; import {ConstantPool, DefinitionKind} from '../../constant_pool'; import * as core from '../../core'; -import {AST, Interpolation, ParsedEvent, ParsedEventType, ParsedProperty} from '../../expression_parser/ast'; +import {AST, ParsedEvent, ParsedEventType, ParsedProperty} from '../../expression_parser/ast'; import {DEFAULT_INTERPOLATION_CONFIG} from '../../ml_parser/interpolation_config'; import * as o from '../../output/output_ast'; -import {ParseError, ParseSourceSpan, typeSourceSpan} from '../../parse_util'; +import {ParseError, ParseSourceSpan} from '../../parse_util'; 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 {BoundEvent} from '../r3_ast'; -import {compileFactoryFromMetadata, compileFactoryFunction, dependenciesFromGlobalMetadata} from '../r3_factory'; +import {R3FactoryTarget, compileFactoryFromMetadata} from '../r3_factory'; import {Identifiers as R3} from '../r3_identifiers'; import {Render3ParseResult} from '../r3_template_transform'; import {prepareSyntheticListenerFunctionName, prepareSyntheticPropertyName, typeWithParameters} from '../util'; @@ -325,7 +325,8 @@ export function compileDirectiveFromRender2( const meta = directiveMetadataFromGlobalMetadata(directive, outputCtx, reflector); const res = compileDirectiveFromMetadata(meta, outputCtx.constantPool, bindingParser); - const factoryRes = compileFactoryFromMetadata({...meta, injectFn: R3.directiveInject}); + const factoryRes = compileFactoryFromMetadata( + {...meta, injectFn: R3.directiveInject, target: R3FactoryTarget.Directive}); const ngFactoryDefStatement = new o.ClassStmt( name, null, [new o.ClassField( @@ -378,7 +379,8 @@ export function compileComponentFromRender2( i18nUseExternalIds: true, }; const res = compileComponentFromMetadata(meta, outputCtx.constantPool, bindingParser); - const factoryRes = compileFactoryFromMetadata({...meta, injectFn: R3.directiveInject}); + const factoryRes = compileFactoryFromMetadata( + {...meta, injectFn: R3.directiveInject, target: R3FactoryTarget.Directive}); const ngFactoryDefStatement = new o.ClassStmt( name, null, [new o.ClassField( diff --git a/packages/compiler/test/compiler_facade_interface_spec.ts b/packages/compiler/test/compiler_facade_interface_spec.ts index efd1f23d1f1e71..6f24811cacc471 100644 --- a/packages/compiler/test/compiler_facade_interface_spec.ts +++ b/packages/compiler/test/compiler_facade_interface_spec.ts @@ -9,6 +9,7 @@ import * as core from '../../core/src/compiler/compiler_facade_interface'; import {R3ResolvedDependencyType} from '../public_api'; import * as compiler from '../src/compiler_facade_interface'; +import {R3FactoryTarget} from '../src/render3/r3_factory'; /** * This file is compiler level file which asserts that the set of interfaces in `@angular/core` and @@ -60,6 +61,15 @@ const coreR3ResolvedDependencyType3: core.R3ResolvedDependencyType = const compilerR3ResolvedDependencyType3: compiler.R3ResolvedDependencyType = null !as R3ResolvedDependencyType; +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 coreR3FactoryTarget3: core.R3FactoryTarget = null !as R3FactoryTarget; +const compilerR3FactoryTarget3: compiler.R3FactoryTarget = null !as R3FactoryTarget; + const coreR3DependencyMetadataFacade: core.R3DependencyMetadataFacade = null !as compiler.R3DependencyMetadataFacade; const compilerR3DependencyMetadataFacade: compiler.R3DependencyMetadataFacade = diff --git a/packages/core/src/compiler/compiler_facade_interface.ts b/packages/core/src/compiler/compiler_facade_interface.ts index b30398b9e58584..f8231fccf46c2e 100644 --- a/packages/core/src/compiler/compiler_facade_interface.ts +++ b/packages/core/src/compiler/compiler_facade_interface.ts @@ -45,6 +45,7 @@ export interface CompilerFacade { createParseSourceSpan(kind: string, typeName: string, sourceUrl: string): ParseSourceSpan; R3ResolvedDependencyType: typeof R3ResolvedDependencyType; + R3FactoryTarget: typeof R3FactoryTarget; ResourceLoader: {new (): ResourceLoader}; } @@ -70,6 +71,14 @@ export enum R3ResolvedDependencyType { ChangeDetectorRef = 2, } +export enum R3FactoryTarget { + Directive = 0, + Component = 1, + Injectable = 2, + Pipe = 3, + NgModule = 4, +} + export interface R3DependencyMetadataFacade { token: any; resolved: R3ResolvedDependencyType; @@ -167,7 +176,7 @@ export interface R3FactoryDefMetadataFacade { typeArgumentCount: number; deps: R3DependencyMetadataFacade[]|null; injectFn: 'directiveInject'|'inject'; - isPipe: boolean; + target: R3FactoryTarget; } export type ViewEncapsulation = number; diff --git a/packages/core/src/di/jit/injectable.ts b/packages/core/src/di/jit/injectable.ts index fdb67b76c66957..28409926c53c20 100644 --- a/packages/core/src/di/jit/injectable.ts +++ b/packages/core/src/di/jit/injectable.ts @@ -48,14 +48,15 @@ export function compileInjectable(type: Type, srcMeta?: Injectable): void { get: () => { if (ngFactoryDef === null) { const metadata = getInjectableMetadata(type, srcMeta); - ngFactoryDef = getCompilerFacade().compileFactory( - angularCoreDiEnv, `ng:///${type.name}/ngFactoryDef.js`, { + const compiler = getCompilerFacade(); + ngFactoryDef = + compiler.compileFactory(angularCoreDiEnv, `ng:///${type.name}/ngFactoryDef.js`, { name: metadata.name, type: metadata.type, typeArgumentCount: metadata.typeArgumentCount, deps: reflectDependencies(type), injectFn: 'inject', - isPipe: false + target: compiler.R3FactoryTarget.Pipe }); } return ngFactoryDef; diff --git a/packages/core/src/render3/jit/directive.ts b/packages/core/src/render3/jit/directive.ts index 2c846dff8e34de..f64b01b36d736b 100644 --- a/packages/core/src/render3/jit/directive.ts +++ b/packages/core/src/render3/jit/directive.ts @@ -165,9 +165,10 @@ function addDirectiveFactoryDef(type: Type, metadata: Directive | Component get: () => { if (ngFactoryDef === null) { const meta = getDirectiveMetadata(type, metadata); - ngFactoryDef = getCompilerFacade().compileFactory( + const compiler = getCompilerFacade(); + ngFactoryDef = compiler.compileFactory( angularCoreEnv, `ng:///${type.name}/ngFactoryDef.js`, - {...meta.metadata, injectFn: 'directiveInject', isPipe: false}); + {...meta.metadata, injectFn: 'directiveInject', target: compiler.R3FactoryTarget.Pipe}); } return ngFactoryDef; }, diff --git a/packages/core/src/render3/jit/pipe.ts b/packages/core/src/render3/jit/pipe.ts index fd17afe88966fa..5ecfe0d4b555a0 100644 --- a/packages/core/src/render3/jit/pipe.ts +++ b/packages/core/src/render3/jit/pipe.ts @@ -22,9 +22,10 @@ export function compilePipe(type: Type, meta: Pipe): void { get: () => { if (ngFactoryDef === null) { const metadata = getPipeMetadata(type, meta); - ngFactoryDef = getCompilerFacade().compileFactory( + const compiler = getCompilerFacade(); + ngFactoryDef = compiler.compileFactory( angularCoreEnv, `ng:///${metadata.name}/ngFactoryDef.js`, - {...metadata, injectFn: 'directiveInject', isPipe: true}); + {...metadata, injectFn: 'directiveInject', target: compiler.R3FactoryTarget.Pipe}); } return ngFactoryDef; },