Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Make no-duplicate-imports consider namespace imports seperate #4231

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 19 additions & 6 deletions src/rules/noDuplicateImportsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
* limitations under the License.
*/

import { isImportDeclaration, isLiteralExpression, isModuleDeclaration } from "tsutils";
import {
isImportDeclaration,
isLiteralExpression,
isModuleDeclaration,
isNamespaceImport,
} from "tsutils";
import * as ts from "typescript";
import * as Lint from "../index";

Expand Down Expand Up @@ -45,21 +50,28 @@ export class Rule extends Lint.Rules.AbstractRule {
}

function walk(ctx: Lint.WalkContext<void>): void {
walkWorker(ctx, ctx.sourceFile.statements, new Set());
walkWorker(ctx, ctx.sourceFile.statements, new Set(), new Set());
}

function walkWorker(
ctx: Lint.WalkContext<void>,
statements: ReadonlyArray<ts.Statement>,
seen: Set<string>,
seenNamedImports: Set<string>,
seenNamespaceImports: Set<string>,
): void {
for (const statement of statements) {
if (isImportDeclaration(statement) && isLiteralExpression(statement.moduleSpecifier)) {
const { text } = statement.moduleSpecifier;
if (seen.has(text)) {
const seenSet =
Boolean(statement.importClause) &&
Boolean(statement.importClause!.namedBindings) &&
isNamespaceImport(statement.importClause!.namedBindings!)
? seenNamespaceImports
: seenNamedImports;
if (seenSet.has(text)) {
ctx.addFailureAtNode(statement, Rule.FAILURE_STRING(text));
}
seen.add(text);
seenSet.add(text);
}

if (
Expand All @@ -73,7 +85,8 @@ function walkWorker(
walkWorker(
ctx,
(statement.body as ts.ModuleBlock).statements,
ts.isExternalModule(ctx.sourceFile) ? seen : new Set(),
ts.isExternalModule(ctx.sourceFile) ? seenNamedImports : new Set(),
ts.isExternalModule(ctx.sourceFile) ? seenNamespaceImports : new Set(),
);
}
}
Expand Down
1 change: 0 additions & 1 deletion test/rules/no-duplicate-imports/test.d.ts.lint
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as fs from 'fs';
declare module "foo" {
import {readFile} from 'fs';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Multiple imports from 'fs' can be combined into one.]
}

declare module "*";
2 changes: 1 addition & 1 deletion test/rules/no-duplicate-imports/test.ts.lint
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as fs from 'fs';
import {readFile} from 'fs';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [err % ('fs')]

import * as path from 'path';
import { join } from 'path';

import {writeFileSync as wfs} from 'fs';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [err % ('fs')]
Expand Down