Skip to content

Commit 53f243f

Browse files
committed
perf(linter/plugins): use singleton for SourceCode
1 parent e622fc9 commit 53f243f

File tree

5 files changed

+110
-84
lines changed

5 files changed

+110
-84
lines changed

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { getFixes } from './fix.js';
2-
import { SourceCode } from './source_code.js';
2+
import { SOURCE_CODE } from './source_code.js';
33

44
import type { Fix, FixFn } from './fix.ts';
5+
import type { SourceCode } from './source_code.ts';
56
import type { Node } from './types.ts';
67

78
// Diagnostic in form passed by user to `Context#report()`
@@ -33,13 +34,11 @@ export const diagnostics: DiagnosticReport[] = [];
3334
* @param context - `Context` object
3435
* @param ruleIndex - Index of this rule within `ruleIds` passed from Rust
3536
* @param filePath - Absolute path of file being linted
36-
* @param sourceText - Source text of file being linted
3737
*/
3838
export let setupContextForFile: (
3939
context: Context,
4040
ruleIndex: number,
4141
filePath: string,
42-
sourceText: string,
4342
) => void;
4443

4544
/**
@@ -68,10 +67,6 @@ export interface InternalContext {
6867
ruleIndex: number;
6968
// Absolute path of file being linted
7069
filePath: string;
71-
// `SourceCode` class instance for this rule.
72-
// Rule has single `SourceCode` instance that is updated for each file
73-
// (NOT new `SourceCode` instance for each file).
74-
sourceCode: SourceCode;
7570
// Options
7671
options: unknown[];
7772
// `true` if rule can provide fixes (`meta.fixable` in `RuleMeta` is 'code' or 'whitespace')
@@ -96,7 +91,6 @@ export class Context {
9691
this.#internal = {
9792
id: fullRuleName,
9893
filePath: '',
99-
sourceCode: new SourceCode(),
10094
ruleIndex: -1,
10195
options: [],
10296
isFixable,
@@ -125,8 +119,9 @@ export class Context {
125119
}
126120

127121
// Getter for `SourceCode` for file being linted.
128-
get sourceCode() {
129-
return getInternal(this, 'access `context.sourceCode`').sourceCode;
122+
get sourceCode(): SourceCode {
123+
getInternal(this, 'access `context.sourceCode`');
124+
return SOURCE_CODE;
130125
}
131126

132127
/**
@@ -148,12 +143,11 @@ export class Context {
148143
}
149144

150145
static {
151-
setupContextForFile = (context, ruleIndex, filePath, sourceText) => {
146+
setupContextForFile = (context, ruleIndex, filePath) => {
152147
// TODO: Support `options`
153148
const internal = context.#internal;
154149
internal.ruleIndex = ruleIndex;
155150
internal.filePath = filePath;
156-
internal.sourceCode.text = sourceText;
157151
};
158152

159153
getInternal = (context, actionDescription) => {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from '../generated/constants.js';
77
import { diagnostics, setupContextForFile } from './context.js';
88
import { registeredRules } from './load.js';
9+
import { resetSource, setupSourceForFile } from './source_code.js';
910
import { assertIs, getErrorMessage } from './utils.js';
1011
import { addVisitorToCompiled, compiledVisitor, finalizeCompiledVisitor, initCompiledVisitor } from './visitor.js';
1112

@@ -111,14 +112,17 @@ function lintFileImpl(filePath: string, bufferId: number, buffer: Uint8Array | n
111112

112113
const sourceText = textDecoder.decode(buffer.subarray(0, sourceByteLen));
113114

115+
const hasBOM = false; // TODO: Set this correctly
116+
setupSourceForFile(sourceText, hasBOM);
117+
114118
// Get visitors for this file from all rules
115119
initCompiledVisitor();
116120

117121
for (let i = 0; i < ruleIds.length; i++) {
118122
const ruleId = ruleIds[i],
119123
ruleAndContext = registeredRules[ruleId];
120124
const { rule, context } = ruleAndContext;
121-
setupContextForFile(context, i, filePath, sourceText);
125+
setupContextForFile(context, i, filePath);
122126

123127
let { visitor } = ruleAndContext;
124128
if (visitor === null) {
@@ -175,4 +179,7 @@ function lintFileImpl(filePath: string, bufferId: number, buffer: Uint8Array | n
175179
// Reset array, ready for next file
176180
afterHooks.length = 0;
177181
}
182+
183+
// Reset source, to free memory
184+
resetSource();
178185
}

0 commit comments

Comments
 (0)