-
Notifications
You must be signed in to change notification settings - Fork 28
Implement Export{Default,Namespace}Specifier parsing ourselves. #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
3772914
49b4a18
5366b4c
5f7f0a1
4b6d0c3
80b30d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| "use strict"; | ||
|
|
||
| const tt = require("acorn").tokTypes; | ||
|
|
||
| exports.enableAll = function (parser) { | ||
| exports.enableTolerance(parser); | ||
| exports.enableExportExtensions(parser); | ||
| }; | ||
|
|
||
| exports.enableTolerance = function (parser) { | ||
| // It's not Reify's job to enforce strictness. | ||
| parser.strict = false; | ||
|
|
||
| // Tolerate recoverable parse errors. | ||
| parser.raiseRecoverable = noopRaiseRecoverable; | ||
| }; | ||
|
|
||
| function noopRaiseRecoverable() {} | ||
|
|
||
| exports.enableExportExtensions = function (parser) { | ||
| // Our custom lookahead method. | ||
| parser.withLookAhead = withLookAhead; | ||
|
|
||
| // Export-related modifications. | ||
| parser.parseExport = parseExport; | ||
| parser.isExportDefaultSpecifier = isExportDefaultSpecifier; | ||
| parser.parseExportSpecifiersMaybe = parseExportSpecifiersMaybe; | ||
| parser.parseExportFrom = parseExportFrom; | ||
| parser.shouldParseExportDeclaration = shouldParseExportDeclaration; | ||
| }; | ||
|
|
||
| function parseExport(node, exports) { | ||
| this.next(); | ||
| if (this.type === tt.star) { | ||
| const specifier = this.startNode(); | ||
| this.next(); | ||
| if (this.eatContextual("as")) { | ||
| // export * as ns from '...' | ||
| specifier.exported = this.parseIdent(true); | ||
| node.specifiers = [ | ||
| this.finishNode(specifier, "ExportNamespaceSpecifier") | ||
| ]; | ||
| this.parseExportSpecifiersMaybe(node); | ||
| this.parseExportFrom(node, exports); | ||
| } else { | ||
| // export * from '...' | ||
| this.parseExportFrom(node, exports); | ||
| return this.finishNode(node, "ExportAllDeclaration"); | ||
| } | ||
| } else if (this.isExportDefaultSpecifier()) { | ||
| // export def from '...' | ||
| const specifier = this.startNode(); | ||
| specifier.exported = this.parseIdent(true); | ||
| node.specifiers = [ | ||
| this.finishNode(specifier, "ExportDefaultSpecifier") | ||
| ]; | ||
| if (this.type === tt.comma && | ||
| peekNextType(this) === tt.star) { | ||
| // export def, * as ns from '...' | ||
| this.expect(tt.comma); | ||
| const specifier = this.startNode(); | ||
| this.expect(tt.star); | ||
| this.expectContextual("as"); | ||
| specifier.exported = this.parseIdent(true); | ||
| node.specifiers.push( | ||
| this.finishNode(specifier, "ExportNamespaceSpecifier") | ||
| ); | ||
| } else { | ||
| // export def, { x, y as z } from '...' | ||
| this.parseExportSpecifiersMaybe(node); | ||
| } | ||
| this.parseExportFrom(node, exports); | ||
| } else if (this.eat(tt._default)) { | ||
| // export default ... | ||
| exports.default = true; | ||
| let isAsync; | ||
| if (this.type === tt._function || (isAsync = this.isAsyncFunction())) { | ||
| let fNode = this.startNode(); | ||
| this.next(); | ||
| if (isAsync) this.next(); | ||
| node.declaration = this.parseFunction(fNode, "nullableID", false, isAsync); | ||
| } else if (this.type === tt._class) { | ||
| let cNode = this.startNode(); | ||
| node.declaration = this.parseClass(cNode, "nullableID"); | ||
| } else { | ||
| node.declaration = this.parseMaybeAssign(); | ||
| this.semicolon(); | ||
| } | ||
| return this.finishNode(node, "ExportDefaultDeclaration"); | ||
| } else if (this.shouldParseExportDeclaration()) { | ||
| // export var|const|let|function|class ... | ||
| node.declaration = this.parseStatement(true); | ||
| if (node.declaration.type === "VariableDeclaration") { | ||
| this.checkVariableExport(exports, node.declaration.declarations); | ||
| } else { | ||
| exports[node.declaration.id.name] = true; | ||
| } | ||
| node.specifiers = []; | ||
| node.source = null; | ||
| } else { | ||
| // export { x, y as z } [from '...'] | ||
| node.declaration = null; | ||
| node.specifiers = this.parseExportSpecifiers(exports); | ||
| this.parseExportFrom(node, exports); | ||
| } | ||
| return this.finishNode(node, "ExportNamedDeclaration"); | ||
| } | ||
|
|
||
| // Calls the given callback with the state of the parser temporarily | ||
| // advanced by calling this.nextToken() n times, then rolls the parser | ||
| // back to its original state and returns whatever the callback returned. | ||
| function withLookAhead(n, callback) { | ||
| const old = Object.assign(Object.create(null), this); | ||
| while (n-- > 0) this.nextToken(); | ||
| try { | ||
| return callback.call(this); | ||
| } finally { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I see
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's another case ( Any preference between these two options? return callback.call(this);
// or
return callback(this);Either of those could work for
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! In event loops it's always sad to see folks default to applying a |
||
| Object.assign(this, old); | ||
| } | ||
| } | ||
|
|
||
| function peekNextType(parser) { | ||
| return parser.withLookAhead(1, () => parser.type); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really like this new interface, personally. |
||
| } | ||
|
|
||
| function isExportDefaultSpecifier() { | ||
| return this.type === tt.name && | ||
| this.withLookAhead(1, isCommaOrFrom); | ||
| } | ||
|
|
||
| function isCommaOrFrom() { | ||
| // Note: `this` should be the parser object. | ||
| return this.type === tt.comma || | ||
| (this.type === tt.name && | ||
| this.value === "from"); | ||
| } | ||
|
|
||
| function parseExportSpecifiersMaybe(node) { | ||
| if (this.eat(tt.comma)) { | ||
| node.specifiers.push.apply( | ||
| node.specifiers, | ||
| this.parseExportSpecifiers() | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| function parseExportFrom(node, exports) { | ||
| const hasFrom = this.eatContextual("from") && this.type === tt.string; | ||
| node.source = hasFrom ? this.parseExprAtom() : null; | ||
|
|
||
| if (node.specifiers) { | ||
| for (let i = 0; i < node.specifiers.length; i++) { | ||
| const s = node.specifiers[i]; | ||
| const exported = s.exported; | ||
| exports[exported.name] = true; | ||
| } | ||
| } | ||
|
|
||
| this.semicolon(); | ||
| } | ||
|
|
||
| function shouldParseExportDeclaration() { | ||
| return this.type.keyword === "var" || | ||
| this.type.keyword === "const" || | ||
| this.type.keyword === "class" || | ||
| this.type.keyword === "function" || | ||
| this.isLet() || | ||
| this.isAsyncFunction(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| "use strict"; | ||
|
|
||
| const acorn = require("acorn"); | ||
| let acorn = null; | ||
| let acornExtensions = null; | ||
|
|
||
| exports.options = { | ||
| ecmaVersion: 8, | ||
|
|
@@ -11,17 +12,19 @@ exports.options = { | |
| }; | ||
|
|
||
| function acornParse(code) { | ||
| const parser = new acorn.Parser(exports.options, code); | ||
| if (acorn === null) { | ||
| acorn = require("acorn"); | ||
| } | ||
|
|
||
| if (acornExtensions === null) { | ||
| acornExtensions = require("./acorn-extensions.js"); | ||
| } | ||
|
|
||
| // It's not Reify's job to enforce strictness. | ||
| parser.strict = false; | ||
| const parser = new acorn.Parser(exports.options, code); | ||
|
|
||
| // Tolerate recoverable parse errors. | ||
| parser.raiseRecoverable = noopRaiseRecoverable; | ||
| acornExtensions.enableAll(parser); | ||
|
|
||
| return parser.parse(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the lazy loading of
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, probably not much if we always use the parser when we import this module (which I think we do). Are you concerned it might hurt? |
||
| } | ||
|
|
||
| function noopRaiseRecoverable() {} | ||
|
|
||
| exports.parse = acornParse; | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious does the execution of the
this.expectstuff just above matter?I noticed one was before the
const specifier =.....There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes,
this.expect(...)modifiesthis.start(by callingthis.eat(...)which callsthis.next()), which is used bythis.startNodeto set the.startlocation of the new node, so the relative ordering ofthis.expect(...)andthis.startNode()is important.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😵