Skip to content

Commit 04abeb9

Browse files
committed
perf(linter/plugins): store if rule is fixable as boolean
1 parent 649d6ab commit 04abeb9

File tree

3 files changed

+13
-24
lines changed

3 files changed

+13
-24
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { getFixes } from './fix.js';
22

33
import type { Fix, FixFn } from './fix.ts';
4-
import type { RuleMeta } from './types.ts';
54

65
// Diagnostic in form passed by user to `Context#report()`
76
export interface Diagnostic {
@@ -70,8 +69,8 @@ export interface InternalContext {
7069
filePath: string;
7170
// Options
7271
options: unknown[];
73-
// Rule metadata
74-
meta: RuleMeta;
72+
// `true` if rule can provide fixes (`meta.fixable` in `RuleMeta` is 'code' or 'whitespace')
73+
isFixable: boolean;
7574
}
7675

7776
/**
@@ -88,13 +87,13 @@ export class Context {
8887
* @class
8988
* @param fullRuleName - Rule name, in form `<plugin>/<rule>`
9089
*/
91-
constructor(fullRuleName: string, meta: RuleMeta) {
90+
constructor(fullRuleName: string, isFixable: boolean) {
9291
this.#internal = {
9392
id: fullRuleName,
9493
filePath: '',
9594
ruleIndex: -1,
9695
options: [],
97-
meta,
96+
isFixable,
9897
};
9998
}
10099

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export function getFixes(diagnostic: Diagnostic, internal: InternalContext): Fix
144144

145145
// ESLint does not throw this error if `fix` function returns only falsy values.
146146
// We've already exited if that is the case, so we're reproducing that behavior.
147-
if (internal.meta.fixable === null) {
147+
if (internal.isFixable === false) {
148148
throw new Error('Fixable rules must set the `meta.fixable` property to "code" or "whitespace".');
149149
}
150150

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

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,6 @@ interface PluginDetails {
6868
ruleNames: string[];
6969
}
7070

71-
// Default rule metadata, used if `rule.meta` property is empty.
72-
const emptyRuleMeta: RuleMeta = {
73-
fixable: null,
74-
};
75-
7671
/**
7772
* Load a plugin.
7873
*
@@ -120,27 +115,22 @@ async function loadPluginImpl(path: string): Promise<PluginDetails> {
120115
const ruleName = ruleNames[i],
121116
rule = rules[ruleName];
122117

123-
// Validate `rule.meta` and convert to object with standardized shape
124-
// (all properties defined with default values if not supplied)
118+
// Validate `rule.meta` and convert to vars with standardized shape
119+
let isFixable = false;
125120
let ruleMeta = rule.meta;
126-
if (ruleMeta == null) {
127-
ruleMeta = emptyRuleMeta;
128-
} else {
121+
if (ruleMeta != null) {
129122
if (typeof ruleMeta !== 'object') throw new TypeError('Invalid `meta`');
130123

131-
let { fixable } = ruleMeta;
132-
if (fixable === void 0) {
133-
fixable = null;
134-
} else if (fixable !== null && fixable !== 'code' && fixable !== 'whitespace') {
135-
throw new TypeError('Invalid `meta.fixable`');
124+
const { fixable } = ruleMeta;
125+
if (fixable != null) {
126+
if (fixable !== 'code' && fixable !== 'whitespace') throw new TypeError('Invalid `meta.fixable`');
127+
isFixable = true;
136128
}
137-
138-
ruleMeta = { fixable };
139129
}
140130

141131
// Create `Context` object for rule. This will be re-used for every file.
142132
// It's updated with file-specific data before linting each file with `setupContextForFile`.
143-
const context = new Context(`${pluginName}/${ruleName}`, ruleMeta);
133+
const context = new Context(`${pluginName}/${ruleName}`, isFixable);
144134

145135
let ruleAndContext;
146136
if ('createOnce' in rule) {

0 commit comments

Comments
 (0)