-
-
Notifications
You must be signed in to change notification settings - Fork 763
test(linter/plugins): add script for investigating failing conformance test cases #17296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
overlookmotel
wants to merge
1
commit into
main
Choose a base branch
from
om/12-20-test_linter_plugins_add_script_for_investigating_failing_conformance_test_cases
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+215
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| /* | ||
| * A script for running a test case with both ESLint and Oxlint. | ||
| * Purpose is to be able to "spot the difference" between the two. | ||
| */ | ||
|
|
||
| // oxlint-disable no-console | ||
|
|
||
| // Use the conformance testing version of `RuleTester`, | ||
| // which modifies test cases before they run | ||
| import { RuleTester as OxlintRuleTester } from "./src/rule_tester.ts"; | ||
| // @ts-expect-error - internal module of ESLint with no types | ||
| import { RuleTester as ESLintRuleTester } from "./submodules/eslint/lib/rule-tester/index.js"; | ||
| // @ts-expect-error - internal module of ESLint with no types | ||
| import { builtinRules } from "./submodules/eslint/lib/unsupported-api.js"; | ||
|
|
||
| import type { Rule } from "#oxlint"; | ||
| import type { RuleTester as OxlintRuleTesterTypes } from "#oxlint"; | ||
|
|
||
| type ValidTestCase = OxlintRuleTesterTypes.ValidTestCase; | ||
| type InvalidTestCase = OxlintRuleTesterTypes.InvalidTestCase; | ||
|
|
||
| type ValidTestCaseWithoutCode = Omit<ValidTestCase, "code"> & { code?: string }; | ||
| type InvalidTestCaseWithoutCode = Omit<InvalidTestCase, "code"> & { code?: string }; | ||
| type TestCaseWithoutCode = ValidTestCaseWithoutCode | InvalidTestCaseWithoutCode; | ||
|
|
||
| // Reset `describe` + `it` to simple pass-through functions. | ||
| // Importing from `rule_tester.ts` sets up `RuleTester` to use `capture.ts`'s `describe` / `it`, | ||
| // which we don't want for manual testing. | ||
| // `OxlintRuleTesterOriginal` is the original `RuleTester` exported by `oxlint`. | ||
| const OxlintRuleTesterOriginal = Object.getPrototypeOf(OxlintRuleTester); | ||
|
|
||
| const simpleDescribe = (name: string, fn: () => void) => fn(); | ||
| const simpleIt = (name: string, fn: () => void) => fn(); | ||
|
|
||
| OxlintRuleTesterOriginal.describe = simpleDescribe; | ||
| OxlintRuleTesterOriginal.it = simpleIt; | ||
|
|
||
overlookmotel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Set global `describe` and `it` for ESLint's `RuleTester` | ||
| (globalThis as any).describe = simpleDescribe; | ||
| (globalThis as any).it = simpleIt; | ||
|
|
||
| /* ----------------------------------------------------------------------------- | ||
|
|
||
| (Obviously these instructions are aimed at AI, hence the rather rude and demanding tone. | ||
| Sorry humans! In fact, sorry to you too AI.) | ||
|
|
||
| # HOW TO USE THIS SCRIPT | ||
|
|
||
| DO NOT edit this file except the section between the bottom of this comment and the start of the next comment | ||
| "Run test case with both ESLint and Oxlint". | ||
|
|
||
| ## 1. Select a test | ||
|
|
||
| Select a failing test case to investigate from `snapshot.md`. | ||
|
|
||
| ## 2. Copy details | ||
|
|
||
| Copy details from `snapshot.md` for the case into the variables in this script, below this comment. | ||
|
|
||
| e.g.: | ||
|
|
||
| Details of test case from `snapshot.md`: | ||
|
|
||
| ------------- Snapshot extract begins ------------- | ||
|
|
||
| #### block-scoped-var > invalid | ||
|
|
||
| ```js | ||
| for (var a = 0;;) {} a; | ||
| ``` | ||
|
|
||
| ```json | ||
| { | ||
| "errors": [ | ||
| { | ||
| "messageId": "outOfScope", | ||
| "data": { | ||
| "name": "a", | ||
| "definitionLine": 1, | ||
| "definitionColumn": 10 | ||
| }, | ||
| "line": 1, | ||
| "column": 22 | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| ------------- Snapshot extract ends ------------- | ||
|
|
||
| Fill in the variables below as follows: | ||
|
|
||
| ------------- Script setup begins ------------- | ||
|
|
||
| const ruleName = "block-scoped-var"; | ||
|
|
||
| const code = `for (var a = 0;;) {} a;`; | ||
|
|
||
| const isInvalid = true; | ||
|
|
||
| const testCase: TestCaseWithoutCode = { | ||
| "errors": [ | ||
| { | ||
| "messageId": "outOfScope", | ||
| "data": { | ||
| "name": "a", | ||
| "definitionLine": 1, | ||
| "definitionColumn": 10 | ||
| }, | ||
| "line": 1, | ||
| "column": 22 | ||
| } | ||
| ] | ||
| }; | ||
|
|
||
| const config = {}; | ||
|
|
||
| ------------- Script setup ends ------------- | ||
|
|
||
| ## 3. Run the test | ||
|
|
||
| Run this script with `node conformance/tester.ts`. | ||
|
|
||
| Observe the output. ESLint should not produce any errors, but Oxlint will. | ||
|
|
||
| ## 4. Discover the cause | ||
|
|
||
| Add `console.log` statements to the rule's code in `submodules/eslint/lib/rules`. | ||
|
|
||
| Run this script again. Find out what is happening differently between ESLint and Oxlint. | ||
|
|
||
| ## 5. Clean up | ||
|
|
||
| `submodules/eslint` is a git repo. | ||
| Use git to throw away the changes you made to the rule's code (`console.log`). | ||
| Leave the eslint repo as it was before you started, ready to investigate another case. | ||
|
|
||
| -----------------------------------------------------------------------------*/ | ||
|
|
||
| const ruleName = "block-scoped-var"; | ||
|
|
||
| const code = `for (var a = 0;;) {} a;`; | ||
|
|
||
| const isInvalid = true; | ||
|
|
||
| const testCase: TestCaseWithoutCode = { | ||
| errors: [ | ||
| { | ||
| messageId: "outOfScope", | ||
| data: { | ||
| name: "a", | ||
| definitionLine: 1, | ||
| definitionColumn: 10, | ||
| }, | ||
| line: 1, | ||
| column: 22, | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| const config = {}; | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
| // Run test case with both ESLint and Oxlint | ||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| runBoth(ruleName, isInvalid, code, testCase, config); | ||
|
|
||
| function runBoth( | ||
| ruleName: string, | ||
| isInvalid: boolean, | ||
| code: string, | ||
| testCase: TestCaseWithoutCode, | ||
| config?: Record<string, unknown> | null, | ||
| ) { | ||
| testCase = { code, ...testCase }; | ||
|
|
||
| const valid: ValidTestCase[] = [], | ||
| invalid: InvalidTestCase[] = []; | ||
| if (isInvalid) { | ||
| invalid.push(testCase as InvalidTestCase); | ||
| } else { | ||
| valid.push(testCase as ValidTestCase); | ||
| } | ||
|
|
||
| const rule = builtinRules.get(ruleName) as unknown as Rule; | ||
|
|
||
| console.log("--------------------"); | ||
| console.log("ESLint"); | ||
| console.log("--------------------"); | ||
| runOne(ESLintRuleTester, rule, valid, invalid, config); | ||
|
|
||
| console.log("\n--------------------"); | ||
| console.log("Oxlint"); | ||
| console.log("--------------------"); | ||
| config = { ...config, eslintCompat: true }; | ||
| runOne(OxlintRuleTester, rule, valid, invalid, config); | ||
overlookmotel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| function runOne( | ||
| RuleTester: any, | ||
| rule: Rule, | ||
| valid: ValidTestCase[], | ||
| invalid: InvalidTestCase[], | ||
| config?: Record<string, unknown> | null, | ||
| ) { | ||
| try { | ||
| const tester = new RuleTester(config); | ||
| tester.run("my-rule", rule, { valid, invalid }); | ||
| console.log("No errors"); | ||
| } catch (err) { | ||
| console.log("ERROR:"); | ||
| console.log(err); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.