-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
prefer-sfc-lang-attr
rule (#267)
* Add `prefer-sfc-lang-attr` rule * fix
- Loading branch information
Showing
6 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
--- | ||
title: '@intlify/vue-i18n/prefer-sfc-lang-attr' | ||
description: require lang attribute on `<i18n>` block | ||
--- | ||
|
||
# @intlify/vue-i18n/prefer-sfc-lang-attr | ||
|
||
> require lang attribute on `<i18n>` block | ||
- :black_nib:️ The `--fix` option on the [command line](http://eslint.org/docs/user-guide/command-line-interface#fix) can automatically fix some of the problems reported by this rule. | ||
|
||
## :book: Rule Details | ||
|
||
This rule enforce `lang` attribute to be specified to `<i18n>` custom block. | ||
|
||
:-1: Examples of **incorrect** code for this rule: | ||
|
||
locale messages: | ||
|
||
<eslint-code-block fix> | ||
|
||
<!-- eslint-skip --> | ||
|
||
```vue | ||
<i18n> | ||
{ | ||
"en": { | ||
"message": "hello!" | ||
} | ||
} | ||
</i18n> | ||
<script> | ||
/* eslint @intlify/vue-i18n/prefer-sfc-lang-attr: 'error' */ | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
:+1: Examples of **correct** code for this rule: | ||
|
||
locale messages: | ||
|
||
<eslint-code-block fix> | ||
|
||
<!-- eslint-skip --> | ||
|
||
```vue | ||
<i18n lang="json"> | ||
{ | ||
"en": { | ||
"message": "hello!" | ||
} | ||
} | ||
</i18n> | ||
<script> | ||
/* eslint @intlify/vue-i18n/prefer-sfc-lang-attr: 'error' */ | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/intlify/eslint-plugin-vue-i18n/blob/master/lib/rules/prefer-sfc-lang-attr.ts) | ||
- [Test source](https://github.com/intlify/eslint-plugin-vue-i18n/tree/master/tests/lib/rules/prefer-sfc-lang-attr.ts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { getAttribute, isI18nBlock } from '../utils/index' | ||
import type { RuleContext, RuleListener } from '../types' | ||
|
||
function create(context: RuleContext): RuleListener { | ||
const df = context.parserServices.getDocumentFragment?.() | ||
if (!df) { | ||
return {} | ||
} | ||
|
||
return { | ||
Program() { | ||
for (const i18n of df.children.filter(isI18nBlock)) { | ||
const srcAttrs = getAttribute(i18n, 'src') | ||
if (srcAttrs != null) { | ||
continue | ||
} | ||
const langAttrs = getAttribute(i18n, 'lang') | ||
if ( | ||
langAttrs == null || | ||
langAttrs.value == null || | ||
!langAttrs.value.value | ||
) { | ||
context.report({ | ||
loc: (langAttrs?.value ?? langAttrs ?? i18n.startTag).loc, | ||
messageId: 'required', | ||
fix(fixer) { | ||
if (langAttrs) { | ||
return fixer.replaceTextRange(langAttrs.range, 'lang="json"') | ||
} | ||
const tokenStore = context.parserServices.getTemplateBodyTokenStore() | ||
const closeToken = tokenStore.getLastToken(i18n.startTag) | ||
const beforeToken = tokenStore.getTokenBefore(closeToken) | ||
return fixer.insertTextBeforeRange( | ||
closeToken.range, | ||
(beforeToken.range[1] < closeToken.range[0] ? '' : ' ') + | ||
'lang="json" ' | ||
) | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
export = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'require lang attribute on `<i18n>` block', | ||
category: 'Best Practices', | ||
recommended: false | ||
}, | ||
fixable: 'code', | ||
schema: [], | ||
messages: { | ||
required: '`lang` attribute is required.' | ||
} | ||
}, | ||
create | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { RuleTester } from 'eslint' | ||
import rule = require('../../../lib/rules/prefer-sfc-lang-attr') | ||
|
||
new RuleTester({ | ||
parser: require.resolve('vue-eslint-parser'), | ||
parserOptions: { ecmaVersion: 2020, sourceType: 'module' } | ||
}).run('prefer-sfc-lang-attr', rule as never, { | ||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<i18n lang="json">{}</i18n> | ||
<template></template> | ||
<script></script>` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<i18n lang="json5">{}</i18n> | ||
<template></template> | ||
<script></script>` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<i18n lang="yaml">{}</i18n> | ||
<template></template> | ||
<script></script>` | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<i18n>{}</i18n> | ||
<template></template> | ||
<script></script>`, | ||
output: ` | ||
<i18n lang="json" >{}</i18n> | ||
<template></template> | ||
<script></script>`, | ||
errors: [ | ||
{ | ||
message: '`lang` attribute is required.', | ||
line: 2, | ||
column: 7, | ||
endLine: 2, | ||
endColumn: 13 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<i18n>{}</i18n> | ||
<template></template>`, | ||
output: ` | ||
<i18n lang="json" >{}</i18n> | ||
<template></template>`, | ||
errors: [ | ||
{ | ||
message: '`lang` attribute is required.', | ||
line: 2, | ||
column: 7, | ||
endLine: 2, | ||
endColumn: 13 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<i18n locale="en" >{}</i18n> | ||
<template></template> | ||
<script></script>`, | ||
output: ` | ||
<i18n locale="en" lang="json" >{}</i18n> | ||
<template></template> | ||
<script></script>`, | ||
errors: [ | ||
{ | ||
message: '`lang` attribute is required.', | ||
line: 2, | ||
column: 7, | ||
endLine: 2, | ||
endColumn: 26 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<i18n lang>{}</i18n> | ||
<template></template>`, | ||
output: ` | ||
<i18n lang="json">{}</i18n> | ||
<template></template>`, | ||
errors: [ | ||
{ | ||
message: '`lang` attribute is required.', | ||
line: 2, | ||
column: 13, | ||
endLine: 2, | ||
endColumn: 17 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<i18n lang="">{}</i18n> | ||
<template></template>`, | ||
output: ` | ||
<i18n lang="json">{}</i18n> | ||
<template></template>`, | ||
errors: [ | ||
{ | ||
message: '`lang` attribute is required.', | ||
line: 2, | ||
column: 18, | ||
endLine: 2, | ||
endColumn: 20 | ||
} | ||
] | ||
} | ||
] | ||
}) |