|
| 1 | +/** |
| 2 | + * @author kazuya kawaguchi (a.k.a. kazupon) |
| 3 | + */ |
| 4 | +'use strict' |
| 5 | + |
| 6 | +const { defineTemplateBodyVisitor } = require('../utils/index') |
| 7 | + |
| 8 | +function checkDirective (context, node) { |
| 9 | + if ((node.value && node.value.type === 'VExpressionContainer') && |
| 10 | + (node.value.expression && node.value.expression.type === 'Identifier')) { |
| 11 | + const name = node.value.expression.name |
| 12 | + context.report({ |
| 13 | + node, |
| 14 | + message: `'${name}' dynamic key is used'` |
| 15 | + }) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +function checkCallExpression (context, node) { |
| 20 | + const funcName = (node.callee.type === 'MemberExpression' && node.callee.property.name) || node.callee.name |
| 21 | + |
| 22 | + if (!/^(\$t|t|\$tc|tc)$/.test(funcName) || !node.arguments || !node.arguments.length) { |
| 23 | + return |
| 24 | + } |
| 25 | + |
| 26 | + const [keyNode] = node.arguments |
| 27 | + if (keyNode.type === 'Identifier') { |
| 28 | + const name = keyNode.name |
| 29 | + context.report({ |
| 30 | + node, |
| 31 | + message: `'${name}' dynamic key is used'` |
| 32 | + }) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function create (context) { |
| 37 | + return defineTemplateBodyVisitor(context, { |
| 38 | + "VAttribute[directive=true][key.name='t']" (node) { |
| 39 | + checkDirective(context, node) |
| 40 | + }, |
| 41 | + |
| 42 | + "VAttribute[directive=true][key.name.name='t']" (node) { |
| 43 | + checkDirective(context, node) |
| 44 | + }, |
| 45 | + |
| 46 | + CallExpression (node) { |
| 47 | + checkCallExpression(context, node) |
| 48 | + } |
| 49 | + }, { |
| 50 | + CallExpression (node) { |
| 51 | + checkCallExpression(context, node) |
| 52 | + } |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | +module.exports = { |
| 57 | + meta: { |
| 58 | + type: 'suggestion', |
| 59 | + docs: { |
| 60 | + description: 'disallow localization dynamic keys at localization methods', |
| 61 | + category: 'Best Practices', |
| 62 | + recommended: false |
| 63 | + }, |
| 64 | + fixable: null, |
| 65 | + schema: [] |
| 66 | + }, |
| 67 | + create |
| 68 | +} |
0 commit comments