Skip to content

Commit e4530dc

Browse files
committed
feat(linter/plugins): implement SourceCode#visitorKeys property
1 parent a04e19a commit e4530dc

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

apps/oxlint/scripts/build.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const parserFilePaths = [
3333
'generated/lazy/walk.js',
3434
*/
3535
'generated/deserialize/ts.js',
36+
'generated/visit/keys.js',
3637
'generated/visit/types.js',
3738
'generated/visit/visitor.d.ts',
3839
'generated/visit/walk.js',

apps/oxlint/src-js/plugins/source_code.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createRequire } from 'node:module';
12
import {
23
DATA_POINTER_POS_32,
34
SOURCE_LEN_OFFSET,
@@ -11,6 +12,8 @@ import type { Program } from '@oxc-project/types';
1112
import type { Scope, ScopeManager, Variable } from './scope.ts';
1213
import type { BufferWithArrays, Comment, LineColumn, Node, NodeOrToken, Token } from './types.ts';
1314

15+
const require = createRequire(import.meta.url);
16+
1417
const { max } = Math;
1518

1619
// Text decoder, for decoding source text from buffer
@@ -28,6 +31,9 @@ let sourceText: string | null = null;
2831
let sourceByteLen: number = 0;
2932
let ast: Program | null = null;
3033

34+
// Lazily populated when `SOURCE_CODE.visitorKeys` is accessed.
35+
let visitorKeys: { [key: string]: string[] } | null = null;
36+
3137
/**
3238
* Set up source for the file about to be linted.
3339
* @param bufferInput - Buffer containing AST
@@ -118,7 +124,9 @@ export const SOURCE_CODE = Object.freeze({
118124

119125
// Get visitor keys to traverse this AST.
120126
get visitorKeys(): { [key: string]: string[] } {
121-
throw new Error('`sourceCode.visitorKeys` not implemented yet'); // TODO
127+
// This is the path relative to `plugins.js` file in `dist` directory
128+
if (visitorKeys === null) visitorKeys = require('./generated/visit/keys.js').default;
129+
return visitorKeys;
122130
},
123131

124132
// Get parser services for the file.

apps/oxlint/test/fixtures/sourceCode/output.snap.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
| text: "let foo, bar;\n"
88
| getText(): "let foo, bar;\n"
99
| ast: "foo"
10+
| visitorKeys: left, right
1011
,-[files/1.js:1:1]
1112
1 | let foo, bar;
1213
: ^
@@ -23,6 +24,7 @@
2324
| text: "let foo, bar;\n"
2425
| getText(): "let foo, bar;\n"
2526
| ast: "foo"
27+
| visitorKeys: left, right
2628
,-[files/1.js:1:1]
2729
1 | let foo, bar;
2830
: ^
@@ -86,6 +88,7 @@
8688
| text: "let qux;\n"
8789
| getText(): "let qux;\n"
8890
| ast: "qux"
91+
| visitorKeys: left, right
8992
,-[files/2.js:1:1]
9093
1 | let qux;
9194
: ^
@@ -102,6 +105,7 @@
102105
| text: "let qux;\n"
103106
| getText(): "let qux;\n"
104107
| ast: "qux"
108+
| visitorKeys: left, right
105109
,-[files/2.js:1:1]
106110
1 | let qux;
107111
: ^

apps/oxlint/test/fixtures/sourceCode/plugin.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const createRule: Rule = {
1414
`text: ${JSON.stringify(context.sourceCode.text)}\n` +
1515
`getText(): ${JSON.stringify(context.sourceCode.getText())}\n` +
1616
// @ts-ignore
17-
`ast: "${ast.body[0].declarations[0].id.name}"`,
17+
`ast: "${ast.body[0].declarations[0].id.name}"\n` +
18+
`visitorKeys: ${context.sourceCode.visitorKeys.BinaryExpression.join(', ')}`,
1819
node: SPAN,
1920
});
2021

@@ -55,7 +56,8 @@ const createOnceRule: Rule = {
5556
`text: ${JSON.stringify(context.sourceCode.text)}\n` +
5657
`getText(): ${JSON.stringify(context.sourceCode.getText())}\n` +
5758
// @ts-ignore
58-
`ast: "${ast.body[0].declarations[0].id.name}"`,
59+
`ast: "${ast.body[0].declarations[0].id.name}"\n` +
60+
`visitorKeys: ${context.sourceCode.visitorKeys.BinaryExpression.join(', ')}`,
5961
node: SPAN,
6062
});
6163
},

0 commit comments

Comments
 (0)