From 981cbc59c9e11e8e73a478b208174ff09856efe6 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Fri, 7 Nov 2025 19:43:56 +0000 Subject: [PATCH] refactor(linter/plugins): allow accessing `context.id` in `createOnce` (#15447) Previously we blocked accessing `context.id` (rule name) in `createOnce`. There's no need to be so restrictive, as it's a static value, and not file-specific. Relax this restriction. --- apps/oxlint/src-js/plugins/context.ts | 3 ++- apps/oxlint/test/fixtures/createOnce/output.snap.md | 4 ++-- apps/oxlint/test/fixtures/createOnce/plugin.ts | 5 +++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/apps/oxlint/src-js/plugins/context.ts b/apps/oxlint/src-js/plugins/context.ts index e6718b66774bc..82ff4cafc9119 100644 --- a/apps/oxlint/src-js/plugins/context.ts +++ b/apps/oxlint/src-js/plugins/context.ts @@ -118,7 +118,8 @@ export class Context { // Getter for full rule name, in form `/` get id() { - return getInternal(this, 'access `context.id`').id; + // Note: We can allow accessing `id` in `createOnce`, as it's not file-specific. So skip `getInternal` call. + return this.#internal.id; } // Getter for absolute path of file being linted. diff --git a/apps/oxlint/test/fixtures/createOnce/output.snap.md b/apps/oxlint/test/fixtures/createOnce/output.snap.md index d74d925b0e015..d3e4fb2235e63 100644 --- a/apps/oxlint/test/fixtures/createOnce/output.snap.md +++ b/apps/oxlint/test/fixtures/createOnce/output.snap.md @@ -39,7 +39,7 @@ : ^ `---- - x create-once-plugin(always-run): createOnce: id: Cannot access `context.id` in `createOnce` + x create-once-plugin(always-run): createOnce: id: create-once-plugin/always-run ,-[files/1.js:1:1] 1 | let x; : ^ @@ -177,7 +177,7 @@ : ^ `---- - x create-once-plugin(always-run): createOnce: id: Cannot access `context.id` in `createOnce` + x create-once-plugin(always-run): createOnce: id: create-once-plugin/always-run ,-[files/2.js:1:1] 1 | let y; : ^ diff --git a/apps/oxlint/test/fixtures/createOnce/plugin.ts b/apps/oxlint/test/fixtures/createOnce/plugin.ts index b6ccdedf4908d..773834f735b5b 100644 --- a/apps/oxlint/test/fixtures/createOnce/plugin.ts +++ b/apps/oxlint/test/fixtures/createOnce/plugin.ts @@ -22,8 +22,9 @@ const alwaysRunRule: Rule = { // oxlint-disable-next-line typescript-eslint/no-this-alias const topLevelThis = this; + const { id } = context; + // Check that these APIs throw here - const idError = tryCatch(() => context.id); const filenameError = tryCatch(() => context.filename); const physicalFilenameError = tryCatch(() => context.physicalFilename); const optionsError = tryCatch(() => context.options); @@ -35,7 +36,7 @@ const alwaysRunRule: Rule = { before() { context.report({ message: `createOnce: call count: ${createOnceCallCount}`, node: SPAN }); context.report({ message: `createOnce: this === rule: ${topLevelThis === alwaysRunRule}`, node: SPAN }); - context.report({ message: `createOnce: id: ${idError?.message}`, node: SPAN }); + context.report({ message: `createOnce: id: ${id}`, node: SPAN }); context.report({ message: `createOnce: filename: ${filenameError?.message}`, node: SPAN }); context.report({ message: `createOnce: physicalFilename: ${physicalFilenameError?.message}`, node: SPAN }); context.report({ message: `createOnce: options: ${optionsError?.message}`, node: SPAN });