Skip to content
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

feat: add lowercase to key-format-style rule #492

Merged
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
5 changes: 5 additions & 0 deletions .changeset/modern-apricots-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@intlify/eslint-plugin-vue-i18n': minor
---

add lowercase option to key-format-style rule
29 changes: 28 additions & 1 deletion docs/rules/key-format-style.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This rule aims to enforces specific casing for localization key names.
```yaml
camelCaseKey: The key for this value is camel case.
kebab-case-key: The key for this value is kebab case.
lowercase: The key for this value is lower case.
snake_case_key: The key for this value is snake case.
mixed_Case-key: Perhaps you don't want to use this casing.
```
Expand Down Expand Up @@ -47,7 +48,7 @@ Also, the following localization key definitions are reported as errors, because
{
"@intlify/vue-i18n/key-format-style": [
"error",
"camelCase" | "kebab-case" | "snake_case",
"camelCase" | "kebab-case" | "lowercase" | "snake_case",
{
"allowArray": false,
"splitByDots": false
Expand Down Expand Up @@ -112,6 +113,32 @@ appTitle: I18N Management System
app_title: I18N Management System
```

:+1: Examples of **correct** code for this rule with `"lowercase"`:

<eslint-code-block language="yaml">

```yaml
# eslint @intlify/vue-i18n/key-format-style: ['error', 'lowercase']

# ✓ GOOD
apptitle: I18N Management System
```

</eslint-code-block>

:-1: Examples of **incorrect** code for this rule with `"lowercase"`:

<eslint-code-block language="yaml">

```yaml
# eslint @intlify/vue-i18n/key-format-style: ['error', 'lowercase']

# ✗ BAD
appTitle: I18N Management System
app_title: I18N Management System
APP_TITLE: I18N Management System
```

</eslint-code-block>

:+1: Examples of **correct** code for this rule with `"snake_case"`:
Expand Down
1 change: 1 addition & 0 deletions lib/rules/key-format-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const debug = debugBuilder('eslint-plugin-vue-i18n:key-format-style')
const allowedCaseOptions = [
'camelCase',
'kebab-case',
'lowercase',
'snake_case',
'SCREAMING_SNAKE_CASE'
] as const
Expand Down
16 changes: 16 additions & 0 deletions lib/utils/casing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ export function isCamelCase(str: string): boolean {
return true
}

/**
* Checks whether the given string is lowercase.
*/
export function isLowerCase(str: string): boolean {
if (
hasSymbols(str) ||
hasUpper(str) ||
/-|_|\s/u.exec(str) // kebab or snake or space
) {
return false
}
return true
}

/**
* Checks whether the given string is PascalCase.
*/
Expand Down Expand Up @@ -104,6 +118,7 @@ const checkersMap = {
'kebab-case': isKebabCase,
snake_case: isSnakeCase,
camelCase: isCamelCase,
lowercase: isLowerCase,
PascalCase: isPascalCase,
SCREAMING_SNAKE_CASE: isScreamingSnakeCase
}
Expand All @@ -127,6 +142,7 @@ export function pascalCase(str: string): string {
export const allowedCaseOptions = [
'camelCase',
'kebab-case',
'lowercase',
'PascalCase',
'snake_case',
'SCREAMING_SNAKE_CASE'
Expand Down
165 changes: 165 additions & 0 deletions tests/lib/rules/key-format-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,41 @@ tester.run('key-format-style', rule as never, {
...options.json.file,
options: ['kebab-case']
},
{
code: `
lowercase:
foobar: kebab-value
`,
...options.yaml.file
},
{
code: `{
"lowercase": {
"foobar": "kebab-value"
}
}
`,
...options.json.file
},
{
code: `
en-US:
lowercase:
foobar: kebab-value
`,
...options.yaml.key
},
{
code: `{
"en-US": {
"lowercase": {
"foobar": "kebab-value"
}
}
}
`,
...options.json.key
},
{
code: `
snake_case:
Expand Down Expand Up @@ -379,6 +414,136 @@ tester.run('key-format-style', rule as never, {
}
]
},
{
code: `
foo-bar: baz
`,
...options.yaml.file,
options: ['lowercase'],
errors: [
{
message: '"foo-bar" is not lowercase',
line: 2
}
]
},
{
code: `
en-US:
foo-bar: baz
`,
...options.yaml.key,
options: ['lowercase'],
errors: [
{
message: '"foo-bar" is not lowercase',
line: 3
}
]
},
{
code: `
{"foo-bar": "baz"}
`,
...options.json.file,
options: ['lowercase'],
errors: [
{
message: '"foo-bar" is not lowercase',
line: 2
}
]
},
{
code: `
{"en-US": {
"foo-bar": "baz"
}}`,
...options.json.key,
options: ['lowercase'],
errors: [
{
message: '"foo-bar" is not lowercase',
line: 3
}
]
},
{
code: `
kebab-case:
snake_case: camelCase
`,
...options.yaml.file,
options: ['lowercase'],
errors: [
{
message: '"kebab-case" is not lowercase',
line: 2
},
{
message: '"snake_case" is not lowercase',
line: 3
}
]
},
{
code: `{
"kebab-case": {
"snake_case": "camelCase"
}
}
`,
...options.json.file,
options: ['lowercase'],
errors: [
{
message: '"kebab-case" is not lowercase',
line: 2
},
{
message: '"snake_case" is not lowercase',
line: 3
}
]
},
{
code: `
SCREAMING_SNAKE_CASE:
PascalCase: camelCase
`,
...options.yaml.file,
options: ['lowercase'],
errors: [
{
message: '"SCREAMING_SNAKE_CASE" is not lowercase',
line: 2
},
{
message: '"PascalCase" is not lowercase',
line: 3
}
]
},
{
code: `{
"SCREAMING_SNAKE_CASE": {
"PascalCase": "camelCase"
}
}
`,
...options.json.file,
options: ['lowercase'],
errors: [
{
message: '"SCREAMING_SNAKE_CASE" is not lowercase',
line: 2
},
{
message: '"PascalCase" is not lowercase',
line: 3
}
]
},
{
code: `
- foo
Expand Down
Loading