Skip to content

Commit 444ebfd

Browse files
committed
perf(linter/plugins): use single object for parserServices (#15378)
Follow-on after #15364. Re-use a single object for `parserServices`, instead of creating a new object each time. This is better for perf, and also matches ESLint where each access to `sourceCode.parserServices` in a `create` invocation results in the same object.
1 parent fcb32ff commit 444ebfd

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ const buffers: (BufferWithArrays | null)[] = [];
2525
// Array of `after` hooks to run after traversal. This array reused for every file.
2626
const afterHooks: AfterHook[] = [];
2727

28+
// Default parser services object (empty object).
29+
const PARSER_SERVICES_DEFAULT: Record<string, unknown> = Object.freeze({});
30+
2831
/**
2932
* Run rules on a file.
3033
*
@@ -108,7 +111,8 @@ function lintFileImpl(
108111
// But... source text and AST can be accessed in body of `create` method, or `before` hook, via `context.sourceCode`.
109112
// So we pass the buffer to source code module here, so it can decode source text / deserialize AST on demand.
110113
const hasBOM = false; // TODO: Set this correctly
111-
setupSourceForFile(buffer, hasBOM);
114+
const parserServices = PARSER_SERVICES_DEFAULT; // TODO: Set this correctly
115+
setupSourceForFile(buffer, hasBOM, parserServices);
112116

113117
// Get visitors for this file from all rules
114118
initCompiledVisitor();

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,23 @@ export let sourceText: string | null = null;
4040
let sourceByteLen: number = 0;
4141
export let ast: Program | null = null;
4242

43+
// Parser services object. Set before linting a file by `setupSourceForFile`.
44+
let parserServices: Record<string, unknown> | null = null;
45+
4346
/**
4447
* Set up source for the file about to be linted.
4548
* @param bufferInput - Buffer containing AST
4649
* @param hasBOMInput - `true` if file's original source text has Unicode BOM
50+
* @param parserServicesInput - Parser services object for the file
4751
*/
48-
export function setupSourceForFile(bufferInput: BufferWithArrays, hasBOMInput: boolean): void {
52+
export function setupSourceForFile(
53+
bufferInput: BufferWithArrays,
54+
hasBOMInput: boolean,
55+
parserServicesInput: Record<string, unknown>,
56+
): void {
4957
buffer = bufferInput;
5058
hasBOM = hasBOMInput;
59+
parserServices = parserServicesInput;
5160
}
5261

5362
/**
@@ -82,6 +91,7 @@ export function resetSourceAndAst(): void {
8291
buffer = null;
8392
sourceText = null;
8493
ast = null;
94+
parserServices = null;
8595
resetBuffer();
8696
resetLines();
8797
resetScopeManager();
@@ -126,8 +136,8 @@ export const SOURCE_CODE = Object.freeze({
126136
},
127137

128138
// Get parser services for the file.
129-
get parserServices(): { [key: string]: unknown } {
130-
return {}; // TODO
139+
get parserServices(): Record<string, unknown> {
140+
return parserServices;
131141
},
132142

133143
// Get source text as array of lines, split according to specification's definition of line breaks.

0 commit comments

Comments
 (0)