Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion apps/oxlint/src-js/plugins/context.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { RuleMeta } from './types.ts';

// Diagnostic in form passed by user to `Context#report()`
interface Diagnostic {
message: string;
Expand Down Expand Up @@ -64,6 +66,8 @@ interface InternalContext {
filePath: string;
// Options
options: unknown[];
// Rule metadata
meta: RuleMeta;
}

/**
Expand All @@ -80,12 +84,13 @@ export class Context {
* @class
* @param fullRuleName - Rule name, in form `<plugin>/<rule>`
*/
constructor(fullRuleName: string) {
constructor(fullRuleName: string, meta: RuleMeta) {
this.#internal = {
id: fullRuleName,
filePath: '',
ruleIndex: -1,
options: [],
meta,
};
}

Expand Down
22 changes: 20 additions & 2 deletions apps/oxlint/src-js/plugins/load.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Context } from './context.js';
import { getErrorMessage } from './utils.js';

import type { AfterHook, BeforeHook, Visitor, VisitorWithHooks } from './types.ts';
import type { AfterHook, BeforeHook, RuleMeta, Visitor, VisitorWithHooks } from './types.ts';

const ObjectKeys = Object.keys;

Expand All @@ -21,10 +21,12 @@ export interface Plugin {
export type Rule = CreateRule | CreateOnceRule;

interface CreateRule {
meta?: RuleMeta;
create: (context: Context) => Visitor;
}

export interface CreateOnceRule {
meta?: RuleMeta;
create?: (context: Context) => Visitor;
createOnce: (context: Context) => VisitorWithHooks;
}
Expand Down Expand Up @@ -66,6 +68,9 @@ interface PluginDetails {
ruleNames: string[];
}

// Default rule metadata, used if `rule.meta` property is empty.
const emptyRuleMeta: RuleMeta = {};

/**
* Load a plugin.
*
Expand Down Expand Up @@ -113,7 +118,20 @@ async function loadPluginImpl(path: string): Promise<PluginDetails> {
const ruleName = ruleNames[i],
rule = rules[ruleName];

const context = new Context(`${pluginName}/${ruleName}`);
// Validate `rule.meta` and convert to object with standardized shape
// (all properties defined with default values if not supplied)
let ruleMeta = rule.meta;
if (ruleMeta == null) {
ruleMeta = emptyRuleMeta;
} else {
if (typeof ruleMeta !== 'object') throw new TypeError('Invalid `meta`');
// TODO: Validate and conform individual properties of `meta` once they're supported
ruleMeta = emptyRuleMeta;
}

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

let ruleAndContext;
if ('createOnce' in rule) {
Expand Down
6 changes: 6 additions & 0 deletions apps/oxlint/src-js/plugins/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ export interface EnterExit {
enter: VisitFn | null;
exit: VisitFn | null;
}

// Rule metadata.
// TODO: Fill in all properties.
export interface RuleMeta {
[key: string]: unknown;
}
Loading