@@ -5,14 +5,53 @@ import { defineTemplateBodyVisitor } from '../utils/index'
5
5
import type { RuleContext , RuleListener } from '../types'
6
6
import type { AST as VAST } from 'vue-eslint-parser'
7
7
8
+ function isStatic (
9
+ node :
10
+ | VAST . ESLintExpression
11
+ | VAST . ESLintSpreadElement
12
+ | VAST . VFilterSequenceExpression
13
+ | VAST . VForExpression
14
+ | VAST . VOnExpression
15
+ | VAST . VSlotScopeExpression
16
+ ) : boolean {
17
+ if ( node . type === 'Literal' ) {
18
+ return true
19
+ }
20
+ if ( node . type === 'TemplateLiteral' && node . expressions . length === 0 ) {
21
+ return true
22
+ }
23
+ return false
24
+ }
25
+
26
+ function getNodeName ( context : RuleContext , node : VAST . Node ) : string {
27
+ if ( node . type === 'Identifier' ) {
28
+ return node . name
29
+ }
30
+ const sourceCode = context . getSourceCode ( )
31
+ if (
32
+ sourceCode . ast . range [ 0 ] <= node . range [ 0 ] &&
33
+ node . range [ 1 ] <= sourceCode . ast . range [ 1 ]
34
+ ) {
35
+ return sourceCode
36
+ . getTokens ( node )
37
+ . map ( t => t . value )
38
+ . join ( '' )
39
+ }
40
+ const tokenStore = context . parserServices . getTemplateBodyTokenStore ( )
41
+ return tokenStore
42
+ . getTokens ( node )
43
+ . map ( t => t . value )
44
+ . join ( '' )
45
+ }
46
+
8
47
function checkDirective ( context : RuleContext , node : VAST . VDirective ) {
9
48
if (
10
49
node . value &&
11
50
node . value . type === 'VExpressionContainer' &&
12
51
node . value . expression &&
13
- node . value . expression . type === 'Identifier'
52
+ ! isStatic ( node . value . expression )
14
53
) {
15
- const name = node . value . expression . name
54
+ const name = getNodeName ( context , node . value . expression )
16
55
context . report ( {
17
56
node,
18
57
message : `'${ name } ' dynamic key is used'`
@@ -21,19 +60,18 @@ function checkDirective(context: RuleContext, node: VAST.VDirective) {
21
60
}
22
61
23
62
function checkComponent ( context : RuleContext , node : VAST . VDirectiveKey ) {
24
- const parent : VAST . VDirective = node . parent as never // typebug?
25
63
if (
26
64
node . name . type === 'VIdentifier' &&
27
65
node . name . name === 'bind' &&
28
66
node . argument &&
29
67
node . argument . type === 'VIdentifier' &&
30
68
node . argument . name === 'path' &&
31
- parent . value &&
32
- parent . value . type === 'VExpressionContainer' &&
33
- parent . value . expression &&
34
- parent . value . expression . type === 'Identifier'
69
+ node . parent . value &&
70
+ node . parent . value . type === 'VExpressionContainer' &&
71
+ node . parent . value . expression &&
72
+ ! isStatic ( node . parent . value . expression )
35
73
) {
36
- const name = parent . value . expression . name
74
+ const name = getNodeName ( context , node . parent . value . expression )
37
75
context . report ( {
38
76
node,
39
77
message : `'${ name } ' dynamic key is used'`
@@ -61,8 +99,8 @@ function checkCallExpression(
61
99
}
62
100
63
101
const [ keyNode ] = node . arguments
64
- if ( keyNode . type === 'Identifier' ) {
65
- const name = keyNode . name
102
+ if ( ! isStatic ( keyNode ) ) {
103
+ const name = getNodeName ( context , keyNode )
66
104
context . report ( {
67
105
node,
68
106
message : `'${ name } ' dynamic key is used'`
0 commit comments