Skip to content

Commit 3a56c13

Browse files
crisbetokirjs
authored andcommitted
fix(compiler): produce accurate span for typeof and void expressions
Fixes that the `typeof` and `void` expressions were starting their spans from the expression start, rather than the keyword. Fixes angular#66174.
1 parent 0d6ac92 commit 3a56c13

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

packages/compiler/src/expression_parser/parser.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,14 +1062,14 @@ class _ParseAST {
10621062
return new PrefixNot(this.span(start), this.sourceSpan(start), result);
10631063
}
10641064
} else if (this.next.isKeywordTypeof()) {
1065-
this.advance();
10661065
const start = this.inputIndex;
1067-
let result = this.parsePrefix();
1066+
this.advance();
1067+
const result = this.parsePrefix();
10681068
return new TypeofExpression(this.span(start), this.sourceSpan(start), result);
10691069
} else if (this.next.isKeywordVoid()) {
1070-
this.advance();
10711070
const start = this.inputIndex;
1072-
let result = this.parsePrefix();
1071+
this.advance();
1072+
const result = this.parsePrefix();
10731073
return new VoidExpression(this.span(start), this.sourceSpan(start), result);
10741074
}
10751075
return this.parseCallChain();

packages/compiler/test/expression_parser/parser_spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,36 @@ describe('parser', () => {
749749
}
750750
});
751751

752+
it('should produce correct span for typeof expression', () => {
753+
const ast = parseAction('foo = typeof bar');
754+
755+
expect(unparseWithSpan(ast)).toEqual([
756+
['foo = typeof bar', 'foo = typeof bar'],
757+
['foo', 'foo'],
758+
['foo', '[nameSpan] foo'],
759+
['', ''],
760+
['typeof bar', 'typeof bar'],
761+
['bar', 'bar'],
762+
['bar', '[nameSpan] bar'],
763+
['', ' '],
764+
]);
765+
});
766+
767+
it('should produce correct span for void expression', () => {
768+
const ast = parseAction('foo = void bar');
769+
770+
expect(unparseWithSpan(ast)).toEqual([
771+
['foo = void bar', 'foo = void bar'],
772+
['foo', 'foo'],
773+
['foo', '[nameSpan] foo'],
774+
['', ''],
775+
['void bar', 'void bar'],
776+
['bar', 'bar'],
777+
['bar', '[nameSpan] bar'],
778+
['', ' '],
779+
]);
780+
});
781+
752782
it('should record span for a regex without flags', () => {
753783
const ast = parseBinding('/^http:\\/\\/foo\\.bar/');
754784
expect(unparseWithSpan(ast)).toContain([

0 commit comments

Comments
 (0)