From 5ebb7f1a8e7b72626ae430da9bdb7b4f8d6ab71d Mon Sep 17 00:00:00 2001 From: Lauren Tan Date: Tue, 6 Jun 2023 10:56:21 -0400 Subject: [PATCH] Try to compile with Forget in eslint plugin Nothing interesting for now, but it does seem to successfully run the compiler --- .../babel-plugin-react-forget/src/index.ts | 8 ++++- .../__tests__/ReactForgetDiagnostics-test.ts | 33 +++++++++++++++++-- .../eslint-plugin-react-forget/package.json | 8 ++++- .../src/rules/ReactForgetDiagnostics.ts | 17 +++++++++- compiler/forget/yarn.lock | 18 +++++++++- 5 files changed, 78 insertions(+), 6 deletions(-) diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/index.ts b/compiler/forget/packages/babel-plugin-react-forget/src/index.ts index 6b939bdcfe..b0d32b600b 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/index.ts +++ b/compiler/forget/packages/babel-plugin-react-forget/src/index.ts @@ -11,7 +11,13 @@ export { CompilerErrorDetail, ErrorSeverity, } from "./CompilerError"; -export { CompilerPipelineValue, compileFn as compile, run } from "./Entrypoint"; +export { + CompilerPipelineValue, + compileFn as compile, + compileProgram, + parsePluginOptions, + run, +} from "./Entrypoint"; export { Effect, Hook, ValueKind, printHIR } from "./HIR"; export { printReactiveFunction } from "./ReactiveScopes"; diff --git a/compiler/forget/packages/eslint-plugin-react-forget/__tests__/ReactForgetDiagnostics-test.ts b/compiler/forget/packages/eslint-plugin-react-forget/__tests__/ReactForgetDiagnostics-test.ts index 91971b88d3..a0001469f4 100644 --- a/compiler/forget/packages/eslint-plugin-react-forget/__tests__/ReactForgetDiagnostics-test.ts +++ b/compiler/forget/packages/eslint-plugin-react-forget/__tests__/ReactForgetDiagnostics-test.ts @@ -1,10 +1,39 @@ +import BabelESLintParser from "@babel/eslint-parser"; import { RuleTester as ESLintTester } from "eslint"; import ReactForgetDiagnostics from "../src/rules/ReactForgetDiagnostics"; +/** + * A string template tag that removes padding from the left side of multi-line strings + * @param {Array} strings array of code strings (only one expected) + */ +function normalizeIndent(strings: TemplateStringsArray): string { + const codeLines = strings[0].split("\n"); + const leftPadding = codeLines[1].match(/\s+/)[0]; + return codeLines.map((line) => line.slice(leftPadding.length)).join("\n"); +} + const tests = { - valid: [{ code: "" }], + valid: [ + { + name: "Basic example", + code: normalizeIndent` + function foo(x, y) { + if (x) { + return foo(false, y); + } + return [y * 10]; + } + `, + }, + ], invalid: [], }; -const eslintTester = new ESLintTester(); +const eslintTester = new ESLintTester({ + parser: BabelESLintParser, + parserOptions: { + ecmaVersion: 6, + sourceType: "module", + }, +}); eslintTester.run("react-forget-diagnostics", ReactForgetDiagnostics, tests); diff --git a/compiler/forget/packages/eslint-plugin-react-forget/package.json b/compiler/forget/packages/eslint-plugin-react-forget/package.json index f6273fa4de..6b3002af8b 100644 --- a/compiler/forget/packages/eslint-plugin-react-forget/package.json +++ b/compiler/forget/packages/eslint-plugin-react-forget/package.json @@ -17,8 +17,14 @@ "test": "tsc && jest", "update:eslint-docs": "eslint-doc-generator" }, - "dependencies": {}, + "dependencies": { + "@babel/core": "^7.19.1", + "@babel/parser": "^7.19.1", + "@babel/traverse": "^7.19.1", + "babel-plugin-react-forget": "*" + }, "devDependencies": { + "@babel/eslint-parser": "^7.19.1", "@babel/preset-env": "^7.22.4", "@babel/preset-typescript": "^7.18.6", "@types/node": "^20.2.5", diff --git a/compiler/forget/packages/eslint-plugin-react-forget/src/rules/ReactForgetDiagnostics.ts b/compiler/forget/packages/eslint-plugin-react-forget/src/rules/ReactForgetDiagnostics.ts index c27cbbfd5b..ff1ef90cf2 100644 --- a/compiler/forget/packages/eslint-plugin-react-forget/src/rules/ReactForgetDiagnostics.ts +++ b/compiler/forget/packages/eslint-plugin-react-forget/src/rules/ReactForgetDiagnostics.ts @@ -5,6 +5,9 @@ * LICENSE file in the root directory of this source tree. */ +import * as parser from "@babel/parser"; +import traverse from "@babel/traverse"; +import { compileProgram, parsePluginOptions } from "babel-plugin-react-forget"; import type { Rule } from "eslint"; const rule: Rule.RuleModule = { @@ -15,7 +18,19 @@ const rule: Rule.RuleModule = { recommended: true, }, }, - create(_context: Rule.RuleContext) { + create(context: Rule.RuleContext) { + const babelAST = parser.parse(context.sourceCode.text); + if (babelAST != null) { + traverse(babelAST, { + Program(prog) { + compileProgram(prog, { + opts: parsePluginOptions(null), // use defaults for now + filename: context.filename, + comments: babelAST.comments ?? [], + }); + }, + }); + } return {}; }, }; diff --git a/compiler/forget/yarn.lock b/compiler/forget/yarn.lock index 1ed32ab4df..841973edd6 100644 --- a/compiler/forget/yarn.lock +++ b/compiler/forget/yarn.lock @@ -75,6 +75,15 @@ json5 "^2.2.2" semver "^6.3.0" +"@babel/eslint-parser@^7.19.1": + version "7.21.8" + resolved "https://registry.yarnpkg.com/@babel/eslint-parser/-/eslint-parser-7.21.8.tgz#59fb6fc4f3b017ab86987c076226ceef7b2b2ef2" + integrity sha512-HLhI+2q+BP3sf78mFUZNCGc10KEmoUqtUT1OCdMZsN+qr4qFeLUod62/zAnF3jNQstwyasDkZnVXwfK2Bml7MQ== + dependencies: + "@nicolo-ribaudo/eslint-scope-5-internals" "5.1.1-v1" + eslint-visitor-keys "^2.1.0" + semver "^6.3.0" + "@babel/generator@7.2.0", "@babel/generator@^7.0.0", "@babel/generator@^7.1.6", "@babel/generator@^7.2.0": version "7.2.0" resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.2.0.tgz#eaf3821fa0301d9d4aef88e63d4bcc19b73ba16c" @@ -2243,6 +2252,13 @@ resolved "https://registry.yarnpkg.com/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.0.4.tgz#2c3ede793e90d632e1dbdb0e4851f6f32132fba8" integrity sha512-vXtbo9N1FdtZZRcv4BliU28tTYrkb1EnVpUiiFFe88I6kS9aZVTMY9Z/OtDR52rl1JF1hgs9sL/59D/TQqSATQ== +"@nicolo-ribaudo/eslint-scope-5-internals@5.1.1-v1": + version "5.1.1-v1" + resolved "https://registry.yarnpkg.com/@nicolo-ribaudo/eslint-scope-5-internals/-/eslint-scope-5-internals-5.1.1-v1.tgz#dbf733a965ca47b1973177dc0bb6c889edcfb129" + integrity sha512-54/JRvkLIzzDWshCWfuhadfrfZVPiElY8Fcgmg1HroEly/EDSszzhBAsarCux+D/kOslTRquNzuyGSmUSTTHGg== + dependencies: + eslint-scope "5.1.1" + "@nodelib/fs.scandir@2.1.5": version "2.1.5" 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: resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-1.3.0.tgz#30ebd1ef7c2fdff01c3a4f151044af25fab0523e" integrity sha512-6J72N8UNa462wa/KFODt/PJ3IU60SDpC3QXC1Hjc1BXXpfL2C9R5+AU7jhe0F6GREqVMh4Juu+NY7xn+6dipUQ== -eslint-visitor-keys@^2.0.0: +eslint-visitor-keys@^2.0.0, eslint-visitor-keys@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-2.1.0.tgz#f65328259305927392c938ed44eb0a5c9b2bd303" integrity sha512-0rSmRBzXgDzIsD6mGdJgevzgezI534Cer5L/vyMX0kHzT/jiB43jRhd9YUlMGYLQy2zprNmoT8qasCGtY+QaKw==