Skip to content

Commit 57897c7

Browse files
committed
fix: use oxlint plugin helpers
1 parent a0e94c9 commit 57897c7

File tree

4 files changed

+31
-30
lines changed

4 files changed

+31
-30
lines changed

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@
333333
},
334334
"dependencies": {
335335
"@oxc-project/types": "catalog:",
336+
"@oxlint/plugins": "catalog:",
336337
"@voidzero-dev/vite-plus-core": "workspace:*",
337338
"@voidzero-dev/vite-plus-test": "workspace:*",
338339
"oxfmt": "catalog:",

packages/cli/src/oxlint-plugin.ts

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
1-
import type { RuleTester } from 'oxlint/plugins-dev';
1+
import { definePlugin, defineRule } from '@oxlint/plugins';
2+
import type { Context, ESTree } from '@oxlint/plugins';
23

34
import {
45
PREFER_VITE_PLUS_IMPORTS_RULE_NAME,
56
VITE_PLUS_OXLINT_PLUGIN_NAME,
67
} from './oxlint-plugin-config.ts';
78

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];
13-
14-
type StringLiteralLike = VisitorNode<'ImportDeclaration'>['source'];
15-
interface OxlintPlugin {
16-
meta: {
17-
name: string;
18-
};
19-
rules: Record<string, OxlintRule>;
20-
}
21-
22-
function isStringLiteralLike(value: unknown): value is StringLiteralLike {
9+
function isStringLiteralLike(value: unknown): value is ESTree.StringLiteral {
2310
return (
2411
typeof value === 'object' &&
2512
value !== null &&
@@ -85,12 +72,12 @@ function rewriteVitePlusImportSpecifier(specifier: string): string | null {
8572
return null;
8673
}
8774

88-
function quoteSpecifier(literal: StringLiteralLike, replacement: string): string {
75+
function quoteSpecifier(literal: ESTree.StringLiteral, replacement: string): string {
8976
const quote = literal.raw?.startsWith("'") ? "'" : '"';
9077
return `${quote}${replacement}${quote}`;
9178
}
9279

93-
function maybeReportLiteral(context: ReportContext, literal: StringLiteralLike | null | undefined) {
80+
function maybeReportLiteral(context: Context, literal: ESTree.StringLiteral | null | undefined) {
9481
if (!literal || typeof literal.value !== 'string') {
9582
return;
9683
}
@@ -113,7 +100,7 @@ function maybeReportLiteral(context: ReportContext, literal: StringLiteralLike |
113100
});
114101
}
115102

116-
export const preferVitePlusImportsRule: OxlintRule = {
103+
export const preferVitePlusImportsRule = defineRule({
117104
meta: {
118105
type: 'problem',
119106
docs: {
@@ -126,47 +113,47 @@ export const preferVitePlusImportsRule: OxlintRule = {
126113
preferVitePlusImports: "Use '{{to}}' instead of '{{from}}' in Vite+ projects.",
127114
},
128115
},
129-
createOnce(context: ReportContext) {
116+
createOnce(context: Context) {
130117
return {
131-
ImportDeclaration(node: VisitorNode<'ImportDeclaration'>) {
118+
ImportDeclaration(node) {
132119
maybeReportLiteral(context, node.source);
133120
},
134-
ExportAllDeclaration(node: VisitorNode<'ExportAllDeclaration'>) {
121+
ExportAllDeclaration(node) {
135122
maybeReportLiteral(context, node.source);
136123
},
137-
ExportNamedDeclaration(node: VisitorNode<'ExportNamedDeclaration'>) {
124+
ExportNamedDeclaration(node) {
138125
maybeReportLiteral(context, node.source);
139126
},
140-
ImportExpression(node: VisitorNode<'ImportExpression'>) {
127+
ImportExpression(node) {
141128
if (!isStringLiteralLike(node.source)) {
142129
return;
143130
}
144131
maybeReportLiteral(context, node.source);
145132
},
146-
TSImportType(node: VisitorNode<'TSImportType'>) {
133+
TSImportType(node) {
147134
maybeReportLiteral(context, node.source);
148135
},
149-
TSExternalModuleReference(node: VisitorNode<'TSExternalModuleReference'>) {
136+
TSExternalModuleReference(node) {
150137
maybeReportLiteral(context, node.expression);
151138
},
152-
TSModuleDeclaration(node: VisitorNode<'TSModuleDeclaration'>) {
139+
TSModuleDeclaration(node) {
153140
if (node.global || !isStringLiteralLike(node.id)) {
154141
return;
155142
}
156143
maybeReportLiteral(context, node.id);
157144
},
158145
};
159146
},
160-
};
147+
});
161148

162-
const plugin: OxlintPlugin = {
149+
const plugin = definePlugin({
163150
meta: {
164151
name: VITE_PLUS_OXLINT_PLUGIN_NAME,
165152
},
166153
rules: {
167154
[PREFER_VITE_PLUS_IMPORTS_RULE_NAME]: preferVitePlusImportsRule,
168155
},
169-
};
156+
});
170157

171158
export default plugin;
172159
export { rewriteVitePlusImportSpecifier };

pnpm-lock.yaml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ catalog:
1919
'@oxc-node/core': ^0.1.0
2020
'@oxc-project/runtime': =0.126.0
2121
'@oxc-project/types': =0.126.0
22+
'@oxlint/plugins': =1.60.0
2223
'@pnpm/find-workspace-packages': ^6.0.9
2324
'@rollup/plugin-commonjs': ^29.0.0
2425
'@rollup/plugin-json': ^6.1.0

0 commit comments

Comments
 (0)