This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 888
no-duplicate-imports: Allow duplicate imports from separate ambient module declarations #3398
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
import { findImports, ImportKind } from "tsutils"; | ||
import { isLiteralExpression, isModuleBlock, isModuleDeclaration } from "tsutils"; | ||
import * as ts from "typescript"; | ||
import * as Lint from "../index"; | ||
|
||
|
@@ -44,13 +44,38 @@ export class Rule extends Lint.Rules.AbstractRule { | |
} | ||
} | ||
|
||
function walk(ctx: Lint.WalkContext<void>) { | ||
const seen = new Set<string>(); | ||
for (const {text, parent} of findImports(ctx.sourceFile, ImportKind.ImportDeclaration)) { | ||
if (seen.has(text)) { | ||
ctx.addFailureAtNode(parent!, Rule.FAILURE_STRING(text)); | ||
} else { | ||
function walk(ctx: Lint.WalkContext<void>): void { | ||
walkWorker(ctx, ctx.sourceFile.statements, new Set()); | ||
} | ||
|
||
function walkWorker(ctx: Lint.WalkContext<void>, statements: ReadonlyArray<ts.Statement>, seen: Set<string>): void { | ||
for (const statement of statements) { | ||
const im = tryGetImportSpecifier(statement); | ||
if (im !== undefined && isLiteralExpression(im)) { | ||
const { text } = im; | ||
if (seen.has(text)) { | ||
ctx.addFailureAtNode(im.parent!, Rule.FAILURE_STRING(text)); | ||
} | ||
seen.add(text); | ||
} | ||
|
||
if (isModuleDeclaration(statement) && statement.body !== undefined && isModuleBlock(statement.body)) { | ||
// If this is a module augmentation, re-use `seen` since those imports could be moved outside. | ||
// If this is an ambient module, create a fresh `seen` | ||
// because they should have separate imports to avoid becoming augmentations. | ||
walkWorker(ctx, statement.body.statements, ts.isExternalModule(ctx.sourceFile) ? seen : new Set()); | ||
} | ||
} | ||
} | ||
|
||
function tryGetImportSpecifier(statement: ts.Statement): ts.Expression | undefined { | ||
switch (statement.kind) { | ||
case ts.SyntaxKind.ImportDeclaration: | ||
return (statement as ts.ImportDeclaration).moduleSpecifier; | ||
case ts.SyntaxKind.ImportEqualsDeclaration: | ||
const ref = (statement as ts.ImportEqualsDeclaration).moduleReference; | ||
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. It didn't check ImportEqualsDeclaration before your change. That's not a problem, but should be mentioned in the changelog. 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. It was right before because you might want named imports from a module in addition to importing it as a function. Removed this part. |
||
return ref.kind === ts.SyntaxKind.ExternalModuleReference ? ref.expression : undefined; | ||
default: | ||
return undefined; | ||
} | ||
} |
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,8 @@ | ||
declare module 'a' { | ||
import foo from 'foo'; | ||
} | ||
|
||
declare module 'b' { | ||
// No error -- this is a separate ambient module declaration. | ||
import foo from 'foo'; | ||
} |
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,11 @@ | ||
export {}; | ||
|
||
declare module 'a' { | ||
import foo from 'foo'; | ||
} | ||
|
||
declare module 'b' { | ||
// Error because these imports could be combined in an outer scope. | ||
import foo from 'foo'; | ||
~~~~~~~~~~~~~~~~~~~~~~ [Multiple imports from 'foo' can be combined into one.] | ||
} |
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.
you could add an additional check here if
statement.name.kind === ts.SyntaxKind.StringLiteral
, because namespaces cannot contain import statements.and then you probably don't need the
isModuleBlock
check.