Skip to content

Commit 9d52d5c

Browse files
authored
fix: check directive value; refactor directive visitor; test (#60)
* fix: check directive value; refactor directive visitor; test * fix: check element and conditional expression text against ignorePattern and ignoreText; test
1 parent 6f86717 commit 9d52d5c

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

lib/rules/no-raw-text.js

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,41 @@ function calculateLoc (node, base = null) {
2525
}
2626
}
2727

28+
function testValue (value) {
29+
return typeof value !== 'string' ||
30+
hasOnlyWhitespace(value) ||
31+
config.ignorePattern.test(value.trim()) ||
32+
config.ignoreText.includes(value.trim())
33+
}
34+
35+
// parent is directive (e.g <p v-xxx="..."></p>)
36+
function checkVAttributeDirective (context, node, baseNode = null) {
37+
const attrNode = node.parent
38+
if (attrNode.key && attrNode.key.type === 'VDirectiveKey') {
39+
if ((attrNode.key.name === 'text' || attrNode.key.name.name === 'text') && node.expression.type === 'Literal') {
40+
const literalNode = node.expression
41+
const value = literalNode.value
42+
43+
if (testValue(value)) { return }
44+
45+
const loc = calculateLoc(literalNode, baseNode)
46+
context.report({
47+
loc,
48+
message: `raw text '${literalNode.value}' is used`
49+
})
50+
}
51+
}
52+
}
53+
2854
function checkVExpressionContainerText (context, node, baseNode = null) {
2955
if (!node.expression) { return }
3056

3157
if (node.parent && node.parent.type === 'VElement') {
3258
// parent is element (e.g. <p>{{ ... }}</p>)
3359
if (node.expression.type === 'Literal') {
3460
const literalNode = node.expression
61+
if (testValue(literalNode.value)) { return }
62+
3563
const loc = calculateLoc(literalNode, baseNode)
3664
context.report({
3765
loc,
@@ -41,6 +69,8 @@ function checkVExpressionContainerText (context, node, baseNode = null) {
4169
const targets = [node.expression.consequent, node.expression.alternate]
4270
targets.forEach(target => {
4371
if (target.type === 'Literal') {
72+
if (testValue(target.value)) { return }
73+
4474
const loc = calculateLoc(target, baseNode)
4575
context.report({
4676
loc,
@@ -50,27 +80,12 @@ function checkVExpressionContainerText (context, node, baseNode = null) {
5080
})
5181
}
5282
} else if (node.parent && node.parent.type === 'VAttribute' && node.parent.directive) {
53-
// parent is directive (e.g <p v-xxx="..."></p>)
54-
const attrNode = node.parent
55-
if (attrNode.key && attrNode.key.type === 'VDirectiveKey') {
56-
if ((attrNode.key.name === 'text' || attrNode.key.name.name === 'text') && node.expression.type === 'Literal') {
57-
const literalNode = node.expression
58-
const loc = calculateLoc(literalNode, baseNode)
59-
context.report({
60-
loc,
61-
message: `raw text '${literalNode.value}' is used`
62-
})
63-
}
64-
}
83+
checkVAttributeDirective(context, node)
6584
}
6685
}
6786

6887
function checkRawText (context, value, loc) {
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 }
88+
if (testValue(value)) { return }
7489

7590
context.report({
7691
loc,

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,36 @@ tester.run('no-raw-text', rule, {
233233
<template>
234234
<p>{{ $t('foo') }}: {{ $t('bar') }}</p>
235235
<p>hello</p>
236+
<p> - </p>
237+
<p>@</p>
238+
<p>{{ true ? $t('ok') : ' - ' }}</p>
239+
<p>{{ true ? $t('ok') : '@' }}</p>
236240
</template>
237241
`,
238242
options: [{ ignorePattern: '^[-.#:()&]+$' }],
239243
errors: [{
240244
message: `raw text 'hello' is used`, line: 4
245+
}, {
246+
message: `raw text '@' is used`, line: 6, column: 12
247+
}, {
248+
message: `raw text '@' is used`, line: 8, column: 33
249+
}]
250+
}, {
251+
code: `
252+
<template>
253+
<v-icon v-text="'mdi-check'" />
254+
<v-icon v-text="'not'" />
255+
<v-icon v-text="'ok'" />
256+
<v-icon v-html="'mdi-check'" />
257+
<v-icon v-html="'ok'" />
258+
</template>
259+
`,
260+
options: [{
261+
ignorePattern: '^mdi[-]|[-#:()/&]+$',
262+
ignoreText: ['ok']
263+
}],
264+
errors: [{
265+
message: `raw text 'not' is used`, line: 4, column: 25
241266
}]
242267
}, {
243268
code: `

0 commit comments

Comments
 (0)