Skip to content

Commit 34b7a25

Browse files
stevelaceykazupon
authored andcommitted
⭐ new(rule): No raw text ignore options (#31) by @stevelacey
1 parent 0cdf776 commit 34b7a25

File tree

4 files changed

+129
-16
lines changed

4 files changed

+129
-16
lines changed

docs/rules/no-raw-text.md

+16
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,19 @@ export default {
6565
// ...
6666
}
6767
```
68+
69+
## :gear: Options
70+
71+
```json
72+
{
73+
"vue-i18n/no-raw-text": ["error", {
74+
"ignoreNodes": ["md-icon", "v-icon"],
75+
"ignorePattern": "^[-#:()&]+$",
76+
"ignoreText": ["EUR", "HKD", "USD"]
77+
}]
78+
}
79+
```
80+
81+
- `ignoreNodes`: specify nodes to ignore such as icon components
82+
- `ignorePattern`: specify a regexp pattern that matches strings to ignore
83+
- `ignoreText`: specify an array of strings to ignore

docs/rules/no-unused-keys.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const i18n = new VueI18n({
7676
i18n.t('hi')
7777
```
7878

79-
## Options
79+
## :gear: Options
8080

8181
```json
8282
{

lib/rules/no-raw-text.js

+41-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
const { parse, AST } = require('vue-eslint-parser')
77
const { defineTemplateBodyVisitor } = require('../utils/index')
88

9-
const hasOnlyLineBreak = value => /^[\r\n\t\f\v]+$/.test(value.replace(/ /g, ''))
9+
const config = {}
10+
const hasOnlyWhitespace = value => /^[\r\n\s\t\f\v]+$/.test(value)
1011
const INNER_START_OFFSET = '<template>'.length
1112

1213
function calculateLoc (node, base = null) {
@@ -65,7 +66,11 @@ function checkVExpressionContainerText (context, node, baseNode = null) {
6566
}
6667

6768
function checkRawText (context, value, loc) {
68-
if (typeof value !== 'string' || hasOnlyLineBreak(value)) { return }
69+
if (typeof value !== 'string' || hasOnlyWhitespace(value)) { return }
70+
71+
if (config.ignorePattern.test(value.trim())) { return }
72+
73+
if (config.ignoreText.includes(value.trim())) { return }
6974

7075
context.report({
7176
loc,
@@ -107,12 +112,30 @@ function getComponentTemplateNode (value) {
107112
}
108113

109114
function create (context) {
115+
config.ignorePattern = /^$/
116+
config.ignoreNodes = []
117+
config.ignoreText = []
118+
119+
if (context.options[0] && context.options[0].ignorePattern) {
120+
config.ignorePattern = new RegExp(context.options[0].ignorePattern, 'u')
121+
}
122+
123+
if (context.options[0] && context.options[0].ignoreNodes) {
124+
config.ignoreNodes = context.options[0].ignoreNodes
125+
}
126+
127+
if (context.options[0] && context.options[0].ignoreText) {
128+
config.ignoreText = context.options[0].ignoreText
129+
}
130+
110131
return defineTemplateBodyVisitor(context, { // template block
111132
VExpressionContainer (node) {
112133
checkVExpressionContainerText(context, node)
113134
},
114135

115136
VText (node) {
137+
if (config.ignoreNodes.includes(node.parent.name)) { return }
138+
116139
checkRawText(context, node.value, node.loc)
117140
}
118141
}, { // script block or scripts
@@ -148,7 +171,22 @@ module.exports = {
148171
recommended: true
149172
},
150173
fixable: null,
151-
schema: []
174+
schema: [
175+
{
176+
type: "object",
177+
properties: {
178+
ignoreNodes: {
179+
type: "array"
180+
},
181+
ignorePattern: {
182+
type: "string"
183+
},
184+
ignoreText: {
185+
type: "array"
186+
},
187+
}
188+
}
189+
]
152190
},
153191
create
154192
}

tests/lib/rules/no-raw-text.js

+71-12
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,20 @@ const tester = new RuleTester({
1919

2020
tester.run('no-raw-text', rule, {
2121
valid: [{
22-
code: `<template>
23-
<div class="app">
24-
<p class="a1">{{ $t('hello') }}</p>
25-
<p class="inner">{{ $t('click') }}<a href="/foo">{{ $t('here') }}</a>{{ $t('terminal') }}</p>
26-
</div>
27-
</template>`
28-
}, {
29-
code: `
30-
<template>
31-
<comp :value="1" :msg="$t('foo.bar')"/>
32-
<p>{{ hello }}</p>
33-
</template>
22+
code: `
23+
<template>
24+
<div class="app">
25+
<p class="a1">{{ $t('hello') }}</p>
26+
<p class="inner">{{ $t('click') }}<a href="/foo">{{ $t('here') }}</a>{{ $t('terminal') }}</p>
27+
</div>
28+
</template>
29+
`
30+
}, {
31+
code: `
32+
<template>
33+
<comp :value="1" :msg="$t('foo.bar')"/>
34+
<p>{{ hello }}</p>
35+
</template>
3436
`
3537
}, {
3638
filename: 'test.vue',
@@ -55,6 +57,29 @@ tester.run('no-raw-text', rule, {
5557
}
5658
}
5759
`
60+
}, {
61+
code: `
62+
<template>
63+
<md-icon>person</md-icon>
64+
<v-icon>menu</v-icon>
65+
</template>
66+
`,
67+
options: [{ ignoreNodes: ['md-icon', 'v-icon'] }],
68+
}, {
69+
code: `
70+
<template>
71+
<p>{{ $t('foo') }}: {{ $t('bar') }}</p>
72+
</template>
73+
`,
74+
options: [{ ignorePattern: '^[-.#:()&]+$' }],
75+
}, {
76+
code: `
77+
<template>
78+
<p>hello</p>
79+
<p>world</p>
80+
</template>
81+
`,
82+
options: [{ ignoreText: ['hello', 'world'] }],
5883
}],
5984

6085
invalid: [{
@@ -191,5 +216,39 @@ tester.run('no-raw-text', rule, {
191216
errors: [{
192217
message: `raw text 'hello' is used`, line: 4
193218
}]
219+
}, {
220+
code: `
221+
<template>
222+
<md-icon>person</md-icon>
223+
<v-icon>menu</v-icon>
224+
<p>hello</p>
225+
</template>
226+
`,
227+
options: [{ ignoreNodes: ['md-icon', 'v-icon'] }],
228+
errors: [{
229+
message: `raw text 'hello' is used`, line: 5
230+
}]
231+
}, {
232+
code: `
233+
<template>
234+
<p>{{ $t('foo') }}: {{ $t('bar') }}</p>
235+
<p>hello</p>
236+
</template>
237+
`,
238+
options: [{ ignorePattern: '^[-.#:()&]+$' }],
239+
errors: [{
240+
message: `raw text 'hello' is used`, line: 4
241+
}]
242+
}, {
243+
code: `
244+
<template>
245+
<p>hello</p>
246+
<p>world</p>
247+
</template>
248+
`,
249+
options: [{ ignoreText: ['hello'] }],
250+
errors: [{
251+
message: `raw text 'world' is used`, line: 4
252+
}]
194253
}]
195254
})

0 commit comments

Comments
 (0)