Skip to content

Add sfc-locale-attr rule #286

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 3 commits into from
Feb 10, 2022
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
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@
| Rule ID | Description | |
|:--------|:------------|:---|
| [@intlify/vue-i18n/<wbr>prefer-linked-key-with-paren](./prefer-linked-key-with-paren.html) | enforce linked key to be enclosed in parentheses | :black_nib: |
| [@intlify/vue-i18n/<wbr>sfc-locale-attr](./sfc-locale-attr.html) | require or disallow the locale attribute on `<i18n>` block | |
89 changes: 89 additions & 0 deletions docs/rules/sfc-locale-attr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
title: '@intlify/vue-i18n/sfc-locale-attr'
description: require or disallow the locale attribute on `<i18n>` block
---

# @intlify/vue-i18n/sfc-locale-attr

> require or disallow the locale attribute on `<i18n>` block

## :book: Rule Details

This rule aims to enforce the `<i18n>` block to use or not the `locale` attribute.

<eslint-code-block>

<!-- eslint-skip -->

```vue
<script>
/* eslint @intlify/vue-i18n/sfc-locale-attr: "error" */
</script>

<!-- ✓ GOOD -->
<i18n locale="en">
{
"message": "hello!"
}
</i18n>

<!-- ✗ BAD -->
<i18n>
{
"en": {
"message": "hello!"
}
}
</i18n>
```

</eslint-code-block>

## :gear: Options

```json
{
"@intlify/vue-i18n/sfc-locale-attr": [
"error",
"always" //"always" or "never"
]
}
```

- `"always"` ... The `<i18n>` blocks requires the locale attribute.
- `"never"` ... Do not use the locale attribute on `<i18n>` blocks.

### Examples of code for this rule with `"never"` option:

<eslint-code-block>

<!-- eslint-skip -->

```vue
<script>
/* eslint @intlify/vue-i18n/sfc-locale-attr: ["error", "never"] */
</script>

<!-- ✓ GOOD -->
<i18n>
{
"en": {
"message": "hello!"
}
}
</i18n>

<!-- ✗ BAD -->
<i18n locale="en">
{
"message": "hello!"
}
</i18n>
```

</eslint-code-block>

## :mag: Implementation

- [Rule source](https://github.com/intlify/eslint-plugin-vue-i18n/blob/master/lib/rules/sfc-locale-attr.ts)
- [Test source](https://github.com/intlify/eslint-plugin-vue-i18n/tree/master/tests/lib/rules/sfc-locale-attr.ts)
2 changes: 2 additions & 0 deletions lib/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import noUnusedKeys from './rules/no-unused-keys'
import noVHtml from './rules/no-v-html'
import preferLinkedKeyWithParen from './rules/prefer-linked-key-with-paren'
import preferSfcLangAttr from './rules/prefer-sfc-lang-attr'
import sfcLocaleAttr from './rules/sfc-locale-attr'
import validMessageSyntax from './rules/valid-message-syntax'

export = {
Expand All @@ -32,5 +33,6 @@ export = {
'no-v-html': noVHtml,
'prefer-linked-key-with-paren': preferLinkedKeyWithParen,
'prefer-sfc-lang-attr': preferSfcLangAttr,
'sfc-locale-attr': sfcLocaleAttr,
'valid-message-syntax': validMessageSyntax
}
67 changes: 67 additions & 0 deletions lib/rules/sfc-locale-attr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type { RuleContext, RuleListener } from '../types'
import { isI18nBlock, getAttribute } from '../utils/index'
import { createRule } from '../utils/rule'

export = createRule({
meta: {
type: 'suggestion',
docs: {
description: 'require or disallow the locale attribute on `<i18n>` block',
category: 'Stylistic Issues',
url: 'https://eslint-plugin-vue-i18n.intlify.dev/rules/sfc-locale-attr.html',
recommended: false
},
fixable: null,
schema: [
{
enum: ['always', 'never']
}
],
messages: {
required: '`locale` attribute is required.',
disallowed: '`locale` attribute is disallowed.'
}
},
create(context: RuleContext): RuleListener {
const df = context.parserServices.getDocumentFragment?.()
if (!df) {
return {}
}
const always = context.options[0] !== 'never'
return {
Program() {
for (const i18n of df.children.filter(isI18nBlock)) {
const srcAttrs = getAttribute(i18n, 'src')
if (srcAttrs != null) {
continue
}
const localeAttrs = getAttribute(i18n, 'locale')

if (
localeAttrs != null &&
localeAttrs.value != null &&
localeAttrs.value.value
) {
if (always) {
continue
}
// disallowed
context.report({
loc: localeAttrs.loc,
messageId: 'disallowed'
})
} else {
if (!always) {
continue
}
// missing
context.report({
loc: i18n.startTag.loc,
messageId: 'required'
})
}
}
}
}
}
})
4 changes: 3 additions & 1 deletion lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as localeMessages from './utils/locale-messages'
import * as parsers from './utils/parsers'
import * as pathUtils from './utils/path-utils'
import * as resourceLoader from './utils/resource-loader'
import * as rule from './utils/rule'

export = {
'cache-function': cacheFunction,
Expand All @@ -32,5 +33,6 @@ export = {
'locale-messages': localeMessages,
parsers,
'path-utils': pathUtils,
'resource-loader': resourceLoader
'resource-loader': resourceLoader,
rule
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@
"coverage": "nyc report --reporter lcov && opener coverage/lcov-report/index.html",
"docs": "npm run build && vuepress dev docs",
"docs:build": "npm run build && vuepress build docs",
"generate": "ts-node scripts/update.ts && prettier . --write",
"generate": "ts-node scripts/update.ts && npm run format",
"lint": "eslint . --ext js,ts,vue,md --ignore-pattern \"/tests/fixtures\"",
"lint-fix": "eslint . --ext js,ts,vue,md --ignore-pattern \"/tests/fixtures\" --fix",
"lint:docs": "prettier docs --check",
"format": "prettier . --write",
"release:prepare": "shipjs prepare",
"release:trigger": "shipjs trigger",
"test": "mocha --require ts-node/register \"./tests/**/*.ts\"",
Expand Down
1 change: 0 additions & 1 deletion scripts/new-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ description: description

> description

## :book: Rule Details
## :book: Rule Details

This rule reports ???.
Expand Down
111 changes: 111 additions & 0 deletions tests/lib/rules/sfc-locale-attr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { RuleTester } from 'eslint'
import rule = require('../../../lib/rules/sfc-locale-attr')
const vueParser = require.resolve('vue-eslint-parser')

const tester = new RuleTester({
parser: vueParser,
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module'
}
})

tester.run('sfc-locale-attr', rule as never, {
valid: [
{
filename: 'test.vue',
code: `
<i18n locale="en">
{
"message": "hello!"
}
</i18n>
`
},
{
filename: 'test.vue',
code: `
<i18n locale="en">
{
"message": "hello!"
}
</i18n>
`,
options: ['always']
},
{
filename: 'test.vue',
code: `
<i18n>
{
"en": {
"message": "hello!"
}
}
</i18n>
`,
options: ['never']
}
],
invalid: [
{
filename: 'test.vue',
code: `
<i18n>
{
"en": {
"message": "hello!"
}
}
</i18n>
`,
errors: [
{
message: '`locale` attribute is required.',
line: 2,
column: 7
}
]
},
{
filename: 'test.vue',
code: `
<i18n>
{
"en": {
"message": "hello!"
}
}
</i18n>
`,
options: ['always'],
errors: [
{
message: '`locale` attribute is required.',
line: 2,
column: 7
}
]
},
{
filename: 'test.vue',
code: `
<i18n locale="en">
{
"en": {
"message": "hello!"
}
}
</i18n>
`,
options: ['never'],
errors: [
{
message: '`locale` attribute is disallowed.',
line: 2,
column: 13
}
]
}
]
})