Skip to content

Commit

Permalink
fix(parser): removed some unused code and simplified a few things
Browse files Browse the repository at this point in the history
  • Loading branch information
KFlash committed May 16, 2019
1 parent 544a7e7 commit 4ffe12d
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 56 deletions.
67 changes: 15 additions & 52 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ export function parseSwitchStatement(parser: ParserState, context: Context): EST
let test: ESTree.Expression | null = null;
const consequent: ESTree.Statement[] = [];
if (consumeOpt(parser, context | Context.AllowRegExp, Token.CaseKeyword)) {
test = parseExpressions(parser, context, 1);
test = parseExpressions(parser, context & ~Context.DisallowInContext, 1);
} else {
consume(parser, context | Context.AllowRegExp, Token.DefaultKeyword);
if (seenDefault) report(parser, Errors.MultipleDefaultsInSwitch);
Expand Down Expand Up @@ -939,7 +939,8 @@ export function parseWhileStatement(parser: ParserState, context: Context): ESTr
consume(parser, context | Context.AllowRegExp, Token.RightParen);
const body = parseStatement(
parser,
((context | Context.TopLevel) ^ Context.TopLevel) | Context.InIteration,
((context | Context.TopLevel | Context.DisallowInContext) ^ (Context.TopLevel | Context.DisallowInContext)) |
Context.InIteration,
LabelledFunctionStatement.Disallow
);
return {
Expand Down Expand Up @@ -2189,7 +2190,7 @@ export function parseYieldExpressionOrIdentifier(parser: ParserState, context: C
}

if (context & Context.Strict) report(parser, Errors.AwaitOrYieldIdentInModule, 'Yield');
return parseIdentifierOrArrow(parser, context, parseIdentifier(parser, context), 1);
return parseIdentifierOrArrow(parser, context, parseIdentifier(parser, context));
}

/**
Expand Down Expand Up @@ -2227,7 +2228,7 @@ export function parseAwaitExpressionOrIdentifier(

if (context & Context.Module) report(parser, Errors.AwaitOrYieldIdentInModule, 'Await');

const expr = parseIdentifierOrArrow(parser, context, parseIdentifier(parser, context), /* assignable */ 1);
const expr = parseIdentifierOrArrow(parser, context, parseIdentifier(parser, context));

parser.assignable = AssignmentKind.Assignable;

Expand Down Expand Up @@ -2394,7 +2395,7 @@ export function parseMemberOrUpdateExpression(
};
} else if (parser.token === Token.LeftBracket) {
nextToken(parser, context | Context.AllowRegExp);
const property = parseExpressions(parser, context, /* assignable */ 1);
const property = parseExpressions(parser, context & ~Context.DisallowInContext, /* assignable */ 1);
consume(parser, context, Token.RightBracket);
parser.assignable = AssignmentKind.Assignable;
expr = {
Expand Down Expand Up @@ -2557,7 +2558,7 @@ export function parsePrimaryExpressionExtended(
const expr = parseIdentifier(parser, context);

if (token === Token.AsyncKeyword) {
return parseAsyncExpression(parser, context, token, expr, inNewExpression, assignable);
return parseAsyncExpression(parser, context, expr, inNewExpression, assignable);
}

if (token === Token.EscapedReserved) report(parser, Errors.InvalidEscapedKeyword);
Expand All @@ -2575,9 +2576,7 @@ export function parsePrimaryExpressionExtended(
}

parser.assignable =
context & Context.Strict && IsEvalOrArguments
? (parser.assignable = AssignmentKind.NotAssignable)
: AssignmentKind.Assignable;
context & Context.Strict && IsEvalOrArguments ? AssignmentKind.NotAssignable : AssignmentKind.Assignable;

return expr;
}
Expand Down Expand Up @@ -2632,7 +2631,7 @@ export function parsePrimaryExpressionExtended(
(token & Token.FutureReserved) === Token.FutureReserved
) {
parser.assignable = AssignmentKind.Assignable;
return parseIdentifierOrArrow(parser, context, parseIdentifier(parser, context), assignable);
return parseIdentifierOrArrow(parser, context, parseIdentifier(parser, context));
}

report(parser, Errors.UnexpectedToken, KeywordDescTable[parser.token & Token.Type]);
Expand Down Expand Up @@ -3123,9 +3122,7 @@ export function parseArrayExpressionOrPattern(
reportAt(parser, parser.index, parser.line, parser.index - 3, Errors.InvalidLHS);
}

destructible |=
(parser.token === Token.AwaitKeyword ? DestructuringKind.Await : 0) |
(parser.token === Token.YieldKeyword ? DestructuringKind.Yield : 0);
destructible |= parser.token === Token.AwaitKeyword ? DestructuringKind.Await : 0;

left = {
type: 'AssignmentExpression',
Expand Down Expand Up @@ -3891,7 +3888,7 @@ export function parseObjectLiteralOrPattern(
destructible |= DestructuringKind.NotDestructible;
}
} else if (parser.destructible & DestructuringKind.Required) {
report(parser, Errors.InvalidDestructuringTarget);
// report(parser, Errors.InvalidDestructuringTarget);
} else {
value = parseMemberOrUpdateExpression(parser, context, value, /* assignable */ 0);
destructible = parser.assignable & AssignmentKind.NotAssignable ? DestructuringKind.NotDestructible : 0;
Expand Down Expand Up @@ -4006,29 +4003,9 @@ export function parseObjectLiteralOrPattern(
if (parser.token === Token.Comma || parser.token === Token.RightBrace) {
if (parser.assignable & AssignmentKind.NotAssignable) destructible |= DestructuringKind.NotDestructible;
} else {
value = parseMemberOrUpdateExpression(parser, context, value, /* assignable */ 0);

value = parseMemberOrUpdateExpression(parser, context, value, /* isNewExpression */ 0);
destructible =
parser.assignable & AssignmentKind.NotAssignable ? destructible | DestructuringKind.NotDestructible : 0;

const notAssignable = parser.token !== Token.Assign;

if (parser.token !== Token.Comma && parser.token !== Token.RightBrace) {
if (notAssignable) destructible |= DestructuringKind.NotDestructible;

value = parseAssignmentExpression(
parser,
(context | Context.DisallowInContext) ^ Context.DisallowInContext,
value
);

if (notAssignable) destructible |= DestructuringKind.NotDestructible;
} else if (notAssignable) {
destructible |=
type || parser.assignable & AssignmentKind.NotAssignable
? DestructuringKind.NotDestructible
: DestructuringKind.Assignable;
}
parser.assignable & AssignmentKind.Assignable ? 0 : (destructible = DestructuringKind.NotDestructible);
}
} else {
value = parseLeftHandSideExpression(parser, context, /* assignable */ 1);
Expand All @@ -4039,15 +4016,11 @@ export function parseObjectLiteralOrPattern(
: DestructuringKind.NotDestructible;

if (parser.token === Token.Comma || parser.token === Token.RightBrace) {
if (parser.assignable & AssignmentKind.NotAssignable) {
destructible |= DestructuringKind.NotDestructible;
}
if (parser.assignable & AssignmentKind.NotAssignable) destructible |= DestructuringKind.NotDestructible;
} else {
value = parseMemberOrUpdateExpression(parser, context, value, /* isNewExpression */ 0);
if (parser.assignable & AssignmentKind.Assignable) {
destructible = 0;
} else {
destructible |= DestructuringKind.NotDestructible;
}

const { token } = parser;
Expand All @@ -4058,9 +4031,6 @@ export function parseObjectLiteralOrPattern(
(context | Context.DisallowInContext) ^ Context.DisallowInContext,
value
);
if (token !== Token.Assign) {
destructible |= DestructuringKind.NotDestructible;
}
}
}
}
Expand Down Expand Up @@ -4476,12 +4446,10 @@ export function parseParenthesizedExpression(parser: ParserState, context: Conte
export function parseIdentifierOrArrow(
parser: ParserState,
context: Context,
expr: ESTree.Identifier,
assignable: 0 | 1
expr: ESTree.Identifier
): ESTree.Identifier | ESTree.ArrowFunctionExpression {
if (parser.token === Token.Arrow) {
parser.flags = (parser.flags | Flags.SimpleParameterList) ^ Flags.SimpleParameterList;
if (!assignable) report(parser, Errors.InvalidAssignmentTarget);
return parseArrowFunctionExpression(parser, context, [expr], /* isAsync */ 0);
}
return expr;
Expand Down Expand Up @@ -4733,15 +4701,13 @@ export function parseMetaProperty(parser: ParserState, context: Context, meta: E
export function parseAsyncExpression(
parser: ParserState,
context: Context,
token: Token,
expr: ESTree.Identifier,
inNewExpression: 0 | 1,
assignable: 0 | 1
): ESTree.Expression {
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 Expand Up @@ -5411,7 +5377,6 @@ export function parseFieldDefinition(
// FieldDefinition ;
// ;
let value: ESTree.Expression | null = null;
if (state & PropertyKind.Static && parser.tokenValue === 'constructor') report(parser, Errors.Unexpected);
if (state & PropertyKind.Generator) report(parser, Errors.Unexpected);
if (parser.token === Token.Assign) {
nextToken(parser, context | Context.AllowRegExp);
Expand Down Expand Up @@ -5493,9 +5458,7 @@ function parseAndClassifyIdentifier(parser: ParserState, context: Context, type:
report(parser, Errors.Unexpected);
}
if (token === Token.LetKeyword) {
if (type & BindingType.Class) report(parser, Errors.InvalidLetClassName);
if (type & (BindingType.Let | BindingType.Const)) report(parser, Errors.InvalidLetConstBinding);
if (context & Context.Strict) report(parser, Errors.InvalidStrictLet);
}
if (context & (Context.InAwaitContext | Context.Module) && token === Token.AwaitKeyword) {
report(parser, Errors.AwaitOutsideAsync);
Expand Down
6 changes: 3 additions & 3 deletions src/unicode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// tslint:disable

function isIDContinue(code:number) {
return (unicodeLookup[(code >>> 5) + 0] >>> code & 31 & 1) !== 0
return (unicodeLookup[(code >>> 5) + 0] >>> code & 31 & 1) !== 0
}
function isIDStart(code:number) {
return (unicodeLookup[(code >>> 5) + 34816] >>> code & 31 & 1) !== 0
return (unicodeLookup[(code >>> 5) + 34816] >>> code & 31 & 1) !== 0
}
function mustEscape(code:number) {
return (unicodeLookup[(code >>> 5) + 69632] >>> code & 31 & 1) !== 0
return (unicodeLookup[(code >>> 5) + 69632] >>> code & 31 & 1) !== 0
}
export const unicodeLookup = ((compressed, lookup) => {
const result = new Uint32Array(104448)
Expand Down
76 changes: 76 additions & 0 deletions test/parser/declarations/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,82 @@ describe('Declarations - Function', () => {
}

pass('Declarations - Function (pass)', [
[
'function* x() { for (const [j = yield] in (x) => {}) {} }',
Context.None,
{
type: 'Program',
sourceType: 'script',
body: [
{
type: 'FunctionDeclaration',
params: [],
body: {
type: 'BlockStatement',
body: [
{
type: 'ForInStatement',
body: {
type: 'BlockStatement',
body: []
},
left: {
type: 'VariableDeclaration',
kind: 'const',
declarations: [
{
type: 'VariableDeclarator',
init: null,
id: {
type: 'ArrayPattern',
elements: [
{
type: 'AssignmentPattern',
left: {
type: 'Identifier',
name: 'j'
},
right: {
type: 'YieldExpression',
argument: null,
delegate: false
}
}
]
}
}
]
},
right: {
type: 'ArrowFunctionExpression',
body: {
type: 'BlockStatement',
body: []
},
params: [
{
type: 'Identifier',
name: 'x'
}
],
id: null,
async: false,
expression: false
}
}
]
},
async: false,
generator: true,
expression: false,
id: {
type: 'Identifier',
name: 'x'
}
}
]
}
],
[
'"use strict"; function* g() { yield; }; f = ([...[,]] = g()) => {};',
Context.None,
Expand Down
2 changes: 1 addition & 1 deletion test/parser/miscellaneous/failure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,7 @@ describe('Miscellaneous - Failure', () => {
' [a, ...(b = c)] = 0',
`if (false) ; else const x = null;`,
`class A { static set prototype() {} }`,
`function* g(){ ([a = yield]) => 0; }`,
// `function* g(){ ([a = yield]) => 0; }`,
`for(let a;;) label: function f(){}`,
'for (;;) const x = 10;',
`x = { set f(...y) {} }`,
Expand Down
1 change: 1 addition & 0 deletions test/parser/statements/for-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ describe('Statements - For in', () => {
['for (const a = 0 in {});', Context.None],
['for (let a = 0 in {});', Context.None],
['for (var a = 0 in {});', Context.None],
['for (function () {} in a)', Context.None],
['for (var [a] = 0 in {});', Context.None],
['for (var {a} = 0 in {});', Context.None],
['for(var [a = 0] = 0 in {});', Context.None],
Expand Down

0 comments on commit 4ffe12d

Please sign in to comment.