Repository navigation
feat(compiler-cli): automatically lower lambda expressions in metadata #18390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
packages/compiler-cli/src/transformers/lower_expressions.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| /** | ||
| * @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 {CollectorOptions, MetadataCollector, MetadataValue, ModuleMetadata} from '@angular/tsc-wrapped'; | ||
| import * as ts from 'typescript'; | ||
|
|
||
| export interface LoweringRequest { | ||
| kind: ts.SyntaxKind; | ||
| location: number; | ||
| end: number; | ||
| name: string; | ||
| } | ||
|
|
||
| export type RequestLocationMap = Map<number, LoweringRequest>; | ||
|
|
||
| interface Declaration { | ||
| name: string; | ||
| node: ts.Node; | ||
| } | ||
|
|
||
| interface DeclarationInsert { | ||
| declarations: Declaration[]; | ||
| priorTo: ts.Node; | ||
| } | ||
|
|
||
| function toMap<T, K>(items: T[], select: (item: T) => K): Map<K, T> { | ||
| return new Map(items.map<[K, T]>(i => [select(i), i])); | ||
| } | ||
|
|
||
| function transformSourceFile( | ||
| sourceFile: ts.SourceFile, requests: RequestLocationMap, | ||
| context: ts.TransformationContext): ts.SourceFile { | ||
| const inserts: DeclarationInsert[] = []; | ||
|
|
||
| // Calculate the range of intersting locations. The transform will only visit nodes in this | ||
| // range to improve the performance on large files. | ||
| const locations = Array.from(requests.keys()); | ||
| const min = Math.min(...locations); | ||
| const max = Math.max(...locations); | ||
|
|
||
| function visitSourceFile(sourceFile: ts.SourceFile): ts.SourceFile { | ||
| function topLevelStatement(node: ts.Node): ts.Node { | ||
| const declarations: Declaration[] = []; | ||
|
|
||
| function visitNode(node: ts.Node): ts.Node { | ||
| const nodeRequest = requests.get(node.pos); | ||
| if (nodeRequest && nodeRequest.kind == node.kind && nodeRequest.end == node.end) { | ||
| // This node is requested to be rewritten as a reference to the exported name. | ||
| // Record that the node needs to be moved to an exported variable with the given name | ||
| const name = nodeRequest.name; | ||
| declarations.push({name, node}); | ||
| return ts.createIdentifier(name); | ||
| } | ||
| if (node.pos <= max && node.end >= min) return ts.visitEachChild(node, visitNode, context); | ||
| return node; | ||
| } | ||
|
|
||
| const result = ts.visitEachChild(node, visitNode, context); | ||
|
|
||
| if (declarations.length) { | ||
| inserts.push({priorTo: result, declarations}) | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| const traversedSource = ts.visitEachChild(sourceFile, topLevelStatement, context); | ||
| if (inserts.length) { | ||
| // Insert the declarations before the rewritten statement that references them. | ||
| const insertMap = toMap(inserts, i => i.priorTo); | ||
| const newStatements: ts.Statement[] = [...traversedSource.statements]; | ||
| for (let i = newStatements.length; i >= 0; i--) { | ||
| const statement = newStatements[i]; | ||
| const insert = insertMap.get(statement); | ||
| if (insert) { | ||
| const declarations = insert.declarations.map( | ||
| i => ts.createVariableDeclaration( | ||
| i.name, /* type */ undefined, i.node as ts.Expression)); | ||
| const statement = ts.createVariableStatement( | ||
| /* modifiers */ undefined, | ||
| ts.createVariableDeclarationList(declarations, ts.NodeFlags.Const)); | ||
| newStatements.splice(i, 0, statement); | ||
| } | ||
| } | ||
|
|
||
| // Insert an exports clause to export the declarations | ||
| newStatements.push(ts.createExportDeclaration( | ||
| /* decorators */ undefined, | ||
| /* modifiers */ undefined, | ||
| ts.createNamedExports( | ||
| inserts | ||
| .reduce( | ||
| (accumulator, insert) => [...accumulator, ...insert.declarations], | ||
| [] as Declaration[]) | ||
| .map( | ||
| declaration => ts.createExportSpecifier( | ||
| /* propertyName */ undefined, declaration.name))))); | ||
| return ts.updateSourceFileNode(traversedSource, newStatements); | ||
| } | ||
| return traversedSource; | ||
| } | ||
|
|
||
| return visitSourceFile(sourceFile); | ||
| } | ||
|
|
||
| export function getExpressionLoweringTransformFactory(requestsMap: RequestsMap): | ||
| (context: ts.TransformationContext) => (sourceFile: ts.SourceFile) => ts.SourceFile { | ||
| // Return the factory | ||
| return (context: ts.TransformationContext) => (sourceFile: ts.SourceFile): ts.SourceFile => { | ||
| const requests = requestsMap.getRequests(sourceFile); | ||
| if (requests && requests.size) { | ||
| return transformSourceFile(sourceFile, requests, context) | ||
| } | ||
| return sourceFile; | ||
| }; | ||
| } | ||
|
|
||
| export interface RequestsMap { getRequests(sourceFile: ts.SourceFile): RequestLocationMap; } | ||
|
|
||
| interface MetadataAndLoweringRequests { | ||
| metadata: ModuleMetadata|undefined; | ||
| requests: RequestLocationMap; | ||
| } | ||
|
|
||
| export class LowerMetadataCache implements RequestsMap { | ||
| private collector: MetadataCollector; | ||
| private metadataCache = new Map<string, MetadataAndLoweringRequests>(); | ||
|
|
||
| constructor(options: CollectorOptions, private strict?: boolean) { | ||
| this.collector = new MetadataCollector(options); | ||
| } | ||
|
|
||
| getMetadata(sourceFile: ts.SourceFile): ModuleMetadata|undefined { | ||
| return this.ensureMetadataAndRequests(sourceFile).metadata; | ||
| } | ||
|
|
||
| getRequests(sourceFile: ts.SourceFile): RequestLocationMap { | ||
| return this.ensureMetadataAndRequests(sourceFile).requests; | ||
| } | ||
|
|
||
| private ensureMetadataAndRequests(sourceFile: ts.SourceFile): MetadataAndLoweringRequests { | ||
| let result = this.metadataCache.get(sourceFile.fileName); | ||
| if (!result) { | ||
| result = this.getMetadataAndRequests(sourceFile); | ||
| this.metadataCache.set(sourceFile.fileName, result); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private getMetadataAndRequests(sourceFile: ts.SourceFile): MetadataAndLoweringRequests { | ||
| let identNumber = 0; | ||
| const freshIdent = () => '\u0275' + identNumber++; | ||
| const requests = new Map<number, LoweringRequest>(); | ||
| const replaceNode = | ||
| (node: ts.Node) => { | ||
| const name = freshIdent(); | ||
| requests.set(node.pos, {name, kind: node.kind, location: node.pos, end: node.end}); | ||
| return {__symbolic: 'reference', name}; | ||
| } | ||
|
|
||
| const substituteExpression = (value: MetadataValue, node: ts.Node): MetadataValue => { | ||
| if (node.kind === ts.SyntaxKind.ArrowFunction || | ||
| node.kind === ts.SyntaxKind.FunctionExpression) { | ||
| return replaceNode(node); | ||
| } | ||
| return value; | ||
| }; | ||
|
|
||
| const metadata = this.collector.getMetadata(sourceFile, this.strict, substituteExpression); | ||
|
|
||
| return {metadata, requests}; | ||
| } | ||
| } | ||
8 changes: 8 additions & 0 deletions
8
packages/compiler-cli/src/transformers/module_filename_resolver.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
createConst ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change this to be const by adding a const modifier to the declaration list.