Skip to content

Commit b04488d

Browse files
ayazhafizmhevery
authored andcommitted
feat(compiler): record absolute span of template expressions in parser (#31897)
Currently, the spans of expressions are recorded only relative to the template node that they reside in, not their source file. Introduce a `sourceSpan` property on expression ASTs that records the location of an expression relative to the entire source code file that it is in. This may allow for reducing duplication of effort in ngtsc/typecheck/src/diagnostics later on as well. Child of #31898 PR Close #31897
1 parent 68f06c8 commit b04488d

8 files changed

Lines changed: 677 additions & 190 deletions

File tree

‎packages/compiler-cli/src/ngtsc/typecheck/src/diagnostics.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,5 +270,5 @@ function parseParseSpanComment(commentText: string): ParseSpan|null {
270270
return null;
271271
}
272272

273-
return {start: +match[1], end: +match[2]};
273+
return new ParseSpan(+match[1], +match[2]);
274274
}

‎packages/compiler/src/compiler_util/expression_converter.ts‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,18 +286,20 @@ class _BuiltinAstConverter extends cdAst.AstTransformer {
286286
visitPipe(ast: cdAst.BindingPipe, context: any): any {
287287
const args = [ast.exp, ...ast.args].map(ast => ast.visit(this, context));
288288
return new BuiltinFunctionCall(
289-
ast.span, args, this._converterFactory.createPipeConverter(ast.name, args.length));
289+
ast.span, ast.sourceSpan, args,
290+
this._converterFactory.createPipeConverter(ast.name, args.length));
290291
}
291292
visitLiteralArray(ast: cdAst.LiteralArray, context: any): any {
292293
const args = ast.expressions.map(ast => ast.visit(this, context));
293294
return new BuiltinFunctionCall(
294-
ast.span, args, this._converterFactory.createLiteralArrayConverter(ast.expressions.length));
295+
ast.span, ast.sourceSpan, args,
296+
this._converterFactory.createLiteralArrayConverter(ast.expressions.length));
295297
}
296298
visitLiteralMap(ast: cdAst.LiteralMap, context: any): any {
297299
const args = ast.values.map(ast => ast.visit(this, context));
298300

299301
return new BuiltinFunctionCall(
300-
ast.span, args, this._converterFactory.createLiteralMapConverter(ast.keys));
302+
ast.span, ast.sourceSpan, args, this._converterFactory.createLiteralMapConverter(ast.keys));
301303
}
302304
}
303305

@@ -642,13 +644,14 @@ class _AstToIrVisitor implements cdAst.AstVisitor {
642644
// leftMostNode with its unguarded version in the call to `this.visit()`.
643645
if (leftMostSafe instanceof cdAst.SafeMethodCall) {
644646
this._nodeMap.set(
645-
leftMostSafe,
646-
new cdAst.MethodCall(
647-
leftMostSafe.span, leftMostSafe.receiver, leftMostSafe.name, leftMostSafe.args));
647+
leftMostSafe, new cdAst.MethodCall(
648+
leftMostSafe.span, leftMostSafe.sourceSpan, leftMostSafe.receiver,
649+
leftMostSafe.name, leftMostSafe.args));
648650
} else {
649651
this._nodeMap.set(
650-
leftMostSafe,
651-
new cdAst.PropertyRead(leftMostSafe.span, leftMostSafe.receiver, leftMostSafe.name));
652+
leftMostSafe, new cdAst.PropertyRead(
653+
leftMostSafe.span, leftMostSafe.sourceSpan, leftMostSafe.receiver,
654+
leftMostSafe.name));
652655
}
653656

654657
// Recursively convert the node now without the guarded member access.
@@ -812,7 +815,9 @@ function convertStmtIntoExpression(stmt: o.Statement): o.Expression|null {
812815
}
813816

814817
export class BuiltinFunctionCall extends cdAst.FunctionCall {
815-
constructor(span: cdAst.ParseSpan, public args: cdAst.AST[], public converter: BuiltinConverter) {
816-
super(span, null, args);
818+
constructor(
819+
span: cdAst.ParseSpan, sourceSpan: cdAst.AbsoluteSourceSpan, public args: cdAst.AST[],
820+
public converter: BuiltinConverter) {
821+
super(span, sourceSpan, null, args);
817822
}
818823
}

0 commit comments

Comments
 (0)