Skip to content

Commit

Permalink
fix(parser): Fixes #25
Browse files Browse the repository at this point in the history
@nchanged You was right. `null` isn't assignable, but everything after `=` is so was missing a `assignable` set to true.

Should be ok now :)
  • Loading branch information
KFlash committed Jul 30, 2019
1 parent 0483d25 commit c2b96cb
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 12 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": "1.4.8",
"version": "1.4.9",
"description": "A 100% compliant, self-hosted javascript parser with high focus on both performance and stability",
"main": "dist/meriyah.umd.js",
"module": "dist/meriyah.esm.js",
Expand Down
2 changes: 1 addition & 1 deletion src/meriyah.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ export function parse(source: string, options?: Options): ESTree.Program {
export { ESTree, Options };

// Export current version
export const version = '1.4.8';
export const version = '1.4.9';
23 changes: 13 additions & 10 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2098,6 +2098,7 @@ function parseVariableDeclaration(
const id = parseBindingPattern(parser, context, scope, kind, origin, tokenPos, linePos, colPos);

if (parser.token === Token.Assign) {
parser.assignable = AssignmentKind.Assignable;
nextToken(parser, context | Context.AllowRegExp);
init = parseExpression(parser, context, 1, 0, 0, parser.tokenPos, parser.linePos, parser.colPos);
if (origin & Origin.ForStatement || (token & Token.IsPatternStart) < 1) {
Expand Down Expand Up @@ -4497,6 +4498,8 @@ export function parseFunctionDeclaration(
if ((flags & HoistedClassFlags.Hoisted) < 1) report(parser, Errors.DeclNoName, 'Function');
if (scope) addBindingToExports(parser, '');
} else {
// In ES6, a function behaves as a lexical binding, except in
// a script scope, or the initial scope of eval or another function.
const type =
origin & Origin.TopLevel && ((context & Context.InGlobal) < 1 || (context & Context.Module) < 1)
? BindingKind.Variable
Expand Down Expand Up @@ -5036,19 +5039,19 @@ function parseSpreadElement(
): ESTree.SpreadElement {
nextToken(parser, context | Context.AllowRegExp); // skip '...'

let argument: ESTree.BindingName | ESTree.Expression | ESTree.PropertyName | null = null;
let destructible: AssignmentKind | DestructuringKind = 0;
let argument: ESTree.Expression | null = null;
let destructible: AssignmentKind | DestructuringKind = DestructuringKind.None;

const { tokenPos, linePos, colPos } = parser;
let { token, tokenPos, linePos, colPos } = parser;

if (parser.token & (Token.Keyword | Token.IsIdentifier)) {
if (token & (Token.Keyword | Token.IsIdentifier)) {
parser.assignable = AssignmentKind.Assignable;

const tokenValue = parser.tokenValue;

argument = parsePrimaryExpressionExtended(parser, context, kind, 0, 1, 0, inGroup, tokenPos, linePos, colPos);

const { token } = parser;
token = parser.token;

argument = parseMemberOrUpdateExpression(
parser,
Expand Down Expand Up @@ -5092,15 +5095,15 @@ function parseSpreadElement(
}

destructible |= parser.destructible & DestructuringKind.Await ? DestructuringKind.Await : 0;
} else if (parser.token === closingToken) {
} else if (token === closingToken) {
report(parser, Errors.RestMissingArg);
} else if (parser.token & Token.IsPatternStart) {
} else if (token & Token.IsPatternStart) {
argument =
parser.token === Token.LeftBrace
? parseObjectLiteralOrPattern(parser, context, scope, 1, inGroup, kind, origin, tokenPos, linePos, colPos)
: parseArrayExpressionOrPattern(parser, context, scope, 1, inGroup, kind, origin, tokenPos, linePos, colPos);

const { token } = parser;
token = parser.token;

if (token !== Token.Assign && token !== closingToken && token !== Token.Comma) {
if (parser.destructible & DestructuringKind.HasToDestruct) report(parser, Errors.InvalidDestructuringTarget);
Expand All @@ -5109,9 +5112,9 @@ function parseSpreadElement(

destructible |= parser.assignable & AssignmentKind.CannotAssign ? DestructuringKind.CannotDestruct : 0;

const { token } = parser;
token = parser.token;

if (parser.token !== Token.Comma && parser.token !== closingToken) {
if (token !== Token.Comma && token !== closingToken) {
argument = parseAssignmentExpression(
parser,
context,
Expand Down
73 changes: 73 additions & 0 deletions test/parser/expressions/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,79 @@ describe('Expressions - Async', () => {
sourceType: 'script'
}
],
[
`async function test(){
const someVar = null;
const done = async foo => {}
}`,
Context.None,
{
type: 'Program',
sourceType: 'script',
body: [
{
type: 'FunctionDeclaration',
params: [],
body: {
type: 'BlockStatement',
body: [
{
type: 'VariableDeclaration',
kind: 'const',
declarations: [
{
type: 'VariableDeclarator',
init: {
type: 'Literal',
value: null
},
id: {
type: 'Identifier',
name: 'someVar'
}
}
]
},
{
type: 'VariableDeclaration',
kind: 'const',
declarations: [
{
type: 'VariableDeclarator',
init: {
type: 'ArrowFunctionExpression',
body: {
type: 'BlockStatement',
body: []
},
params: [
{
type: 'Identifier',
name: 'foo'
}
],
async: true,
expression: false
},
id: {
type: 'Identifier',
name: 'done'
}
}
]
}
]
},
async: true,
generator: false,
id: {
type: 'Identifier',
name: 'test'
}
}
]
}
],
[
'foo(async(), x)',
Context.OptionsRanges,
Expand Down

0 comments on commit c2b96cb

Please sign in to comment.