Skip to content

Commit d7518f0

Browse files
author
alxndrsn
committed
no-unused-keys: allow configuring of callExpression
Closes #643
1 parent 915ce6d commit d7518f0

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

docs/rules/no-unused-keys.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ i18n.t('hi')
164164
"src": "./src",
165165
"extensions": [".js", ".vue"],
166166
"ignores": [],
167-
"enableFix": false
167+
"enableFix": false,
168+
"callExpression": "^(\\$t|t|\\$tc|tc)$"
168169
}
169170
]
170171
}
@@ -174,6 +175,7 @@ i18n.t('hi')
174175
- `extensions`: an array to allow specified lintable target file extension. If you don't set any options, it set to `.js` and `.vue` as default.
175176
- `ignores`: An array of key names and patterns to exclude from the check. If you want to specify a pattern, specify a string such as `/pattern/`.
176177
- `enableFix`: if `true`, enable automatically remove unused keys on `eslint --fix`. If you don't set any options, it set to `false` as default. (This is an experimental feature.)
178+
- `callExpression`: A regular expression used to identify translation function calls.
177179

178180
## :couple: Related Rules
179181

lib/rules/no-unused-keys.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ function create(context: RuleContext): RuleListener {
8181
const options = (context.options && context.options[0]) || {}
8282
const enableFix = options.enableFix
8383
const ignores = ((options.ignores || []) as string[]).map(toRegExp)
84+
const callExpression =
85+
(options.callExpression && new RegExp(options.callExpression)) ||
86+
/^(\$t|t|\$tc|tc)$/
8487

8588
function createVerifyContext<N extends JSONAST.JSONNode | YAMLAST.YAMLNode>(
8689
usedKeys: UsedKeys,
@@ -512,7 +515,8 @@ function create(context: RuleContext): RuleListener {
512515
const localeMessages = getLocaleMessages(context)
513516
const usedLocaleMessageKeys = collectKeysFromAST(
514517
sourceCode.ast as VAST.ESLintProgram,
515-
sourceCode.visitorKeys
518+
sourceCode.visitorKeys,
519+
callExpression
516520
)
517521
const targetLocaleMessage = localeMessages.findBlockLocaleMessage(
518522
ctx.parserServices.customBlock
@@ -601,6 +605,9 @@ export = createRule({
601605
},
602606
enableFix: {
603607
type: 'boolean'
608+
},
609+
callExpression: {
610+
type: 'string'
604611
}
605612
},
606613
additionalProperties: false

lib/utils/collect-keys.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ const debug = debugBuilder('eslint-plugin-vue-i18n:collect-keys')
2020
*
2121
* @param {CallExpression} node
2222
*/
23-
function getKeyFromCallExpression(node: VAST.ESLintCallExpression) {
23+
function getKeyFromCallExpression(
24+
node: VAST.ESLintCallExpression,
25+
callExpression?: RegExp
26+
) {
2427
const funcName =
2528
(node.callee.type === 'MemberExpression' &&
2629
node.callee.property.type === 'Identifier' &&
@@ -29,7 +32,7 @@ function getKeyFromCallExpression(node: VAST.ESLintCallExpression) {
2932
''
3033

3134
if (
32-
!/^(\$t|t|\$tc|tc)$/.test(funcName) ||
35+
!(callExpression ?? /^(\$t|t|\$tc|tc)$/).test(funcName) ||
3336
!node.arguments ||
3437
!node.arguments.length
3538
) {
@@ -121,7 +124,8 @@ function collectKeyResourcesFromFiles(fileNames: string[], cwd: string) {
121124
*/
122125
export function collectKeysFromAST(
123126
node: VAST.ESLintProgram,
124-
visitorKeys?: VisitorKeys
127+
visitorKeys?: VisitorKeys,
128+
callExpression?: RegExp
125129
): string[] {
126130
debug('collectKeysFromAST')
127131

@@ -168,7 +172,7 @@ export function collectKeysFromAST(
168172
}
169173
} else if (node.type === 'CallExpression') {
170174
debug('CallExpression handling ...')
171-
const key = getKeyFromCallExpression(node)
175+
const key = getKeyFromCallExpression(node, callExpression)
172176
if (key) {
173177
results.add(String(key))
174178
}

tests/lib/rules/no-unused-keys.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,27 @@ new RuleTester({
289289
}
290290
}
291291
</script>`
292+
},
293+
{
294+
filename: 'test.vue',
295+
code: `
296+
<i18n locale="en">
297+
{
298+
"foo": "foo",
299+
"bar": {
300+
"nest": "nest",
301+
},
302+
}
303+
</i18n>
304+
<script>
305+
export default {
306+
created () {
307+
this.customFn1('foo')
308+
this.customFn2('bar.nest')
309+
}
310+
}
311+
</script>`,
312+
options: [{ callExpression: 'customFn\\d' }]
292313
}
293314
],
294315
invalid: [

0 commit comments

Comments
 (0)