Skip to content

Commit

Permalink
fix reportDuplicateImport not reporting duplicated aliases and dupl…
Browse files Browse the repository at this point in the history
…icated imports across multiple import statements
  • Loading branch information
DetachHead committed Feb 14, 2024
1 parent 2bc89d3 commit 675e329
Showing 1 changed file with 27 additions and 30 deletions.
57 changes: 27 additions & 30 deletions packages/pyright-internal/src/analyzer/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
ImportAsNode,
ImportFromAsNode,
ImportFromNode,
ImportNode,
IndexNode,
LambdaNode,
ListComprehensionIfNode,
Expand Down Expand Up @@ -6993,43 +6994,39 @@ export class Checker extends ParseTreeWalker {
}

private _reportDuplicateImports() {
const importStatements = getTopLevelImports(this._moduleNode);

const importModuleMap = new Map<string, ImportAsNode>();

importStatements.orderedImports.forEach((importStatement) => {
const importedNames: string[] = [];
getTopLevelImports(this._moduleNode).orderedImports.forEach((importStatement) => {
if (importStatement.node.nodeType === ParseNodeType.ImportFrom) {
const symbolMap = new Map<string, ImportFromAsNode>();

importStatement.node.imports.forEach((importFromAs) => {
// Ignore duplicates if they're aliased.
if (!importFromAs.alias) {
const prevImport = symbolMap.get(importFromAs.name.value);
if (prevImport) {
this._evaluator.addDiagnostic(
DiagnosticRule.reportDuplicateImport,
LocMessage.duplicateImport().format({ importName: importFromAs.name.value }),
importFromAs.name
);
} else {
symbolMap.set(importFromAs.name.value, importFromAs);
}
}
});
} else if (importStatement.subnode) {
// Ignore duplicates if they're aliased.
if (!importStatement.subnode.alias) {
const prevImport = importModuleMap.get(importStatement.moduleName);
if (prevImport) {
const node = importFromAs.alias ?? importFromAs.name;
const name = node.value;
if (importedNames.includes(name)) {
this._evaluator.addDiagnostic(
DiagnosticRule.reportDuplicateImport,
LocMessage.duplicateImport().format({ importName: importStatement.moduleName }),
importStatement.subnode
LocMessage.duplicateImport().format({ importName: name }),
node
);
} else {
importModuleMap.set(importStatement.moduleName, importStatement.subnode);
}
importedNames.push(name);
});
} else if (importStatement.subnode) {
let node: NameNode | ImportNode;
let name: string;
if (importStatement.subnode.alias) {
node = importStatement.subnode.alias;
name = node.value;
} else {
node = importStatement.node;
name = importStatement.moduleName;
}
if (importedNames.includes(name)) {
this._evaluator.addDiagnostic(
DiagnosticRule.reportDuplicateImport,
LocMessage.duplicateImport().format({ importName: name }),
node
);
}
importedNames.push(name);
}
});
}
Expand Down

0 comments on commit 675e329

Please sign in to comment.