Skip to content

Commit 47895c0

Browse files
a-tarasyukbradzacher
authored andcommitted
fix(eslint-plugin): [class-name-casing] allow unicode letters (#1043)
1 parent 192e23d commit 47895c0

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

packages/eslint-plugin/src/rules/class-name-casing.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,28 @@ export default util.createRule<Options, MessageIds>({
3838
},
3939
defaultOptions: [{ allowUnderscorePrefix: false }],
4040
create(context, [options]) {
41+
const UNDERSCORE = '_';
42+
43+
/**
44+
* Determine if the string is Upper cased
45+
* @param str
46+
*/
47+
function isUpperCase(str: string): boolean {
48+
return str === str.toUpperCase();
49+
}
50+
4151
/**
4252
* Determine if the identifier name is PascalCased
4353
* @param name The identifier name
4454
*/
4555
function isPascalCase(name: string): boolean {
46-
if (options.allowUnderscorePrefix) {
47-
return /^_?[A-Z][0-9A-Za-z]*$/.test(name);
48-
} else {
49-
return /^[A-Z][0-9A-Za-z]*$/.test(name);
50-
}
56+
const startIndex =
57+
options.allowUnderscorePrefix && name.startsWith(UNDERSCORE) ? 1 : 0;
58+
59+
return (
60+
isUpperCase(name.charAt(startIndex)) &&
61+
!name.includes(UNDERSCORE, startIndex)
62+
);
5163
}
5264

5365
/**

packages/eslint-plugin/tests/rules/class-name-casing.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,22 @@ ruleTester.run('class-name-casing', rule, {
1818
code: 'class _NameWithUnderscore {}',
1919
options: [{ allowUnderscorePrefix: true }],
2020
},
21+
{
22+
code: 'class Foo {}',
23+
options: [{ allowUnderscorePrefix: true }],
24+
},
25+
{
26+
code: 'class _ÈFoo {}',
27+
options: [{ allowUnderscorePrefix: true }],
28+
},
2129
'var Foo = class {};',
2230
'interface SomeInterface {}',
2331
'class ClassNameWithDigit2 {}',
2432
'abstract class ClassNameWithDigit2 {}',
2533
'var ba_zz = class Foo {};',
34+
'class ClassNameWithUnicodeÈ {}',
35+
'class ÈClassNameWithUnicode {}',
36+
'class ClassNameWithæUnicode {}',
2637
],
2738

2839
invalid: [
@@ -152,5 +163,19 @@ ruleTester.run('class-name-casing', rule, {
152163
},
153164
],
154165
},
166+
{
167+
code: `class æInvalidClassNameWithUnicode {}`,
168+
errors: [
169+
{
170+
messageId: 'notPascalCased',
171+
data: {
172+
friendlyName: 'Class',
173+
name: 'æInvalidClassNameWithUnicode',
174+
},
175+
line: 1,
176+
column: 7,
177+
},
178+
],
179+
},
155180
],
156181
});

0 commit comments

Comments
 (0)