Skip to content

Commit

Permalink
fix(parser): fixed issue with template & sequence expr
Browse files Browse the repository at this point in the history
  • Loading branch information
KFlash committed May 15, 2019
1 parent b2cf29f commit 627cf3b
Show file tree
Hide file tree
Showing 5 changed files with 347 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "meriyah",
"version": "0.1.7",
"version": "0.1.8",
"description": "Fast and lightweight, standard-compliant javascript parser written in ECMAScript",
"main": "dist/meriyah.umd.js",
"module": "dist/meriyah.esm.js",
Expand Down
16 changes: 10 additions & 6 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2407,7 +2407,7 @@ export function parseMemberOrUpdateExpression(
parser.assignable = AssignmentKind.NotAssignable;
return expr;
} else if (parser.token === Token.LeftParen) {
const args = parseArguments(parser, context);
const args = parseArguments(parser, context & ~Context.DisallowInContext);
parser.assignable = AssignmentKind.NotAssignable;
expr = {
type: 'CallExpression',
Expand All @@ -2421,7 +2421,7 @@ export function parseMemberOrUpdateExpression(
tag: expr,
quasi:
parser.token === Token.TemplateContinuation
? parseTemplate(parser, context)
? parseTemplate(parser, context | Context.TaggedTemplate)
: parseTemplateLiteral(parser, context)
};
}
Expand Down Expand Up @@ -2732,11 +2732,11 @@ export function parseTemplateTail(parser: ParserState, context: Context): ESTree
export function parseTemplate(parser: ParserState, context: Context): any {
const quasis = [parseTemplateSpans(parser, /* tail */ false)];
consume(parser, context | Context.AllowRegExp, Token.TemplateContinuation);
const expressions = [parseExpression(parser, context, /* assignable */ 1)];
const expressions = [parseExpressions(parser, context, /* assignable */ 1)];
while ((parser.token = scanTemplateTail(parser, context)) !== Token.TemplateTail) {
quasis.push(parseTemplateSpans(parser, /* tail */ false));
consume(parser, context | Context.AllowRegExp, Token.TemplateContinuation);
expressions.push(parseExpression(parser, context, /* assignable */ 1));
expressions.push(parseExpressions(parser, context, /* assignable */ 1));
}
quasis.push(parseTemplateSpans(parser, /* tail */ true));

Expand Down Expand Up @@ -3647,7 +3647,11 @@ export function parseObjectLiteralOrPattern(
value = {
type: 'AssignmentPattern',
left: key,
right: parseExpression(parser, context, /* assignable */ 1)
right: parseExpression(
parser,
(context | Context.DisallowInContext) ^ Context.DisallowInContext,
/* assignable */ 1
)
};
} else {
value = key;
Expand Down Expand Up @@ -4697,7 +4701,7 @@ export function parseNewExpression(
return {
type: 'NewExpression',
callee,
arguments: parser.token === Token.LeftParen ? parseArguments(parser, context) : []
arguments: parser.token === Token.LeftParen ? parseArguments(parser, context & ~Context.DisallowInContext) : []
} as ESTree.NewExpression;
}

Expand Down
132 changes: 132 additions & 0 deletions test/parser/expressions/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,138 @@ describe('Expressions - Template', () => {
type: 'Program'
}
],*/
[
'`${y, x`)`}`',
Context.None,
{
type: 'Program',
sourceType: 'script',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'TemplateLiteral',
expressions: [
{
type: 'SequenceExpression',
expressions: [
{
type: 'Identifier',
name: 'y'
},
{
type: 'TaggedTemplateExpression',
tag: {
type: 'Identifier',
name: 'x'
},
quasi: {
type: 'TemplateLiteral',
expressions: [],
quasis: [
{
type: 'TemplateElement',
value: {
cooked: ')',
raw: ')'
},
tail: true
}
]
}
}
]
}
],
quasis: [
{
type: 'TemplateElement',
value: {
cooked: '',
raw: ''
},
tail: false
},
{
type: 'TemplateElement',
value: {
cooked: '',
raw: ''
},
tail: true
}
]
}
}
]
}
],
[
'`${x`)`, y}`',
Context.None,
{
type: 'Program',
sourceType: 'script',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'TemplateLiteral',
expressions: [
{
type: 'SequenceExpression',
expressions: [
{
type: 'TaggedTemplateExpression',
tag: {
type: 'Identifier',
name: 'x'
},
quasi: {
type: 'TemplateLiteral',
expressions: [],
quasis: [
{
type: 'TemplateElement',
value: {
cooked: ')',
raw: ')'
},
tail: true
}
]
}
},
{
type: 'Identifier',
name: 'y'
}
]
}
],
quasis: [
{
type: 'TemplateElement',
value: {
cooked: '',
raw: ''
},
tail: false
},
{
type: 'TemplateElement',
value: {
cooked: '',
raw: ''
},
tail: true
}
]
}
}
]
}
],
[
'`a${b=c}d`',
Context.None,
Expand Down
71 changes: 71 additions & 0 deletions test/parser/statements/for-of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,77 @@ describe('Statements - For of', () => {
]
}
],
[
'for (const {f = x in /([--])|[--]|=+|[-\x1c$-\x9a+-\xad-]/y} of []) {}',
Context.None,
{
type: 'Program',
sourceType: 'script',
body: [
{
type: 'ForOfStatement',
body: {
type: 'BlockStatement',
body: []
},
left: {
type: 'VariableDeclaration',
kind: 'const',
declarations: [
{
type: 'VariableDeclarator',
init: null,
id: {
type: 'ObjectPattern',
properties: [
{
type: 'Property',
kind: 'init',
key: {
type: 'Identifier',
name: 'f'
},
computed: false,
value: {
type: 'AssignmentPattern',
left: {
type: 'Identifier',
name: 'f'
},
right: {
type: 'BinaryExpression',
left: {
type: 'Identifier',
name: 'x'
},
right: {
type: 'Literal',
value: /([--])|[--]|=+|[-$-š+-­-]/y,
regex: {
pattern: '([--])|[--]|=+|[-\u001c$-š+-­-]',
flags: 'y'
}
},
operator: 'in'
}
},
method: false,
shorthand: true
}
]
}
}
]
},
right: {
type: 'ArrayExpression',
elements: []
},
await: false
}
]
}
],
[
'for ([a.b].foo of c) d',
Context.None,
Expand Down
Loading

0 comments on commit 627cf3b

Please sign in to comment.