Skip to content

Commit

Permalink
fix(parser): fix wrong starting loc for any non-trival expression in …
Browse files Browse the repository at this point in the history
…return statement

closes #207
  • Loading branch information
3cp committed Mar 13, 2022
1 parent 10486d8 commit 7063af5
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/parser.ts
Expand Up @@ -834,7 +834,7 @@ export function parseReturnStatement(
const argument =
parser.flags & Flags.NewLine || parser.token & Token.IsAutoSemicolon
? null
: parseExpressions(parser, context, 0, 1, parser.tokenPos, parser.line, parser.column);
: parseExpressions(parser, context, 0, 1, parser.tokenPos, parser.linePos, parser.colPos);

matchOrInsertSemicolon(parser, context | Context.AllowRegExp);

Expand Down
146 changes: 135 additions & 11 deletions test/parser/statements/return.ts
Expand Up @@ -565,18 +565,48 @@ describe('Statements - Return', () => {
}
],
[
'function a(foo) { return x; }',
Context.None,
'function a(x) { return x+y; }',
Context.OptionsLoc | Context.OptionsRanges,
{
type: 'Program',
sourceType: 'script',
body: [
{
type: 'FunctionDeclaration',
id: {
type: 'Identifier',
name: 'a',
start: 9,
end: 10,
range: [9, 10],
loc: {
start: {
line: 1,
column: 9
},
end: {
line: 1,
column: 10
}
}
},
params: [
{
type: 'Identifier',
name: 'foo'
name: 'x',
start: 11,
end: 12,
range: [11, 12],
loc: {
start: {
line: 1,
column: 11
},
end: {
line: 1,
column: 12
}
}
}
],
body: {
Expand All @@ -585,21 +615,115 @@ describe('Statements - Return', () => {
{
type: 'ReturnStatement',
argument: {
type: 'Identifier',
name: 'x'
type: 'BinaryExpression',
left: {
type: 'Identifier',
name: 'x',
start: 23,
end: 24,
range: [23, 24],
loc: {
start: {
line: 1,
column: 23
},
end: {
line: 1,
column: 24
}
}
},
right: {
type: 'Identifier',
name: 'y',
start: 25,
end: 26,
range: [25, 26],
loc: {
start: {
line: 1,
column: 25
},
end: {
line: 1,
column: 26
}
}
},
operator: '+',
start: 23,
end: 26,
range: [23, 26],
loc: {
start: {
line: 1,
column: 23
},
end: {
line: 1,
column: 26
}
}
},
start: 16,
end: 27,
range: [16, 27],
loc: {
start: {
line: 1,
column: 16
},
end: {
line: 1,
column: 27
}
}
}
]
],
start: 14,
end: 29,
range: [14, 29],
loc: {
start: {
line: 1,
column: 14
},
end: {
line: 1,
column: 29
}
}
},
async: false,
generator: false,

id: {
type: 'Identifier',
name: 'a'
start: 0,
end: 29,
range: [0, 29],
loc: {
start: {
line: 1,
column: 0
},
end: {
line: 1,
column: 29
}
}
}
]
],
start: 0,
end: 29,
range: [0, 29],
loc: {
start: {
line: 1,
column: 0
},
end: {
line: 1,
column: 29
}
}
}
]
]);
Expand Down

0 comments on commit 7063af5

Please sign in to comment.