Skip to content

Commit 3c3ae0c

Browse files
committed
fix(compiler): provide location information for literal map keys
Adds spans for the keys of a `LiteralMap`. Fixes #66175. (cherry picked from commit 76fa180)
1 parent b37f46b commit 3c3ae0c

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

packages/compiler/src/expression_parser/ast.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,13 @@ export class LiteralArray extends AST {
218218
}
219219
}
220220

221-
export type LiteralMapKey = {
221+
export interface LiteralMapKey {
222222
key: string;
223223
quoted: boolean;
224+
span: ParseSpan;
225+
sourceSpan: AbsoluteSourceSpan;
224226
isShorthandInitialized?: boolean;
225-
};
227+
}
226228

227229
export class LiteralMap extends AST {
228230
constructor(

packages/compiler/src/expression_parser/parser.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,14 @@ class _ParseAST {
11971197
const keyStart = this.inputIndex;
11981198
const quoted = this.next.isString();
11991199
const key = this.expectIdentifierOrKeywordOrString();
1200-
const literalMapKey: LiteralMapKey = {key, quoted};
1200+
const keySpan = this.span(keyStart);
1201+
const keySourceSpan = this.sourceSpan(keyStart);
1202+
const literalMapKey: LiteralMapKey = {
1203+
key,
1204+
quoted,
1205+
span: keySpan,
1206+
sourceSpan: keySourceSpan,
1207+
};
12011208
keys.push(literalMapKey);
12021209

12031210
// Properties with quoted keys can't use the shorthand syntax.
@@ -1209,14 +1216,12 @@ class _ParseAST {
12091216
} else {
12101217
literalMapKey.isShorthandInitialized = true;
12111218

1212-
const span = this.span(keyStart);
1213-
const sourceSpan = this.sourceSpan(keyStart);
12141219
values.push(
12151220
new PropertyRead(
1216-
span,
1217-
sourceSpan,
1218-
sourceSpan,
1219-
new ImplicitReceiver(span, sourceSpan),
1221+
keySpan,
1222+
keySourceSpan,
1223+
keySourceSpan,
1224+
new ImplicitReceiver(keySpan, keySourceSpan),
12201225
key,
12211226
),
12221227
);

packages/compiler/test/expression_parser/parser_spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
TemplateBinding,
1919
VariableBinding,
2020
BindingPipeType,
21+
ParseSpan,
2122
} from '../../src/expression_parser/ast';
2223
import {ParseError} from '../../src/parse_util';
2324
import {Lexer} from '../../src/expression_parser/lexer';
@@ -733,6 +734,17 @@ describe('parser', () => {
733734
'/^http:\\/\\/foo\\.bar/gim',
734735
]);
735736
});
737+
738+
it('should record span for literal map keys', () => {
739+
const ast = parseBinding('{one: 1, two: "the number two", three, "four": 4}');
740+
const literal = ast.ast as LiteralMap;
741+
const getSource = (span: ParseSpan) => ast.source?.substring(span.start, span.end);
742+
743+
expect(getSource(literal.keys[0].span)).toBe('one');
744+
expect(getSource(literal.keys[1].span)).toBe('two');
745+
expect(getSource(literal.keys[2].span)).toBe('three');
746+
expect(getSource(literal.keys[3].span)).toBe('"four"');
747+
});
736748
});
737749

738750
describe('general error handling', () => {

0 commit comments

Comments
 (0)