-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3772914
Implement Export{Default,Namespace}Specifier parsing ourselves.
benjamn 49b4a18
Better naming for functions exported by acorn-extensions.js.
benjamn 5366b4c
Eliminate useless Parser#checkExport override.
benjamn 5f7f0a1
Combine parseExportFromWithCheck with parseExportFrom and simplify.
benjamn 4b6d0c3
Avoid making extra copy of parser state while looking ahead.
benjamn 80b30d0
Pass parser as argument rather than `this` to withLookAhead callback.
benjamn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| "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(this); | ||
| } finally { | ||
| 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(parser) { | ||
| return parser.type === tt.comma || | ||
| (parser.type === tt.name && | ||
| parser.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(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
😵