Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit

Permalink
[FIX] [class-name-casing] handling of TSAbstractClassDeclaration (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 authored and bradzacher committed Nov 28, 2018
1 parent df47571 commit 576b1aa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/rules/class-name-casing.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @fileoverview Enforces PascalCased class and interface names.
* @author Jed Fox
* @author Armano <https://github.com/armano2>
*/
"use strict";

Expand Down Expand Up @@ -54,6 +55,9 @@ module.exports = {
case "ClassExpression":
friendlyName = "Class";
break;
case "TSAbstractClassDeclaration":
friendlyName = "Abstract class";
break;
case "TSInterfaceDeclaration":
friendlyName = "Interface";
break;
Expand All @@ -74,21 +78,19 @@ module.exports = {
//----------------------------------------------------------------------

return {
"ClassDeclaration, TSInterfaceDeclaration"(node) {
"ClassDeclaration, TSInterfaceDeclaration, TSAbstractClassDeclaration, ClassExpression"(
node
) {
// class expressions (i.e. export default class {}) are OK
if (node.id && !isPascalCase(node.id.name)) {
report(node);
}
},
VariableDeclarator(node) {
if (node.init && node.init.type === "ClassExpression") {
const id = node.id;
"VariableDeclarator[init.type='ClassExpression']"(node) {
const id = node.id;

if (node.init.id && !isPascalCase(node.init.id.name)) {
report(node.init);
} else if (id && !isPascalCase(id.name)) {
report(node.init, id);
}
if (id && !node.init.id && !isPascalCase(id.name)) {
report(node.init, id);
}
},
};
Expand Down
34 changes: 34 additions & 0 deletions tests/lib/rules/class-name-casing.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @fileoverview Enforces PascalCased class and interface names.
* @author Jed Fox
* @author Armano <https://github.com/armano2>
*/
"use strict";

Expand Down Expand Up @@ -31,6 +32,8 @@ ruleTester.run("class-name-casing", rule, {
"var Foo = class {};",
"interface SomeInterface {}",
"class ClassNameWithDigit2 {}",
"abstract class ClassNameWithDigit2 {}",
"var ba_zz = class Foo {};",
],

invalid: [
Expand Down Expand Up @@ -65,6 +68,16 @@ ruleTester.run("class-name-casing", rule, {
},
],
},
{
code: "const foo = class {};",
errors: [
{
message: "Class 'foo' must be PascalCased",
line: 1,
column: 7,
},
],
},
{
code: "var bar = class invalidName {}",
errors: [
Expand All @@ -85,5 +98,26 @@ ruleTester.run("class-name-casing", rule, {
},
],
},
{
code: "abstract class invalidClassName {}",
errors: [
{
message:
"Abstract class 'invalidClassName' must be PascalCased",
line: 1,
column: 16,
},
],
},
{
code: "declare class invalidClassName {}",
errors: [
{
message: "Class 'invalidClassName' must be PascalCased",
line: 1,
column: 15,
},
],
},
],
});

0 comments on commit 576b1aa

Please sign in to comment.