mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[eslint-plugin] Use babel plugin instead of manual traversal
This was causing some issues in the eslint plugin where the babel `hub` wasn't defined. afaik the hub is only setup when running the plugin as part of a babel pipeline, instead of a manual parse/traversal. We're using some of that infra for printing codeframes
This commit is contained in:
+38
-35
@@ -5,13 +5,11 @@
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import { transformFromAstSync } from "@babel/core";
|
||||
import * as parser from "@babel/parser";
|
||||
import traverse from "@babel/traverse";
|
||||
import {
|
||||
import ReactForgetBabelPlugin, {
|
||||
CompilerError,
|
||||
PluginOptions,
|
||||
compileProgram,
|
||||
parsePluginOptions,
|
||||
type PluginOptions,
|
||||
} from "babel-plugin-react-forget";
|
||||
import type { Rule } from "eslint";
|
||||
|
||||
@@ -26,43 +24,48 @@ const rule: Rule.RuleModule = {
|
||||
create(context: Rule.RuleContext) {
|
||||
// Compat with older versions of eslint
|
||||
const sourceCode = context.sourceCode?.text ?? context.getSourceCode().text;
|
||||
const filename = context.filename ?? context.getFilename();
|
||||
|
||||
const opts: Partial<PluginOptions> = {
|
||||
panicOnBailout: false,
|
||||
environment: {
|
||||
validateHooksUsage: true,
|
||||
validateFrozenLambdas: true,
|
||||
validateRefAccessDuringRender: true,
|
||||
},
|
||||
};
|
||||
const babelAST = parser.parse(sourceCode, {
|
||||
sourceFilename: filename,
|
||||
plugins: ["jsx", "flow"],
|
||||
sourceType: "module",
|
||||
});
|
||||
if (babelAST != null) {
|
||||
traverse(babelAST, {
|
||||
Program(prog) {
|
||||
try {
|
||||
const opts: Partial<PluginOptions> = {
|
||||
panicOnBailout: false,
|
||||
environment: {
|
||||
validateHooksUsage: true,
|
||||
validateFrozenLambdas: true,
|
||||
validateRefAccessDuringRender: true,
|
||||
},
|
||||
};
|
||||
compileProgram(prog, {
|
||||
opts: parsePluginOptions(opts),
|
||||
filename: context.filename,
|
||||
comments: babelAST.comments ?? [],
|
||||
});
|
||||
} catch (err) {
|
||||
if (err instanceof CompilerError) {
|
||||
for (const detail of err.details) {
|
||||
if (detail.loc != null) {
|
||||
context.report({
|
||||
message: detail.toString(),
|
||||
loc: detail.loc,
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new Error(err);
|
||||
try {
|
||||
transformFromAstSync(babelAST, sourceCode, {
|
||||
filename,
|
||||
highlightCode: false,
|
||||
retainLines: true,
|
||||
plugins: [
|
||||
[ReactForgetBabelPlugin, opts],
|
||||
"babel-plugin-fbt",
|
||||
"babel-plugin-fbt-runtime",
|
||||
],
|
||||
sourceType: "module",
|
||||
});
|
||||
} catch (err) {
|
||||
if (err instanceof CompilerError) {
|
||||
for (const detail of err.details) {
|
||||
if (detail.loc != null) {
|
||||
context.report({
|
||||
message: detail.toString(),
|
||||
loc: detail.loc,
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
} else {
|
||||
throw new Error(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
return {};
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user