Skip to content

Commit

Permalink
fix(parser): dedupe some code
Browse files Browse the repository at this point in the history
  • Loading branch information
KFlash committed Jul 23, 2019
1 parent 30a5b42 commit c41a671
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 19 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.3.5",
"version": "1.4.1",
"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
6 changes: 6 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,12 @@ export function isEqualTagName(elementName: any): any {
}
}

export function createArrowScope(parser: ParserState, context: Context, value: string) {
const scope = addChildScope(createScope(), ScopeKind.ArrowParams);
addBlockName(parser, context, scope, value, BindingKind.ArgumentList, BindingOrigin.Other);
return scope;
}

export function recordScopeError(parser: ParserState, type: Errors): ScopeError {
const { index, line, column } = parser;
return {
Expand Down
8 changes: 6 additions & 2 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ export const enum Errors {
DuplicateIdentifier,
ShadowedCatchClause,
InvalidDotProperty,
UnclosedSpreadElement
UnclosedSpreadElement,
CatchWithoutTry,
FinallyWithoutTry
}

/*@internal*/
Expand Down Expand Up @@ -342,7 +344,9 @@ export const errorMessages: {
[Errors.DuplicateIdentifier]: "'%0' has already been declared",
[Errors.ShadowedCatchClause]: "'%0' shadowed a catch clause binding",
[Errors.InvalidDotProperty]: 'Dot property must be an identifier',
[Errors.UnclosedSpreadElement]: 'Encountered invalid input after spread/rest argument'
[Errors.UnclosedSpreadElement]: 'Encountered invalid input after spread/rest argument',
[Errors.CatchWithoutTry]: 'Catch without try',
[Errors.FinallyWithoutTry]: 'Finally without try'
};

export class ParseError extends SyntaxError {
Expand Down
2 changes: 2 additions & 0 deletions src/estree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,8 @@ export interface LabeledStatement extends _Node {

export interface Literal extends _Node {
type: 'Literal';
value: boolean | number | string | null;
raw?: string;
}

export interface LogicalExpression extends _Node {
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.0';
export const version = '1.4.1';
47 changes: 33 additions & 14 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import {
addBindingToExports,
updateExportsList,
isEqualTagName,
isValidStrictMode
isValidStrictMode,
createArrowScope
} from './common';

/**
Expand Down Expand Up @@ -196,7 +197,6 @@ export interface Options {
jsx?: boolean;
// Allow edge cases that deviate from the spec
specDeviation?: boolean;

// Allowes comment extraction. Accepts either a function or array
onComment?: OnComment;
}
Expand Down Expand Up @@ -598,6 +598,11 @@ export function parseStatement(
return parseDebuggerStatement(parser, context, start, line, column);
case Token.AsyncKeyword:
return parseAsyncArrowOrAsyncFunctionDeclaration(parser, context, scope, origin, labels, 0, start, line, column);
// Miscellaneous error cases arguably better caught here than elsewhere
case Token.CatchKeyword:
report(parser, Errors.CatchWithoutTry);
case Token.FinallyKeyword:
report(parser, Errors.FinallyWithoutTry);
case Token.FunctionKeyword:
// FunctionDeclaration & ClassDeclaration is forbidden by lookahead
// restriction in an arbitrary statement position.
Expand Down Expand Up @@ -1835,6 +1840,27 @@ export function parseLetIdentOrVarDeclarationStatement(
);
}

/**
* ArrowFunction :
* ArrowParameters => ConciseBody
*
* ConciseBody :
* [lookahead not {] AssignmentExpression
* { FunctionBody }
*
*/
if (parser.token === Token.Arrow) {
let scope: ScopeState | undefined = void 0;

if (context & Context.OptionsLexical) scope = createArrowScope(parser, context, tokenValue);

parser.flags = (parser.flags | Flags.SimpleParameterList) ^ Flags.SimpleParameterList;

expr = parseArrowFunctionExpression(parser, context, scope, [expr], /* isAsync */ 0, start, line, column) as any;

return parseExpressionStatement(parser, context, expr, start, line, column);
}

/**
* UpdateExpression ::
* ('++' | '--')? LeftHandSideExpression
Expand Down Expand Up @@ -2652,10 +2678,7 @@ function parseExportDeclaration(
declaration as any
);
} else if (parser.token & Token.IsIdentifier) {
if (scope) {
scope = addChildScope(createScope(), ScopeKind.ArrowParams);
addBlockName(parser, context, scope, parser.tokenValue, BindingKind.ArgumentList, BindingOrigin.Other);
}
if (scope) scope = createArrowScope(parser, context, parser.tokenValue);

declaration = parseIdentifier(parser, context, 0, parser.tokenPos, parser.linePos, parser.colPos);
declaration = parseArrowFunctionExpression(
Expand Down Expand Up @@ -3790,10 +3813,8 @@ export function parsePrimaryExpressionExtended(

let scope: ScopeState | undefined = void 0;

if (context & Context.OptionsLexical) {
scope = addChildScope(createScope(), ScopeKind.ArrowParams);
addBlockName(parser, context, scope, tokenValue, BindingKind.ArgumentList, BindingOrigin.Other);
}
if (context & Context.OptionsLexical) scope = createArrowScope(parser, context, tokenValue);

return parseArrowFunctionExpression(parser, context, scope, [expr], /* isAsync */ 0, start, line, column);
}

Expand Down Expand Up @@ -6583,10 +6604,8 @@ export function parseIdentifierOrArrow(
parser.assignable = AssignmentKind.Assignable;
if (parser.token === Token.Arrow) {
let scope: ScopeState | undefined = void 0;
if (context & Context.OptionsLexical) {
scope = addChildScope(createScope(), ScopeKind.ArrowParams);
addBlockName(parser, context, scope, tokenValue, BindingKind.ArgumentList, BindingOrigin.Other);
}

if (context & Context.OptionsLexical) scope = createArrowScope(parser, context, tokenValue);

parser.flags = (parser.flags | Flags.SimpleParameterList) ^ Flags.SimpleParameterList;

Expand Down
28 changes: 28 additions & 0 deletions test/parser/expressions/arrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,34 @@ describe('Expressions - Arrow', () => {
]
}
],
[
'let => {}',
Context.OptionsWebCompat,
{
type: 'Program',
sourceType: 'script',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'ArrowFunctionExpression',
body: {
type: 'BlockStatement',
body: []
},
params: [
{
type: 'Identifier',
name: 'let'
}
],
async: false,
expression: false
}
}
]
}
],
[
'f = ([[,] = g()]) => {}',
Context.OptionsWebCompat,
Expand Down
4 changes: 3 additions & 1 deletion test/parser/miscellaneous/escaped-keyword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ describe('Miscellaneous - Escaped keywords', () => {
'(st\\u0061tic);',
'var st\\u0061tic = 1;',
'var { st\\u0061tic } = {};',
'l\\u0065t\na',
'if (true) l\\u0065t: ;',
'function l\\u0065t() { }',
'(function l\\u0065t() { })',
'async function l\\u0065t() { }',
'(async function l\\u0065t() { })',
//'(async ()=>{\\u0061wait 100})()',
//'(async ()=>{var \\u0061wait = 100})()',
//'l\\u0065t => 42',
'l\\u0065t => 42',
// '(\\u0061sync ())',
'async l\\u0065t => 42',
'function packag\\u0065() {}',
Expand All @@ -60,6 +61,7 @@ describe('Miscellaneous - Escaped keywords', () => {
['v\\u0061r', Context.None],
['({\\u0067et get(){}})', Context.None],
['({\\u0073et set(){}})', Context.None],
// ['le\\u0074 x = 5', Context.None],
['var v\\u0061r = 2000000;', Context.None],
['var v\\u{0061}r = 2000000', Context.None],
// ['String.raw`var {v\\u0061r} = obj`', Context.None],
Expand Down

0 comments on commit c41a671

Please sign in to comment.