Skip to content

Commit

Permalink
Add snake_case option for file-name-casing rule (palantir#4081)
Browse files Browse the repository at this point in the history
* Add snake_case option for file-name-casing rule

* Add tests
  • Loading branch information
oleg-codaio authored and giladgray committed Aug 1, 2018
1 parent a2fac98 commit 8603273
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/rules/fileNameCasingRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ import * as path from "path";
import * as ts from "typescript";

import * as Lint from "../index";
import { isCamelCased, isKebabCased, isPascalCased } from "../utils";
import { isCamelCased, isKebabCased, isPascalCased, isSnakeCased } from "../utils";

enum Casing {
CamelCase = "camel-case",
PascalCase = "pascal-case",
KebabCase = "kebab-case",
SnakeCase = "snake-case",
}

export class Rule extends Lint.Rules.AbstractRule {
Expand All @@ -38,20 +39,22 @@ export class Rule extends Lint.Rules.AbstractRule {
* \`${Casing.CamelCase}\`: File names must be camel-cased: \`fileName.ts\`.
* \`${Casing.PascalCase}\`: File names must be Pascal-cased: \`FileName.ts\`.
* \`${Casing.KebabCase}\`: File names must be kebab-cased: \`file-name.ts\`.`,
* \`${Casing.KebabCase}\`: File names must be kebab-cased: \`file-name.ts\`.
* \`${Casing.SnakeCase}\`: File names must be snake-cased: \`file_name.ts\`.`,
options: {
type: "array",
items: [
{
type: "string",
enum: [Casing.CamelCase, Casing.PascalCase, Casing.KebabCase],
enum: [Casing.CamelCase, Casing.PascalCase, Casing.KebabCase, Casing.SnakeCase],
},
],
},
optionExamples: [
[true, Casing.CamelCase],
[true, Casing.PascalCase],
[true, Casing.KebabCase],
[true, Casing.SnakeCase],
],
hasFix: false,
type: "style",
Expand All @@ -71,6 +74,8 @@ export class Rule extends Lint.Rules.AbstractRule {
return "PascalCase";
case Casing.KebabCase:
return "kebab-case";
case Casing.SnakeCase:
return "snake_case";
}
}

Expand All @@ -82,6 +87,8 @@ export class Rule extends Lint.Rules.AbstractRule {
return isPascalCased(fileName);
case Casing.KebabCase:
return isKebabCased(fileName);
case Casing.SnakeCase:
return isSnakeCased(fileName);
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,20 @@ export function isCamelCased(name: string): boolean {
return isLowerCase(name[0]) && !name.includes("_") && !name.includes("-");
}

export function isKebabCased(name: string): boolean {
function isSeparatorCased(name: string, disallowedSeparator: string): boolean {
for (let i = 0; i < name.length; i++) {
const c = name.charAt(i);
if (c === "_" || !isLowerCase(c)) {
if (c === disallowedSeparator || !isLowerCase(c)) {
return false;
}
}
return true;
}

export function isKebabCased(name: string): boolean {
return isSeparatorCased(name, "_");
}

export function isSnakeCased(name: string): boolean {
return isSeparatorCased(name, "-");
}
2 changes: 2 additions & 0 deletions test/rules/file-name-casing/snake-case/NoPascalCase.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

~nil [File name must be snake_case]
2 changes: 2 additions & 0 deletions test/rules/file-name-casing/snake-case/no-camel-case.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

~nil [File name must be snake_case]
Empty file.
5 changes: 5 additions & 0 deletions test/rules/file-name-casing/snake-case/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"file-name-casing": [true, "snake-case"]
}
}

0 comments on commit 8603273

Please sign in to comment.