From b847e4e862ed7882b8863e78534ccc0364fd682e Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Sat, 30 Oct 2021 17:11:46 +0900 Subject: [PATCH] Add `SCREAMING_SNAKE_CASE` option to `key-format-style` rule --- lib/rules/key-format-style.ts | 7 ++++- lib/utils/casing.ts | 25 +++++++++++++++-- tests/lib/rules/key-format-style.ts | 43 +++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 3 deletions(-) diff --git a/lib/rules/key-format-style.ts b/lib/rules/key-format-style.ts index 01d8fc79..1d6ebf18 100644 --- a/lib/rules/key-format-style.ts +++ b/lib/rules/key-format-style.ts @@ -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 { diff --git a/lib/utils/casing.ts b/lib/utils/casing.ts index 74077329..5686f638 100644 --- a/lib/utils/casing.ts +++ b/lib/utils/casing.ts @@ -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. */ @@ -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 /** diff --git a/tests/lib/rules/key-format-style.ts b/tests/lib/rules/key-format-style.ts index 8e0695dc..5d3439f0 100644 --- a/tests/lib/rules/key-format-style.ts +++ b/tests/lib/rules/key-format-style.ts @@ -489,6 +489,49 @@ tester.run('key-format-style', rule as never, { line: 5 } ] + }, + { + filename: 'test.vue', + code: ` + + { + "en-US": { + "foo-bar": "baz", + "fooBar": "baz", + "foo_bar": "baz", + "FOO_BAR": "baz", + } + } + + + `, + errors: [ + '"foo-bar" is not camelCase', + '"foo_bar" is not camelCase', + '"FOO_BAR" is not camelCase' + ] + }, + { + filename: 'test.vue', + code: ` + + { + "en-US": { + "foo-bar": "baz", + "fooBar": "baz", + "foo_bar": "baz", + "FOO_BAR": "baz", + } + } + + + `, + 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' + ] } ] })