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

import-blacklist: Refactor to use AbstractWalker #2613

Merged
merged 2 commits into from
Apr 29, 2017
Merged
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
55 changes: 25 additions & 30 deletions src/rules/importBlacklistRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
* limitations under the License.
*/

import * as utils from "tsutils";
import {
isCallExpression, isExternalModuleReference, isIdentifier, isImportDeclaration, isImportEqualsDeclaration, isTextualLiteral,
} from "tsutils";
import * as ts from "typescript";
import * as Lint from "../index";

Expand Down Expand Up @@ -49,43 +51,36 @@ export class Rule extends Lint.Rules.AbstractRule {
}

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

class ImportBlacklistWalker extends Lint.RuleWalker {
public visitCallExpression(node: ts.CallExpression) {
if (node.expression.kind === ts.SyntaxKind.Identifier &&
(node.expression as ts.Identifier).text === "require" &&
node.arguments !== undefined &&
node.arguments.length === 1) {
class ImportBlacklistWalker extends Lint.AbstractWalker<string[]> {
public walk(sourceFile: ts.SourceFile) {
const findRequire = (node: ts.Node): void => {
if (isCallExpression(node) && node.arguments.length === 1 &&
isIdentifier(node.expression) && node.expression.text === "require") {
this.checkForBannedImport(node.arguments[0]);
}
return ts.forEachChild(node, findRequire);
};

this.checkForBannedImport(node.arguments[0]);
for (const statement of sourceFile.statements) {
if (isImportDeclaration(statement)) {
this.checkForBannedImport(statement.moduleSpecifier);
} else if (isImportEqualsDeclaration(statement)) {
if (isExternalModuleReference(statement.moduleReference) && statement.moduleReference.expression !== undefined) {
this.checkForBannedImport(statement.moduleReference.expression);
}
} else {
ts.forEachChild(statement, findRequire);
}
}
super.visitCallExpression(node);
}

public visitImportEqualsDeclaration(node: ts.ImportEqualsDeclaration) {
if (utils.isExternalModuleReference(node.moduleReference) &&
node.moduleReference.expression !== undefined) {
// If it's an import require and not an import alias
this.checkForBannedImport(node.moduleReference.expression);
}
super.visitImportEqualsDeclaration(node);
}

public visitImportDeclaration(node: ts.ImportDeclaration) {
this.checkForBannedImport(node.moduleSpecifier);
super.visitImportDeclaration(node);
}

private checkForBannedImport(expression: ts.Expression) {
if (utils.isTextualLiteral(expression) && this.hasOption(expression.text)) {
this.addFailureFromStartToEnd(
expression.getStart(this.getSourceFile()) + 1,
expression.getEnd() - 1,
Rule.FAILURE_STRING,
);
if (isTextualLiteral(expression) && this.options.indexOf(expression.text) !== -1) {
this.addFailure(expression.getStart(this.sourceFile) + 1, expression.end - 1, Rule.FAILURE_STRING);
}
}
}