-
Notifications
You must be signed in to change notification settings - Fork 11.9k
feat(@angular/build): migrate Angular Linker to oxc-parser and magic-string #33625
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
Open
clydin
wants to merge
1
commit into
angular:main
Choose a base branch
from
clydin:feat/linker-oxc-migration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
297 changes: 297 additions & 0 deletions
297
packages/angular/build/src/tools/angular/linker/oxc-ast-host.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,297 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC 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.dev/license | ||
| */ | ||
|
|
||
| import type { AstHost, Range } from '@angular/compiler-cli/linker'; | ||
| import { FatalLinkerError } from '@angular/compiler-cli/linker'; | ||
| import type { | ||
| ArrayExpression, | ||
| ArrowFunctionExpression, | ||
| BooleanLiteral, | ||
| CallExpression, | ||
| Function as FunctionNode, | ||
| Node, | ||
| NullLiteral, | ||
| NumericLiteral, | ||
| ObjectExpression, | ||
| StringLiteral, | ||
| UnaryExpression, | ||
| } from '@oxc-project/types'; | ||
|
|
||
| function isNode(node: unknown): node is Node { | ||
| return typeof node === 'object' && node !== null && 'type' in node; | ||
| } | ||
|
|
||
| /** | ||
| * An implementation of `AstHost` that queries information from `oxc-parser` AST nodes. | ||
| */ | ||
| export class OxcAstHost implements AstHost<unknown> { | ||
| getSymbolName(node: unknown): string | null { | ||
| if (!isNode(node)) { | ||
| return null; | ||
| } | ||
|
|
||
| if (node.type === 'Identifier') { | ||
| return node.name; | ||
| } else if (node.type === 'MemberExpression') { | ||
| if (!node.computed && node.property.type === 'Identifier') { | ||
| return node.property.name; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| isStringLiteral(node: unknown): node is StringLiteral { | ||
| return isNode(node) && node.type === 'Literal' && typeof node.value === 'string'; | ||
| } | ||
|
clydin marked this conversation as resolved.
|
||
|
|
||
| parseStringLiteral(str: unknown): string { | ||
| if (!this.isStringLiteral(str)) { | ||
| throw new FatalLinkerError(str as object, 'Unsupported syntax, expected a string literal.'); | ||
| } | ||
|
|
||
| return str.value; | ||
| } | ||
|
|
||
| isNumericLiteral(node: unknown): node is NumericLiteral { | ||
| return isNode(node) && node.type === 'Literal' && typeof node.value === 'number'; | ||
| } | ||
|
clydin marked this conversation as resolved.
|
||
|
|
||
| parseNumericLiteral(num: unknown): number { | ||
| if (!this.isNumericLiteral(num)) { | ||
| throw new FatalLinkerError(num as object, 'Unsupported syntax, expected a numeric literal.'); | ||
| } | ||
|
|
||
| return num.value; | ||
| } | ||
|
|
||
| isBooleanLiteral(node: unknown): node is BooleanLiteral | UnaryExpression { | ||
| if (!isNode(node)) { | ||
| return false; | ||
| } | ||
|
|
||
| return ( | ||
| (node.type === 'Literal' && typeof node.value === 'boolean') || isMinifiedBooleanLiteral(node) | ||
| ); | ||
| } | ||
|
clydin marked this conversation as resolved.
|
||
|
|
||
| parseBooleanLiteral(bool: unknown): boolean { | ||
| if (!this.isBooleanLiteral(bool)) { | ||
| throw new FatalLinkerError(bool as object, 'Unsupported syntax, expected a boolean literal.'); | ||
| } | ||
|
|
||
| if (bool.type === 'Literal') { | ||
| return bool.value; | ||
| } else { | ||
| const arg = bool.argument; | ||
| if (arg.type === 'Literal' && typeof arg.value === 'number') { | ||
| return !arg.value; | ||
| } | ||
| throw new FatalLinkerError(bool as object, 'Unsupported syntax, expected a boolean literal.'); | ||
| } | ||
| } | ||
|
clydin marked this conversation as resolved.
|
||
|
|
||
| isNull(node: unknown): node is NullLiteral { | ||
| return isNode(node) && node.type === 'Literal' && node.value === null; | ||
| } | ||
|
clydin marked this conversation as resolved.
|
||
|
|
||
| isArrayLiteral(node: unknown): node is ArrayExpression { | ||
| return isNode(node) && node.type === 'ArrayExpression'; | ||
| } | ||
|
|
||
| parseArrayLiteral(array: unknown): unknown[] { | ||
| if (!this.isArrayLiteral(array)) { | ||
| throw new FatalLinkerError(array as object, 'Unsupported syntax, expected an array literal.'); | ||
| } | ||
|
|
||
| const result: unknown[] = []; | ||
|
|
||
| for (const element of array.elements) { | ||
| if (element === null) { | ||
| throw new FatalLinkerError( | ||
| array as object, | ||
| 'Unsupported syntax, element in array not to be empty.', | ||
| ); | ||
| } | ||
| if (element.type === 'SpreadElement') { | ||
| throw new FatalLinkerError( | ||
| element as object, | ||
| 'Unsupported syntax, element in array not to use spread syntax.', | ||
| ); | ||
| } | ||
| result.push(element); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| isObjectLiteral(node: unknown): node is ObjectExpression { | ||
| return isNode(node) && node.type === 'ObjectExpression'; | ||
| } | ||
|
|
||
| parseObjectLiteral(obj: unknown): Map<string, unknown> { | ||
| if (!this.isObjectLiteral(obj)) { | ||
| throw new FatalLinkerError(obj as object, 'Unsupported syntax, expected an object literal.'); | ||
| } | ||
|
|
||
| const result = new Map<string, unknown>(); | ||
|
|
||
| for (const property of obj.properties) { | ||
| if (property.type !== 'Property') { | ||
| throw new FatalLinkerError( | ||
| property as object, | ||
| 'Unsupported syntax, expected a property assignment.', | ||
| ); | ||
| } | ||
|
|
||
| const keyNode = property.key; | ||
|
|
||
| let key: string; | ||
| if (keyNode.type === 'Identifier') { | ||
| key = keyNode.name; | ||
| } else if (this.isStringLiteral(keyNode)) { | ||
| key = this.parseStringLiteral(keyNode); | ||
| } else if (this.isNumericLiteral(keyNode)) { | ||
| key = String(this.parseNumericLiteral(keyNode)); | ||
| } else { | ||
| throw new FatalLinkerError( | ||
| keyNode as object, | ||
| 'Unsupported syntax, expected a property name.', | ||
| ); | ||
| } | ||
|
|
||
| result.set(key, property.value); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
clydin marked this conversation as resolved.
|
||
|
|
||
| isFunctionExpression(node: unknown): node is FunctionNode | ArrowFunctionExpression { | ||
| if (!isNode(node)) { | ||
| return false; | ||
| } | ||
|
|
||
| return ( | ||
| node.type === 'FunctionDeclaration' || | ||
| node.type === 'FunctionExpression' || | ||
| node.type === 'ArrowFunctionExpression' | ||
| ); | ||
| } | ||
|
|
||
| parseReturnValue(fn: unknown): unknown { | ||
| if (!this.isFunctionExpression(fn)) { | ||
| throw new FatalLinkerError(fn as object, 'Unsupported syntax, expected a function.'); | ||
| } | ||
|
|
||
| const body = fn.body; | ||
| if (!body || !isNode(body)) { | ||
| throw new FatalLinkerError(fn as object, 'Unsupported syntax, expected a function body.'); | ||
| } | ||
|
|
||
| if (body.type !== 'BlockStatement') { | ||
| return body; | ||
| } | ||
|
|
||
| const statements = body.body; | ||
| if (statements.length !== 1) { | ||
| throw new FatalLinkerError( | ||
| body as object, | ||
| 'Unsupported syntax, expected a function body with a single return statement.', | ||
| ); | ||
| } | ||
|
|
||
| const stmt = statements[0]; | ||
| if (stmt.type !== 'ReturnStatement') { | ||
| throw new FatalLinkerError( | ||
| stmt as object, | ||
| 'Unsupported syntax, expected a function body with a single return statement.', | ||
| ); | ||
| } | ||
|
|
||
| if (!stmt.argument) { | ||
| throw new FatalLinkerError( | ||
| stmt as object, | ||
| 'Unsupported syntax, expected function to return a value.', | ||
| ); | ||
| } | ||
|
|
||
| return stmt.argument; | ||
| } | ||
|
|
||
| parseParameters(fn: unknown): unknown[] { | ||
| if (!this.isFunctionExpression(fn)) { | ||
| throw new FatalLinkerError(fn as object, 'Unsupported syntax, expected a function.'); | ||
| } | ||
|
|
||
| return fn.params; | ||
| } | ||
|
|
||
| isCallExpression(node: unknown): node is CallExpression { | ||
| return isNode(node) && node.type === 'CallExpression'; | ||
| } | ||
|
|
||
| parseCallee(call: unknown): unknown { | ||
| if (!this.isCallExpression(call)) { | ||
| throw new FatalLinkerError(call as object, 'Unsupported syntax, expected a call expression.'); | ||
| } | ||
|
|
||
| return call.callee; | ||
| } | ||
|
|
||
| parseArguments(call: unknown): unknown[] { | ||
| if (!this.isCallExpression(call)) { | ||
| throw new FatalLinkerError(call as object, 'Unsupported syntax, expected a call expression.'); | ||
| } | ||
|
|
||
| const result: unknown[] = []; | ||
|
|
||
| for (const arg of call.arguments) { | ||
| if (arg.type === 'SpreadElement') { | ||
| throw new FatalLinkerError( | ||
| arg as object, | ||
| 'Unsupported syntax, argument not to use spread syntax.', | ||
| ); | ||
| } | ||
| result.push(arg); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| getRange(node: unknown): Range { | ||
| if (!isNode(node) || typeof node.start !== 'number' || typeof node.end !== 'number') { | ||
| throw new FatalLinkerError( | ||
| node as object, | ||
| 'Unable to read range for node - it is missing location information.', | ||
| ); | ||
| } | ||
|
|
||
| return { | ||
| startPos: node.start, | ||
| startLine: 0, | ||
| startCol: 0, | ||
| endPos: node.end, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| function isMinifiedBooleanLiteral(node: Node): node is UnaryExpression { | ||
| if (node.type !== 'UnaryExpression') { | ||
| return false; | ||
| } | ||
|
|
||
| const arg = node.argument; | ||
|
|
||
| return ( | ||
| node.prefix === true && | ||
| node.operator === '!' && | ||
| arg.type === 'Literal' && | ||
| typeof arg.value === 'number' && | ||
| (arg.value === 0 || arg.value === 1) | ||
| ); | ||
| } | ||
|
clydin marked this conversation as resolved.
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.