From 533f479706c6d5924011937714bc6eb053766a71 Mon Sep 17 00:00:00 2001 From: Oleg Vaskevich Date: Mon, 30 Jul 2018 14:30:51 -0700 Subject: [PATCH 1/2] Add snake_case option for file-name-casing rule --- src/rules/fileNameCasingRule.ts | 13 ++++++++++--- src/utils.ts | 12 ++++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/rules/fileNameCasingRule.ts b/src/rules/fileNameCasingRule.ts index ea837f3e7bc..fcd7bdc5a17 100644 --- a/src/rules/fileNameCasingRule.ts +++ b/src/rules/fileNameCasingRule.ts @@ -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 { @@ -38,13 +39,14 @@ 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], }, ], }, @@ -52,6 +54,7 @@ export class Rule extends Lint.Rules.AbstractRule { [true, Casing.CamelCase], [true, Casing.PascalCase], [true, Casing.KebabCase], + [true, Casing.SnakeCase], ], hasFix: false, type: "style", @@ -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"; } } @@ -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); } } diff --git a/src/utils.ts b/src/utils.ts index 46e55eece35..def6c061f32 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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, "-"); +} From 5d8bbdb680858f317c28c2fc877b5dbf886b4ba2 Mon Sep 17 00:00:00 2001 From: Oleg Vaskevich Date: Tue, 31 Jul 2018 12:05:22 -0700 Subject: [PATCH 2/2] Add tests --- test/rules/file-name-casing/snake-case/NoPascalCase.lint | 2 ++ test/rules/file-name-casing/snake-case/no-camel-case.ts.lint | 2 ++ test/rules/file-name-casing/snake-case/snake_case.ts.lint | 0 test/rules/file-name-casing/snake-case/tslint.json | 5 +++++ 4 files changed, 9 insertions(+) create mode 100644 test/rules/file-name-casing/snake-case/NoPascalCase.lint create mode 100644 test/rules/file-name-casing/snake-case/no-camel-case.ts.lint create mode 100644 test/rules/file-name-casing/snake-case/snake_case.ts.lint create mode 100644 test/rules/file-name-casing/snake-case/tslint.json diff --git a/test/rules/file-name-casing/snake-case/NoPascalCase.lint b/test/rules/file-name-casing/snake-case/NoPascalCase.lint new file mode 100644 index 00000000000..d37aa03d910 --- /dev/null +++ b/test/rules/file-name-casing/snake-case/NoPascalCase.lint @@ -0,0 +1,2 @@ + +~nil [File name must be snake_case] diff --git a/test/rules/file-name-casing/snake-case/no-camel-case.ts.lint b/test/rules/file-name-casing/snake-case/no-camel-case.ts.lint new file mode 100644 index 00000000000..d37aa03d910 --- /dev/null +++ b/test/rules/file-name-casing/snake-case/no-camel-case.ts.lint @@ -0,0 +1,2 @@ + +~nil [File name must be snake_case] diff --git a/test/rules/file-name-casing/snake-case/snake_case.ts.lint b/test/rules/file-name-casing/snake-case/snake_case.ts.lint new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/rules/file-name-casing/snake-case/tslint.json b/test/rules/file-name-casing/snake-case/tslint.json new file mode 100644 index 00000000000..a08000df93c --- /dev/null +++ b/test/rules/file-name-casing/snake-case/tslint.json @@ -0,0 +1,5 @@ +{ + "rules": { + "file-name-casing": [true, "snake-case"] + } +}