Skip to content

Commit 63b1cdc

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 #66174. (cherry picked from commit 3a56c13)
1 parent c3f3ebe commit 63b1cdc

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

packages/compiler/src/expression_parser/parser.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,14 +1059,14 @@ class _ParseAST {
10591059
return new PrefixNot(this.span(start), this.sourceSpan(start), result);
10601060
}
10611061
} else if (this.next.isKeywordTypeof()) {
1062-
this.advance();
10631062
const start = this.inputIndex;
1064-
let result = this.parsePrefix();
1063+
this.advance();
1064+
const result = this.parsePrefix();
10651065
return new TypeofExpression(this.span(start), this.sourceSpan(start), result);
10661066
} else if (this.next.isKeywordVoid()) {
1067-
this.advance();
10681067
const start = this.inputIndex;
1069-
let result = this.parsePrefix();
1068+
this.advance();
1069+
const result = this.parsePrefix();
10701070
return new VoidExpression(this.span(start), this.sourceSpan(start), result);
10711071
}
10721072
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
@@ -719,6 +719,36 @@ describe('parser', () => {
719719
}
720720
});
721721

722+
it('should produce correct span for typeof expression', () => {
723+
const ast = parseAction('foo = typeof bar');
724+
725+
expect(unparseWithSpan(ast)).toEqual([
726+
['foo = typeof bar', 'foo = typeof bar'],
727+
['foo', 'foo'],
728+
['foo', '[nameSpan] foo'],
729+
['', ''],
730+
['typeof bar', 'typeof bar'],
731+
['bar', 'bar'],
732+
['bar', '[nameSpan] bar'],
733+
['', ' '],
734+
]);
735+
});
736+
737+
it('should produce correct span for void expression', () => {
738+
const ast = parseAction('foo = void bar');
739+
740+
expect(unparseWithSpan(ast)).toEqual([
741+
['foo = void bar', 'foo = void bar'],
742+
['foo', 'foo'],
743+
['foo', '[nameSpan] foo'],
744+
['', ''],
745+
['void bar', 'void bar'],
746+
['bar', 'bar'],
747+
['bar', '[nameSpan] bar'],
748+
['', ' '],
749+
]);
750+
});
751+
722752
it('should record span for a regex without flags', () => {
723753
const ast = parseBinding('/^http:\\/\\/foo\\.bar/');
724754
expect(unparseWithSpan(ast)).toContain([

0 commit comments

Comments
 (0)