2019-07-29 16:23:29 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2019-07-29 16:23:29 -04:00
|
|
|
*
|
|
|
|
* 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 {AbsoluteSourceSpan} from '@angular/compiler';
|
|
|
|
import * as e from '../../../src/expression_parser/ast';
|
|
|
|
import * as t from '../../../src/render3/r3_ast';
|
|
|
|
import {unparse} from '../../expression_parser/utils/unparser';
|
|
|
|
|
|
|
|
type HumanizedExpressionSource = [string, AbsoluteSourceSpan];
|
|
|
|
class ExpressionSourceHumanizer extends e.RecursiveAstVisitor implements t.Visitor {
|
|
|
|
result: HumanizedExpressionSource[] = [];
|
|
|
|
|
2020-04-08 13:14:18 -04:00
|
|
|
private recordAst(ast: e.AST) {
|
|
|
|
this.result.push([unparse(ast), ast.sourceSpan]);
|
|
|
|
}
|
2019-07-29 16:23:29 -04:00
|
|
|
|
refactor(compiler): Remove NullAstVisitor and visitAstChildren (#35619)
This commit removes the `NullAstVisitor` and `visitAstChildren` exported
from `packages/compiler/src/expression_parser/ast.ts` because they
contain duplicate and buggy implementation, and their use cases could be
sufficiently covered by `RecursiveAstVisitor` if the latter implements the
`visit` method. This use case is only needed in the language service.
With this change, any visitor that extends `RecursiveAstVisitor` could
just define their own `visit` function and the parent class will behave
correctly.
A bit of historical context:
In language service, we need a way to tranverse the expression AST in a
selective manner based on where the user's cursor is. This means we need a
"filtering" function to decide which node to visit and which node to not
visit. Instead of refactoring `RecursiveAstVisitor` to support this,
`visitAstChildren` was created. `visitAstChildren` duplicates the
implementation of `RecursiveAstVisitor`, but introduced some bugs along
the way. For example, in `visitKeyedWrite`, it visits
```
obj -> key -> obj
```
instead of
```
obj -> key -> value
```
Moreover, because of the following line
```
visitor.visit && visitor.visit(ast, context) || ast.visit(visitor, context);
```
`visitAstChildren` visits every node *twice*.
PR Close #35619
2020-02-21 13:20:52 -05:00
|
|
|
// This method is defined to reconcile the type of ExpressionSourceHumanizer
|
|
|
|
// since both RecursiveAstVisitor and Visitor define the visit() method in
|
|
|
|
// their interfaces.
|
2021-06-07 11:10:51 -04:00
|
|
|
override visit(node: e.AST|t.Node, context?: any) {
|
refactor(compiler): Remove NullAstVisitor and visitAstChildren (#35619)
This commit removes the `NullAstVisitor` and `visitAstChildren` exported
from `packages/compiler/src/expression_parser/ast.ts` because they
contain duplicate and buggy implementation, and their use cases could be
sufficiently covered by `RecursiveAstVisitor` if the latter implements the
`visit` method. This use case is only needed in the language service.
With this change, any visitor that extends `RecursiveAstVisitor` could
just define their own `visit` function and the parent class will behave
correctly.
A bit of historical context:
In language service, we need a way to tranverse the expression AST in a
selective manner based on where the user's cursor is. This means we need a
"filtering" function to decide which node to visit and which node to not
visit. Instead of refactoring `RecursiveAstVisitor` to support this,
`visitAstChildren` was created. `visitAstChildren` duplicates the
implementation of `RecursiveAstVisitor`, but introduced some bugs along
the way. For example, in `visitKeyedWrite`, it visits
```
obj -> key -> obj
```
instead of
```
obj -> key -> value
```
Moreover, because of the following line
```
visitor.visit && visitor.visit(ast, context) || ast.visit(visitor, context);
```
`visitAstChildren` visits every node *twice*.
PR Close #35619
2020-02-21 13:20:52 -05:00
|
|
|
if (node instanceof e.AST) {
|
|
|
|
node.visit(this, context);
|
|
|
|
} else {
|
|
|
|
node.visit(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 16:23:29 -04:00
|
|
|
visitASTWithSource(ast: e.ASTWithSource) {
|
|
|
|
this.recordAst(ast);
|
|
|
|
this.visitAll([ast.ast], null);
|
|
|
|
}
|
2021-06-07 11:10:51 -04:00
|
|
|
override visitBinary(ast: e.Binary) {
|
2019-07-29 16:23:29 -04:00
|
|
|
this.recordAst(ast);
|
|
|
|
super.visitBinary(ast, null);
|
|
|
|
}
|
2021-06-07 11:10:51 -04:00
|
|
|
override visitChain(ast: e.Chain) {
|
2019-07-29 16:23:29 -04:00
|
|
|
this.recordAst(ast);
|
|
|
|
super.visitChain(ast, null);
|
|
|
|
}
|
2021-06-07 11:10:51 -04:00
|
|
|
override visitConditional(ast: e.Conditional) {
|
2019-07-29 16:23:29 -04:00
|
|
|
this.recordAst(ast);
|
|
|
|
super.visitConditional(ast, null);
|
|
|
|
}
|
2021-06-07 11:10:51 -04:00
|
|
|
override visitFunctionCall(ast: e.FunctionCall) {
|
2019-07-29 16:23:29 -04:00
|
|
|
this.recordAst(ast);
|
|
|
|
super.visitFunctionCall(ast, null);
|
|
|
|
}
|
2021-06-07 11:10:51 -04:00
|
|
|
override visitImplicitReceiver(ast: e.ImplicitReceiver) {
|
2019-07-29 16:23:29 -04:00
|
|
|
this.recordAst(ast);
|
|
|
|
super.visitImplicitReceiver(ast, null);
|
|
|
|
}
|
2021-06-07 11:10:51 -04:00
|
|
|
override visitInterpolation(ast: e.Interpolation) {
|
2019-07-29 16:23:29 -04:00
|
|
|
this.recordAst(ast);
|
|
|
|
super.visitInterpolation(ast, null);
|
|
|
|
}
|
2021-06-07 11:10:51 -04:00
|
|
|
override visitKeyedRead(ast: e.KeyedRead) {
|
2019-07-29 16:23:29 -04:00
|
|
|
this.recordAst(ast);
|
|
|
|
super.visitKeyedRead(ast, null);
|
|
|
|
}
|
2021-06-07 11:10:51 -04:00
|
|
|
override visitKeyedWrite(ast: e.KeyedWrite) {
|
2019-07-29 16:23:29 -04:00
|
|
|
this.recordAst(ast);
|
|
|
|
super.visitKeyedWrite(ast, null);
|
|
|
|
}
|
2021-06-07 11:10:51 -04:00
|
|
|
override visitLiteralPrimitive(ast: e.LiteralPrimitive) {
|
2019-07-29 16:23:29 -04:00
|
|
|
this.recordAst(ast);
|
|
|
|
super.visitLiteralPrimitive(ast, null);
|
|
|
|
}
|
2021-06-07 11:10:51 -04:00
|
|
|
override visitLiteralArray(ast: e.LiteralArray) {
|
2019-07-29 16:23:29 -04:00
|
|
|
this.recordAst(ast);
|
|
|
|
super.visitLiteralArray(ast, null);
|
|
|
|
}
|
2021-06-07 11:10:51 -04:00
|
|
|
override visitLiteralMap(ast: e.LiteralMap) {
|
2019-07-29 16:23:29 -04:00
|
|
|
this.recordAst(ast);
|
|
|
|
super.visitLiteralMap(ast, null);
|
|
|
|
}
|
2021-06-07 11:10:51 -04:00
|
|
|
override visitMethodCall(ast: e.MethodCall) {
|
2019-07-29 16:23:29 -04:00
|
|
|
this.recordAst(ast);
|
|
|
|
super.visitMethodCall(ast, null);
|
|
|
|
}
|
2021-06-07 11:10:51 -04:00
|
|
|
override visitNonNullAssert(ast: e.NonNullAssert) {
|
2019-07-29 16:23:29 -04:00
|
|
|
this.recordAst(ast);
|
|
|
|
super.visitNonNullAssert(ast, null);
|
|
|
|
}
|
2021-06-07 11:10:51 -04:00
|
|
|
override visitPipe(ast: e.BindingPipe) {
|
2019-07-29 16:23:29 -04:00
|
|
|
this.recordAst(ast);
|
|
|
|
super.visitPipe(ast, null);
|
|
|
|
}
|
2021-06-07 11:10:51 -04:00
|
|
|
override visitPrefixNot(ast: e.PrefixNot) {
|
2019-07-29 16:23:29 -04:00
|
|
|
this.recordAst(ast);
|
|
|
|
super.visitPrefixNot(ast, null);
|
|
|
|
}
|
2021-06-07 11:10:51 -04:00
|
|
|
override visitPropertyRead(ast: e.PropertyRead) {
|
2019-07-29 16:23:29 -04:00
|
|
|
this.recordAst(ast);
|
|
|
|
super.visitPropertyRead(ast, null);
|
|
|
|
}
|
2021-06-07 11:10:51 -04:00
|
|
|
override visitPropertyWrite(ast: e.PropertyWrite) {
|
2019-07-29 16:23:29 -04:00
|
|
|
this.recordAst(ast);
|
|
|
|
super.visitPropertyWrite(ast, null);
|
|
|
|
}
|
2021-06-07 11:10:51 -04:00
|
|
|
override visitSafeMethodCall(ast: e.SafeMethodCall) {
|
2019-07-29 16:23:29 -04:00
|
|
|
this.recordAst(ast);
|
|
|
|
super.visitSafeMethodCall(ast, null);
|
|
|
|
}
|
2021-06-07 11:10:51 -04:00
|
|
|
override visitSafePropertyRead(ast: e.SafePropertyRead) {
|
2019-07-29 16:23:29 -04:00
|
|
|
this.recordAst(ast);
|
|
|
|
super.visitSafePropertyRead(ast, null);
|
|
|
|
}
|
2021-06-07 11:10:51 -04:00
|
|
|
override visitQuote(ast: e.Quote) {
|
2019-07-29 16:23:29 -04:00
|
|
|
this.recordAst(ast);
|
|
|
|
super.visitQuote(ast, null);
|
|
|
|
}
|
2021-06-07 11:10:51 -04:00
|
|
|
override visitSafeKeyedRead(ast: e.SafeKeyedRead) {
|
2021-05-01 12:46:34 -04:00
|
|
|
this.recordAst(ast);
|
|
|
|
super.visitSafeKeyedRead(ast, null);
|
|
|
|
}
|
2019-07-29 16:23:29 -04:00
|
|
|
|
2019-10-15 18:02:56 -04:00
|
|
|
visitTemplate(ast: t.Template) {
|
|
|
|
t.visitAll(this, ast.children);
|
|
|
|
t.visitAll(this, ast.templateAttrs);
|
|
|
|
}
|
2019-07-29 16:23:29 -04:00
|
|
|
visitElement(ast: t.Element) {
|
|
|
|
t.visitAll(this, ast.children);
|
|
|
|
t.visitAll(this, ast.inputs);
|
|
|
|
t.visitAll(this, ast.outputs);
|
|
|
|
}
|
|
|
|
visitReference(ast: t.Reference) {}
|
|
|
|
visitVariable(ast: t.Variable) {}
|
2020-04-08 13:14:18 -04:00
|
|
|
visitEvent(ast: t.BoundEvent) {
|
|
|
|
ast.handler.visit(this);
|
|
|
|
}
|
2019-07-29 16:23:29 -04:00
|
|
|
visitTextAttribute(ast: t.TextAttribute) {}
|
2020-04-08 13:14:18 -04:00
|
|
|
visitBoundAttribute(ast: t.BoundAttribute) {
|
|
|
|
ast.value.visit(this);
|
|
|
|
}
|
|
|
|
visitBoundEvent(ast: t.BoundEvent) {
|
|
|
|
ast.handler.visit(this);
|
|
|
|
}
|
|
|
|
visitBoundText(ast: t.BoundText) {
|
|
|
|
ast.value.visit(this);
|
|
|
|
}
|
2019-07-29 16:23:29 -04:00
|
|
|
visitContent(ast: t.Content) {}
|
|
|
|
visitText(ast: t.Text) {}
|
2020-09-30 18:17:21 -04:00
|
|
|
visitIcu(ast: t.Icu) {
|
|
|
|
for (const key of Object.keys(ast.vars)) {
|
|
|
|
ast.vars[key].visit(this);
|
|
|
|
}
|
|
|
|
for (const key of Object.keys(ast.placeholders)) {
|
|
|
|
ast.placeholders[key].visit(this);
|
|
|
|
}
|
|
|
|
}
|
2019-07-29 16:23:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Humanizes expression AST source spans in a template by returning an array of tuples
|
|
|
|
* [unparsed AST, AST source span]
|
|
|
|
* for each expression in the template.
|
|
|
|
* @param templateAsts template AST to humanize
|
|
|
|
*/
|
|
|
|
export function humanizeExpressionSource(templateAsts: t.Node[]): HumanizedExpressionSource[] {
|
|
|
|
const humanizer = new ExpressionSourceHumanizer();
|
|
|
|
t.visitAll(humanizer, templateAsts);
|
|
|
|
return humanizer.result;
|
|
|
|
}
|