Skip to content
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

fix: prefer-pascal-case report error for non-latin characters #122

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 33 additions & 1 deletion lib/rules/prefer-pascal-case.ts
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
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