2018-02-05 17:31:12 -08:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2018-04-18 16:23:49 -07:00
|
|
|
import {CompilePipeMetadata, identifierName} from '../compile_metadata';
|
2018-02-05 17:31:12 -08:00
|
|
|
import {CompileReflector} from '../compile_reflector';
|
|
|
|
import {DefinitionKind} from '../constant_pool';
|
|
|
|
import * as o from '../output/output_ast';
|
2020-04-08 10:14:18 -07:00
|
|
|
import {error, OutputContext} from '../util';
|
2018-02-05 17:31:12 -08:00
|
|
|
|
2020-04-08 10:14:18 -07:00
|
|
|
import {compileFactoryFunction, dependenciesFromGlobalMetadata, R3DependencyMetadata, R3FactoryTarget} from './r3_factory';
|
2018-02-05 17:31:12 -08:00
|
|
|
import {Identifiers as R3} from './r3_identifiers';
|
2019-12-18 14:03:05 +00:00
|
|
|
import {R3Reference, typeWithParameters, wrapReference} from './util';
|
2018-04-24 11:34:11 -07:00
|
|
|
|
2018-06-26 10:43:06 -07:00
|
|
|
export interface R3PipeMetadata {
|
2019-03-19 21:22:03 +01:00
|
|
|
/**
|
|
|
|
* Name of the pipe type.
|
|
|
|
*/
|
2018-06-26 10:43:06 -07:00
|
|
|
name: string;
|
2019-03-19 21:22:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An expression representing a reference to the pipe itself.
|
|
|
|
*/
|
2019-12-18 14:03:05 +00:00
|
|
|
type: R3Reference;
|
2019-03-19 21:22:03 +01:00
|
|
|
|
refactor(ivy): split `type` into `type`, `internalType` and `adjacentType` (#33533)
When compiling an Angular decorator (e.g. Directive), @angular/compiler
generates an 'expression' to be added as a static definition field
on the class, a 'type' which will be added for that field to the .d.ts
file, and a statement adjacent to the class that calls `setClassMetadata()`.
Previously, the same WrappedNodeExpr of the class' ts.Identifier was used
within each of this situations.
In the ngtsc case, this is proper. In the ngcc case, if the class being
compiled is within an ES5 IIFE, the outer name of the class may have
changed. Thus, the class has both an inner and outer name. The outer name
should continue to be used elsewhere in the compiler and in 'type'.
The 'expression' will live within the IIFE, the `internalType` should be used.
The adjacent statement will also live within the IIFE, the `adjacentType` should be used.
This commit introduces `ReflectionHost.getInternalNameOfClass()` and
`ReflectionHost.getAdjacentNameOfClass()`, which the compiler can use to
query for the correct name to use.
PR Close #33533
2019-11-01 16:55:09 +00:00
|
|
|
/**
|
|
|
|
* An expression representing the pipe being compiled, intended for use within a class definition
|
|
|
|
* itself.
|
|
|
|
*
|
|
|
|
* This can differ from the outer `type` if the class is being compiled by ngcc and is inside an
|
|
|
|
* IIFE structure that uses a different name internally.
|
|
|
|
*/
|
|
|
|
internalType: o.Expression;
|
|
|
|
|
2019-03-19 21:22:03 +01:00
|
|
|
/**
|
|
|
|
* Number of generic type parameters of the type itself.
|
|
|
|
*/
|
|
|
|
typeArgumentCount: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the pipe.
|
|
|
|
*/
|
2018-06-26 10:43:06 -07:00
|
|
|
pipeName: string;
|
2019-03-19 21:22:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Dependencies of the pipe's constructor.
|
|
|
|
*/
|
2018-07-16 16:36:31 -07:00
|
|
|
deps: R3DependencyMetadata[]|null;
|
2018-02-05 17:31:12 -08:00
|
|
|
|
2019-03-19 21:22:03 +01:00
|
|
|
/**
|
|
|
|
* Whether the pipe is marked as pure.
|
|
|
|
*/
|
|
|
|
pure: boolean;
|
2018-06-26 10:43:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export function compilePipeFromMetadata(metadata: R3PipeMetadata) {
|
2018-02-05 17:31:12 -08:00
|
|
|
const definitionMapValues: {key: string, quoted: boolean, value: o.Expression}[] = [];
|
|
|
|
|
2018-03-30 16:07:37 -07:00
|
|
|
// e.g. `name: 'myPipe'`
|
2018-06-26 10:43:06 -07:00
|
|
|
definitionMapValues.push({key: 'name', value: o.literal(metadata.pipeName), quoted: false});
|
2018-03-30 16:07:37 -07:00
|
|
|
|
|
|
|
// e.g. `type: MyPipe`
|
2019-12-18 14:03:05 +00:00
|
|
|
definitionMapValues.push({key: 'type', value: metadata.type.value, quoted: false});
|
2018-02-05 17:31:12 -08:00
|
|
|
|
2018-04-18 16:23:49 -07:00
|
|
|
// e.g. `pure: true`
|
2018-06-26 10:43:06 -07:00
|
|
|
definitionMapValues.push({key: 'pure', value: o.literal(metadata.pure), quoted: false});
|
|
|
|
|
|
|
|
const expression = o.importExpr(R3.definePipe).callFn([o.literalMap(definitionMapValues)]);
|
2018-09-21 12:12:06 -07:00
|
|
|
const type = new o.ExpressionType(o.importExpr(R3.PipeDefWithMeta, [
|
2019-12-18 14:03:05 +00:00
|
|
|
typeWithParameters(metadata.type.type, metadata.typeArgumentCount),
|
2018-06-26 10:43:06 -07:00
|
|
|
new o.ExpressionType(new o.LiteralExpr(metadata.pipeName)),
|
|
|
|
]));
|
2019-08-12 09:26:20 +03:00
|
|
|
|
|
|
|
return {expression, type};
|
2018-06-26 10:43:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a pipe definition to the output context.
|
|
|
|
*/
|
|
|
|
export function compilePipeFromRender2(
|
|
|
|
outputCtx: OutputContext, pipe: CompilePipeMetadata, reflector: CompileReflector) {
|
|
|
|
const name = identifierName(pipe.type);
|
2019-08-12 09:26:20 +03:00
|
|
|
|
2018-06-26 10:43:06 -07:00
|
|
|
if (!name) {
|
|
|
|
return error(`Cannot resolve the name of ${pipe.type}`);
|
2018-02-05 17:31:12 -08:00
|
|
|
}
|
|
|
|
|
refactor(ivy): split `type` into `type`, `internalType` and `adjacentType` (#33533)
When compiling an Angular decorator (e.g. Directive), @angular/compiler
generates an 'expression' to be added as a static definition field
on the class, a 'type' which will be added for that field to the .d.ts
file, and a statement adjacent to the class that calls `setClassMetadata()`.
Previously, the same WrappedNodeExpr of the class' ts.Identifier was used
within each of this situations.
In the ngtsc case, this is proper. In the ngcc case, if the class being
compiled is within an ES5 IIFE, the outer name of the class may have
changed. Thus, the class has both an inner and outer name. The outer name
should continue to be used elsewhere in the compiler and in 'type'.
The 'expression' will live within the IIFE, the `internalType` should be used.
The adjacent statement will also live within the IIFE, the `adjacentType` should be used.
This commit introduces `ReflectionHost.getInternalNameOfClass()` and
`ReflectionHost.getAdjacentNameOfClass()`, which the compiler can use to
query for the correct name to use.
PR Close #33533
2019-11-01 16:55:09 +00:00
|
|
|
const type = outputCtx.importExpr(pipe.type.reference);
|
2018-06-26 10:43:06 -07:00
|
|
|
const metadata: R3PipeMetadata = {
|
|
|
|
name,
|
2019-12-18 14:03:05 +00:00
|
|
|
type: wrapReference(type),
|
refactor(ivy): split `type` into `type`, `internalType` and `adjacentType` (#33533)
When compiling an Angular decorator (e.g. Directive), @angular/compiler
generates an 'expression' to be added as a static definition field
on the class, a 'type' which will be added for that field to the .d.ts
file, and a statement adjacent to the class that calls `setClassMetadata()`.
Previously, the same WrappedNodeExpr of the class' ts.Identifier was used
within each of this situations.
In the ngtsc case, this is proper. In the ngcc case, if the class being
compiled is within an ES5 IIFE, the outer name of the class may have
changed. Thus, the class has both an inner and outer name. The outer name
should continue to be used elsewhere in the compiler and in 'type'.
The 'expression' will live within the IIFE, the `internalType` should be used.
The adjacent statement will also live within the IIFE, the `adjacentType` should be used.
This commit introduces `ReflectionHost.getInternalNameOfClass()` and
`ReflectionHost.getAdjacentNameOfClass()`, which the compiler can use to
query for the correct name to use.
PR Close #33533
2019-11-01 16:55:09 +00:00
|
|
|
internalType: type,
|
2018-06-26 10:43:06 -07:00
|
|
|
pipeName: pipe.name,
|
2019-03-19 21:22:03 +01:00
|
|
|
typeArgumentCount: 0,
|
2018-06-26 10:43:06 -07:00
|
|
|
deps: dependenciesFromGlobalMetadata(pipe.type, outputCtx, reflector),
|
|
|
|
pure: pipe.pure,
|
|
|
|
};
|
|
|
|
const res = compilePipeFromMetadata(metadata);
|
2019-10-03 21:54:49 +02:00
|
|
|
const factoryRes = compileFactoryFunction(
|
|
|
|
{...metadata, injectFn: R3.directiveInject, target: R3FactoryTarget.Pipe});
|
2018-02-28 14:56:41 -08:00
|
|
|
const definitionField = outputCtx.constantPool.propertyNameOf(DefinitionKind.Pipe);
|
2019-08-12 09:26:20 +03:00
|
|
|
const ngFactoryDefStatement = new o.ClassStmt(
|
|
|
|
/* name */ name,
|
|
|
|
/* parent */ null,
|
|
|
|
/* fields */
|
|
|
|
[new o.ClassField(
|
2019-10-11 14:18:45 -07:00
|
|
|
/* name */ 'ɵfac',
|
2019-08-12 09:26:20 +03:00
|
|
|
/* type */ o.INFERRED_TYPE,
|
|
|
|
/* modifiers */[o.StmtModifier.Static],
|
|
|
|
/* initializer */ factoryRes.factory)],
|
|
|
|
/* getters */[],
|
|
|
|
/* constructorMethod */ new o.ClassMethod(null, [], []),
|
|
|
|
/* methods */[]);
|
|
|
|
const pipeDefStatement = new o.ClassStmt(
|
2018-06-26 10:43:06 -07:00
|
|
|
/* name */ name,
|
2018-04-18 16:23:38 -07:00
|
|
|
/* parent */ null,
|
|
|
|
/* fields */[new o.ClassField(
|
|
|
|
/* name */ definitionField,
|
|
|
|
/* type */ o.INFERRED_TYPE,
|
|
|
|
/* modifiers */[o.StmtModifier.Static],
|
2018-06-26 10:43:06 -07:00
|
|
|
/* initializer */ res.expression)],
|
2018-04-18 16:23:38 -07:00
|
|
|
/* getters */[],
|
|
|
|
/* constructorMethod */ new o.ClassMethod(null, [], []),
|
2019-08-12 09:26:20 +03:00
|
|
|
/* methods */[]);
|
|
|
|
|
|
|
|
outputCtx.statements.push(ngFactoryDefStatement, pipeDefStatement);
|
2018-06-26 10:43:06 -07:00
|
|
|
}
|