Try to compile with Forget in eslint plugin
Nothing interesting for now, but it does seem to successfully run the compiler
Lauren Tan committed
Jun 6, 2023 at 10:56 UTC
5ebb7f1a8e7b72626ae430da9bdb7b4f8d6ab71d
5 files changed
+78
-6
compiler/forget/packages/babel-plugin-react-forget/src/index.ts
+7
-1
@@ -11,7 +11,13 @@ export {
11
CompilerErrorDetail,
12
ErrorSeverity,
13
} from "./CompilerError";
14
-export { CompilerPipelineValue, compileFn as compile, run } from "./Entrypoint";
14
+export {
15
+ CompilerPipelineValue,
16
+ compileFn as compile,
17
+ compileProgram,
18
+ parsePluginOptions,
19
+ run,
20
+} from "./Entrypoint";
21
export { Effect, Hook, ValueKind, printHIR } from "./HIR";
22
export { printReactiveFunction } from "./ReactiveScopes";
23
compiler/forget/packages/eslint-plugin-react-forget/__tests__/ReactForgetDiagnostics-test.ts
+31
-2
@@ -1,10 +1,39 @@
1
+import BabelESLintParser from "@babel/eslint-parser";
2
import { RuleTester as ESLintTester } from "eslint";
3
import ReactForgetDiagnostics from "../src/rules/ReactForgetDiagnostics";
4
5
+/**
6
+ * A string template tag that removes padding from the left side of multi-line strings
7
+ * @param {Array} strings array of code strings (only one expected)
8
+ */
9
+function normalizeIndent(strings: TemplateStringsArray): string {
10
+ const codeLines = strings[0].split("\n");
11
+ const leftPadding = codeLines[1].match(/\s+/)[0];
12
+ return codeLines.map((line) => line.slice(leftPadding.length)).join("\n");
13
+}
14
+
15
const tests = {
5
- valid: [{ code: "" }],
16
+ valid: [
17
+ {
18
+ name: "Basic example",
19
+ code: normalizeIndent`
20
+ function foo(x, y) {
21
+ if (x) {
22
+ return foo(false, y);
23
+ }
24
+ return [y * 10];
25
+ }
26
+ `,
27
+ },
28
+ ],
29
invalid: [],
30
};
31
9
-const eslintTester = new ESLintTester();
32
+const eslintTester = new ESLintTester({
33
+ parser: BabelESLintParser,
34
+ parserOptions: {
35
+ ecmaVersion: 6,
36
+ sourceType: "module",
37
+ },
38
+});
39
eslintTester.run("react-forget-diagnostics", ReactForgetDiagnostics, tests);
compiler/forget/packages/eslint-plugin-react-forget/package.json
+7
-1
@@ -17,8 +17,14 @@
17
"test": "tsc && jest",
18
"update:eslint-docs": "eslint-doc-generator"
19
},
20
- "dependencies": {},
20
+ "dependencies": {
21
+ "@babel/core": "^7.19.1",
22
+ "@babel/parser": "^7.19.1",
23
+ "@babel/traverse": "^7.19.1",
24
+ "babel-plugin-react-forget": "*"
25
+ },
26
"devDependencies": {
27
+ "@babel/eslint-parser": "^7.19.1",
28
"@babel/preset-env": "^7.22.4",
29
"@babel/preset-typescript": "^7.18.6",
30
"@types/node": "^20.2.5",
compiler/forget/packages/eslint-plugin-react-forget/src/rules/ReactForgetDiagnostics.ts
+16
-1
@@ -5,6 +5,9 @@
5
* LICENSE file in the root directory of this source tree.
6
*/
7
8
+import * as parser from "@babel/parser";
9
+import traverse from "@babel/traverse";
10
+import { compileProgram, parsePluginOptions } from "babel-plugin-react-forget";
11
import type { Rule } from "eslint";
12
13
const rule: Rule.RuleModule = {
@@ -15,7 +18,19 @@ const rule: Rule.RuleModule = {
18
recommended: true,
19
},
20
},
18
- create(_context: Rule.RuleContext) {
21
+ create(context: Rule.RuleContext) {
22
+ const babelAST = parser.parse(context.sourceCode.text);
23
+ if (babelAST != null) {
24
+ traverse(babelAST, {
25
+ Program(prog) {
26
+ compileProgram(prog, {
27
+ opts: parsePluginOptions(null), // use defaults for now
28
+ filename: context.filename,
29
+ comments: babelAST.comments ?? [],
30
+ });
31
+ },
32
+ });
33
+ }
34
return {};
35
},
36
};
compiler/forget/yarn.lock
+17
-1
@@ -75,6 +75,15 @@
75
json5 "^2.2.2"
76
semver "^6.3.0"
77
78
+"@babel/eslint-parser@^7.19.1":
79
+ version "7.21.8"
80
+ resolved "https://registry.yarnpkg.com/@babel/eslint-parser/-/eslint-parser-7.21.8.tgz#59fb6fc4f3b017ab86987c076226ceef7b2b2ef2"
81
+ integrity sha512-HLhI+2q+BP3sf78mFUZNCGc10KEmoUqtUT1OCdMZsN+qr4qFeLUod62/zAnF3jNQstwyasDkZnVXwfK2Bml7MQ==
82
+ dependencies:
83
+ "@nicolo-ribaudo/eslint-scope-5-internals" "5.1.1-v1"
84
+ eslint-visitor-keys "^2.1.0"
85
+ semver "^6.3.0"
86
+
87
"@babel/generator@7.2.0", "@babel/generator@^7.0.0", "@babel/generator@^7.1.6", "@babel/generator@^7.2.0":
88
version "7.2.0"
89
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.2.0.tgz#eaf3821fa0301d9d4aef88e63d4bcc19b73ba16c"
@@ -2243,6 +2252,13 @@
2252
resolved "https://registry.yarnpkg.com/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.0.4.tgz#2c3ede793e90d632e1dbdb0e4851f6f32132fba8"
2253
integrity sha512-vXtbo9N1FdtZZRcv4BliU28tTYrkb1EnVpUiiFFe88I6kS9aZVTMY9Z/OtDR52rl1JF1hgs9sL/59D/TQqSATQ==
2254
2255
+"@nicolo-ribaudo/eslint-scope-5-internals@5.1.1-v1":
2256
+ version "5.1.1-v1"
2257
+ resolved "https://registry.yarnpkg.com/@nicolo-ribaudo/eslint-scope-5-internals/-/eslint-scope-5-internals-5.1.1-v1.tgz#dbf733a965ca47b1973177dc0bb6c889edcfb129"
2258
+ integrity sha512-54/JRvkLIzzDWshCWfuhadfrfZVPiElY8Fcgmg1HroEly/EDSszzhBAsarCux+D/kOslTRquNzuyGSmUSTTHGg==
2259
+ dependencies:
2260
+ eslint-scope "5.1.1"
2261
+
2262
"@nodelib/fs.scandir@2.1.5":
2263
version "2.1.5"
2264
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5"
@@ -4806,7 +4822,7 @@ eslint-visitor-keys@^1.1.0:
4822
resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-1.3.0.tgz#30ebd1ef7c2fdff01c3a4f151044af25fab0523e"
4823
integrity sha512-6J72N8UNa462wa/KFODt/PJ3IU60SDpC3QXC1Hjc1BXXpfL2C9R5+AU7jhe0F6GREqVMh4Juu+NY7xn+6dipUQ==
4824
4809
-eslint-visitor-keys@^2.0.0:
4825
+eslint-visitor-keys@^2.0.0, eslint-visitor-keys@^2.1.0:
4826
version "2.1.0"
4827
resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-2.1.0.tgz#f65328259305927392c938ed44eb0a5c9b2bd303"
4828
integrity sha512-0rSmRBzXgDzIsD6mGdJgevzgezI534Cer5L/vyMX0kHzT/jiB43jRhd9YUlMGYLQy2zprNmoT8qasCGtY+QaKw==