Skip to content

Commit

Permalink
fix(parser): fixed Test262 test suite edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
KFlash committed May 9, 2019
1 parent 999ac80 commit c9545fe
Show file tree
Hide file tree
Showing 14 changed files with 682 additions and 6,191 deletions.
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "meriyah",
"version": "0.1.1",
"version": "0.1.2",
"description": "Fast and lightweight, standard-compliant javascript parser written in ECMAScript",
"main": "dist/meriyah.umd.js",
"module": "dist/meriyah.esm.js",
Expand Down Expand Up @@ -90,8 +90,5 @@
"hooks": {
"pre-commit": "node ./scripts/prettier.js check-changed"
}
},
"dependencies": {
"tslib": "^1.9.3"
}
}
11 changes: 9 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2470,7 +2470,7 @@ export function parsePrimaryExpressionExtended(
// falls through
}

if (token === Token.AsyncKeyword) {
if (token === Token.AsyncKeyword || (token === Token.EscapedReserved && parser.tokenValue === 'async')) {
return parseAsyncExpression(parser, context, inNewExpression, assignable);
}

Expand Down Expand Up @@ -3688,10 +3688,12 @@ export function parseObjectLiteralOrPattern(
}
} else if (parser.token === Token.LeftBracket) {
destructible |= DestructuringKind.NotDestructible;

if (token === Token.AsyncKeyword) state |= Kind.Async;
if (token === Token.GetKeyword) state |= Kind.Getter;
else if (token === Token.SetKeyword) state |= Kind.Setter;
else state |= Kind.Method;

key = parseComputedPropertyName(parser, context);
destructible |= parser.assignable;
state |= Kind.Computed;
Expand All @@ -3703,6 +3705,8 @@ export function parseObjectLiteralOrPattern(
state |= Kind.Async;
}
key = parseIdentifier(parser, context);
if (token === Token.EscapedReserved) report(parser, Errors.Unexpected);

if (token === Token.GetKeyword) state |= Kind.Getter;
else if (token === Token.SetKeyword) state |= Kind.Setter;
else state |= Kind.Method;
Expand Down Expand Up @@ -4094,7 +4098,7 @@ export function parseComputedPropertyName(parser: ParserState, context: Context)
* [ AssignmentExpression ]
*/
nextToken(parser, context | Context.AllowRegExp);
const key = parseExpression(parser, context, /* assignable */ 1);
const key = parseExpression(parser, context & ~Context.DisallowInContext, /* assignable */ 1);
consume(parser, context, Token.RightBracket);
return key;
}
Expand Down Expand Up @@ -4546,11 +4550,14 @@ export function parseAsyncExpression(
inNewExpression: 0 | 1,
assignable: 0 | 1
): ESTree.Expression {
const { token } = parser;

const expr: ESTree.Identifier = parseIdentifier(parser, context);

const isNewLine = parser.flags & Flags.NewLine;

if (!isNewLine) {
if (token === Token.EscapedReserved) report(parser, Errors.InvalidEscapedKeyword);
// async function ...
if (parser.token === Token.FunctionKeyword) return parseFunctionExpression(parser, context, /* isAsync */ 1);

Expand Down
46 changes: 46 additions & 0 deletions test/parser/declarations/async-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,52 @@ describe('Declarations - Async Function', () => {
type: 'Program'
}
],
[
'async function *gen() { yield [...yield]; }',
Context.Strict,
{
body: [
{
async: true,
body: {
body: [
{
expression: {
argument: {
elements: [
{
argument: {
argument: null,
delegate: false,
type: 'YieldExpression'
},
type: 'SpreadElement'
}
],
type: 'ArrayExpression'
},
delegate: false,
type: 'YieldExpression'
},
type: 'ExpressionStatement'
}
],
type: 'BlockStatement'
},
expression: false,
generator: true,
id: {
name: 'gen',
type: 'Identifier'
},
params: [],
type: 'FunctionDeclaration'
}
],
sourceType: 'script',
type: 'Program'
}
],
[
'"use strict"; async function foo() { function bar() { await = 1; } bar(); }',
Context.Strict,
Expand Down
83 changes: 83 additions & 0 deletions test/parser/declarations/async-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,89 @@ describe('Declarations - Async Generator', () => {
]
}
],
[
'var gen = async function *() { yield { ...yield, y: 1, ...yield yield, }; };',
Context.Strict,
{
body: [
{
declarations: [
{
id: {
name: 'gen',
type: 'Identifier'
},
init: {
async: true,
body: {
body: [
{
expression: {
argument: {
properties: [
{
argument: {
argument: null,
delegate: false,
type: 'YieldExpression'
},
type: 'SpreadElement'
},
{
computed: false,
key: {
name: 'y',
type: 'Identifier'
},
kind: 'init',
method: false,
shorthand: false,
type: 'Property',
value: {
type: 'Literal',
value: 1
}
},
{
argument: {
argument: {
argument: null,
delegate: false,
type: 'YieldExpression'
},
delegate: false,
type: 'YieldExpression'
},
type: 'SpreadElement'
}
],
type: 'ObjectExpression'
},
delegate: false,
type: 'YieldExpression'
},
type: 'ExpressionStatement'
}
],
type: 'BlockStatement'
},
expression: false,
generator: true,
id: null,
params: [],
type: 'FunctionExpression'
},
type: 'VariableDeclarator'
}
],
kind: 'var',
type: 'VariableDeclaration'
}
],
sourceType: 'script',
type: 'Program'
}
],
[
'async function* f([arrow = () => {}]) { }',
Context.None,
Expand Down
9 changes: 9 additions & 0 deletions test/parser/expressions/arrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,15 @@ describe('Expressions - Arrow', () => {
['({x: async ()=>x}) => {}', Context.OptionsWebCompat],
['x = a`c`=>{}', Context.None],
['([(a)]) => {};', Context.None],
['(x, /x/g) => x', Context.None],
['(x, /x/g) => x', Context.None],
['(a=/i/) = /i/', Context.None],
['(x => y) = {}', Context.None],
['(x => y) = {}', Context.None],
['(async x => y) = {}', Context.None],
['((x, z) => y) = {}', Context.None],
['(async (x, z) => y) = {}', Context.None],
['async("foo".bar) => x', Context.None],
['function x(){([(a)]) => {} }', Context.None],
['(a)[1]=>{}', Context.None],
['(a)[c]=>{};', Context.None],
Expand Down
1 change: 1 addition & 0 deletions test/parser/expressions/async-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ describe('Expressions - Async Generator', () => {
'(async function* h([fn = function () {}, xFn = function x() {}] = []) { })',
'(async function* h([x] = []) { })',
'(async function* h({} = null) { })',
'var gen = async function *() { yield [...yield]; };',
'(async function* h({a, b, ...rest} = {x: 1, y: 2, a: 5, b: 3}) { })',
'(async function* h({ x, }) { })',
'(async function* h({ w: [x, y, z] = [4, 5, 6] }) { })',
Expand Down
36 changes: 36 additions & 0 deletions test/parser/expressions/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,42 @@ describe('Expressions - Async', () => {
]
}
],
[
'\\u0061sync\np => {}',
Context.None,
{
body: [
{
expression: {
name: 'async',
type: 'Identifier'
},
type: 'ExpressionStatement'
},
{
expression: {
async: false,
body: {
body: [],
type: 'BlockStatement'
},
expression: false,
id: null,
params: [
{
name: 'p',
type: 'Identifier'
}
],
type: 'ArrowFunctionExpression'
},
type: 'ExpressionStatement'
}
],
sourceType: 'script',
type: 'Program'
}
],
[
'class x {\nasync foo() {}}',
Context.None,
Expand Down
3 changes: 2 additions & 1 deletion test/parser/expressions/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,8 @@ describe('Expressions - Object', () => {
['(({a, ...b = 0}) => {})', Context.None],
['({...x,}) => z', Context.None],
['let {...x, ...y} = {}', Context.None],
['({...rest, b} = {})', Context.None]
['({...rest, b} = {})', Context.None],
['({g\\u0065t m() {} });', Context.None]
]);

pass('Expressions - Object (pass)', [
Expand Down
Loading

0 comments on commit c9545fe

Please sign in to comment.