From 2a7d0d4ba8351ef7d8296f5632113607e9f50f96 Mon Sep 17 00:00:00 2001 From: "ryan.waskiewicz" Date: Fri, 5 Oct 2018 23:03:00 -0400 Subject: [PATCH 1/7] Add Exclusion Names for Rule file-name-casing - Add exclusion option for one or more files - Add test files --- src/rules/fileNameCasingRule.ts | 48 +++++++++++++++---- .../exclude/pascal-case/MyFile.ts.lint | 0 .../exclude/pascal-case/file.ts.lint | 0 .../exclude/pascal-case/index.ts.lint | 0 .../exclude/pascal-case/main.ts.lint | 2 + .../exclude/pascal-case/tslint.json | 5 ++ 6 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 test/rules/file-name-casing/exclude/pascal-case/MyFile.ts.lint create mode 100644 test/rules/file-name-casing/exclude/pascal-case/file.ts.lint create mode 100644 test/rules/file-name-casing/exclude/pascal-case/index.ts.lint create mode 100644 test/rules/file-name-casing/exclude/pascal-case/main.ts.lint create mode 100644 test/rules/file-name-casing/exclude/pascal-case/tslint.json diff --git a/src/rules/fileNameCasingRule.ts b/src/rules/fileNameCasingRule.ts index fcd7bdc5a17..ee1a06deda8 100644 --- a/src/rules/fileNameCasingRule.ts +++ b/src/rules/fileNameCasingRule.ts @@ -25,7 +25,7 @@ enum Casing { CamelCase = "camel-case", PascalCase = "pascal-case", KebabCase = "kebab-case", - SnakeCase = "snake-case", + SnakeCase = "snake-case" } export class Rule extends Lint.Rules.AbstractRule { @@ -40,25 +40,39 @@ 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.SnakeCase}\`: File names must be snake-cased: \`file_name.ts\`.`, + * \`${Casing.SnakeCase}\`: File names must be snake-cased: \`file_name.ts\`. + + If one of the above above arguments is specified, an additional array parameter may be specified containing the names of files + to exclude from the specified casing rule (including their extensions).`, options: { type: "array", items: [ { type: "string", - enum: [Casing.CamelCase, Casing.PascalCase, Casing.KebabCase, Casing.SnakeCase], + enum: [Casing.CamelCase, Casing.PascalCase, Casing.KebabCase, Casing.SnakeCase] }, + { + type: "array", + items: { + type: "string" + } + } ], + minLength: 1 }, optionExamples: [ [true, Casing.CamelCase], + [true, Casing.CamelCase, ["index.ts", "myFile.ts"]], [true, Casing.PascalCase], + [true, Casing.PascalCase, ["index.ts", "MyFile.ts"]], [true, Casing.KebabCase], + [true, Casing.KebabCase, ["index.ts", "my-file.ts"]], [true, Casing.SnakeCase], + [true, Casing.SnakeCase, ["index.ts", "my_file.ts"]] ], hasFix: false, type: "style", - typescriptOnly: false, + typescriptOnly: false }; /* tslint:enable:object-literal-sort-keys */ @@ -79,6 +93,13 @@ export class Rule extends Lint.Rules.AbstractRule { } } + private static isFileIgnored( + filesToIgnore: string[] | undefined, + fileNameWithExtension: string + ) { + return !filesToIgnore ? true : filesToIgnore.indexOf(fileNameWithExtension) === -1; + } + private static isCorrectCasing(fileName: string, casing: Casing): boolean { switch (casing) { case Casing.CamelCase: @@ -93,14 +114,23 @@ export class Rule extends Lint.Rules.AbstractRule { } public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { - if (this.ruleArguments.length !== 1) { + if (this.ruleArguments.length < 1) { return []; } - const casing = this.ruleArguments[0] as Casing; - const fileName = path.parse(sourceFile.fileName).name; - if (!Rule.isCorrectCasing(fileName, casing)) { - return [new Lint.RuleFailure(sourceFile, 0, 0, Rule.FAILURE_STRING(casing), this.ruleName)]; + const [casing, filesToIgnore] = this.ruleArguments; + + const parsedPath = path.parse(sourceFile.fileName); + const fileNameWithExtension = parsedPath.base; + const fileName = parsedPath.name; + + if ( + Rule.isFileIgnored(filesToIgnore, fileNameWithExtension) && + !Rule.isCorrectCasing(fileName, casing) + ) { + return [ + new Lint.RuleFailure(sourceFile, 0, 0, Rule.FAILURE_STRING(casing), this.ruleName) + ]; } return []; diff --git a/test/rules/file-name-casing/exclude/pascal-case/MyFile.ts.lint b/test/rules/file-name-casing/exclude/pascal-case/MyFile.ts.lint new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/rules/file-name-casing/exclude/pascal-case/file.ts.lint b/test/rules/file-name-casing/exclude/pascal-case/file.ts.lint new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/rules/file-name-casing/exclude/pascal-case/index.ts.lint b/test/rules/file-name-casing/exclude/pascal-case/index.ts.lint new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/rules/file-name-casing/exclude/pascal-case/main.ts.lint b/test/rules/file-name-casing/exclude/pascal-case/main.ts.lint new file mode 100644 index 00000000000..947411ef4df --- /dev/null +++ b/test/rules/file-name-casing/exclude/pascal-case/main.ts.lint @@ -0,0 +1,2 @@ + +~nil [File name must be PascalCase] diff --git a/test/rules/file-name-casing/exclude/pascal-case/tslint.json b/test/rules/file-name-casing/exclude/pascal-case/tslint.json new file mode 100644 index 00000000000..c27b2e4f6b6 --- /dev/null +++ b/test/rules/file-name-casing/exclude/pascal-case/tslint.json @@ -0,0 +1,5 @@ +{ + "rules": { + "file-name-casing": [true, "pascal-case", ["index.ts", "file.ts"]] + } +} From 51dd0b3de3aba32775f630232edf6676c46769c4 Mon Sep 17 00:00:00 2001 From: "ryan.waskiewicz" Date: Fri, 5 Oct 2018 23:10:47 -0400 Subject: [PATCH 2/7] Rename method for checking file exemption --- src/rules/fileNameCasingRule.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rules/fileNameCasingRule.ts b/src/rules/fileNameCasingRule.ts index ee1a06deda8..28ea771d58c 100644 --- a/src/rules/fileNameCasingRule.ts +++ b/src/rules/fileNameCasingRule.ts @@ -93,7 +93,7 @@ export class Rule extends Lint.Rules.AbstractRule { } } - private static isFileIgnored( + private static shouldFileBeConsidered( filesToIgnore: string[] | undefined, fileNameWithExtension: string ) { @@ -125,7 +125,7 @@ export class Rule extends Lint.Rules.AbstractRule { const fileName = parsedPath.name; if ( - Rule.isFileIgnored(filesToIgnore, fileNameWithExtension) && + Rule.shouldFileBeConsidered(filesToIgnore, fileNameWithExtension) && !Rule.isCorrectCasing(fileName, casing) ) { return [ From 4bf7b5ea91e4d1240c93bb2e3634c459f5c36bed Mon Sep 17 00:00:00 2001 From: "ryan.waskiewicz" Date: Sat, 6 Oct 2018 00:00:47 -0400 Subject: [PATCH 3/7] Fix lint errors --- src/rules/fileNameCasingRule.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/rules/fileNameCasingRule.ts b/src/rules/fileNameCasingRule.ts index 28ea771d58c..663dcc5e7f6 100644 --- a/src/rules/fileNameCasingRule.ts +++ b/src/rules/fileNameCasingRule.ts @@ -97,7 +97,9 @@ export class Rule extends Lint.Rules.AbstractRule { filesToIgnore: string[] | undefined, fileNameWithExtension: string ) { - return !filesToIgnore ? true : filesToIgnore.indexOf(fileNameWithExtension) === -1; + return filesToIgnore == undefined + ? true + : filesToIgnore.indexOf(fileNameWithExtension) === -1; } private static isCorrectCasing(fileName: string, casing: Casing): boolean { @@ -118,7 +120,8 @@ export class Rule extends Lint.Rules.AbstractRule { return []; } - const [casing, filesToIgnore] = this.ruleArguments; + const casing = this.ruleArguments[0] as Casing; + const filesToIgnore = this.ruleArguments[1] as string[]; const parsedPath = path.parse(sourceFile.fileName); const fileNameWithExtension = parsedPath.base; From 2aa1d9d7c85c119e2e34bde6cd8df3103ee8b1d1 Mon Sep 17 00:00:00 2001 From: "ryan.waskiewicz" Date: Sun, 7 Oct 2018 21:51:52 -0400 Subject: [PATCH 4/7] address review comments --- src/rules/fileNameCasingRule.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/rules/fileNameCasingRule.ts b/src/rules/fileNameCasingRule.ts index 663dcc5e7f6..1ad52c9b700 100644 --- a/src/rules/fileNameCasingRule.ts +++ b/src/rules/fileNameCasingRule.ts @@ -42,8 +42,8 @@ export class Rule extends Lint.Rules.AbstractRule { * \`${Casing.KebabCase}\`: File names must be kebab-cased: \`file-name.ts\`. * \`${Casing.SnakeCase}\`: File names must be snake-cased: \`file_name.ts\`. - If one of the above above arguments is specified, an additional array parameter may be specified containing the names of files - to exclude from the specified casing rule (including their extensions).`, + If one of the above arguments is specified, an additional array parameter may be specified containing the names of files to + exclude from the specified casing rule (including their extensions).`, options: { type: "array", items: [ @@ -121,7 +121,7 @@ export class Rule extends Lint.Rules.AbstractRule { } const casing = this.ruleArguments[0] as Casing; - const filesToIgnore = this.ruleArguments[1] as string[]; + const filesToIgnore = this.ruleArguments[1] as string[] | undefined; const parsedPath = path.parse(sourceFile.fileName); const fileNameWithExtension = parsedPath.base; From 596a13177a93df4106ed65828782665842223c7d Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 16 Jun 2019 15:38:23 -0400 Subject: [PATCH 5/7] Post-merge: remove some now unnecessary tests --- src/rules/fileNameCasingRule.ts | 5 +---- .../file-name-casing/exclude/pascal-case/MyFile.ts.lint | 0 test/rules/file-name-casing/exclude/pascal-case/file.ts.lint | 0 .../rules/file-name-casing/exclude/pascal-case/index.ts.lint | 0 test/rules/file-name-casing/exclude/pascal-case/main.ts.lint | 2 -- test/rules/file-name-casing/exclude/pascal-case/tslint.json | 5 ----- 6 files changed, 1 insertion(+), 11 deletions(-) delete mode 100644 test/rules/file-name-casing/exclude/pascal-case/MyFile.ts.lint delete mode 100644 test/rules/file-name-casing/exclude/pascal-case/file.ts.lint delete mode 100644 test/rules/file-name-casing/exclude/pascal-case/index.ts.lint delete mode 100644 test/rules/file-name-casing/exclude/pascal-case/main.ts.lint delete mode 100644 test/rules/file-name-casing/exclude/pascal-case/tslint.json diff --git a/src/rules/fileNameCasingRule.ts b/src/rules/fileNameCasingRule.ts index f2f6ed6d79c..0aab1979f10 100644 --- a/src/rules/fileNameCasingRule.ts +++ b/src/rules/fileNameCasingRule.ts @@ -179,11 +179,8 @@ export class Rule extends Lint.Rules.AbstractRule { }, optionExamples: [ [true, Casing.CamelCase], - [true, Casing.CamelCase, ["index.ts", "myFile.ts"]], [true, Casing.PascalCase], - [true, Casing.PascalCase, ["index.ts", "MyFile.ts"]], [true, Casing.KebabCase], - [true, Casing.KebabCase, ["index.ts", "my-file.ts"]], [true, Casing.SnakeCase], [ true, @@ -238,7 +235,7 @@ export class Rule extends Lint.Rules.AbstractRule { } public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { - if (this.ruleArguments.length < 1) { + if (this.ruleArguments.length !== 1) { return []; } diff --git a/test/rules/file-name-casing/exclude/pascal-case/MyFile.ts.lint b/test/rules/file-name-casing/exclude/pascal-case/MyFile.ts.lint deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/test/rules/file-name-casing/exclude/pascal-case/file.ts.lint b/test/rules/file-name-casing/exclude/pascal-case/file.ts.lint deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/test/rules/file-name-casing/exclude/pascal-case/index.ts.lint b/test/rules/file-name-casing/exclude/pascal-case/index.ts.lint deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/test/rules/file-name-casing/exclude/pascal-case/main.ts.lint b/test/rules/file-name-casing/exclude/pascal-case/main.ts.lint deleted file mode 100644 index 947411ef4df..00000000000 --- a/test/rules/file-name-casing/exclude/pascal-case/main.ts.lint +++ /dev/null @@ -1,2 +0,0 @@ - -~nil [File name must be PascalCase] diff --git a/test/rules/file-name-casing/exclude/pascal-case/tslint.json b/test/rules/file-name-casing/exclude/pascal-case/tslint.json deleted file mode 100644 index c27b2e4f6b6..00000000000 --- a/test/rules/file-name-casing/exclude/pascal-case/tslint.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "rules": { - "file-name-casing": [true, "pascal-case", ["index.ts", "file.ts"]] - } -} From 25a3d45802a525776d34a406cb0e64aa199cf32e Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 16 Jun 2019 15:44:53 -0400 Subject: [PATCH 6/7] Ah, corrected new test cases to fail as needed --- test/rules/file-name-casing/ignore/complaint.tsx.lint | 2 +- test/rules/file-name-casing/ignore/tslint.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/rules/file-name-casing/ignore/complaint.tsx.lint b/test/rules/file-name-casing/ignore/complaint.tsx.lint index c6a2024811e..947411ef4df 100644 --- a/test/rules/file-name-casing/ignore/complaint.tsx.lint +++ b/test/rules/file-name-casing/ignore/complaint.tsx.lint @@ -1,2 +1,2 @@ -~nil [File name must be camelCase] +~nil [File name must be PascalCase] diff --git a/test/rules/file-name-casing/ignore/tslint.json b/test/rules/file-name-casing/ignore/tslint.json index 5bf202c34c4..b9920d2c320 100644 --- a/test/rules/file-name-casing/ignore/tslint.json +++ b/test/rules/file-name-casing/ignore/tslint.json @@ -2,7 +2,7 @@ "rules": { "file-name-casing": [true, { ".ts": "ignore", - ".tsx": "camel-case" + ".tsx": "pascal-case" }] } } From ded67cafdf98c28e0519b42bb38f4767d0d78ec8 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 16 Jun 2019 15:49:43 -0400 Subject: [PATCH 7/7] Lint complaint --- src/rules/fileNameCasingRule.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rules/fileNameCasingRule.ts b/src/rules/fileNameCasingRule.ts index 0aab1979f10..342b1e970f9 100644 --- a/src/rules/fileNameCasingRule.ts +++ b/src/rules/fileNameCasingRule.ts @@ -207,7 +207,7 @@ export class Rule extends Lint.Rules.AbstractRule { ], hasFix: false, type: "style", - typescriptOnly: false + typescriptOnly: false, }; /* tslint:enable:object-literal-sort-keys */