Repository navigation
[babel 8] Move ESLint parsing to a Worker #13199
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
nicolo-ribaudo
merged 6 commits into
babel:main
from
nicolo-ribaudo:babel-8-eslint-parser-worker
May 17, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bd22b12
[babel 8] Move ESLint parsing to a Worker
nicolo-ribaudo 38103e8
Parse in the main thread when using the default parser
nicolo-ribaudo 4b7284f
Support `babel.config.mjs` in `@babel/eslint-parser`
nicolo-ribaudo 525a8f0
Review
nicolo-ribaudo fd7dfda
Also lint with `@babel/eslint-parser` 8 in tests
nicolo-ribaudo 3febba7
Add back trailing commas
nicolo-ribaudo 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| const path = require("path"); | ||
|
|
||
| let send; | ||
|
|
||
| exports.getVersion = sendCached("GET_VERSION"); | ||
|
|
||
| exports.getTypesInfo = sendCached("GET_TYPES_INFO"); | ||
|
|
||
| exports.getVisitorKeys = sendCached("GET_VISITOR_KEYS"); | ||
|
|
||
| exports.getTokLabels = sendCached("GET_TOKEN_LABELS"); | ||
|
|
||
| exports.maybeParse = (code, options) => send("MAYBE_PARSE", { code, options }); | ||
|
|
||
| function sendCached(action) { | ||
| let cache = null; | ||
|
|
||
| return () => { | ||
| if (!cache) cache = send(action, undefined); | ||
| return cache; | ||
| }; | ||
| } | ||
|
|
||
| if (process.env.BABEL_8_BREAKING) { | ||
| const { | ||
|
nicolo-ribaudo marked this conversation as resolved.
Resolved
|
||
| Worker, | ||
| receiveMessageOnPort, | ||
| MessageChannel, | ||
| SHARE_ENV, | ||
| } = require("worker_threads"); | ||
|
|
||
| // We need to run Babel in a worker for two reasons: | ||
| // 1. ESLint workers must be CJS files, and this is a problem | ||
| // since Babel 8+ uses native ESM | ||
| // 2. ESLint parsers must run synchronously, but many steps | ||
| // of Babel's config loading (which is done for each file) | ||
| // can be asynchronous | ||
| // If ESLint starts supporting async parsers, we can move | ||
| // everything back to the main thread. | ||
| const worker = new Worker( | ||
| path.resolve(__dirname, "../lib/worker/index.cjs"), | ||
| { env: SHARE_ENV }, | ||
| ); | ||
|
|
||
| // The worker will never exit by itself. Prevent it from keeping | ||
| // the main process alive. | ||
| worker.unref(); | ||
|
|
||
| const signal = new Int32Array(new SharedArrayBuffer(4)); | ||
|
|
||
| send = (action, payload) => { | ||
| signal[0] = 0; | ||
| const subChannel = new MessageChannel(); | ||
|
|
||
| worker.postMessage({ signal, port: subChannel.port1, action, payload }, [ | ||
| subChannel.port1, | ||
| ]); | ||
|
|
||
| Atomics.wait(signal, 0, 0); | ||
| const { message } = receiveMessageOnPort(subChannel.port2); | ||
|
|
||
| if (message.error) throw Object.assign(message.error, message.errorData); | ||
| else return message.result; | ||
| }; | ||
| } else { | ||
| send = require("./worker/index.cjs"); | ||
| } | ||
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,20 @@ | ||
| exports.normalizeESLintConfig = function (options) { | ||
| const { | ||
| babelOptions = {}, | ||
| // ESLint sets ecmaVersion: undefined when ecmaVersion is not set in the config. | ||
| ecmaVersion = 2020, | ||
| sourceType = "module", | ||
| allowImportExportEverywhere = false, | ||
| requireConfigFile = true, | ||
| ...otherOptions | ||
| } = options; | ||
|
|
||
| return { | ||
| babelOptions, | ||
| ecmaVersion, | ||
| sourceType, | ||
| allowImportExportEverywhere, | ||
| requireConfigFile, | ||
| ...otherOptions, | ||
| }; | ||
| }; |
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,138 @@ | ||
| function* it(children) { | ||
| if (Array.isArray(children)) yield* children; | ||
| else yield children; | ||
| } | ||
|
|
||
| function traverse(node, visitorKeys, visitor) { | ||
| const { type } = node; | ||
| if (!type) return; | ||
| const keys = visitorKeys[type]; | ||
| if (!keys) return; | ||
|
|
||
| for (const key of keys) { | ||
| for (const child of it(node[key])) { | ||
| if (child && typeof child === "object") { | ||
| visitor.enter(child); | ||
| traverse(child, visitorKeys, visitor); | ||
| visitor.exit(child); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const convertNodesVisitor = { | ||
| enter(node) { | ||
| if (node.innerComments) { | ||
| delete node.innerComments; | ||
| } | ||
|
|
||
| if (node.trailingComments) { | ||
| delete node.trailingComments; | ||
| } | ||
|
|
||
| if (node.leadingComments) { | ||
| delete node.leadingComments; | ||
| } | ||
| }, | ||
| exit(node) { | ||
| // Used internally by @babel/parser. | ||
| if (node.extra) { | ||
| delete node.extra; | ||
| } | ||
|
|
||
| if (node?.loc.identifierName) { | ||
| delete node.loc.identifierName; | ||
| } | ||
|
|
||
| if (node.type === "TypeParameter") { | ||
| node.type = "Identifier"; | ||
| node.typeAnnotation = node.bound; | ||
| delete node.bound; | ||
| } | ||
|
|
||
| // flow: prevent "no-undef" | ||
| // for "Component" in: "let x: React.Component" | ||
| if (node.type === "QualifiedTypeIdentifier") { | ||
| delete node.id; | ||
| } | ||
| // for "b" in: "var a: { b: Foo }" | ||
| if (node.type === "ObjectTypeProperty") { | ||
| delete node.key; | ||
| } | ||
| // for "indexer" in: "var a: {[indexer: string]: number}" | ||
| if (node.type === "ObjectTypeIndexer") { | ||
| delete node.id; | ||
| } | ||
| // for "param" in: "var a: { func(param: Foo): Bar };" | ||
| if (node.type === "FunctionTypeParam") { | ||
| delete node.name; | ||
| } | ||
|
|
||
| // modules | ||
| if (node.type === "ImportDeclaration") { | ||
| delete node.isType; | ||
| } | ||
|
|
||
| // template string range fixes | ||
| if (node.type === "TemplateLiteral") { | ||
| for (let i = 0; i < node.quasis.length; i++) { | ||
| const q = node.quasis[i]; | ||
| q.range[0] -= 1; | ||
| if (q.tail) { | ||
| q.range[1] += 1; | ||
| } else { | ||
| q.range[1] += 2; | ||
| } | ||
| q.loc.start.column -= 1; | ||
| if (q.tail) { | ||
| q.loc.end.column += 1; | ||
| } else { | ||
| q.loc.end.column += 2; | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| function convertNodes(ast, visitorKeys) { | ||
| traverse(ast, visitorKeys, convertNodesVisitor); | ||
| } | ||
|
|
||
| function convertProgramNode(ast) { | ||
| ast.type = "Program"; | ||
| ast.sourceType = ast.program.sourceType; | ||
| ast.body = ast.program.body; | ||
| delete ast.program; | ||
| delete ast.errors; | ||
|
|
||
| if (ast.comments.length) { | ||
| const lastComment = ast.comments[ast.comments.length - 1]; | ||
|
|
||
| if (ast.tokens.length) { | ||
| const lastToken = ast.tokens[ast.tokens.length - 1]; | ||
|
|
||
| if (lastComment.end > lastToken.end) { | ||
| // If there is a comment after the last token, the program ends at the | ||
| // last token and not the comment | ||
| ast.range[1] = lastToken.end; | ||
| ast.loc.end.line = lastToken.loc.end.line; | ||
| ast.loc.end.column = lastToken.loc.end.column; | ||
| } | ||
| } | ||
| } else { | ||
| if (!ast.tokens.length) { | ||
| ast.loc.start.line = 1; | ||
| ast.loc.end.line = 1; | ||
| } | ||
| } | ||
|
|
||
| if (ast.body && ast.body.length > 0) { | ||
| ast.loc.start.line = ast.body[0].loc.start.line; | ||
| ast.range[0] = ast.body[0].start; | ||
| } | ||
| } | ||
|
|
||
| module.exports = function convertAST(ast, visitorKeys) { | ||
| convertNodes(ast, visitorKeys); | ||
| convertProgramNode(ast); | ||
| }; |
Oops, something went wrong.
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.
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.
Could we extract these action types to a constant that we reuse everywhere so we don't risk bugs due to typos?
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.
I moved them to this file (rather than always using the string) so that we only have to care about those strings in the two "communication files" (worker client and server). Everywhere else we use
getVersion()etc.Also, https://github.com/babel/babel/pull/13199/files#diff-62c6b0f19ec3e004fffbecf4acab02bc005e538ea3c08e9bd558def1f28634c7R32 throws so that we catch any invalid string early.