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

Make export-name RegExp more strict #642

Merged
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
2 changes: 1 addition & 1 deletion src/exportNameRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class ExportNameWalker extends Lint.RuleWalker {

private validateExport(exportedName: string, node: ts.Node): void {
const flags = Rule.getIgnoreCase(this.getOptions()) ? 'i' : '';
const regex: RegExp = new RegExp(exportedName + '..*', flags); // filename must be exported name plus any extension
const regex: RegExp = new RegExp(`^${exportedName}\\..+`, flags); // filename must be exported name plus any extension
const fileName = Utils.fileBasename(this.getSourceFile().fileName);
if (!regex.test(fileName)) {
if (!this.isSuppressed(exportedName)) {
Expand Down
16 changes: 15 additions & 1 deletion src/tests/ExportNameRuleTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,28 @@ describe('exportNameRule', (): void => {
{
failure:
'The exported module or identifier name must match the file name. ' +
'Found: ExportNameRuleFailingTestInput2.tsx and ThisIsNotTheNameOfTheFile',
'Found: ExportNameRuleFailingTestInput2.tsx and ExportNameRuleFailingTestInput',
name: 'test-data/ExportName/ExportNameRuleFailingTestInput2.tsx',
ruleName: 'export-name',
startPosition: { character: 10, line: 2 }
}
]);
});

it('for prefixed name', (): void => {
const inputFile: string = 'test-data/ExportName/PrefixedExpectedClassName.ts';
TestHelper.assertViolations(ruleName, inputFile, [
{
failure:
'The exported module or identifier name must match the file name. ' +
'Found: PrefixedExpectedClassName.ts and ExpectedClassName',
name: 'test-data/ExportName/PrefixedExpectedClassName.ts',
ruleName: 'export-name',
startPosition: { character: 10, line: 2 }
}
]);
});

it('for conflicting name in namespace', (): void => {
const inputScript: string = `
namespace com.example {
Expand Down
4 changes: 2 additions & 2 deletions test-data/ExportName/ExportNameRuleFailingTestInput2.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
class ThisIsNotTheNameOfTheFile {}
export = ThisIsNotTheNameOfTheFile; // does not match filename
class ExportNameRuleFailingTestInput {}
export = ExportNameRuleFailingTestInput; // does not match filename
4 changes: 2 additions & 2 deletions test-data/ExportName/ExportNameRulePassingTestInput2.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
class ExportNameRulePassingTestInput {}
export = ExportNameRulePassingTestInput; // matches filename
class ExportNameRulePassingTestInput2 {}
export = ExportNameRulePassingTestInput2; // matches filename
2 changes: 2 additions & 0 deletions test-data/ExportName/PrefixedExpectedClassName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ExpectedClassName {}
export = ExpectedClassName; // doesn't match file name with prefix
2 changes: 1 addition & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"chai-prefer-contains-to-index-of": true,
"chai-vague-errors": true,
"encoding": true,
"export-name": true,
"export-name": [true, { "allow": ["^Rule$"] }],
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
"function-name": true,
"import-name": true,
"informative-docs": true,
Expand Down