This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Issue #125] new rule: import name must match export name
closes #125
- Loading branch information
Showing
4 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import * as ts from 'typescript'; | ||
import * as Lint from 'tslint/lib/lint'; | ||
|
||
import ErrorTolerantWalker = require('./utils/ErrorTolerantWalker'); | ||
import SyntaxKind = require('./utils/SyntaxKind'); | ||
|
||
/** | ||
* Implementation of the import-name rule. | ||
*/ | ||
export class Rule extends Lint.Rules.AbstractRule { | ||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithWalker(new ImportNameRuleWalker(sourceFile, this.getOptions())); | ||
} | ||
} | ||
|
||
class ImportNameRuleWalker extends ErrorTolerantWalker { | ||
|
||
protected visitImportEqualsDeclaration(node: ts.ImportEqualsDeclaration): void { | ||
let name: string = node.name.text; | ||
|
||
if (node.moduleReference.kind === SyntaxKind.current().ExternalModuleReference) { | ||
let moduleRef: ts.ExternalModuleReference = <ts.ExternalModuleReference>node.moduleReference; | ||
if (moduleRef.expression.kind === SyntaxKind.current().StringLiteral) { | ||
let moduleName: string = (<ts.StringLiteral>moduleRef.expression).text; | ||
this.validateImport(node, name, moduleName); | ||
} | ||
} else if (node.moduleReference.kind === SyntaxKind.current().QualifiedName) { | ||
let moduleName = node.moduleReference.getText(); | ||
moduleName = moduleName.replace(/.*\./, ''); // chop off the qualified parts | ||
this.validateImport(node, name, moduleName); | ||
} | ||
super.visitImportEqualsDeclaration(node); | ||
} | ||
|
||
|
||
protected visitImportDeclaration(node: ts.ImportDeclaration): void { | ||
if (node.importClause.name != null) { | ||
let name: string = node.importClause.name.text; | ||
if (node.moduleSpecifier.kind === SyntaxKind.current().StringLiteral) { | ||
let moduleName: string = (<ts.StringLiteral>node.moduleSpecifier).text; | ||
this.validateImport(node, name, moduleName); | ||
} | ||
} | ||
super.visitImportDeclaration(node); | ||
} | ||
|
||
private validateImport(node: ts.Node, importedName: string, moduleName: string): void { | ||
moduleName = moduleName.replace(/.*\//, ''); // chop off the path | ||
if (moduleName !== importedName) { | ||
let message: string = `Misnamed import. Import should be named '${moduleName}' but found '${importedName}'`; | ||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), message)); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/// <reference path="../typings/mocha.d.ts" /> | ||
/// <reference path="../typings/chai.d.ts" /> | ||
|
||
/* tslint:disable:quotemark */ | ||
/* tslint:disable:no-multiline-string */ | ||
|
||
import TestHelper = require('./TestHelper'); | ||
|
||
/** | ||
* Unit tests. | ||
*/ | ||
describe('importNameRule', () : void => { | ||
|
||
var ruleName : string = 'import-name'; | ||
|
||
it('should pass on matching names of external module', () : void => { | ||
var script : string = ` | ||
import App = require('App'); | ||
import App = require('x/y/z/App'); | ||
`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [ ]); | ||
}); | ||
|
||
it('should pass on matching names of ES6 import', () : void => { | ||
var script : string = ` | ||
import App from 'App'; | ||
import App from 'x/y/z/App'; | ||
`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [ ]); | ||
}); | ||
|
||
it('should pass on matching names of simple import', () : void => { | ||
var script : string = ` | ||
import DependencyManager = DM.DependencyManager; | ||
`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [ ]); | ||
}); | ||
|
||
it('should fail on misnamed external module', () : void => { | ||
var script : string = ` | ||
import MyCoolApp = require('App'); | ||
import MyCoolApp2 = require('x/y/z/App'); | ||
`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [ | ||
{ | ||
"failure": "Misnamed import. Import should be named 'App' but found 'MyCoolApp'", | ||
"name": "file.ts", | ||
"ruleName": "import-name", | ||
"startPosition": { "character": 13, "line": 2 } | ||
}, | ||
{ | ||
"failure": "Misnamed import. Import should be named 'App' but found 'MyCoolApp2'", | ||
"name": "file.ts", | ||
"ruleName": "import-name", | ||
"startPosition": { "character": 13, "line": 3 } | ||
} | ||
]); | ||
}); | ||
|
||
it('should fail on misnamed import', () : void => { | ||
var script : string = ` | ||
import MyCoolApp from 'App'; | ||
import MyCoolApp2 from 'x/y/z/App'; | ||
`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [ | ||
{ | ||
"failure": "Misnamed import. Import should be named 'App' but found 'MyCoolApp'", | ||
"name": "file.ts", | ||
"ruleName": "import-name", | ||
"startPosition": { "character": 13, "line": 2 } | ||
}, | ||
{ | ||
"failure": "Misnamed import. Import should be named 'App' but found 'MyCoolApp2'", | ||
"name": "file.ts", | ||
"ruleName": "import-name", | ||
"startPosition": { "character": 13, "line": 3 } | ||
} | ||
]); | ||
}); | ||
|
||
it('should fail on misnamed rename', () : void => { | ||
var script : string = ` | ||
import Service = DM.DependencyManager; | ||
`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [ | ||
{ | ||
"failure": "Misnamed import. Import should be named 'DependencyManager' but found 'Service'", | ||
"name": "file.ts", | ||
"ruleName": "import-name", | ||
"startPosition": { "character": 13, "line": 2 } | ||
} | ||
]); | ||
}); | ||
|
||
}); | ||
/* tslint:enable:quotemark */ | ||
/* tslint:enable:no-multiline-string */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters