Skip to content

Commit

Permalink
fix(parser): follow latest estree spec on ExportAllDeclaration
Browse files Browse the repository at this point in the history
closes #97
  • Loading branch information
3cp committed Oct 6, 2020
1 parent 9318a01 commit 7a7fc76
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 105 deletions.
9 changes: 2 additions & 7 deletions src/estree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export type Node =
| EmptyStatement
| ExportAllDeclaration
| ExportDefaultDeclaration
| ExportNamespaceSpecifier
| ExportNamedDeclaration
| ExportSpecifier
| ExpressionStatement
Expand Down Expand Up @@ -140,7 +139,6 @@ export type DeclarationStatement =
| ClassDeclaration
| ClassExpression
| ExportDefaultDeclaration
| ExportNamespaceSpecifier
| ExportAllDeclaration
| ExportNamedDeclaration
| FunctionDeclaration;
Expand Down Expand Up @@ -415,12 +413,9 @@ export interface EmptyStatement extends _Node {
export interface ExportAllDeclaration extends _Node {
type: 'ExportAllDeclaration';
source: Literal;
exported: Identifier | null;
}

export interface ExportNamespaceSpecifier extends _Node {
type: 'ExportNamespaceSpecifier';
specifier: Identifier;
}
export interface ExportDefaultDeclaration extends _Node {
type: 'ExportDefaultDeclaration';
declaration: ExportDeclaration | Expression;
Expand All @@ -429,7 +424,7 @@ export interface ExportDefaultDeclaration extends _Node {
export interface ExportNamedDeclaration extends _Node {
type: 'ExportNamedDeclaration';
declaration: ExportDeclaration | null;
specifiers: (ExportNamespaceSpecifier | ExportSpecifier)[];
specifiers: ExportSpecifier[];
source: Literal | null;
}

Expand Down
130 changes: 62 additions & 68 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,16 +337,18 @@ export function parseStatementList(
}

while (parser.token !== Token.EOF) {
statements.push(parseStatementListItem(
parser,
context,
scope,
Origin.TopLevel,
{},
parser.tokenPos,
parser.linePos,
parser.colPos
) as ESTree.Statement);
statements.push(
parseStatementListItem(
parser,
context,
scope,
Origin.TopLevel,
{},
parser.tokenPos,
parser.linePos,
parser.colPos
) as ESTree.Statement
);
}
return statements;
}
Expand Down Expand Up @@ -387,14 +389,9 @@ export function parseModuleItemList(
}

while (parser.token !== Token.EOF) {
statements.push(parseModuleItem(
parser,
context,
scope,
parser.tokenPos,
parser.linePos,
parser.colPos
) as ESTree.Statement);
statements.push(
parseModuleItem(parser, context, scope, parser.tokenPos, parser.linePos, parser.colPos) as ESTree.Statement
);
}
return statements;
}
Expand Down Expand Up @@ -805,16 +802,18 @@ export function parseBlock(
const body: ESTree.Statement[] = [];
consume(parser, context | Context.AllowRegExp, Token.LeftBrace);
while (parser.token !== Token.RightBrace) {
body.push(parseStatementListItem(
parser,
context,
scope,
Origin.BlockStatement,
{ $: labels },
parser.tokenPos,
parser.linePos,
parser.colPos
) as any);
body.push(
parseStatementListItem(
parser,
context,
scope,
Origin.BlockStatement,
{ $: labels },
parser.tokenPos,
parser.linePos,
parser.colPos
) as any
);
}

consume(parser, context | Context.AllowRegExp, Token.RightBrace);
Expand Down Expand Up @@ -1406,18 +1405,20 @@ export function parseSwitchStatement(
parser.token !== Token.RightBrace &&
parser.token !== Token.DefaultKeyword
) {
consequent.push(parseStatementListItem(
parser,
context | Context.InSwitch,
scope,
Origin.BlockStatement,
{
$: labels
},
parser.tokenPos,
parser.linePos,
parser.colPos
) as ESTree.Statement);
consequent.push(
parseStatementListItem(
parser,
context | Context.InSwitch,
scope,
Origin.BlockStatement,
{
$: labels
},
parser.tokenPos,
parser.linePos,
parser.colPos
) as ESTree.Statement
);
}

cases.push(
Expand Down Expand Up @@ -2742,7 +2743,7 @@ function parseExportDeclaration(
// https://tc39.github.io/ecma262/#sec-exports
nextToken(parser, context | Context.AllowRegExp);

let specifiers: (ESTree.ExportSpecifier | ESTree.ExportNamespaceSpecifier)[] = [];
const specifiers: ESTree.ExportSpecifier[] = [];

let declaration: ESTree.ExportDeclaration | ESTree.Expression | null = null;
let source: ESTree.Literal | null = null;
Expand Down Expand Up @@ -2884,16 +2885,12 @@ function parseExportDeclaration(

nextToken(parser, context); // Skips: '*'

let exported: ESTree.Identifier | null = null;
const isNamedDeclaration = consumeOpt(parser, context, Token.AsKeyword);

if (isNamedDeclaration) {
if (scope) declareUnboundVariable(parser, parser.tokenValue);
specifiers = [
finishNode(parser, context, parser.tokenPos, parser.linePos, parser.colPos, {
type: 'ExportNamespaceSpecifier',
specifier: parseIdentifier(parser, context, 0)
})
];
exported = parseIdentifier(parser, context, 0);
}

consume(parser, context, Token.FromKeyword);
Expand All @@ -2904,16 +2901,11 @@ function parseExportDeclaration(

matchOrInsertSemicolon(parser, context | Context.AllowRegExp);

return isNamedDeclaration
? finishNode(parser, context, start, line, column, {
type: 'ExportNamedDeclaration',
source,
specifiers
} as any)
: finishNode(parser, context, start, line, column, {
type: 'ExportAllDeclaration',
source
} as any);
return finishNode(parser, context, start, line, column, {
type: 'ExportAllDeclaration',
source,
exported
} as any);
}
case Token.LeftBrace: {
// ExportClause :
Expand Down Expand Up @@ -3733,16 +3725,18 @@ export function parseFunctionBody(
parser.destructible = (parser.destructible | DestructuringKind.Yield) ^ DestructuringKind.Yield;

while (parser.token !== Token.RightBrace) {
body.push(parseStatementListItem(
parser,
context,
scope,
Origin.TopLevel,
{},
parser.tokenPos,
parser.linePos,
parser.colPos
) as ESTree.Statement);
body.push(
parseStatementListItem(
parser,
context,
scope,
Origin.TopLevel,
{},
parser.tokenPos,
parser.linePos,
parser.colPos
) as ESTree.Statement
);
}

consume(
Expand Down Expand Up @@ -4004,7 +3998,7 @@ export function parseMemberOrUpdateExpression(

expr = finishNode(parser, context, start, line, column, {
type: 'ChainExpression',
expression: expr as (ESTree.CallExpression | ESTree.MemberExpression)
expression: expr as ESTree.CallExpression | ESTree.MemberExpression
});
}

Expand Down
46 changes: 16 additions & 30 deletions test/parser/module/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -908,16 +908,11 @@ describe('Module - Export', () => {
type: 'Literal',
value: 'source'
},
specifiers: [
{
specifier: {
name: 'class',
type: 'Identifier'
},
type: 'ExportNamespaceSpecifier'
}
],
type: 'ExportNamedDeclaration'
exported: {
name: 'class',
type: 'Identifier'
},
type: 'ExportAllDeclaration'
}
],
sourceType: 'module',
Expand All @@ -934,16 +929,11 @@ describe('Module - Export', () => {
type: 'Literal',
value: 'source'
},
specifiers: [
{
specifier: {
name: 'ns',
type: 'Identifier'
},
type: 'ExportNamespaceSpecifier'
}
],
type: 'ExportNamedDeclaration'
exported: {
name: 'ns',
type: 'Identifier'
},
type: 'ExportAllDeclaration'
}
],
sourceType: 'module',
Expand All @@ -963,6 +953,7 @@ describe('Module - Export', () => {
type: 'Literal',
value: 'a'
},
exported: null,
start: 0,
end: 17,
range: [0, 17],
Expand All @@ -986,16 +977,11 @@ describe('Module - Export', () => {
type: 'Literal',
value: './foo'
},
specifiers: [
{
specifier: {
name: 'foo',
type: 'Identifier'
},
type: 'ExportNamespaceSpecifier'
}
],
type: 'ExportNamedDeclaration'
exported: {
name: 'foo',
type: 'Identifier'
},
type: 'ExportAllDeclaration'
}
],
sourceType: 'module',
Expand Down

0 comments on commit 7a7fc76

Please sign in to comment.