Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Add kebab and snake case support to export-name rule #843

Merged
merged 1 commit into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/exportNameRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,22 @@ function walk(ctx: Lint.WalkContext<Options>) {
const flags = ignoreCase ? 'i' : '';
const regex: RegExp = new RegExp(`^${exportedName}\\..+`, flags); // filename must be exported name plus any extension
const fileName = Utils.fileBasename(ctx.sourceFile.fileName);
const fileNameAsCamelCase = convertSnakeOrKebabCaseName(fileName);

if (!regex.test(fileName)) {
if (!regex.test(fileNameAsCamelCase)) {
if (!isSuppressed(exportedName)) {
const failureString: string = Rule.FAILURE_STRING + fileName + ' and ' + exportedName;
ctx.addFailureAt(tsNode.getStart(), tsNode.getWidth(), failureString);
}
}
}

function convertSnakeOrKebabCaseName(rawName: string): string {
const snakeOrKebabRegex = /((\-|\_)\w)/g;

return rawName.replace(snakeOrKebabRegex, (match: string): string => match[1].toUpperCase());
}

function isSuppressed(exportedName: string): boolean {
return Utils.exists(
allExceptions,
Expand Down
10 changes: 10 additions & 0 deletions src/tests/ExportNameRuleTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,16 @@ describe('exportNameRule', (): void => {

TestHelper.assertViolations(ruleName, script, []);
});

it('when file name is in kebab case', (): void => {
const inputFile: string = 'test-data/ExportName/export-name-rule-passing-test-input-3.tsx';
TestHelper.assertViolations(ruleName, inputFile, []);
});

it('when file name is in snake case', (): void => {
const inputFile: string = 'test-data/ExportName/export_name_rule_passing_test_input_4.tsx';
TestHelper.assertViolations(ruleName, inputFile, []);
});
});

describe('should fail', (): void => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ExportNameRulePassingTestInput3 {}
export = ExportNameRulePassingTestInput3; // matches filename
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ExportNameRulePassingTestInput4 {}
export = ExportNameRulePassingTestInput4; // matches filename