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
#451 : Add logic to support more specifiers for import-name #522
Merged
JoshuaKGoldberg
merged 1 commit into
microsoft:master
from
monicbhanushali:fix/451-allow-more-specifiers-import-name
Oct 7, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,15 +84,15 @@ class ImportNameRuleWalker extends ErrorTolerantWalker { | |
} | ||
|
||
private validateImport(node: ts.ImportEqualsDeclaration | ts.ImportDeclaration, importedName: string, moduleName: string): void { | ||
moduleName = moduleName.replace(/.*\//, ''); // chop off the path | ||
moduleName = this.makeCamelCase(moduleName); | ||
if (this.isImportNameValid(importedName, moduleName) === false) { | ||
const message: string = `Misnamed import. Import should be named '${moduleName}' but found '${importedName}'`; | ||
let expectedImportedName = moduleName.replace(/.*\//, ''); // chop off the path | ||
expectedImportedName = this.makeCamelCase(expectedImportedName); | ||
if (this.isImportNameValid(importedName, expectedImportedName, moduleName) === false) { | ||
const message: string = `Misnamed import. Import should be named '${expectedImportedName}' but found '${importedName}'`; | ||
const nameNode = node.kind === ts.SyntaxKind.ImportEqualsDeclaration | ||
? (<ts.ImportEqualsDeclaration>node).name | ||
: (<ts.ImportDeclaration>node).importClause!.name; | ||
const nameNodeStartPos = nameNode!.getStart(); | ||
const fix = new Lint.Replacement(nameNodeStartPos, nameNode!.end - nameNodeStartPos, moduleName); | ||
const fix = new Lint.Replacement(nameNodeStartPos, nameNode!.end - nameNodeStartPos, expectedImportedName); | ||
this.addFailureAt(node.getStart(), node.getWidth(), message, fix); | ||
} | ||
} | ||
|
@@ -104,14 +104,22 @@ class ImportNameRuleWalker extends ErrorTolerantWalker { | |
}); | ||
} | ||
|
||
private isImportNameValid(importedName: string, moduleName: string): boolean { | ||
if (moduleName === importedName) { | ||
private isImportNameValid(importedName: string, expectedImportedName: string, moduleName: string): boolean { | ||
if (expectedImportedName === importedName) { | ||
return true; | ||
} | ||
|
||
// Allowed Replacement keys are specifiers that are allowed when overriding or adding exceptions | ||
// to import-name rule. | ||
// Example: for below import statement | ||
// `import cgi from 'fs-util/cgi-common'` | ||
// The Valid specifiers are: [cgiCommon, fs-util/cgi-common, cgi-common] | ||
const allowedReplacementKeys: string[] = [expectedImportedName, moduleName, moduleName.replace(/.*\//, '')]; | ||
return Utils.exists(Object.keys(this.replacements), (replacementKey: string): boolean => { | ||
if (new RegExp(replacementKey).test(moduleName)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I replaced this Regex check with exact string match because in scenarios like below, the regex would match for both the import-names
|
||
return importedName === this.replacements[replacementKey]; | ||
for (let index = 0; allowedReplacementKeys.length > index; index = index + 1) { | ||
if (replacementKey === allowedReplacementKeys[index]) { | ||
return importedName === this.replacements[replacementKey]; | ||
} | ||
} | ||
return false; | ||
}); | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the variable name
moduleName
toexpectedModuleName
as it was confusing.Also the primary reason for new variable was to make sure that we aren't changing the input we get which the code was doing earlier.