Skip to content

Add SCREAMING_SNAKE_CASE option to key-format-style rule #257

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
Nov 16, 2021
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
7 changes: 6 additions & 1 deletion lib/rules/key-format-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import { getCasingChecker } from '../utils/casing'
import type { LocaleMessage } from '../utils/locale-messages'
const debug = debugBuilder('eslint-plugin-vue-i18n:key-format-style')

const allowedCaseOptions = ['camelCase', 'kebab-case', 'snake_case'] as const
const allowedCaseOptions = [
'camelCase',
'kebab-case',
'snake_case',
'SCREAMING_SNAKE_CASE'
] as const
type CaseOption = typeof allowedCaseOptions[number]

function create(context: RuleContext): RuleListener {
Expand Down
25 changes: 23 additions & 2 deletions lib/utils/casing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ function hasUpper(str: string) {
return /[A-Z]/u.exec(str)
}

/**
* Checks whether the given string has lower.
* @param {string} str
*/
function hasLower(str: string) {
return /[a-z]/u.test(str)
}

/**
* Checks whether the given string is kebab-case.
*/
Expand Down Expand Up @@ -74,18 +82,31 @@ export function isPascalCase(str: string): boolean {
return true
}

/**
* Checks whether the given string is SCREAMING_SNAKE_CASE.
* @param {string} str
*/
export function isScreamingSnakeCase(str: string): boolean {
if (hasLower(str) || hasSymbols(str) || /-|__|\s/u.test(str)) {
return false
}
return true
}

const checkersMap = {
'kebab-case': isKebabCase,
snake_case: isSnakeCase,
camelCase: isCamelCase,
PascalCase: isPascalCase
PascalCase: isPascalCase,
SCREAMING_SNAKE_CASE: isScreamingSnakeCase
}

export const allowedCaseOptions = [
'camelCase',
'kebab-case',
'PascalCase',
'snake_case'
'snake_case',
'SCREAMING_SNAKE_CASE'
] as const

/**
Expand Down
43 changes: 43 additions & 0 deletions tests/lib/rules/key-format-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,49 @@ tester.run('key-format-style', rule as never, {
line: 5
}
]
},
{
filename: 'test.vue',
code: `
<i18n>
{
"en-US": {
"foo-bar": "baz",
"fooBar": "baz",
"foo_bar": "baz",
"FOO_BAR": "baz",
}
}
</i18n>
<template></template>
<script></script>`,
errors: [
'"foo-bar" is not camelCase',
'"foo_bar" is not camelCase',
'"FOO_BAR" is not camelCase'
]
},
{
filename: 'test.vue',
code: `
<i18n>
{
"en-US": {
"foo-bar": "baz",
"fooBar": "baz",
"foo_bar": "baz",
"FOO_BAR": "baz",
}
}
</i18n>
<template></template>
<script></script>`,
options: ['SCREAMING_SNAKE_CASE'],
errors: [
'"foo-bar" is not SCREAMING_SNAKE_CASE',
'"fooBar" is not SCREAMING_SNAKE_CASE',
'"foo_bar" is not SCREAMING_SNAKE_CASE'
]
}
]
})