diff --git a/lib/rules/prefer-pascal-case.ts b/lib/rules/prefer-pascal-case.ts index 882bbbb..b0859bf 100644 --- a/lib/rules/prefer-pascal-case.ts +++ b/lib/rules/prefer-pascal-case.ts @@ -15,6 +15,38 @@ import { createStorybookRule } from '../utils/create-storybook-rule' // Rule Definition //------------------------------------------------------------------------------ +function testDigit(char: string) { + const charCode = char.charCodeAt(0); + return charCode >= 48 && charCode <= 57; +} + +function testUpperCase(char: string) { + const upperCase = char.toUpperCase(); + return char === upperCase && upperCase !== char.toLowerCase(); +} + +function testLowerCase(char: string) { + const lowerCase = char.toLowerCase(); + return char === lowerCase && lowerCase !== char.toUpperCase(); +} + +function testPascalCase(name: string) { + if (!testUpperCase(name.charAt(0))) { + return false; + } + const anyNonAlphaNumeric = Array.prototype.some.call( + name.slice(1), + (char) => char.toLowerCase() === char.toUpperCase() && !testDigit(char) + ); + if (anyNonAlphaNumeric) { + return false; + } + return Array.prototype.some.call( + name.slice(1), + (char) => testLowerCase(char) || testDigit(char) + ); +} + export = createStorybookRule({ name: 'prefer-pascal-case', defaultOptions: [], @@ -41,7 +73,7 @@ export = createStorybookRule({ // Helpers //---------------------------------------------------------------------- - const isPascalCase = (str: string) => /^[A-Z]+([a-z0-9]?)+/.test(str) + const isPascalCase = (str: string) => testPascalCase(str) const toPascalCase = (str: string) => { return str .replace(new RegExp(/[-_]+/, 'g'), ' ') diff --git a/tests/lib/rules/prefer-pascal-case.test.ts b/tests/lib/rules/prefer-pascal-case.test.ts index 9d2e884..4f93c82 100644 --- a/tests/lib/rules/prefer-pascal-case.test.ts +++ b/tests/lib/rules/prefer-pascal-case.test.ts @@ -20,6 +20,7 @@ import ruleTester from '../../utils/rule-tester' ruleTester.run('prefer-pascal-case', rule, { valid: [ 'export const Primary = {}', + 'export const 测试 = {}', `export const __namedExportsOrder = ['Secondary', 'Primary']`, `export const _primary = {}`, 'export const Primary: Story = {}',