Skip to content

Commit

Permalink
🐛 bug(rule): fix detect literal
Browse files Browse the repository at this point in the history
closes #24
  • Loading branch information
kazupon committed Jul 22, 2019
1 parent 82ddc5f commit 6b6efae
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
46 changes: 30 additions & 16 deletions lib/rules/no-raw-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,40 @@ function calculateLoc (node, base = null) {
function checkVExpressionContainerText (context, node, baseNode = null) {
if (!node.expression) { return }

if (node.expression.type === 'Literal') {
const literalNode = node.expression
const loc = calculateLoc(literalNode, baseNode)
context.report({
loc,
message: `raw text '${literalNode.value}' is used`
})
} else if (node.expression.type === 'ConditionalExpression') {
const targets = [node.expression.consequent, node.expression.alternate]
targets.forEach(target => {
if (target.type === 'Literal') {
const loc = calculateLoc(target, baseNode)
if (node.parent && node.parent.type === 'VElement') {
// parent is element (e.g. <p>{{ ... }}</p>)
if (node.expression.type === 'Literal') {
const literalNode = node.expression
const loc = calculateLoc(literalNode, baseNode)
context.report({
loc,
message: `raw text '${literalNode.value}' is used`
})
} else if (node.expression.type === 'ConditionalExpression') {
const targets = [node.expression.consequent, node.expression.alternate]
targets.forEach(target => {
if (target.type === 'Literal') {
const loc = calculateLoc(target, baseNode)
context.report({
loc,
message: `raw text '${target.value}' is used`
})
}
})
}
} else if (node.parent && node.parent.type === 'VAttribute' && node.parent.directive) {
// parent is directive (e.g <p v-xxx="..."></p>)
const attrNode = node.parent
if (attrNode.key && attrNode.key.type === 'VDirectiveKey') {
if ((attrNode.key.name === 'text' || attrNode.key.name.name === 'text') && node.expression.type === 'Literal') {
const literalNode = node.expression
const loc = calculateLoc(literalNode, baseNode)
context.report({
loc,
message: `raw text '${target.value}' is used`
message: `raw text '${literalNode.value}' is used`
})
}
})
} else if ((node.parent && node.parent.type === 'VAttribute' && node.parent.directive) &&
(node.parent.key && node.parent.key.type === 'VDirectiveKey')) {
}
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-raw-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ tester.run('no-raw-text', rule, {
}, {
code: `
<template>
<comp :value="1" :msg="$t('foo.bar')"/>
<p>{{ hello }}</p>
</template>
`
Expand Down

0 comments on commit 6b6efae

Please sign in to comment.