1- import type { RuleTester } from 'oxlint/plugins-dev' ;
1+ import { definePlugin , defineRule } from '@oxlint/plugins' ;
2+ import type { Context , ESTree } from '@oxlint/plugins' ;
23
34import {
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
171158export default plugin ;
172159export { rewriteVitePlusImportSpecifier } ;
0 commit comments