[eslint-plugin] Add basic test for unsupported syntax

Kinda works?
This commit is contained in:
Lauren Tan
2023-06-07 15:41:22 -04:00
parent 246936ca57
commit 3673272ca4
2 changed files with 43 additions and 8 deletions
@@ -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({
@@ -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,
});
}
}
}
}
},
});
}