Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Add Exclusion Names for Rule file-name-casing #4204

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 41 additions & 8 deletions src/rules/fileNameCasingRule.ts
Expand Up @@ -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 {
Expand All @@ -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
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
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 */

Expand All @@ -79,6 +93,15 @@ export class Rule extends Lint.Rules.AbstractRule {
}
}

private static shouldFileBeConsidered(
filesToIgnore: string[] | undefined,
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
fileNameWithExtension: string
) {
return filesToIgnore == undefined
? true
: filesToIgnore.indexOf(fileNameWithExtension) === -1;
}

private static isCorrectCasing(fileName: string, casing: Casing): boolean {
switch (casing) {
case Casing.CamelCase:
Expand All @@ -93,14 +116,24 @@ 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 filesToIgnore = this.ruleArguments[1] as string[];

const parsedPath = path.parse(sourceFile.fileName);
const fileNameWithExtension = parsedPath.base;
const fileName = parsedPath.name;

if (
Rule.shouldFileBeConsidered(filesToIgnore, fileNameWithExtension) &&
!Rule.isCorrectCasing(fileName, casing)
) {
return [
new Lint.RuleFailure(sourceFile, 0, 0, Rule.FAILURE_STRING(casing), this.ruleName)
];
}

return [];
Expand Down
2 changes: 2 additions & 0 deletions test/rules/file-name-casing/exclude/pascal-case/main.ts.lint
@@ -0,0 +1,2 @@

~nil [File name must be PascalCase]
5 changes: 5 additions & 0 deletions 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"]]
}
}