Skip to content
Open
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
34 changes: 33 additions & 1 deletion lib/rules/prefer-pascal-case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand All @@ -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'), ' ')
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/prefer-pascal-case.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}',
Expand Down