11/**
22 * @author Yosuke Ota <https://github.com/ota-meshi>
33 */
4- "use strict"
54
65//------------------------------------------------------------------------------
76// Requirements
87//------------------------------------------------------------------------------
98
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"
1519
1620//------------------------------------------------------------------------------
1721// Helpers
1822//------------------------------------------------------------------------------
19- const Linter = eslint . Linter
2023
21- const parser = require ( "../src/index.ts" )
22-
23- const noNumberLiteralRule = {
24+ const noNumberLiteralRule : Rule . RuleModule = {
2425 create ( context ) {
2526 let count = 0
2627 return {
@@ -35,7 +36,7 @@ const noNumberLiteralRule = {
3536 }
3637 } ,
3738}
38- const noNoForbiddenKeyRule = {
39+ const noNoForbiddenKeyRule : Rule . RuleModule = {
3940 create ( context ) {
4041 return {
4142 'JSONProperty > JSONLiteral[value="forbidden"]' ( node ) {
@@ -49,7 +50,7 @@ const noNoForbiddenKeyRule = {
4950 }
5051 } ,
5152}
52- const noParsingErrorRule = {
53+ const noParsingErrorRule : Rule . RuleModule = {
5354 create ( context ) {
5455 const parseError = context . getSourceCode ( ) . parserServices . parseError
5556 if ( parseError ) {
@@ -73,8 +74,8 @@ const noParsingErrorRule = {
7374 return { }
7475 } ,
7576}
76- const noParsingErrorRule2 = {
77- create ( context ) {
77+ const noParsingErrorRule2 : Rule . RuleModule = {
78+ create ( context : any ) {
7879 const parseError = context . parserServices . parseError
7980 if ( parseError ) {
8081 let loc = undefined
@@ -97,7 +98,7 @@ const noParsingErrorRule2 = {
9798 return { }
9899 } ,
99100}
100- const noProgramExitRule = {
101+ const noProgramExitRule : Rule . RuleModule = {
101102 create ( context ) {
102103 return {
103104 "Program:exit" ( node ) {
@@ -109,7 +110,7 @@ const noProgramExitRule = {
109110 }
110111 } ,
111112}
112- const siblingSelectorRule = {
113+ const siblingSelectorRule : Rule . RuleModule = {
113114 create ( context ) {
114115 return {
115116 "* ~ *" ( node ) {
@@ -122,27 +123,23 @@ const siblingSelectorRule = {
122123 } ,
123124}
124125
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" ) !
138135
139136 return {
140137 files : [ "**" ] ,
141138 plugins : {
142139 test : {
143140 rules : {
144141 "test-no-number-literal" : {
145- create : ( context ) =>
142+ create : ( context : any ) =>
146143 context . sourceCode . parserServices . defineCustomBlocksVisitor (
147144 context ,
148145 jsonParser ,
@@ -153,7 +150,7 @@ function getConfig(target = "json") {
153150 ) ,
154151 } ,
155152 "test-no-forbidden-key" : {
156- create : ( context ) =>
153+ create : ( context : any ) =>
157154 context . sourceCode . parserServices . defineCustomBlocksVisitor (
158155 context ,
159156 jsonParser ,
@@ -164,7 +161,7 @@ function getConfig(target = "json") {
164161 ) ,
165162 } ,
166163 "test-no-parsing-error" : {
167- create : ( context ) =>
164+ create : ( context : any ) =>
168165 context . sourceCode . parserServices . defineCustomBlocksVisitor (
169166 context ,
170167 jsonParser ,
@@ -175,7 +172,7 @@ function getConfig(target = "json") {
175172 ) ,
176173 } ,
177174 "test-no-parsing-error2" : {
178- create : ( context ) =>
175+ create : ( context : any ) =>
179176 context . sourceCode . parserServices . defineCustomBlocksVisitor (
180177 context ,
181178 jsonParser ,
@@ -186,7 +183,7 @@ function getConfig(target = "json") {
186183 ) ,
187184 } ,
188185 "test-no-program-exit" : {
189- create : ( context ) =>
186+ create : ( context : any ) =>
190187 context . sourceCode . parserServices . defineCustomBlocksVisitor (
191188 context ,
192189 jsonParser ,
@@ -198,7 +195,7 @@ function getConfig(target = "json") {
198195 ) ,
199196 } ,
200197 "test-no-yml-parsing-error" : {
201- create : ( context ) =>
198+ create : ( context : any ) =>
202199 context . sourceCode . parserServices . defineCustomBlocksVisitor (
203200 context ,
204201 {
@@ -214,7 +211,7 @@ function getConfig(target = "json") {
214211 } ,
215212
216213 "test-for-sibling-selector" : {
217- create : ( context ) =>
214+ create : ( context : any ) =>
218215 context . sourceCode . parserServices . defineCustomBlocksVisitor (
219216 context ,
220217 jsonParser ,
@@ -225,15 +222,15 @@ function getConfig(target = "json") {
225222 ) ,
226223 } ,
227224 "test-for-parse-custom-block-element" : {
228- create : ( context ) =>
225+ create : ( context : any ) =>
229226 context . sourceCode . parserServices . defineCustomBlocksVisitor (
230227 context ,
231228 jsonParser ,
232229 {
233230 target : "json" ,
234- create ( ctx ) {
231+ create ( ctx : CustomBlockContext ) {
235232 return {
236- Program ( node ) {
233+ Program ( node : Program ) {
237234 const error =
238235 ctx . sourceCode . parserServices . parseCustomBlockElement (
239236 jsonParser ,
@@ -255,13 +252,15 @@ function getConfig(target = "json") {
255252 ) ,
256253 } ,
257254 "test-mark-vars" : {
258- create ( context ) {
255+ create ( context : any ) {
259256 return context . sourceCode . parserServices . defineCustomBlocksVisitor (
260257 context ,
261258 espree ,
262259 {
263260 target : "js" ,
264- create ( customBlockContext ) {
261+ create (
262+ customBlockContext : CustomBlockContext ,
263+ ) {
265264 return {
266265 Literal ( ) {
267266 customBlockContext . markVariableAsUsed (
@@ -280,13 +279,15 @@ function getConfig(target = "json") {
280279
281280 "test-space-unary-ops" : {
282281 ...spaceUnaryOps ,
283- create ( context ) {
282+ create ( context : any ) {
284283 return context . sourceCode . parserServices . defineCustomBlocksVisitor (
285284 context ,
286285 espree ,
287286 {
288287 target : "js" ,
289- create ( customBlockContext ) {
288+ create (
289+ customBlockContext : Rule . RuleContext ,
290+ ) {
290291 return spaceUnaryOps . create (
291292 customBlockContext ,
292293 )
@@ -297,13 +298,15 @@ function getConfig(target = "json") {
297298 } ,
298299 "test-no-param-reassign" : {
299300 ...noParamReassign ,
300- create ( context ) {
301+ create ( context : any ) {
301302 return context . sourceCode . parserServices . defineCustomBlocksVisitor (
302303 context ,
303304 espree ,
304305 {
305306 target : "js" ,
306- create ( customBlockContext ) {
307+ create (
308+ customBlockContext : Rule . RuleContext ,
309+ ) {
307310 return noParamReassign . create (
308311 customBlockContext ,
309312 )
@@ -314,13 +317,15 @@ function getConfig(target = "json") {
314317 } ,
315318 "test-no-unused-vars" : {
316319 ...noUnusedVars ,
317- create ( context ) {
320+ create ( context : any ) {
318321 return context . sourceCode . parserServices . defineCustomBlocksVisitor (
319322 context ,
320323 espree ,
321324 {
322325 target : "js" ,
323- create ( customBlockContext ) {
326+ create (
327+ customBlockContext : Rule . RuleContext ,
328+ ) {
324329 return noUnusedVars . create (
325330 customBlockContext ,
326331 )
@@ -341,7 +346,7 @@ function getConfig(target = "json") {
341346 "test/test-no-parsing-error" : "error" ,
342347 "test/test-no-parsing-error2" : "error" ,
343348 } ,
344- }
349+ } satisfies Linter . Config
345350}
346351
347352function createLinter ( ) {
@@ -637,19 +642,21 @@ describe("parserServices.defineCustomBlocksVisitor tests", () => {
637642 const linter = createLinter ( )
638643 const baseConfig = getConfig ( )
639644
640- const plugin = {
645+ const plugin : ESLint . Plugin = {
641646 rules : {
642647 test : {
643- create : ( context ) =>
648+ create : ( context : any ) =>
644649 context . sourceCode . parserServices . defineCustomBlocksVisitor (
645650 context ,
646651 jsonParser ,
647652 {
648653 target : "json" ,
649- create ( customBlockContext ) {
654+ create (
655+ customBlockContext : CustomBlockContext ,
656+ ) {
650657 return {
651658 "JSONLiteral[value='target']" (
652- node ,
659+ node : any ,
653660 ) {
654661 customBlockContext . report ( {
655662 node,
@@ -696,19 +703,21 @@ describe("parserServices.defineCustomBlocksVisitor tests", () => {
696703 const linter = createLinter ( )
697704 const baseConfig = getConfig ( )
698705
699- const plugin = {
706+ const plugin : ESLint . Plugin = {
700707 rules : {
701708 test : {
702- create : ( context ) =>
709+ create : ( context : any ) =>
703710 context . sourceCode . parserServices . defineCustomBlocksVisitor (
704711 context ,
705712 jsonParser ,
706713 {
707714 target : "json" ,
708- create ( customBlockContext ) {
715+ create (
716+ customBlockContext : CustomBlockContext ,
717+ ) {
709718 return {
710719 "JSONLiteral[value='target']" (
711- node ,
720+ node : any ,
712721 ) {
713722 customBlockContext . report ( {
714723 node,
0 commit comments