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 Aug 1, 2019
1 parent a33701b commit ca79f80
Show file tree
Hide file tree
Showing 8 changed files with 463 additions and 21 deletions.
23 changes: 4 additions & 19 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4098,7 +4098,7 @@ export function parseImportExpression(
line: number,
column: number
): ESTree.ImportExpression {
consume(parser, context, Token.LeftParen);
consume(parser, context | Context.AllowRegExp, Token.LeftParen);

if (parser.token === Token.Ellipsis) report(parser, Errors.InvalidSpreadInImport);

Expand Down Expand Up @@ -5110,7 +5110,6 @@ function parseSpreadElement(
addVarName(parser, context, scope, tokenValue, kind);
if (origin & Origin.Export) {
updateExportsList(parser, tokenValue);
addBindingToExports(parser, tokenValue);
}
}
} else {
Expand Down Expand Up @@ -5937,9 +5936,7 @@ export function parseObjectLiteralOrPattern(
if (parser.assignable & AssignmentKind.CannotAssign) {
destructible |= DestructuringKind.CannotDestruct;
}
} else if (parser.destructible & DestructuringKind.HasToDestruct) {
report(parser, Errors.InvalidDestructuringTarget);
} else {
} else if ((parser.destructible & DestructuringKind.HasToDestruct) !== DestructuringKind.HasToDestruct) {
value = parseMemberOrUpdateExpression(
parser,
context,
Expand Down Expand Up @@ -6344,11 +6341,7 @@ export function parseMethodFormals(
}

if (scope && (parser.token & Token.IsIdentifier) === Token.IsIdentifier) {
if (type & BindingKind.Variable) {
addVarName(parser, context, scope, tokenValue, BindingKind.ArgumentList);
} else {
addBlockName(parser, context, scope, tokenValue, BindingKind.ArgumentList, Origin.None);
}
addVarOrBlock(parser, context, scope, parser.tokenValue, BindingKind.ArgumentList, Origin.None);
}
left = parseAndClassifyIdentifier(parser, context, type, tokenPos, linePos, colPos);
} else {
Expand Down Expand Up @@ -6895,11 +6888,7 @@ export function parseFormalParametersOrFormalList(
// in strict mode or not (since the function body hasn't been parsed).
// In such cases the potential error will be saved on the parser object
// and thrown later if there was any duplicates.
if (kind & BindingKind.Variable) {
addVarName(parser, context, scope, tokenValue, BindingKind.ArgumentList);
} else {
addBlockName(parser, context, scope, tokenValue, BindingKind.ArgumentList, Origin.None);
}
addVarOrBlock(parser, context, scope, parser.tokenValue, BindingKind.ArgumentList, Origin.None);
}
left = parseAndClassifyIdentifier(parser, context, kind, tokenPos, linePos, colPos);
} else {
Expand Down Expand Up @@ -8357,10 +8346,6 @@ export function parseJSXClosingFragment(
): ESTree.JSXClosingFragment {
consume(parser, context, Token.JSXClose);

if ((parser.token & Token.IsIdentifier) === Token.IsIdentifier) {
report(parser, Errors.UnCorrespondingFragmentTag);
}

if (inJSXChild) {
consume(parser, context, Token.GreaterThan);
} else {
Expand Down
4 changes: 4 additions & 0 deletions test/lexer/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,8 @@ describe('src/lexer/scan', () => {
fail('fails on .3e-1n', '.3e-1n', Context.None);
fail('fails on .3e-1n', '.3e-n', Context.None);
fail('fails on .0000000001n', '.0000000001n', Context.None);

fail('fails on 0xabcinstanceof x', '0xabcinstanceof x', Context.None);
fail('fails on .0000000001n', '.0000000001n', Context.None);
fail('fails on .0000000001n', '.0000000001n', Context.None);
});
4 changes: 4 additions & 0 deletions test/parser/declarations/async-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ describe('Declarations - Async Function', () => {
}

fail('Declarations - Async Function (fail)', [
['async function f() { delete await; }', Context.None],
['delete await;', Context.Strict | Context.Module],
['async function foo (foo = super()) { let bar; }', Context.None],
['async function foo (foo = super()) { let bar; }', Context.None],
['async function foo (foo = super()) { let bar; }', Context.None],
['\\u0061sync function f(){}', Context.None],
['abc: async function a() {}', Context.None],
Expand Down
53 changes: 53 additions & 0 deletions test/parser/declarations/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,59 @@ describe('Declarations - Function', () => {
}
}
],
[
'function f(){ foo: bar: function f(){} }',
Context.OptionsWebCompat,
{
type: 'Program',
sourceType: 'script',
body: [
{
type: 'FunctionDeclaration',
params: [],
body: {
type: 'BlockStatement',
body: [
{
type: 'LabeledStatement',
label: {
type: 'Identifier',
name: 'foo'
},
body: {
type: 'LabeledStatement',
label: {
type: 'Identifier',
name: 'bar'
},
body: {
type: 'FunctionDeclaration',
params: [],
body: {
type: 'BlockStatement',
body: []
},
async: false,
generator: false,
id: {
type: 'Identifier',
name: 'f'
}
}
}
}
]
},
async: false,
generator: false,
id: {
type: 'Identifier',
name: 'f'
}
}
]
}
],
[
'function f(){ let f; }',
Context.OptionsLoc,
Expand Down
9 changes: 8 additions & 1 deletion test/parser/expressions/import_call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ describe('Next - ImportCall', () => {
['import("") <<= 2', Context.None],
['import("") >>= 2', Context.None],
['import("") >>>= 2', Context.None],
['import("") **= 2', Context.None]
['import("") **= 2', Context.None],
['new import(x);', Context.None]
]);

for (const arg of [
Expand All @@ -116,6 +117,7 @@ describe('Next - ImportCall', () => {
'import(delete obj.prop);',
'import(void 0);',
'import(typeof {});',
'import(a + b);',
'import(+void 0);',
'import(-void 0);',
'import(!void 0);',
Expand All @@ -133,8 +135,13 @@ describe('Next - ImportCall', () => {
`import('./module.js')`,
'import(import(x))',
'x = import(x)',
'foo(import("foo").den());',
'import(/foo/)',
'var x = import(x)',
'let x = import(x)',
'new (import(x));',
'new (import(x));',
'foo(import("foo").den());',
'for(x of import(x)) {}',
'import(x).then()'
]) {
Expand Down
8 changes: 8 additions & 0 deletions test/parser/expressions/unary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ describe('Expressions - Unary', () => {
'delete ((x) => x).foo',
'delete new Number(8)',
'delete a[2]',
'delete await;',
'delete false;',
'delete null;',
'delete this;',
'delete true;',
'delete yield;',
'delete o[Math.pow(2,30)]'
]) {
it(`${arg}`, () => {
Expand Down Expand Up @@ -46,6 +52,8 @@ describe('Expressions - Unary', () => {
['(((x)))\n++;', Context.None],
['(x)\n++;', Context.None],
['if (a) a\n++;', Context.None],
['function *f() { delete yield; }', Context.None],
['class X extends Y { constructor() { delete super; } }', Context.None],
['function f(){ return a\n++; }', Context.None],
['if (a\n++b);', Context.None],
['if (a\n++\nb);', Context.None],
Expand Down
38 changes: 37 additions & 1 deletion test/parser/miscellaneous/pass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14920,12 +14920,48 @@ let kTrapMsgs = [
const module = new x.y(buf);
test(module);
}`,
'.24e9',
'0B00',
'0x0',
'20',
'.24E40',
'0O034 in x',
'.24+e0 in x',
'{0}',
'{01234567}',
'{0B0}',
'{0O034}',
'{0X456}',
'{0Xf}',
'{0o465}',
'{0xe}',
'{23.e2}',
'{5}',
'{.24e1}',
'{.24e83}',
'23.E+1',
'3.42+1',
'0B0;',
'0X456;',
'8;',
'3.42E9',
`Async = 0x10000`,
`a = 1 << 12`,
`HASH_NAME(assert, 0x08D130F2, 0x08D130F2)
HASH_NAME(async, 0x0084CDEE, 0x0084CDEE)
HASH_NAME(await, 0x0084FF56, 0x0084FF56)
HASH_NAME(boolean, 0x96F94400, 0x96F94400)`
HASH_NAME(boolean, 0x96F94400, 0x96F94400)`,
`var gen = async function *g() {
callCount += 1;
yield {
...yield yield,
...(function(arg) {
var yield = arg;
return {...yield};
}(yield)),
...yield,
}
};`
]) {
it(`${arg}`, () => {
t.doesNotThrow(() => {
Expand Down
Loading

0 comments on commit ca79f80

Please sign in to comment.