diff --git a/apps/oxlint/scripts/build.js b/apps/oxlint/scripts/build.js index ead8553f002e9..271b432164e7c 100755 --- a/apps/oxlint/scripts/build.js +++ b/apps/oxlint/scripts/build.js @@ -33,6 +33,7 @@ const parserFilePaths = [ 'generated/lazy/walk.js', */ 'generated/deserialize/ts.js', + 'generated/visit/keys.js', 'generated/visit/types.js', 'generated/visit/visitor.d.ts', 'generated/visit/walk.js', diff --git a/apps/oxlint/src-js/plugins/source_code.ts b/apps/oxlint/src-js/plugins/source_code.ts index e2a6a82a438f0..7630ac9b79cc0 100644 --- a/apps/oxlint/src-js/plugins/source_code.ts +++ b/apps/oxlint/src-js/plugins/source_code.ts @@ -1,3 +1,4 @@ +import { createRequire } from 'node:module'; import { DATA_POINTER_POS_32, SOURCE_LEN_OFFSET, @@ -11,6 +12,8 @@ import type { Program } from '@oxc-project/types'; import type { Scope, ScopeManager, Variable } from './scope.ts'; import type { BufferWithArrays, Comment, LineColumn, Node, NodeOrToken, Token } from './types.ts'; +const require = createRequire(import.meta.url); + const { max } = Math; // Text decoder, for decoding source text from buffer @@ -28,6 +31,9 @@ let sourceText: string | null = null; let sourceByteLen: number = 0; let ast: Program | null = null; +// Lazily populated when `SOURCE_CODE.visitorKeys` is accessed. +let visitorKeys: { [key: string]: string[] } | null = null; + /** * Set up source for the file about to be linted. * @param bufferInput - Buffer containing AST @@ -118,7 +124,9 @@ export const SOURCE_CODE = Object.freeze({ // Get visitor keys to traverse this AST. get visitorKeys(): { [key: string]: string[] } { - throw new Error('`sourceCode.visitorKeys` not implemented yet'); // TODO + // This is the path relative to `plugins.js` file in `dist` directory + if (visitorKeys === null) visitorKeys = require('./generated/visit/keys.js').default; + return visitorKeys; }, // Get parser services for the file. diff --git a/apps/oxlint/test/fixtures/sourceCode/output.snap.md b/apps/oxlint/test/fixtures/sourceCode/output.snap.md index d308acf0db08f..731fe98157a60 100644 --- a/apps/oxlint/test/fixtures/sourceCode/output.snap.md +++ b/apps/oxlint/test/fixtures/sourceCode/output.snap.md @@ -7,6 +7,7 @@ | text: "let foo, bar;\n" | getText(): "let foo, bar;\n" | ast: "foo" + | visitorKeys: left, right ,-[files/1.js:1:1] 1 | let foo, bar; : ^ @@ -23,6 +24,7 @@ | text: "let foo, bar;\n" | getText(): "let foo, bar;\n" | ast: "foo" + | visitorKeys: left, right ,-[files/1.js:1:1] 1 | let foo, bar; : ^ @@ -86,6 +88,7 @@ | text: "let qux;\n" | getText(): "let qux;\n" | ast: "qux" + | visitorKeys: left, right ,-[files/2.js:1:1] 1 | let qux; : ^ @@ -102,6 +105,7 @@ | text: "let qux;\n" | getText(): "let qux;\n" | ast: "qux" + | visitorKeys: left, right ,-[files/2.js:1:1] 1 | let qux; : ^ diff --git a/apps/oxlint/test/fixtures/sourceCode/plugin.ts b/apps/oxlint/test/fixtures/sourceCode/plugin.ts index 3c1862d355ffa..406b92dddfa1f 100644 --- a/apps/oxlint/test/fixtures/sourceCode/plugin.ts +++ b/apps/oxlint/test/fixtures/sourceCode/plugin.ts @@ -14,7 +14,8 @@ const createRule: Rule = { `text: ${JSON.stringify(context.sourceCode.text)}\n` + `getText(): ${JSON.stringify(context.sourceCode.getText())}\n` + // @ts-ignore - `ast: "${ast.body[0].declarations[0].id.name}"`, + `ast: "${ast.body[0].declarations[0].id.name}"\n` + + `visitorKeys: ${context.sourceCode.visitorKeys.BinaryExpression.join(', ')}`, node: SPAN, }); @@ -55,7 +56,8 @@ const createOnceRule: Rule = { `text: ${JSON.stringify(context.sourceCode.text)}\n` + `getText(): ${JSON.stringify(context.sourceCode.getText())}\n` + // @ts-ignore - `ast: "${ast.body[0].declarations[0].id.name}"`, + `ast: "${ast.body[0].declarations[0].id.name}"\n` + + `visitorKeys: ${context.sourceCode.visitorKeys.BinaryExpression.join(', ')}`, node: SPAN, }); },