-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathsfc-locale-attr.ts
67 lines (65 loc) · 1.73 KB
/
sfc-locale-attr.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
import type { RuleContext, RuleListener } from '../types'
import { isI18nBlock, getAttribute } from '../utils/index'
import { createRule } from '../utils/rule'
export default createRule({
meta: {
type: 'suggestion',
docs: {
description: 'require or disallow the locale attribute on `<i18n>` block',
category: 'Stylistic Issues',
url: 'https://eslint-plugin-vue-i18n.intlify.dev/rules/sfc-locale-attr.html',
recommended: false
},
fixable: null,
schema: [
{
enum: ['always', 'never']
}
],
messages: {
required: '`locale` attribute is required.',
disallowed: '`locale` attribute is disallowed.'
}
},
create(context: RuleContext): RuleListener {
const df = context.parserServices.getDocumentFragment?.()
if (!df) {
return {}
}
const always = context.options[0] !== 'never'
return {
Program() {
for (const i18n of df.children.filter(isI18nBlock)) {
const srcAttrs = getAttribute(i18n, 'src')
if (srcAttrs != null) {
continue
}
const localeAttrs = getAttribute(i18n, 'locale')
if (
localeAttrs != null &&
localeAttrs.value != null &&
localeAttrs.value.value
) {
if (always) {
continue
}
// disallowed
context.report({
loc: localeAttrs.loc,
messageId: 'disallowed'
})
} else {
if (!always) {
continue
}
// missing
context.report({
loc: i18n.startTag.loc,
messageId: 'required'
})
}
}
}
}
}
})