@samitouri / QOS-React-2 / commits / 3673272ca4

[eslint-plugin] Add basic test for unsupported syntax

Kinda works?

Lauren Tan committed Jun 7, 2023 at 15:41 UTC 3673272ca4068ca04906479ed5d142a269be8901
2 files changed +43 -8
compiler/forget/packages/eslint-plugin-react-forget/__tests__/ReactForgetDiagnostics-test.ts
+20 -2
@@ -12,7 +12,12 @@ function normalizeIndent(strings: TemplateStringsArray): string {
12 return codeLines.map((line) => line.slice(leftPadding.length)).join("\n");
13 }
14
15 -const tests = {
15 +type ForgetTestCases = {
16 + valid: ESLintTester.ValidTestCase[];
17 + invalid: ESLintTester.InvalidTestCase[];
18 +};
19 +
20 +const tests: ForgetTestCases = {
21 valid: [
22 {
23 name: "Basic example",
@@ -26,7 +31,20 @@ const tests = {
31 `,
32 },
33 ],
29 - invalid: [],
34 + invalid: [
35 + {
36 + name: "Unsupported syntax",
37 + code: normalizeIndent`
38 + function foo(x) {
39 + var y = 1;
40 + return y * x;
41 + }
42 + `,
43 + errors: [
44 + "[ReactForget] Todo: (BuildHIR::lowerStatement) Handle var kinds in VariableDeclaration (3:3)",
45 + ],
46 + },
47 + ],
48 };
49
50 const eslintTester = new ESLintTester({
compiler/forget/packages/eslint-plugin-react-forget/src/rules/ReactForgetDiagnostics.ts
+23 -6
@@ -7,7 +7,11 @@
7
8 import * as parser from "@babel/parser";
9 import traverse from "@babel/traverse";
10 -import { compileProgram, parsePluginOptions } from "babel-plugin-react-forget";
10 +import {
11 + CompilerError,
12 + compileProgram,
13 + parsePluginOptions,
14 +} from "babel-plugin-react-forget";
15 import type { Rule } from "eslint";
16
17 const rule: Rule.RuleModule = {
@@ -23,11 +27,24 @@ const rule: Rule.RuleModule = {
27 if (babelAST != null) {
28 traverse(babelAST, {
29 Program(prog) {
26 - compileProgram(prog, {
27 - opts: parsePluginOptions(null), // use defaults for now
28 - filename: context.filename,
29 - comments: babelAST.comments ?? [],
30 - });
30 + try {
31 + compileProgram(prog, {
32 + opts: parsePluginOptions(null), // use defaults for now
33 + filename: context.filename,
34 + comments: babelAST.comments ?? [],
35 + });
36 + } catch (err) {
37 + if (err instanceof CompilerError) {
38 + for (const detail of err.details) {
39 + if (detail.loc != null) {
40 + context.report({
41 + message: detail.toString(),
42 + loc: detail.loc,
43 + });
44 + }
45 + }
46 + }
47 + }
48 },
49 });
50 }