From afbee736ea300e88dd3323cc85c12a1ca244ae89 Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Tue, 30 Oct 2018 10:04:10 -0700 Subject: [PATCH] refactor(ivy): use wrapped metadata in all DecoratorHandlers (#26860) Previously, the Directive, Injectable, and Pipe DecoratorHandlers were directly returning @angular/compiler metadata from their analyze() steps. This precludes returning any additional information along with that metadata. This commit introduces a wrapper interface for these handlers, opening the door for additional information to be returned from analyze(). Testing strategy: this is a refactor commit, existing test coverage is sufficient. PR Close #26860 --- .../src/ngtsc/annotations/src/directive.ts | 20 ++++++++++++----- .../src/ngtsc/annotations/src/injectable.ts | 13 ++++++----- .../src/ngtsc/annotations/src/pipe.ts | 22 +++++++++++-------- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/packages/compiler-cli/src/ngtsc/annotations/src/directive.ts b/packages/compiler-cli/src/ngtsc/annotations/src/directive.ts index cb9a8bf5a7..79d660688b 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/src/directive.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/src/directive.ts @@ -19,7 +19,9 @@ import {extractDirectiveGuards, getConstructorDependencies, isAngularCore, unwra const EMPTY_OBJECT: {[key: string]: string} = {}; -export class DirectiveDecoratorHandler implements DecoratorHandler { +export interface DirectiveHandlerData { meta: R3DirectiveMetadata; } +export class DirectiveDecoratorHandler implements + DecoratorHandler { constructor( private checker: ts.TypeChecker, private reflector: ReflectionHost, private scopeRegistry: SelectorScopeRegistry, private isCore: boolean) {} @@ -32,7 +34,7 @@ export class DirectiveDecoratorHandler implements DecoratorHandler decorator.name === 'Directive' && (this.isCore || isAngularCore(decorator))); } - analyze(node: ts.ClassDeclaration, decorator: Decorator): AnalysisOutput { + analyze(node: ts.ClassDeclaration, decorator: Decorator): AnalysisOutput { const directiveResult = extractDirectiveMetadata(node, decorator, this.checker, this.reflector, this.isCore); const analysis = directiveResult && directiveResult.metadata; @@ -54,12 +56,20 @@ export class DirectiveDecoratorHandler implements DecoratorHandler { + DecoratorHandler { constructor(private reflector: ReflectionHost, private isCore: boolean) {} detect(node: ts.Declaration, decorators: Decorator[]|null): Decorator|undefined { @@ -32,14 +33,16 @@ export class InjectableDecoratorHandler implements decorator => decorator.name === 'Injectable' && (this.isCore || isAngularCore(decorator))); } - analyze(node: ts.ClassDeclaration, decorator: Decorator): AnalysisOutput { + analyze(node: ts.ClassDeclaration, decorator: Decorator): AnalysisOutput { return { - analysis: extractInjectableMetadata(node, decorator, this.reflector, this.isCore), + analysis: { + meta: extractInjectableMetadata(node, decorator, this.reflector, this.isCore), + }, }; } - compile(node: ts.ClassDeclaration, analysis: R3InjectableMetadata): CompileResult { - const res = compileIvyInjectable(analysis); + compile(node: ts.ClassDeclaration, analysis: InjectableHandlerData): CompileResult { + const res = compileIvyInjectable(analysis.meta); return { name: 'ngInjectableDef', initializer: res.expression, diff --git a/packages/compiler-cli/src/ngtsc/annotations/src/pipe.ts b/packages/compiler-cli/src/ngtsc/annotations/src/pipe.ts index b8d21c7aaf..6bb923bec2 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/src/pipe.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/src/pipe.ts @@ -17,7 +17,9 @@ import {AnalysisOutput, CompileResult, DecoratorHandler} from '../../transform'; import {SelectorScopeRegistry} from './selector_scope'; import {getConstructorDependencies, isAngularCore, unwrapExpression} from './util'; -export class PipeDecoratorHandler implements DecoratorHandler { +export interface PipeHandlerData { meta: R3PipeMetadata; } + +export class PipeDecoratorHandler implements DecoratorHandler { constructor( private checker: ts.TypeChecker, private reflector: ReflectionHost, private scopeRegistry: SelectorScopeRegistry, private isCore: boolean) {} @@ -30,7 +32,7 @@ export class PipeDecoratorHandler implements DecoratorHandler decorator.name === 'Pipe' && (this.isCore || isAngularCore(decorator))); } - analyze(clazz: ts.ClassDeclaration, decorator: Decorator): AnalysisOutput { + analyze(clazz: ts.ClassDeclaration, decorator: Decorator): AnalysisOutput { if (clazz.name === undefined) { throw new FatalDiagnosticError( ErrorCode.DECORATOR_ON_ANONYMOUS_CLASS, clazz, `@Pipes must have names`); @@ -77,16 +79,18 @@ export class PipeDecoratorHandler implements DecoratorHandler