@@ -5,41 +5,17 @@ import type { AST } from 'vue-eslint-parser'
55import { createRule } from './_'
66
77export default createRule < [ { prefix : string , enableFix : boolean } ] , 'missing' > ( {
8- name : 'enforce-class-compile' ,
9- meta : {
10- type : 'problem' ,
11- fixable : 'code' ,
12- docs : {
13- description : 'Enforce class compilation' ,
14- } ,
15- messages : {
16- missing : 'prefix: `{{prefix}}` is missing' ,
17- } ,
18- schema : [ {
19- type : 'object' ,
20- properties : {
21- prefix : {
22- type : 'string' ,
23- } ,
24- enableFix : {
25- type : 'boolean' ,
26- } ,
27- } ,
28- additionalProperties : false ,
29- } ] ,
30- } ,
31- defaultOptions : [ { prefix : ':uno:' , enableFix : true } ] ,
328 create ( context , [ mergedOptions ] ) {
339 const CLASS_COMPILE_PREFIX = `${ mergedOptions . prefix } `
3410 const ENABLE_FIX = mergedOptions . enableFix
3511
36- function report ( { node , fix } : { node : AST . VNode | AST . ESLintNode , fix : ReportFixFunction } ) {
12+ function report ( { fix , node } : { node : AST . VNode | AST . ESLintNode , fix : ReportFixFunction } ) {
3713 context . report ( {
38- node : node as unknown as TSESTree . Node ,
39- loc : node . loc ,
40- messageId : 'missing' ,
4114 data : { prefix : CLASS_COMPILE_PREFIX . trim ( ) } ,
4215 fix : ( ...args ) => ENABLE_FIX ? fix ( ...args ) : null ,
16+ loc : node . loc ,
17+ messageId : 'missing' ,
18+ node : node as unknown as TSESTree . Node ,
4319 } )
4420 }
4521
@@ -57,21 +33,14 @@ export default createRule<[{ prefix: string, enableFix: boolean }], 'missing'>({
5733 return
5834
5935 report ( {
60- node,
6136 fix ( fixer ) {
6237 return fixer . replaceTextRange ( [ node . range [ 0 ] + 1 , node . range [ 1 ] - 1 ] , `${ CLASS_COMPILE_PREFIX } ${ classList } ` )
6338 } ,
39+ node,
6440 } )
6541 }
6642
6743 const templateBodyVisitor : RuleListener = {
68- [ `VAttribute[key.name=class]` ] ( attr : AST . VAttribute ) {
69- const valueNode = attr . value
70- if ( ! valueNode || ! valueNode . value )
71- return
72-
73- reportClassList ( valueNode , valueNode . value )
74- } ,
7544 [ `VAttribute[key.argument.name=class] VExpressionContainer Literal:not(ConditionalExpression .test Literal):not(Property .value Literal)` ] (
7645 literal : AST . ESLintStringLiteral ,
7746 ) {
@@ -80,14 +49,6 @@ export default createRule<[{ prefix: string, enableFix: boolean }], 'missing'>({
8049
8150 reportClassList ( literal , literal . value )
8251 } ,
83- [ `VAttribute[key.argument.name=class] VExpressionContainer TemplateElement` ] (
84- templateElement : AST . ESLintTemplateElement ,
85- ) {
86- if ( ! templateElement . value . raw )
87- return
88-
89- reportClassList ( templateElement , templateElement . value . raw )
90- } ,
9152 [ `VAttribute[key.argument.name=class] VExpressionContainer Property` ] (
9253 property : AST . ESLintProperty ,
9354 ) {
@@ -99,7 +60,6 @@ export default createRule<[{ prefix: string, enableFix: boolean }], 'missing'>({
9960 return
10061
10162 report ( {
102- node : property . key ,
10363 fix ( fixer ) {
10464 let replacePropertyKeyText = `'${ CLASS_COMPILE_PREFIX } ${ classListString } '`
10565
@@ -108,8 +68,24 @@ export default createRule<[{ prefix: string, enableFix: boolean }], 'missing'>({
10868
10969 return fixer . replaceTextRange ( property . key . range , replacePropertyKeyText )
11070 } ,
71+ node : property . key ,
11172 } )
11273 } ,
74+ [ `VAttribute[key.argument.name=class] VExpressionContainer TemplateElement` ] (
75+ templateElement : AST . ESLintTemplateElement ,
76+ ) {
77+ if ( ! templateElement . value . raw )
78+ return
79+
80+ reportClassList ( templateElement , templateElement . value . raw )
81+ } ,
82+ [ `VAttribute[key.name=class]` ] ( attr : AST . VAttribute ) {
83+ const valueNode = attr . value
84+ if ( ! valueNode || ! valueNode . value )
85+ return
86+
87+ reportClassList ( valueNode , valueNode . value )
88+ } ,
11389 }
11490
11591 const parserServices = context ?. sourceCode . parserServices || context . parserServices
@@ -123,4 +99,28 @@ export default createRule<[{ prefix: string, enableFix: boolean }], 'missing'>({
12399 return parserServices ?. defineTemplateBodyVisitor ( templateBodyVisitor , scriptVisitor )
124100 }
125101 } ,
102+ defaultOptions : [ { enableFix : true , prefix : ':uno:' } ] ,
103+ meta : {
104+ docs : {
105+ description : 'Enforce class compilation' ,
106+ } ,
107+ fixable : 'code' ,
108+ messages : {
109+ missing : 'prefix: `{{prefix}}` is missing' ,
110+ } ,
111+ schema : [ {
112+ additionalProperties : false ,
113+ properties : {
114+ enableFix : {
115+ type : 'boolean' ,
116+ } ,
117+ prefix : {
118+ type : 'string' ,
119+ } ,
120+ } ,
121+ type : 'object' ,
122+ } ] ,
123+ type : 'problem' ,
124+ } ,
125+ name : 'enforce-class-compile' ,
126126} ) as any as ESLintUtils . RuleWithMeta < [ ] , '' >
0 commit comments