From ef1c6d8c260cc33c918a196762b1efaaa6c5f708 Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Wed, 20 Jun 2018 15:59:15 -0700 Subject: [PATCH] feat(ivy): dummy handler for @Pipe to cause decorator removal (#24677) Currently ngtsc does not compile @Pipe. This has a side effect of not removing the @Pipe decorator. This adds a dummy DecoratorHandler that compiles @Pipe into an empty ngPipeDef. Eventually this will be replaced with a full implementation, but for now this solution allows compield code to be tree-shaken properly. PR Close #24677 --- .../src/ngtsc/annotations/index.ts | 1 + .../src/ngtsc/annotations/src/pipe.ts | 39 +++++++++++++++++++ packages/compiler-cli/src/ngtsc/program.ts | 3 +- 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 packages/compiler-cli/src/ngtsc/annotations/src/pipe.ts diff --git a/packages/compiler-cli/src/ngtsc/annotations/index.ts b/packages/compiler-cli/src/ngtsc/annotations/index.ts index f56e3074cc..4328de07ba 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/index.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/index.ts @@ -10,4 +10,5 @@ export {ComponentDecoratorHandler} from './src/component'; export {DirectiveDecoratorHandler} from './src/directive'; export {InjectableDecoratorHandler} from './src/injectable'; export {NgModuleDecoratorHandler} from './src/ng_module'; +export {PipeDecoratorHandler} from './src/pipe'; export {CompilationScope, SelectorScopeRegistry} from './src/selector_scope'; diff --git a/packages/compiler-cli/src/ngtsc/annotations/src/pipe.ts b/packages/compiler-cli/src/ngtsc/annotations/src/pipe.ts new file mode 100644 index 0000000000..365d1ce33a --- /dev/null +++ b/packages/compiler-cli/src/ngtsc/annotations/src/pipe.ts @@ -0,0 +1,39 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import {Expression, ExpressionType, LiteralExpr, R3DependencyMetadata, R3InjectableMetadata, R3ResolvedDependencyType, WrappedNodeExpr, compileInjectable as compileIvyInjectable} from '@angular/compiler'; +import * as ts from 'typescript'; + +import {Decorator, ReflectionHost} from '../../host'; +import {AnalysisOutput, CompileResult, DecoratorHandler} from '../../transform'; + +import {isAngularCore} from './util'; + +export class PipeDecoratorHandler implements DecoratorHandler { + constructor(private reflector: ReflectionHost, private isCore: boolean) {} + + detect(decorator: Decorator[]): Decorator|undefined { + return decorator.find( + decorator => decorator.name === 'Pipe' && (this.isCore || isAngularCore(decorator))); + } + + analyze(node: ts.ClassDeclaration, decorator: Decorator): AnalysisOutput { + return { + analysis: 'test', + }; + } + + compile(node: ts.ClassDeclaration, analysis: string): CompileResult { + return { + name: 'ngPipeDef', + initializer: new LiteralExpr(null), + statements: [], + type: new ExpressionType(new LiteralExpr(null)), + }; + } +} diff --git a/packages/compiler-cli/src/ngtsc/program.ts b/packages/compiler-cli/src/ngtsc/program.ts index bd3dcb537c..314718e948 100644 --- a/packages/compiler-cli/src/ngtsc/program.ts +++ b/packages/compiler-cli/src/ngtsc/program.ts @@ -12,7 +12,7 @@ import * as ts from 'typescript'; import * as api from '../transformers/api'; -import {ComponentDecoratorHandler, DirectiveDecoratorHandler, InjectableDecoratorHandler, NgModuleDecoratorHandler, SelectorScopeRegistry} from './annotations'; +import {ComponentDecoratorHandler, DirectiveDecoratorHandler, InjectableDecoratorHandler, NgModuleDecoratorHandler, PipeDecoratorHandler, SelectorScopeRegistry} from './annotations'; import {CompilerHost} from './compiler_host'; import {TypeScriptReflectionHost} from './metadata'; import {IvyCompilation, ivyTransformFactory} from './transform'; @@ -101,6 +101,7 @@ export class NgtscProgram implements api.Program { new DirectiveDecoratorHandler(checker, reflector, scopeRegistry, isCore), new InjectableDecoratorHandler(reflector, isCore), new NgModuleDecoratorHandler(checker, reflector, scopeRegistry, isCore), + new PipeDecoratorHandler(reflector, isCore), ]; const coreImportsFrom = isCore && getR3SymbolsFile(this.tsProgram) || null;