提交 f6e52ec2 编写于 作者: A Alex Dima

Add support for old-style require statements in linters

上级 19dff43f
......@@ -14,8 +14,10 @@ var __extends = (this && this.__extends) || (function () {
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var ts = require("typescript");
var Lint = require("tslint");
var minimatch = require("minimatch");
var path_1 = require("path");
var Rule = (function (_super) {
__extends(Rule, _super);
function Rule() {
......@@ -41,13 +43,20 @@ var ImportPatterns = (function (_super) {
_this._config = _config;
return _this;
}
ImportPatterns.prototype.visitImportEqualsDeclaration = function (node) {
if (node.moduleReference.kind === ts.SyntaxKind.ExternalModuleReference) {
this._validateImport(node.moduleReference.expression.getText(), node);
}
};
ImportPatterns.prototype.visitImportDeclaration = function (node) {
var path = node.moduleSpecifier.getText();
this._validateImport(node.moduleSpecifier.getText(), node);
};
ImportPatterns.prototype._validateImport = function (path, node) {
// remove quotes
path = path.slice(1, -1);
// ignore relative paths
// resolve relative paths
if (path[0] === '.') {
return;
path = path_1.join(this.getSourceFile().fileName, path);
}
var restrictions;
if (typeof this._config.restrictions === 'string') {
......
......@@ -6,6 +6,7 @@
import * as ts from 'typescript';
import * as Lint from 'tslint';
import * as minimatch from 'minimatch';
import { join } from 'path';
interface ImportPatternsConfig {
target: string;
......@@ -34,15 +35,23 @@ class ImportPatterns extends Lint.RuleWalker {
super(file, opts);
}
protected visitImportEqualsDeclaration(node: ts.ImportEqualsDeclaration): void {
if (node.moduleReference.kind === ts.SyntaxKind.ExternalModuleReference) {
this._validateImport(node.moduleReference.expression.getText(), node);
}
}
protected visitImportDeclaration(node: ts.ImportDeclaration): void {
let path = node.moduleSpecifier.getText();
this._validateImport(node.moduleSpecifier.getText(), node);
}
private _validateImport(path: string, node: ts.Node): void {
// remove quotes
path = path.slice(1, -1);
// ignore relative paths
// resolve relative paths
if (path[0] === '.') {
return;
path = join(this.getSourceFile().fileName, path);
}
let restrictions: string[];
......
......@@ -14,6 +14,7 @@ var __extends = (this && this.__extends) || (function () {
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var ts = require("typescript");
var Lint = require("tslint");
var path_1 = require("path");
var Rule = (function (_super) {
......@@ -54,8 +55,15 @@ var LayeringRule = (function (_super) {
_this._config = config;
return _this;
}
LayeringRule.prototype.visitImportEqualsDeclaration = function (node) {
if (node.moduleReference.kind === ts.SyntaxKind.ExternalModuleReference) {
this._validateImport(node.moduleReference.expression.getText(), node);
}
};
LayeringRule.prototype.visitImportDeclaration = function (node) {
var path = node.moduleSpecifier.getText();
this._validateImport(node.moduleSpecifier.getText(), node);
};
LayeringRule.prototype._validateImport = function (path, node) {
// remove quotes
path = path.slice(1, -1);
if (path[0] === '.') {
......
......@@ -51,9 +51,17 @@ class LayeringRule extends Lint.RuleWalker {
this._config = config;
}
protected visitImportEqualsDeclaration(node: ts.ImportEqualsDeclaration): void {
if (node.moduleReference.kind === ts.SyntaxKind.ExternalModuleReference) {
this._validateImport(node.moduleReference.expression.getText(), node);
}
}
protected visitImportDeclaration(node: ts.ImportDeclaration): void {
let path = node.moduleSpecifier.getText();
this._validateImport(node.moduleSpecifier.getText(), node);
}
private _validateImport(path: string, node: ts.Node): void {
// remove quotes
path = path.slice(1, -1);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册