Skip to content

Commit 6b6efae

Browse files
committed
🐛 bug(rule): fix detect literal
closes #24
1 parent 82ddc5f commit 6b6efae

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

Diff for: lib/rules/no-raw-text.js

+30-16
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,40 @@ function calculateLoc (node, base = null) {
2727
function checkVExpressionContainerText (context, node, baseNode = null) {
2828
if (!node.expression) { return }
2929

30-
if (node.expression.type === 'Literal') {
31-
const literalNode = node.expression
32-
const loc = calculateLoc(literalNode, baseNode)
33-
context.report({
34-
loc,
35-
message: `raw text '${literalNode.value}' is used`
36-
})
37-
} else if (node.expression.type === 'ConditionalExpression') {
38-
const targets = [node.expression.consequent, node.expression.alternate]
39-
targets.forEach(target => {
40-
if (target.type === 'Literal') {
41-
const loc = calculateLoc(target, baseNode)
30+
if (node.parent && node.parent.type === 'VElement') {
31+
// parent is element (e.g. <p>{{ ... }}</p>)
32+
if (node.expression.type === 'Literal') {
33+
const literalNode = node.expression
34+
const loc = calculateLoc(literalNode, baseNode)
35+
context.report({
36+
loc,
37+
message: `raw text '${literalNode.value}' is used`
38+
})
39+
} else if (node.expression.type === 'ConditionalExpression') {
40+
const targets = [node.expression.consequent, node.expression.alternate]
41+
targets.forEach(target => {
42+
if (target.type === 'Literal') {
43+
const loc = calculateLoc(target, baseNode)
44+
context.report({
45+
loc,
46+
message: `raw text '${target.value}' is used`
47+
})
48+
}
49+
})
50+
}
51+
} else if (node.parent && node.parent.type === 'VAttribute' && node.parent.directive) {
52+
// parent is directive (e.g <p v-xxx="..."></p>)
53+
const attrNode = node.parent
54+
if (attrNode.key && attrNode.key.type === 'VDirectiveKey') {
55+
if ((attrNode.key.name === 'text' || attrNode.key.name.name === 'text') && node.expression.type === 'Literal') {
56+
const literalNode = node.expression
57+
const loc = calculateLoc(literalNode, baseNode)
4258
context.report({
4359
loc,
44-
message: `raw text '${target.value}' is used`
60+
message: `raw text '${literalNode.value}' is used`
4561
})
4662
}
47-
})
48-
} else if ((node.parent && node.parent.type === 'VAttribute' && node.parent.directive) &&
49-
(node.parent.key && node.parent.key.type === 'VDirectiveKey')) {
63+
}
5064
}
5165
}
5266

Diff for: tests/lib/rules/no-raw-text.js

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ tester.run('no-raw-text', rule, {
2828
}, {
2929
code: `
3030
<template>
31+
<comp :value="1" :msg="$t('foo.bar')"/>
3132
<p>{{ hello }}</p>
3233
</template>
3334
`

0 commit comments

Comments
 (0)