Skip to content

Commit

Permalink
Refactor exports
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed Oct 28, 2021
1 parent cb0f943 commit 91918b7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
31 changes: 13 additions & 18 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -2169,35 +2169,30 @@ export default class StatementParser extends ExpressionParser {
}
const isMaybeTypeOnly = this.isContextual(tt._type);
const isString = this.match(tt.string);
const local = this.parseModuleExportName();
const node = this.parseExportSpecifier(
local,
isString,
isInTypeExport,
isMaybeTypeOnly,
const node = this.startNode();
node.local = this.parseModuleExportName();
nodes.push(
this.parseExportSpecifier(
node,
isString,
isInTypeExport,
isMaybeTypeOnly,
),
);
nodes.push(node);
}

return nodes;
}

parseExportSpecifier(
local: N.StringLiteral | N.Identifier,
node: any,
isString: boolean,
// eslint-disable-next-line no-unused-vars
isInTypeExport: boolean,
// eslint-disable-next-line no-unused-vars
isMaybeTypeOnly: boolean,
): N.ExportSpecifier {
const node = this.startNodeAtNode(local);
node.local = local;
const canParseAsKeyword = this.parseTypeOnlyImportExportSpecifier(
node,
/* isImport */ false,
isString,
isInTypeExport,
isMaybeTypeOnly,
);
if (canParseAsKeyword && this.eatContextual(tt._as)) {
if (this.eatContextual(tt._as)) {
node.exported = this.parseModuleExportName();
} else if (isString) {
node.exported = cloneStringLiteral(node.local);
Expand Down
34 changes: 34 additions & 0 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -42,6 +42,7 @@ import {
type ErrorTemplate,
ErrorCodes,
} from "../../parser/error";
import { cloneIdentifier } from "../../parser/node";

type TsModifier =
| "readonly"
Expand Down Expand Up @@ -3317,6 +3318,31 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return super.getExpression();
}

parseExportSpecifier(
node: any,
isString: boolean,
isInTypeExport: boolean,
isMaybeTypeOnly: boolean,
) {
if (!isString && isMaybeTypeOnly) {
this.parseTypeOnlyImportExportSpecifier(
node,
/* isImport */ false,
isString,
isInTypeExport,
isMaybeTypeOnly,
);
return this.finishNode<N.ExportSpecifier>(node, "ExportSpecifier");
}
node.exportKind = "value";
return super.parseExportSpecifier(
node,
isString,
isInTypeExport,
isMaybeTypeOnly,
);
}

parseTypeOnlyImportExportSpecifier(
node: any,
isImport: boolean,
Expand Down Expand Up @@ -3388,6 +3414,14 @@ export default (superClass: Class<Parser>): Class<Parser> =>
const kindKey = isImport ? "importKind" : "exportKind";
node[kindKey] = hasTypeSpecifier ? "type" : "value";

if (!isImport) {
if (canParseAsKeyword && this.eatContextual(tt._as)) {
node[rightOfAsKey] = this.parseModuleExportName();
} else if (!node[rightOfAsKey]) {
node[rightOfAsKey] = cloneIdentifier(node[leftOfAsKey]);
}
}

return canParseAsKeyword;
}
};

0 comments on commit 91918b7

Please sign in to comment.