mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[eslint-plugin] Add basic test for unsupported syntax
Kinda works?
This commit is contained in:
+20
-2
@@ -12,7 +12,12 @@ function normalizeIndent(strings: TemplateStringsArray): string {
|
||||
return codeLines.map((line) => line.slice(leftPadding.length)).join("\n");
|
||||
}
|
||||
|
||||
const tests = {
|
||||
type ForgetTestCases = {
|
||||
valid: ESLintTester.ValidTestCase[];
|
||||
invalid: ESLintTester.InvalidTestCase[];
|
||||
};
|
||||
|
||||
const tests: ForgetTestCases = {
|
||||
valid: [
|
||||
{
|
||||
name: "Basic example",
|
||||
@@ -26,7 +31,20 @@ const tests = {
|
||||
`,
|
||||
},
|
||||
],
|
||||
invalid: [],
|
||||
invalid: [
|
||||
{
|
||||
name: "Unsupported syntax",
|
||||
code: normalizeIndent`
|
||||
function foo(x) {
|
||||
var y = 1;
|
||||
return y * x;
|
||||
}
|
||||
`,
|
||||
errors: [
|
||||
"[ReactForget] Todo: (BuildHIR::lowerStatement) Handle var kinds in VariableDeclaration (3:3)",
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const eslintTester = new ESLintTester({
|
||||
|
||||
+23
-6
@@ -7,7 +7,11 @@
|
||||
|
||||
import * as parser from "@babel/parser";
|
||||
import traverse from "@babel/traverse";
|
||||
import { compileProgram, parsePluginOptions } from "babel-plugin-react-forget";
|
||||
import {
|
||||
CompilerError,
|
||||
compileProgram,
|
||||
parsePluginOptions,
|
||||
} from "babel-plugin-react-forget";
|
||||
import type { Rule } from "eslint";
|
||||
|
||||
const rule: Rule.RuleModule = {
|
||||
@@ -23,11 +27,24 @@ const rule: Rule.RuleModule = {
|
||||
if (babelAST != null) {
|
||||
traverse(babelAST, {
|
||||
Program(prog) {
|
||||
compileProgram(prog, {
|
||||
opts: parsePluginOptions(null), // use defaults for now
|
||||
filename: context.filename,
|
||||
comments: babelAST.comments ?? [],
|
||||
});
|
||||
try {
|
||||
compileProgram(prog, {
|
||||
opts: parsePluginOptions(null), // use defaults for now
|
||||
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,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user