Skip to content

Commit 6957fb9

Browse files
committed
fix(linter/plugins): do not allow access to Context#id in createOnce (#15489)
Revert #15447. It's easy to allow `Context#id` to be accessed in `createOnce` when running rule in Oxlint, but not possible to do in ESLint compat mode with current API, because `id` isn't a property of the rule, so `defineRule` doesn't know it. We could support it by making `definePlugin` compulsory for ESLint interop, and get it to set `id` property on `Context` objects properly. But that's a bit tricky, so just disable access for now. It's probably not very useful anyway - a rule should know what its own name is!
1 parent 7409630 commit 6957fb9

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

apps/oxlint/src-js/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,6 @@ function createContextAndVisitor(rule: CreateOnceRule): {
230230
// But any such bugs should have been caught when testing the rule in Oxlint, so should be OK to take this shortcut.
231231
// `FILE_CONTEXT` prototype provides `cwd` property and `extends` method, which are available in `createOnce`.
232232
const context = ObjectCreate(FILE_CONTEXT, {
233-
// TODO: Need to set `id` to full rule name - it's available in `createOnce`.
234-
// Or make it inaccessible in `createOnce`.
235233
id: { value: '', enumerable: true, configurable: true },
236234
options: { value: null, enumerable: true, configurable: true },
237235
report: { value: null, enumerable: true, configurable: true },

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,12 @@ export function createContext(fullRuleName: string, ruleDetails: RuleDetails): R
352352
// Inherit from `FILE_CONTEXT`, which provides getters for file-specific properties
353353
__proto__: FILE_CONTEXT,
354354
// Rule ID, in form `<plugin>/<rule>`
355-
id: fullRuleName,
355+
get id(): string {
356+
// It's not possible to allow access to `id` in `createOnce` in ESLint compatibility mode, so we don't
357+
// allow it here either. It's probably not very useful anyway - a rule should know what its own name is!
358+
if (filePath === null) throw new Error('Cannot access `context.id` in `createOnce`');
359+
return fullRuleName;
360+
},
356361
// Getter for rule options for this rule on this file
357362
get options(): Readonly<unknown[]> {
358363
if (filePath === null) throw new Error('Cannot access `context.options` in `createOnce`');

apps/oxlint/test/fixtures/createOnce/output.snap.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
: ^
4040
`----
4141
42-
x create-once-plugin(always-run): createOnce: id: create-once-plugin/always-run
42+
x create-once-plugin(always-run): createOnce: id error: Cannot access `context.id` in `createOnce`
4343
,-[files/1.js:1:1]
4444
1 | let x;
4545
: ^
@@ -177,7 +177,7 @@
177177
: ^
178178
`----
179179
180-
x create-once-plugin(always-run): createOnce: id: create-once-plugin/always-run
180+
x create-once-plugin(always-run): createOnce: id error: Cannot access `context.id` in `createOnce`
181181
,-[files/2.js:1:1]
182182
1 | let y;
183183
: ^

apps/oxlint/test/fixtures/createOnce/plugin.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ const alwaysRunRule: Rule = {
2222
// oxlint-disable-next-line typescript-eslint/no-this-alias
2323
const topLevelThis = this;
2424

25-
const { id } = context;
26-
2725
// Check that these APIs throw here
26+
const idError = tryCatch(() => context.id);
2827
const filenameError = tryCatch(() => context.filename);
2928
const physicalFilenameError = tryCatch(() => context.physicalFilename);
3029
const optionsError = tryCatch(() => context.options);
@@ -36,7 +35,7 @@ const alwaysRunRule: Rule = {
3635
before() {
3736
context.report({ message: `createOnce: call count: ${createOnceCallCount}`, node: SPAN });
3837
context.report({ message: `createOnce: this === rule: ${topLevelThis === alwaysRunRule}`, node: SPAN });
39-
context.report({ message: `createOnce: id: ${id}`, node: SPAN });
38+
context.report({ message: `createOnce: id error: ${idError?.message}`, node: SPAN });
4039
context.report({ message: `createOnce: filename error: ${filenameError?.message}`, node: SPAN });
4140
context.report({
4241
message: `createOnce: physicalFilename error: ${physicalFilenameError?.message}`,

0 commit comments

Comments
 (0)