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

Commit

Permalink
Make export-name RegExp more strict
Browse files Browse the repository at this point in the history
  • Loading branch information
IllusionMH committed Nov 5, 2018
1 parent 9b4180f commit d014bc5
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
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
if (!regex.test(this.getFilename())) {
if (!this.isSuppressed(exportedName)) {
const failureString: string = Rule.FAILURE_STRING + this.getSourceFile().fileName + ' and ' + 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: test-data/ExportName/ExportNameRuleFailingTestInput2.tsx and ThisIsNotTheNameOfTheFile',
'Found: test-data/ExportName/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: test-data/ExportName/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: 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$"] }],
"function-name": true,
"import-name": true,
"informative-docs": true,
Expand Down

0 comments on commit d014bc5

Please sign in to comment.