Skip to content

Commit d271435

Browse files
committed
refactor oxlint plugin typing
1 parent a6cc1bf commit d271435

1 file changed

Lines changed: 23 additions & 26 deletions

File tree

packages/cli/src/oxlint-plugin.ts

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1+
import type { RuleTester } from 'oxlint/plugins-dev';
2+
13
import {
24
PREFER_VITE_PLUS_IMPORTS_RULE_NAME,
35
VITE_PLUS_OXLINT_PLUGIN_NAME,
46
} from './oxlint-plugin-config.ts';
57

6-
interface StringLiteralLike {
7-
raw: string | null;
8-
type: 'Literal';
9-
value: string;
10-
}
11-
12-
interface ReportContext {
13-
report(diagnostic: {
14-
data: Record<string, string>;
15-
fix: (fixer: Fixer) => unknown;
16-
messageId: 'preferVitePlusImports';
17-
node: StringLiteralLike;
18-
}): void;
19-
}
8+
type OxlintRule = Parameters<RuleTester['run']>[1];
9+
type CreateFn = Exclude<OxlintRule['create'], undefined>;
10+
type OxlintVisitor = ReturnType<CreateFn>;
11+
type ReportContext = Parameters<CreateFn>[0];
12+
type VisitorNode<K extends keyof OxlintVisitor> = Parameters<NonNullable<OxlintVisitor[K]>>[0];
2013

21-
interface Fixer {
22-
replaceText(node: unknown, text: string): unknown;
14+
type StringLiteralLike = VisitorNode<'ImportDeclaration'>['source'];
15+
interface OxlintPlugin {
16+
meta: {
17+
name: string;
18+
};
19+
rules: Record<string, OxlintRule>;
2320
}
2421

2522
function isStringLiteralLike(value: unknown): value is StringLiteralLike {
@@ -116,7 +113,7 @@ function maybeReportLiteral(context: ReportContext, literal: StringLiteralLike |
116113
});
117114
}
118115

119-
export const preferVitePlusImportsRule = {
116+
export const preferVitePlusImportsRule: OxlintRule = {
120117
meta: {
121118
type: 'problem',
122119
docs: {
@@ -129,30 +126,30 @@ export const preferVitePlusImportsRule = {
129126
preferVitePlusImports: "Use '{{to}}' instead of '{{from}}' in Vite+ projects.",
130127
},
131128
},
132-
create(context: any) {
129+
create(context: ReportContext) {
133130
return {
134-
ImportDeclaration(node: { source: StringLiteralLike }) {
131+
ImportDeclaration(node: VisitorNode<'ImportDeclaration'>) {
135132
maybeReportLiteral(context, node.source);
136133
},
137-
ExportAllDeclaration(node: { source: StringLiteralLike }) {
134+
ExportAllDeclaration(node: VisitorNode<'ExportAllDeclaration'>) {
138135
maybeReportLiteral(context, node.source);
139136
},
140-
ExportNamedDeclaration(node: { source: StringLiteralLike | null }) {
137+
ExportNamedDeclaration(node: VisitorNode<'ExportNamedDeclaration'>) {
141138
maybeReportLiteral(context, node.source);
142139
},
143-
ImportExpression(node: { source: unknown }) {
140+
ImportExpression(node: VisitorNode<'ImportExpression'>) {
144141
if (!isStringLiteralLike(node.source)) {
145142
return;
146143
}
147144
maybeReportLiteral(context, node.source);
148145
},
149-
TSImportType(node: { source: StringLiteralLike }) {
146+
TSImportType(node: VisitorNode<'TSImportType'>) {
150147
maybeReportLiteral(context, node.source);
151148
},
152-
TSExternalModuleReference(node: { expression: StringLiteralLike }) {
149+
TSExternalModuleReference(node: VisitorNode<'TSExternalModuleReference'>) {
153150
maybeReportLiteral(context, node.expression);
154151
},
155-
TSModuleDeclaration(node: { global?: boolean; id: StringLiteralLike | { type: string } }) {
152+
TSModuleDeclaration(node: VisitorNode<'TSModuleDeclaration'>) {
156153
if (node.global || !isStringLiteralLike(node.id)) {
157154
return;
158155
}
@@ -162,7 +159,7 @@ export const preferVitePlusImportsRule = {
162159
},
163160
};
164161

165-
const plugin = {
162+
const plugin: OxlintPlugin = {
166163
meta: {
167164
name: VITE_PLUS_OXLINT_PLUGIN_NAME,
168165
},

0 commit comments

Comments
 (0)