1
1
/**
2
2
* @author Yosuke Ota <https://github.com/ota-meshi>
3
3
*/
4
- "use strict"
5
4
6
5
//------------------------------------------------------------------------------
7
6
// Requirements
8
7
//------------------------------------------------------------------------------
9
8
10
- const assert = require ( "assert" )
11
- const path = require ( "path" )
12
- const eslint = require ( "eslint" )
13
- const jsonParser = require ( "jsonc-eslint-parser" )
14
- const espree = require ( "espree" )
9
+ import type { ESLint , Rule } from "eslint"
10
+ import type { VElement } from "../src/ast"
11
+ import type { CustomBlockContext } from "../src/sfc/custom-block"
12
+ import { assert , describe , it } from "vitest"
13
+ import { Linter } from "eslint"
14
+ import { builtinRules } from "eslint/use-at-your-own-risk"
15
+ import jsonParser from "jsonc-eslint-parser"
16
+ import espree from "espree"
17
+ import * as parser from "../src"
18
+ import type { Program } from "estree"
15
19
16
20
//------------------------------------------------------------------------------
17
21
// Helpers
18
22
//------------------------------------------------------------------------------
19
- const Linter = eslint . Linter
20
23
21
- const parser = require ( "../src/index.ts" )
22
-
23
- const noNumberLiteralRule = {
24
+ const noNumberLiteralRule : Rule . RuleModule = {
24
25
create ( context ) {
25
26
let count = 0
26
27
return {
@@ -35,7 +36,7 @@ const noNumberLiteralRule = {
35
36
}
36
37
} ,
37
38
}
38
- const noNoForbiddenKeyRule = {
39
+ const noNoForbiddenKeyRule : Rule . RuleModule = {
39
40
create ( context ) {
40
41
return {
41
42
'JSONProperty > JSONLiteral[value="forbidden"]' ( node ) {
@@ -49,7 +50,7 @@ const noNoForbiddenKeyRule = {
49
50
}
50
51
} ,
51
52
}
52
- const noParsingErrorRule = {
53
+ const noParsingErrorRule : Rule . RuleModule = {
53
54
create ( context ) {
54
55
const parseError = context . getSourceCode ( ) . parserServices . parseError
55
56
if ( parseError ) {
@@ -73,8 +74,8 @@ const noParsingErrorRule = {
73
74
return { }
74
75
} ,
75
76
}
76
- const noParsingErrorRule2 = {
77
- create ( context ) {
77
+ const noParsingErrorRule2 : Rule . RuleModule = {
78
+ create ( context : any ) {
78
79
const parseError = context . parserServices . parseError
79
80
if ( parseError ) {
80
81
let loc = undefined
@@ -97,7 +98,7 @@ const noParsingErrorRule2 = {
97
98
return { }
98
99
} ,
99
100
}
100
- const noProgramExitRule = {
101
+ const noProgramExitRule : Rule . RuleModule = {
101
102
create ( context ) {
102
103
return {
103
104
"Program:exit" ( node ) {
@@ -109,7 +110,7 @@ const noProgramExitRule = {
109
110
}
110
111
} ,
111
112
}
112
- const siblingSelectorRule = {
113
+ const siblingSelectorRule : Rule . RuleModule = {
113
114
create ( context ) {
114
115
return {
115
116
"* ~ *" ( node ) {
@@ -122,27 +123,23 @@ const siblingSelectorRule = {
122
123
} ,
123
124
}
124
125
125
- function getConfig ( target = "json" ) {
126
- const spaceUnaryOps =
127
- require ( "eslint/use-at-your-own-risk" ) . builtinRules . get (
128
- "space-unary-ops" ,
129
- )
130
- const noParamReassign =
131
- require ( "eslint/use-at-your-own-risk" ) . builtinRules . get (
132
- "no-param-reassign" ,
133
- )
134
- const noUnusedVars =
135
- require ( "eslint/use-at-your-own-risk" ) . builtinRules . get (
136
- "no-unused-vars" ,
137
- )
126
+ function getConfig (
127
+ target :
128
+ | string
129
+ | string [ ]
130
+ | ( ( lang : string | null , customBlock : VElement ) => boolean ) = "json" ,
131
+ ) : Linter . Config {
132
+ const spaceUnaryOps = builtinRules . get ( "space-unary-ops" ) !
133
+ const noParamReassign = builtinRules . get ( "no-param-reassign" ) !
134
+ const noUnusedVars = builtinRules . get ( "no-unused-vars" ) !
138
135
139
136
return {
140
137
files : [ "**" ] ,
141
138
plugins : {
142
139
test : {
143
140
rules : {
144
141
"test-no-number-literal" : {
145
- create : ( context ) =>
142
+ create : ( context : any ) =>
146
143
context . sourceCode . parserServices . defineCustomBlocksVisitor (
147
144
context ,
148
145
jsonParser ,
@@ -153,7 +150,7 @@ function getConfig(target = "json") {
153
150
) ,
154
151
} ,
155
152
"test-no-forbidden-key" : {
156
- create : ( context ) =>
153
+ create : ( context : any ) =>
157
154
context . sourceCode . parserServices . defineCustomBlocksVisitor (
158
155
context ,
159
156
jsonParser ,
@@ -164,7 +161,7 @@ function getConfig(target = "json") {
164
161
) ,
165
162
} ,
166
163
"test-no-parsing-error" : {
167
- create : ( context ) =>
164
+ create : ( context : any ) =>
168
165
context . sourceCode . parserServices . defineCustomBlocksVisitor (
169
166
context ,
170
167
jsonParser ,
@@ -175,7 +172,7 @@ function getConfig(target = "json") {
175
172
) ,
176
173
} ,
177
174
"test-no-parsing-error2" : {
178
- create : ( context ) =>
175
+ create : ( context : any ) =>
179
176
context . sourceCode . parserServices . defineCustomBlocksVisitor (
180
177
context ,
181
178
jsonParser ,
@@ -186,7 +183,7 @@ function getConfig(target = "json") {
186
183
) ,
187
184
} ,
188
185
"test-no-program-exit" : {
189
- create : ( context ) =>
186
+ create : ( context : any ) =>
190
187
context . sourceCode . parserServices . defineCustomBlocksVisitor (
191
188
context ,
192
189
jsonParser ,
@@ -198,7 +195,7 @@ function getConfig(target = "json") {
198
195
) ,
199
196
} ,
200
197
"test-no-yml-parsing-error" : {
201
- create : ( context ) =>
198
+ create : ( context : any ) =>
202
199
context . sourceCode . parserServices . defineCustomBlocksVisitor (
203
200
context ,
204
201
{
@@ -214,7 +211,7 @@ function getConfig(target = "json") {
214
211
} ,
215
212
216
213
"test-for-sibling-selector" : {
217
- create : ( context ) =>
214
+ create : ( context : any ) =>
218
215
context . sourceCode . parserServices . defineCustomBlocksVisitor (
219
216
context ,
220
217
jsonParser ,
@@ -225,15 +222,15 @@ function getConfig(target = "json") {
225
222
) ,
226
223
} ,
227
224
"test-for-parse-custom-block-element" : {
228
- create : ( context ) =>
225
+ create : ( context : any ) =>
229
226
context . sourceCode . parserServices . defineCustomBlocksVisitor (
230
227
context ,
231
228
jsonParser ,
232
229
{
233
230
target : "json" ,
234
- create ( ctx ) {
231
+ create ( ctx : CustomBlockContext ) {
235
232
return {
236
- Program ( node ) {
233
+ Program ( node : Program ) {
237
234
const error =
238
235
ctx . sourceCode . parserServices . parseCustomBlockElement (
239
236
jsonParser ,
@@ -255,13 +252,15 @@ function getConfig(target = "json") {
255
252
) ,
256
253
} ,
257
254
"test-mark-vars" : {
258
- create ( context ) {
255
+ create ( context : any ) {
259
256
return context . sourceCode . parserServices . defineCustomBlocksVisitor (
260
257
context ,
261
258
espree ,
262
259
{
263
260
target : "js" ,
264
- create ( customBlockContext ) {
261
+ create (
262
+ customBlockContext : CustomBlockContext ,
263
+ ) {
265
264
return {
266
265
Literal ( ) {
267
266
customBlockContext . markVariableAsUsed (
@@ -280,13 +279,15 @@ function getConfig(target = "json") {
280
279
281
280
"test-space-unary-ops" : {
282
281
...spaceUnaryOps ,
283
- create ( context ) {
282
+ create ( context : any ) {
284
283
return context . sourceCode . parserServices . defineCustomBlocksVisitor (
285
284
context ,
286
285
espree ,
287
286
{
288
287
target : "js" ,
289
- create ( customBlockContext ) {
288
+ create (
289
+ customBlockContext : Rule . RuleContext ,
290
+ ) {
290
291
return spaceUnaryOps . create (
291
292
customBlockContext ,
292
293
)
@@ -297,13 +298,15 @@ function getConfig(target = "json") {
297
298
} ,
298
299
"test-no-param-reassign" : {
299
300
...noParamReassign ,
300
- create ( context ) {
301
+ create ( context : any ) {
301
302
return context . sourceCode . parserServices . defineCustomBlocksVisitor (
302
303
context ,
303
304
espree ,
304
305
{
305
306
target : "js" ,
306
- create ( customBlockContext ) {
307
+ create (
308
+ customBlockContext : Rule . RuleContext ,
309
+ ) {
307
310
return noParamReassign . create (
308
311
customBlockContext ,
309
312
)
@@ -314,13 +317,15 @@ function getConfig(target = "json") {
314
317
} ,
315
318
"test-no-unused-vars" : {
316
319
...noUnusedVars ,
317
- create ( context ) {
320
+ create ( context : any ) {
318
321
return context . sourceCode . parserServices . defineCustomBlocksVisitor (
319
322
context ,
320
323
espree ,
321
324
{
322
325
target : "js" ,
323
- create ( customBlockContext ) {
326
+ create (
327
+ customBlockContext : Rule . RuleContext ,
328
+ ) {
324
329
return noUnusedVars . create (
325
330
customBlockContext ,
326
331
)
@@ -341,7 +346,7 @@ function getConfig(target = "json") {
341
346
"test/test-no-parsing-error" : "error" ,
342
347
"test/test-no-parsing-error2" : "error" ,
343
348
} ,
344
- }
349
+ } satisfies Linter . Config
345
350
}
346
351
347
352
function createLinter ( ) {
@@ -637,19 +642,21 @@ describe("parserServices.defineCustomBlocksVisitor tests", () => {
637
642
const linter = createLinter ( )
638
643
const baseConfig = getConfig ( )
639
644
640
- const plugin = {
645
+ const plugin : ESLint . Plugin = {
641
646
rules : {
642
647
test : {
643
- create : ( context ) =>
648
+ create : ( context : any ) =>
644
649
context . sourceCode . parserServices . defineCustomBlocksVisitor (
645
650
context ,
646
651
jsonParser ,
647
652
{
648
653
target : "json" ,
649
- create ( customBlockContext ) {
654
+ create (
655
+ customBlockContext : CustomBlockContext ,
656
+ ) {
650
657
return {
651
658
"JSONLiteral[value='target']" (
652
- node ,
659
+ node : any ,
653
660
) {
654
661
customBlockContext . report ( {
655
662
node,
@@ -696,19 +703,21 @@ describe("parserServices.defineCustomBlocksVisitor tests", () => {
696
703
const linter = createLinter ( )
697
704
const baseConfig = getConfig ( )
698
705
699
- const plugin = {
706
+ const plugin : ESLint . Plugin = {
700
707
rules : {
701
708
test : {
702
- create : ( context ) =>
709
+ create : ( context : any ) =>
703
710
context . sourceCode . parserServices . defineCustomBlocksVisitor (
704
711
context ,
705
712
jsonParser ,
706
713
{
707
714
target : "json" ,
708
- create ( customBlockContext ) {
715
+ create (
716
+ customBlockContext : CustomBlockContext ,
717
+ ) {
709
718
return {
710
719
"JSONLiteral[value='target']" (
711
- node ,
720
+ node : any ,
712
721
) {
713
722
customBlockContext . report ( {
714
723
node,
0 commit comments