Skip to content

Commit 13f1003

Browse files
committed
refactor(linter/plugins): share ast between files (#14349)
Pure refactor. Remove `getAst` getter function, and share `ast` directly between files with `import` / `export`.
1 parent ec2f410 commit 13f1003

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { diagnostics, setupContextForFile } from './context.js';
22
import { registeredRules } from './load.js';
3-
import { getAst, resetSource, setupSourceForFile } from './source_code.js';
3+
import { ast, initAst, resetSource, setupSourceForFile } from './source_code.js';
44
import { assertIs, getErrorMessage } from './utils.js';
55
import { addVisitorToCompiled, compiledVisitor, finalizeCompiledVisitor, initCompiledVisitor } from './visitor.js';
66

@@ -134,8 +134,8 @@ function lintFileImpl(filePath: string, bufferId: number, buffer: Uint8Array | n
134134
// Some rules seen in the wild return an empty visitor object from `create` if some initial check fails
135135
// e.g. file extension is not one the rule acts on.
136136
if (needsVisit) {
137-
const program = getAst();
138-
walkProgram(program, compiledVisitor);
137+
if (ast === null) initAst();
138+
walkProgram(ast, compiledVisitor);
139139

140140
// Lazy implementation
141141
/*

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ let buffer: BufferWithArrays | null = null;
3030
let hasBOM = false;
3131

3232
// Lazily populated when `SOURCE_CODE.text` or `SOURCE_CODE.ast` is accessed,
33-
// or `getAst()` is called before the AST is walked.
33+
// or `initAst()` is called before the AST is walked.
3434
let sourceText: string | null = null;
3535
let sourceByteLen: number = 0;
36-
let ast: Program | null = null;
36+
export let ast: Program | null = null;
3737

3838
// Lazily populated when `SOURCE_CODE.lines` is accessed.
3939
// `lineStartOffsets` starts as `[0]`, and `resetSource` doesn't remove that initial element, so it's never empty.
@@ -66,21 +66,11 @@ function initSourceText(): void {
6666
/**
6767
* Deserialize AST from buffer.
6868
*/
69-
function initAst(): void {
69+
export function initAst(): void {
7070
if (sourceText === null) initSourceText();
7171
ast = deserializeProgramOnly(buffer, sourceText, sourceByteLen);
7272
}
7373

74-
/**
75-
* Get AST of the file being linted.
76-
* If AST has not already been deserialized, do it now.
77-
* @returns AST of the file being linted.
78-
*/
79-
export function getAst(): Program {
80-
if (ast === null) initAst();
81-
return ast;
82-
}
83-
8474
/**
8575
* Split source text into lines.
8676
*/
@@ -151,7 +141,8 @@ export const SOURCE_CODE = Object.freeze({
151141

152142
// Get AST of the file.
153143
get ast(): Program {
154-
return getAst();
144+
if (ast === null) initAst();
145+
return ast;
155146
},
156147

157148
// Get `ScopeManager` for the file.

0 commit comments

Comments
 (0)