Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/oxlint/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
10 changes: 9 additions & 1 deletion apps/oxlint/src-js/plugins/source_code.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createRequire } from 'node:module';
import {
DATA_POINTER_POS_32,
SOURCE_LEN_OFFSET,
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions apps/oxlint/test/fixtures/sourceCode/output.snap.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
: ^
Expand All @@ -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;
: ^
Expand Down Expand Up @@ -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;
: ^
Expand All @@ -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;
: ^
Expand Down
6 changes: 4 additions & 2 deletions apps/oxlint/test/fixtures/sourceCode/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand Down Expand Up @@ -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,
});
},
Expand Down
Loading