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

Implement new rule no-duplicate-imports #3075

Merged
merged 1 commit into from
Jul 27, 2017
Merged
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
1 change: 1 addition & 0 deletions src/configs/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export const rules = {
"max-file-line-count": [true, 1000],
"max-line-length": [true, 120],
"no-default-export": true,
"no-duplicate-imports": true,
"no-irregular-whitespace": true,
"no-mergeable-namespace": true,
"no-require-imports": true,
Expand Down
3 changes: 3 additions & 0 deletions src/configs/latest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export const rules = {
],
"no-this-assignment": true,
"space-within-parens": [true, 0],

// added in v5.6
"no-duplicate-imports": true,
};
// tslint:enable object-literal-sort-keys

Expand Down
74 changes: 74 additions & 0 deletions src/rules/noDuplicateImportsRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* @license
* Copyright 2017 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "no-duplicate-imports",
description: Lint.Utils.dedent`
Disallows multiple import statements from the same module.`,
rationale: Lint.Utils.dedent`
Using a single import statement per module will make the code clearer because you can see everything being imported
from that module on one line.`,
optionsDescription: "Not configurable",
options: null,
optionExamples: [true],
type: "maintainability",
typescriptOnly: false,
};

public static FAILURE_STRING(module: string) {
return `Multiple imports from '${module}' can be combined into one.`;
}

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NoDuplicateImportsWalker(sourceFile, this.ruleName, undefined));
}
}

class NoDuplicateImportsWalker extends Lint.AbstractWalker<void> {
private seenImports = new Set<string>();

public walk(sourceFile: ts.SourceFile) {
this.checkStatements(sourceFile.statements);
}

private checkStatements(statements: ts.NodeArray<ts.Statement>) {
for (const statement of statements) {
if (isImportDeclaration(statement)) {
this.checkImport(statement);
} else if (this.sourceFile.isDeclarationFile && isModuleDeclaration(statement) &&
statement.body !== undefined && statement.name.kind === ts.SyntaxKind.StringLiteral) {
// module augmentations in declaration files can contain imports
this.checkStatements((statement.body as ts.ModuleBlock).statements);
}
}
}

private checkImport(statement: ts.ImportDeclaration) {
if (isTextualLiteral(statement.moduleSpecifier)) {
if (this.seenImports.has(statement.moduleSpecifier.text)) {
return this.addFailureAtNode(statement, Rule.FAILURE_STRING(statement.moduleSpecifier.text));
}
this.seenImports.add(statement.moduleSpecifier.text);
}
}
}
7 changes: 7 additions & 0 deletions test/rules/no-duplicate-imports/test.d.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as fs from 'fs';
declare module "foo" {
import {readFile} from 'fs';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Multiple imports from 'fs' can be combined into one.]
}

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

import * as path from 'path';

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

import {parse} from 'url';
import {format} from 'url';
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [err % ('url')]

import foo from './foo';
import {bar} from './foo';
~~~~~~~~~~~~~~~~~~~~~~~~~~ [err % ('./foo')]

import {writeFile} from './fs';

[err]: Multiple imports from '%s' can be combined into one.
5 changes: 5 additions & 0 deletions test/rules/no-duplicate-imports/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-duplicate-imports": true
}
}