Skip to content

Commit

Permalink
fix(parser): fix wrong loc for TemplateLiteral
Browse files Browse the repository at this point in the history
closes #167
  • Loading branch information
3cp committed Jan 28, 2021
1 parent 811b078 commit a893c16
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3958,7 +3958,7 @@ export function parseMemberOrUpdateExpression(
tag: expr,
quasi:
parser.token === Token.TemplateContinuation
? parseTemplate(parser, context | Context.TaggedTemplate, parser.tokenPos, parser.linePos, parser.colPos)
? parseTemplate(parser, context | Context.TaggedTemplate)
: parseTemplateLiteral(parser, context, parser.tokenPos, parser.linePos, parser.colPos)
});
}
Expand Down Expand Up @@ -4249,7 +4249,7 @@ export function parsePrimaryExpression(
case Token.TemplateSpan:
return parseTemplateLiteral(parser, context, start, line, column);
case Token.TemplateContinuation:
return parseTemplate(parser, context, start, line, column);
return parseTemplate(parser, context);
case Token.NewKeyword:
return parseNewExpression(parser, context, inGroup, start, line, column);
case Token.BigIntLiteral:
Expand Down Expand Up @@ -4473,13 +4473,7 @@ export function parseTemplateLiteral(
* @param parser Parser object
* @param context Context masks
*/
export function parseTemplate(
parser: ParserState,
context: Context,
start: number,
line: number,
column: number
): ESTree.TemplateLiteral {
export function parseTemplate(parser: ParserState, context: Context): ESTree.TemplateLiteral {
context = (context | Context.DisallowIn) ^ Context.DisallowIn;

const { tokenValue, tokenRaw, tokenPos, linePos, colPos } = parser;
Expand Down Expand Up @@ -4511,7 +4505,7 @@ export function parseTemplate(
);
}

return finishNode(parser, context, start, line, column, {
return finishNode(parser, context, tokenPos, linePos, colPos, {
type: 'TemplateLiteral',
expressions,
quasis
Expand Down Expand Up @@ -7428,7 +7422,7 @@ export function parseMembeExpressionNoCall(
tag: expr,
quasi:
parser.token === Token.TemplateContinuation
? parseTemplate(parser, context | Context.TaggedTemplate, parser.tokenPos, parser.linePos, parser.colPos)
? parseTemplate(parser, context | Context.TaggedTemplate)
: parseTemplateLiteral(parser, context, parser.tokenPos, parser.linePos, parser.colPos)
}),
0,
Expand Down
172 changes: 172 additions & 0 deletions test/parser/expressions/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3861,6 +3861,178 @@ describe('Expressions - Template', () => {
}
}
}
],
[
'function a() {\nreturn `1234${b}`;\n}\n',
Context.None | Context.OptionsRanges | Context.OptionsLoc,
{
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: [],
body: {
type: 'BlockStatement',
body: [
{
type: 'ReturnStatement',
argument: {
type: 'TemplateLiteral',
expressions: [
{
type: 'Identifier',
name: 'b',
start: 29,
end: 30,
range: [29, 30],
loc: {
start: {
line: 2,
column: 14
},
end: {
line: 2,
column: 15
}
}
}
],
quasis: [
{
type: 'TemplateElement',
value: {
cooked: '1234',
raw: '1234'
},
tail: false,
start: 23,
end: 27,
range: [23, 27],
loc: {
start: {
line: 2,
column: 8
},
end: {
line: 2,
column: 12
}
}
},
{
type: 'TemplateElement',
value: {
cooked: '',
raw: ''
},
tail: true,
start: 31,
end: 31,
range: [31, 31],
loc: {
start: {
line: 2,
column: 16
},
end: {
line: 2,
column: 16
}
}
}
],
start: 22,
end: 32,
range: [22, 32],
loc: {
start: {
line: 2,
column: 7
},
end: {
line: 2,
column: 17
}
}
},
start: 15,
end: 33,
range: [15, 33],
loc: {
start: {
line: 2,
column: 0
},
end: {
line: 2,
column: 18
}
}
}
],
start: 13,
end: 35,
range: [13, 35],
loc: {
start: {
line: 1,
column: 13
},
end: {
line: 3,
column: 1
}
}
},
async: false,
generator: false,
start: 0,
end: 35,
range: [0, 35],
loc: {
start: {
line: 1,
column: 0
},
end: {
line: 3,
column: 1
}
}
}
],
start: 0,
end: 36,
range: [0, 36],
loc: {
start: {
line: 1,
column: 0
},
end: {
line: 4,
column: 0
}
}
}
]
]);
});

0 comments on commit a893c16

Please sign in to comment.