Skip to content

Fix false negatives in <i18n> block without <template> in no-unused-keys rule. #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 43 additions & 8 deletions lib/utils/parsers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,46 @@ export interface YAMLParsed extends Parsed<YAMLAST.YAMLNode> {
ast: YAMLAST.YAMLProgram
}

function hasEndTag(
element: VAST.VElement
): element is VAST.VElement & { endTag: VAST.VEndTag } {
return !!element.endTag
}

function getSourceCodeString(
context: RuleContext,
i18nBlock: VAST.VElement & { endTag: VAST.VEndTag }
): string {
const tokenStore = context.parserServices.getTemplateBodyTokenStore()
const tokens = tokenStore.getTokensBetween(
i18nBlock.startTag,
i18nBlock.endTag
)
if (
tokens.length ||
i18nBlock.startTag.range[1] === i18nBlock.endTag.range[0]
) {
return tokens.map(t => t.value).join('')
}
// without <template></template>
const df = context.parserServices.getDocumentFragment?.()
if (!df) {
return ''
}
const start = i18nBlock.startTag.range[1]
const end = i18nBlock.endTag.range[0]
let sourceCode = ''
for (const token of df.tokens) {
if (start <= token.range[0] && token.range[1] <= end) {
sourceCode += token.value
}
if (end <= token.range[0]) {
break
}
}
return sourceCode
}

/**
* @param {RuleContext} context
* @param {VElement} i18nBlock
Expand All @@ -68,19 +108,14 @@ function parseInI18nBlock<P extends JSONAST.JSONProgram | YAMLAST.YAMLProgram>(
option: unknown
) => { ast: P; visitorKeys: VisitorKeys }
) {
if (!i18nBlock.endTag) {
if (!hasEndTag(i18nBlock)) {
return null
}
const offsetIndex = i18nBlock.startTag.range[1]
const tokenStore = context.parserServices.getTemplateBodyTokenStore()
const tokens = tokenStore.getTokensBetween(
i18nBlock.startTag,
i18nBlock.endTag
)
const sourceString = tokens.map(t => t.value).join('')
const sourceString = getSourceCodeString(context, i18nBlock)
if (!sourceString.trim()) {
return null
}
const offsetIndex = i18nBlock.startTag.range[1]
const sourceCode = context.getSourceCode()
const locationFixer = new LocationFixer(
sourceCode,
Expand Down
45 changes: 45 additions & 0 deletions tests/lib/rules/no-unused-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,51 @@ ${' '.repeat(6)}
"unused 'unuse' key",
"unused 'array-unuse[0]' key"
]
},
{
// without <template>
filename: 'test.vue',
code: `
<i18n lang="yaml" locale="en">
test: Test
</i18n>
<script>
// without <template>
export default {
render (createElement) {}
}
</script>`,
options: [{ enableFix: true }],
output: `
<i18n lang="yaml" locale="en">
{}
</i18n>
<script>
// without <template>
export default {
render (createElement) {}
}
</script>`,
errors: [
{
message: "unused 'test' key",
suggestions: [
{
desc: "Remove the 'test' key.",
output: `
<i18n lang="yaml" locale="en">
{}
</i18n>
<script>
// without <template>
export default {
render (createElement) {}
}
</script>`
}
]
}
]
}
]
})
Expand Down