Skip to content

Commit b8d2f27

Browse files
authored
Add SCREAMING_SNAKE_CASE option to key-format-style rule (#257)
1 parent cda51de commit b8d2f27

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

lib/rules/key-format-style.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import { getCasingChecker } from '../utils/casing'
1111
import type { LocaleMessage } from '../utils/locale-messages'
1212
const debug = debugBuilder('eslint-plugin-vue-i18n:key-format-style')
1313

14-
const allowedCaseOptions = ['camelCase', 'kebab-case', 'snake_case'] as const
14+
const allowedCaseOptions = [
15+
'camelCase',
16+
'kebab-case',
17+
'snake_case',
18+
'SCREAMING_SNAKE_CASE'
19+
] as const
1520
type CaseOption = typeof allowedCaseOptions[number]
1621

1722
function create(context: RuleContext): RuleListener {

lib/utils/casing.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ function hasUpper(str: string) {
1616
return /[A-Z]/u.exec(str)
1717
}
1818

19+
/**
20+
* Checks whether the given string has lower.
21+
* @param {string} str
22+
*/
23+
function hasLower(str: string) {
24+
return /[a-z]/u.test(str)
25+
}
26+
1927
/**
2028
* Checks whether the given string is kebab-case.
2129
*/
@@ -74,18 +82,31 @@ export function isPascalCase(str: string): boolean {
7482
return true
7583
}
7684

85+
/**
86+
* Checks whether the given string is SCREAMING_SNAKE_CASE.
87+
* @param {string} str
88+
*/
89+
export function isScreamingSnakeCase(str: string): boolean {
90+
if (hasLower(str) || hasSymbols(str) || /-|__|\s/u.test(str)) {
91+
return false
92+
}
93+
return true
94+
}
95+
7796
const checkersMap = {
7897
'kebab-case': isKebabCase,
7998
snake_case: isSnakeCase,
8099
camelCase: isCamelCase,
81-
PascalCase: isPascalCase
100+
PascalCase: isPascalCase,
101+
SCREAMING_SNAKE_CASE: isScreamingSnakeCase
82102
}
83103

84104
export const allowedCaseOptions = [
85105
'camelCase',
86106
'kebab-case',
87107
'PascalCase',
88-
'snake_case'
108+
'snake_case',
109+
'SCREAMING_SNAKE_CASE'
89110
] as const
90111

91112
/**

tests/lib/rules/key-format-style.ts

+43
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,49 @@ tester.run('key-format-style', rule as never, {
489489
line: 5
490490
}
491491
]
492+
},
493+
{
494+
filename: 'test.vue',
495+
code: `
496+
<i18n>
497+
{
498+
"en-US": {
499+
"foo-bar": "baz",
500+
"fooBar": "baz",
501+
"foo_bar": "baz",
502+
"FOO_BAR": "baz",
503+
}
504+
}
505+
</i18n>
506+
<template></template>
507+
<script></script>`,
508+
errors: [
509+
'"foo-bar" is not camelCase',
510+
'"foo_bar" is not camelCase',
511+
'"FOO_BAR" is not camelCase'
512+
]
513+
},
514+
{
515+
filename: 'test.vue',
516+
code: `
517+
<i18n>
518+
{
519+
"en-US": {
520+
"foo-bar": "baz",
521+
"fooBar": "baz",
522+
"foo_bar": "baz",
523+
"FOO_BAR": "baz",
524+
}
525+
}
526+
</i18n>
527+
<template></template>
528+
<script></script>`,
529+
options: ['SCREAMING_SNAKE_CASE'],
530+
errors: [
531+
'"foo-bar" is not SCREAMING_SNAKE_CASE',
532+
'"fooBar" is not SCREAMING_SNAKE_CASE',
533+
'"foo_bar" is not SCREAMING_SNAKE_CASE'
534+
]
492535
}
493536
]
494537
})

0 commit comments

Comments
 (0)