Skip to content

Commit

Permalink
refactor(compiler): capture interpolation source-spans in expression …
Browse files Browse the repository at this point in the history
…parser (#38747)

The expression parser will split the expression up at the interpolation markers
into expressions and static strings. This commit also captures the positions of
these strings in the expression to be used in source-mapping later.

PR Close #38747
  • Loading branch information
petebacondarwin authored and atscott committed Sep 8, 2020
1 parent a1c34c6 commit 3876202
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/compiler/src/expression_parser/parser.ts
Expand Up @@ -14,7 +14,10 @@ import {AbsoluteSourceSpan, AST, AstVisitor, ASTWithSource, Binary, BindingPipe,
import {EOF, isIdentifier, isQuote, Lexer, Token, TokenType} from './lexer';

export class SplitInterpolation {
constructor(public strings: string[], public expressions: string[], public offsets: number[]) {}
constructor(
public strings: string[], public stringSpans: {start: number, end: number}[],
public expressions: string[], public expressionsSpans: {start: number, end: number}[],
public offsets: number[]) {}
}

export class TemplateBindingParseResult {
Expand Down Expand Up @@ -194,28 +197,35 @@ export class Parser {
const strings: string[] = [];
const expressions: string[] = [];
const offsets: number[] = [];
const stringSpans: {start: number, end: number}[] = [];
const expressionSpans: {start: number, end: number}[] = [];
let offset = 0;
for (let i = 0; i < parts.length; i++) {
const part: string = parts[i];
if (i % 2 === 0) {
// fixed string
strings.push(part);
const start = offset;
offset += part.length;
stringSpans.push({start, end: offset});
} else if (part.trim().length > 0) {
const start = offset;
offset += interpolationConfig.start.length;
expressions.push(part);
offsets.push(offset);
offset += part.length + interpolationConfig.end.length;
expressionSpans.push({start, end: offset});
} else {
this._reportError(
'Blank expressions are not allowed in interpolated strings', input,
`at column ${this._findInterpolationErrorColumn(parts, i, interpolationConfig)} in`,
location);
expressions.push('$implicit');
offsets.push(offset);
expressionSpans.push({start: offset, end: offset});
}
}
return new SplitInterpolation(strings, expressions, offsets);
return new SplitInterpolation(strings, stringSpans, expressions, expressionSpans, offsets);
}

wrapLiteralPrimitive(input: string|null, location: any, absoluteOffset: number): ASTWithSource {
Expand Down

0 comments on commit 3876202

Please sign in to comment.