Skip to content

Commit 453dfde

Browse files
armano2JamesHenry
authored andcommitted
[FIX] [class-name-casing] handling of TSAbstractClassDeclaration (typescript-eslint#187)
1 parent ee247c5 commit 453dfde

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

Diff for: packages/eslint-plugin-typescript/lib/rules/class-name-casing.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @fileoverview Enforces PascalCased class and interface names.
33
* @author Jed Fox
4+
* @author Armano <https://github.com/armano2>
45
*/
56
"use strict";
67

@@ -54,6 +55,9 @@ module.exports = {
5455
case "ClassExpression":
5556
friendlyName = "Class";
5657
break;
58+
case "TSAbstractClassDeclaration":
59+
friendlyName = "Abstract class";
60+
break;
5761
case "TSInterfaceDeclaration":
5862
friendlyName = "Interface";
5963
break;
@@ -74,21 +78,19 @@ module.exports = {
7478
//----------------------------------------------------------------------
7579

7680
return {
77-
"ClassDeclaration, TSInterfaceDeclaration"(node) {
81+
"ClassDeclaration, TSInterfaceDeclaration, TSAbstractClassDeclaration, ClassExpression"(
82+
node
83+
) {
7884
// class expressions (i.e. export default class {}) are OK
7985
if (node.id && !isPascalCase(node.id.name)) {
8086
report(node);
8187
}
8288
},
83-
VariableDeclarator(node) {
84-
if (node.init && node.init.type === "ClassExpression") {
85-
const id = node.id;
89+
"VariableDeclarator[init.type='ClassExpression']"(node) {
90+
const id = node.id;
8691

87-
if (node.init.id && !isPascalCase(node.init.id.name)) {
88-
report(node.init);
89-
} else if (id && !isPascalCase(id.name)) {
90-
report(node.init, id);
91-
}
92+
if (id && !node.init.id && !isPascalCase(id.name)) {
93+
report(node.init, id);
9294
}
9395
},
9496
};

Diff for: packages/eslint-plugin-typescript/tests/lib/rules/class-name-casing.js

+34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @fileoverview Enforces PascalCased class and interface names.
33
* @author Jed Fox
4+
* @author Armano <https://github.com/armano2>
45
*/
56
"use strict";
67

@@ -31,6 +32,8 @@ ruleTester.run("class-name-casing", rule, {
3132
"var Foo = class {};",
3233
"interface SomeInterface {}",
3334
"class ClassNameWithDigit2 {}",
35+
"abstract class ClassNameWithDigit2 {}",
36+
"var ba_zz = class Foo {};",
3437
],
3538

3639
invalid: [
@@ -65,6 +68,16 @@ ruleTester.run("class-name-casing", rule, {
6568
},
6669
],
6770
},
71+
{
72+
code: "const foo = class {};",
73+
errors: [
74+
{
75+
message: "Class 'foo' must be PascalCased",
76+
line: 1,
77+
column: 7,
78+
},
79+
],
80+
},
6881
{
6982
code: "var bar = class invalidName {}",
7083
errors: [
@@ -85,5 +98,26 @@ ruleTester.run("class-name-casing", rule, {
8598
},
8699
],
87100
},
101+
{
102+
code: "abstract class invalidClassName {}",
103+
errors: [
104+
{
105+
message:
106+
"Abstract class 'invalidClassName' must be PascalCased",
107+
line: 1,
108+
column: 16,
109+
},
110+
],
111+
},
112+
{
113+
code: "declare class invalidClassName {}",
114+
errors: [
115+
{
116+
message: "Class 'invalidClassName' must be PascalCased",
117+
line: 1,
118+
column: 15,
119+
},
120+
],
121+
},
88122
],
89123
});

0 commit comments

Comments
 (0)