-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathno-deprecated-i18n-component.ts
103 lines (99 loc) · 3.15 KB
/
no-deprecated-i18n-component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* @author Yosuke Ota
*/
import { defineTemplateBodyVisitor } from '../utils/index'
import type { RuleContext, RuleListener } from '../types'
import type { AST as VAST } from 'vue-eslint-parser'
import { createRule } from '../utils/rule'
function create(context: RuleContext): RuleListener {
return defineTemplateBodyVisitor(context, {
VElement(node: VAST.VElement) {
if (node.name !== 'i18n') {
return
}
const tokenStore = context.parserServices.getTemplateBodyTokenStore()
const tagNameToken = tokenStore.getFirstToken(node.startTag)
context.report({
node: tagNameToken,
messageId: 'deprecated',
*fix(fixer) {
yield fixer.replaceText(tagNameToken, '<i18n-t')
let hasTag = false
for (const attr of node.startTag.attributes) {
if (attr.directive) {
if (attr.key.name.name !== 'bind') {
continue
}
if (
!attr.key.argument ||
attr.key.argument.type !== 'VIdentifier'
) {
continue
}
if (attr.key.argument.name === 'path') {
yield fixer.replaceText(attr.key.argument, 'keypath')
} else if (attr.key.argument.name === 'tag') {
hasTag = true
if (
attr.value &&
attr.value.expression &&
attr.value.expression.type === 'Literal' &&
typeof attr.value.expression.value === 'boolean'
) {
if (attr.value.expression.value) {
// :tag="true"
yield fixer.replaceText(attr, 'tag="span"')
} else {
yield fixer.remove(attr)
}
}
}
} else {
if (attr.key.name === 'path') {
yield fixer.replaceText(attr.key, 'keypath')
} else if (attr.key.name === 'tag') {
hasTag = true
}
}
}
if (!hasTag) {
// Add a default `tag` for the <i18n> component.
yield fixer.insertTextAfter(
(node.startTag.attributes.length > 0 &&
node.startTag.attributes[
node.startTag.attributes.length - 1
]) ||
tagNameToken,
' tag="span"'
)
}
if (node.endTag) {
yield fixer.replaceText(
tokenStore.getFirstToken(node.endTag),
'</i18n-t'
)
}
}
})
}
})
}
export default createRule({
meta: {
type: 'problem',
docs: {
description:
'disallow using deprecated `<i18n>` components (in Vue I18n 9.0.0+)',
category: 'Recommended',
url: 'https://eslint-plugin-vue-i18n.intlify.dev/rules/no-deprecated-i18n-component.html',
recommended: false
},
fixable: 'code',
schema: [],
messages: {
deprecated:
'Deprecated <i18n> component was found. For VueI18n v9.0, use <i18n-t> component instead.'
}
},
create
})