Skip to content

Commit 7063af5

Browse files
committed
fix(parser): fix wrong starting loc for any non-trival expression in return statement
closes #207
1 parent 10486d8 commit 7063af5

2 files changed

Lines changed: 136 additions & 12 deletions

File tree

src/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ export function parseReturnStatement(
834834
const argument =
835835
parser.flags & Flags.NewLine || parser.token & Token.IsAutoSemicolon
836836
? null
837-
: parseExpressions(parser, context, 0, 1, parser.tokenPos, parser.line, parser.column);
837+
: parseExpressions(parser, context, 0, 1, parser.tokenPos, parser.linePos, parser.colPos);
838838

839839
matchOrInsertSemicolon(parser, context | Context.AllowRegExp);
840840

test/parser/statements/return.ts

Lines changed: 135 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -565,18 +565,48 @@ describe('Statements - Return', () => {
565565
}
566566
],
567567
[
568-
'function a(foo) { return x; }',
569-
Context.None,
568+
'function a(x) { return x+y; }',
569+
Context.OptionsLoc | Context.OptionsRanges,
570570
{
571571
type: 'Program',
572572
sourceType: 'script',
573573
body: [
574574
{
575575
type: 'FunctionDeclaration',
576+
id: {
577+
type: 'Identifier',
578+
name: 'a',
579+
start: 9,
580+
end: 10,
581+
range: [9, 10],
582+
loc: {
583+
start: {
584+
line: 1,
585+
column: 9
586+
},
587+
end: {
588+
line: 1,
589+
column: 10
590+
}
591+
}
592+
},
576593
params: [
577594
{
578595
type: 'Identifier',
579-
name: 'foo'
596+
name: 'x',
597+
start: 11,
598+
end: 12,
599+
range: [11, 12],
600+
loc: {
601+
start: {
602+
line: 1,
603+
column: 11
604+
},
605+
end: {
606+
line: 1,
607+
column: 12
608+
}
609+
}
580610
}
581611
],
582612
body: {
@@ -585,21 +615,115 @@ describe('Statements - Return', () => {
585615
{
586616
type: 'ReturnStatement',
587617
argument: {
588-
type: 'Identifier',
589-
name: 'x'
618+
type: 'BinaryExpression',
619+
left: {
620+
type: 'Identifier',
621+
name: 'x',
622+
start: 23,
623+
end: 24,
624+
range: [23, 24],
625+
loc: {
626+
start: {
627+
line: 1,
628+
column: 23
629+
},
630+
end: {
631+
line: 1,
632+
column: 24
633+
}
634+
}
635+
},
636+
right: {
637+
type: 'Identifier',
638+
name: 'y',
639+
start: 25,
640+
end: 26,
641+
range: [25, 26],
642+
loc: {
643+
start: {
644+
line: 1,
645+
column: 25
646+
},
647+
end: {
648+
line: 1,
649+
column: 26
650+
}
651+
}
652+
},
653+
operator: '+',
654+
start: 23,
655+
end: 26,
656+
range: [23, 26],
657+
loc: {
658+
start: {
659+
line: 1,
660+
column: 23
661+
},
662+
end: {
663+
line: 1,
664+
column: 26
665+
}
666+
}
667+
},
668+
start: 16,
669+
end: 27,
670+
range: [16, 27],
671+
loc: {
672+
start: {
673+
line: 1,
674+
column: 16
675+
},
676+
end: {
677+
line: 1,
678+
column: 27
679+
}
590680
}
591681
}
592-
]
682+
],
683+
start: 14,
684+
end: 29,
685+
range: [14, 29],
686+
loc: {
687+
start: {
688+
line: 1,
689+
column: 14
690+
},
691+
end: {
692+
line: 1,
693+
column: 29
694+
}
695+
}
593696
},
594697
async: false,
595698
generator: false,
596-
597-
id: {
598-
type: 'Identifier',
599-
name: 'a'
699+
start: 0,
700+
end: 29,
701+
range: [0, 29],
702+
loc: {
703+
start: {
704+
line: 1,
705+
column: 0
706+
},
707+
end: {
708+
line: 1,
709+
column: 29
710+
}
600711
}
601712
}
602-
]
713+
],
714+
start: 0,
715+
end: 29,
716+
range: [0, 29],
717+
loc: {
718+
start: {
719+
line: 1,
720+
column: 0
721+
},
722+
end: {
723+
line: 1,
724+
column: 29
725+
}
726+
}
603727
}
604728
]
605729
]);

0 commit comments

Comments
 (0)