@@ -11,54 +11,49 @@ module.exports = {
11
11
12
12
/**
13
13
* Checks whether the given node can convert to the `v-slot`.
14
- * @param {VAttribute | null } slotAttr node of `slot`
15
- * @param {VElement } slotAttr node of `slot`
14
+ * @param {VStartTag } startTag node of `<element v-slot ... >`
16
15
* @returns {boolean } `true` if the given node can convert to the `v-slot`
17
16
*/
18
- function canConvertToVSlot ( slotAttr , element ) {
17
+ function canConvertToVSlot ( startTag ) {
18
+ if ( startTag . parent . name !== 'template' ) {
19
+ return false
20
+ }
21
+
22
+ const slotAttr = startTag . attributes
23
+ . find ( attr => attr . directive === false && attr . key . name === 'slot' )
19
24
if ( slotAttr ) {
20
- if ( ! slotAttr . value ) {
21
- return true
22
- }
23
- const slotName = slotAttr . value . value
24
- // If non-Latin characters are included it can not be converted.
25
- return ! / [ ^ a - z ] / i. test ( slotName )
25
+ // if the element have `slot` it can not be converted.
26
+ // Conversion of `slot` is done with `vue/no-deprecated-slot-attribute`.
27
+ return false
26
28
}
27
29
28
- const vBindSlotAttr = element . attributes
30
+ const vBindSlotAttr = startTag . attributes
29
31
. find ( attr =>
30
32
attr . directive === true &&
31
33
attr . key . name . name === 'bind' &&
32
34
attr . key . argument &&
33
35
attr . key . argument . name === 'slot' )
34
- // if the element have `v-bind:slot` it can not be converted.
35
- // Conversion of `v-bind:slot` is done with `vue/no-deprecated-slot-attribute`.
36
- return ! vBindSlotAttr
36
+ if ( vBindSlotAttr ) {
37
+ // if the element have `v-bind:slot` it can not be converted.
38
+ // Conversion of `v-bind:slot` is done with `vue/no-deprecated-slot-attribute`.
39
+ return false
40
+ }
41
+ return true
37
42
}
38
43
39
44
/**
40
45
* Convert to `v-slot`.
41
46
* @param {object } fixer fixer
42
- * @param {VAttribute | null } slotAttr node of `slot`
43
47
* @param {VAttribute | null } scopeAttr node of `slot-scope`
44
48
* @returns {* } fix data
45
49
*/
46
- function fixSlotToVSlot ( fixer , slotAttr , scopeAttr ) {
47
- const nameArgument = slotAttr && slotAttr . value && slotAttr . value . value
48
- ? `:${ slotAttr . value . value } `
49
- : ''
50
+ function fixSlotScopeToVSlot ( fixer , scopeAttr ) {
50
51
const scopeValue = scopeAttr && scopeAttr . value
51
52
? `=${ sourceCode . getText ( scopeAttr . value ) } `
52
53
: ''
53
54
54
- const replaceText = `v-slot${ nameArgument } ${ scopeValue } `
55
- const fixers = [
56
- fixer . replaceText ( slotAttr || scopeAttr , replaceText )
57
- ]
58
- if ( slotAttr && scopeAttr ) {
59
- fixers . push ( fixer . remove ( scopeAttr ) )
60
- }
61
- return fixers
55
+ const replaceText = `v-slot${ scopeValue } `
56
+ return fixer . replaceText ( scopeAttr , replaceText )
62
57
}
63
58
/**
64
59
* Reports `slot-scope` node
@@ -72,13 +67,11 @@ module.exports = {
72
67
fix : fixToUpgrade
73
68
// fix to use `v-slot`
74
69
? ( fixer ) => {
75
- const element = scopeAttr . parent
76
- const slotAttr = element . attributes
77
- . find ( attr => attr . directive === false && attr . key . name === 'slot' )
78
- if ( ! canConvertToVSlot ( slotAttr , element ) ) {
70
+ const startTag = scopeAttr . parent
71
+ if ( ! canConvertToVSlot ( startTag ) ) {
79
72
return null
80
73
}
81
- return fixSlotToVSlot ( fixer , slotAttr , scopeAttr )
74
+ return fixSlotScopeToVSlot ( fixer , scopeAttr )
82
75
}
83
76
: null
84
77
} )
0 commit comments