From f6e52ec2933e2b3af08a85104257fd8ab6b5a114 Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Fri, 16 Jun 2017 12:06:51 +0200 Subject: [PATCH] Add support for old-style require statements in linters --- build/lib/tslint/importPatternsRule.js | 15 ++++++++++++--- build/lib/tslint/importPatternsRule.ts | 15 ++++++++++++--- build/lib/tslint/layeringRule.js | 10 +++++++++- build/lib/tslint/layeringRule.ts | 10 +++++++++- .../test/{ => node}/backupFileService.test.ts | 0 .../test/{ => node}/keybindingEditing.test.ts | 0 6 files changed, 42 insertions(+), 8 deletions(-) rename src/vs/workbench/services/backup/test/{ => node}/backupFileService.test.ts (100%) rename src/vs/workbench/services/keybinding/test/{ => node}/keybindingEditing.test.ts (100%) diff --git a/build/lib/tslint/importPatternsRule.js b/build/lib/tslint/importPatternsRule.js index 9a6284febef..f2c6fd56233 100644 --- a/build/lib/tslint/importPatternsRule.js +++ b/build/lib/tslint/importPatternsRule.js @@ -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') { diff --git a/build/lib/tslint/importPatternsRule.ts b/build/lib/tslint/importPatternsRule.ts index 47f56627efd..6365e2d96b3 100644 --- a/build/lib/tslint/importPatternsRule.ts +++ b/build/lib/tslint/importPatternsRule.ts @@ -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[]; diff --git a/build/lib/tslint/layeringRule.js b/build/lib/tslint/layeringRule.js index 37a60b2b44f..31b19f3fb10 100644 --- a/build/lib/tslint/layeringRule.js +++ b/build/lib/tslint/layeringRule.js @@ -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] === '.') { diff --git a/build/lib/tslint/layeringRule.ts b/build/lib/tslint/layeringRule.ts index c6e34623b2b..8217f11d923 100644 --- a/build/lib/tslint/layeringRule.ts +++ b/build/lib/tslint/layeringRule.ts @@ -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); diff --git a/src/vs/workbench/services/backup/test/backupFileService.test.ts b/src/vs/workbench/services/backup/test/node/backupFileService.test.ts similarity index 100% rename from src/vs/workbench/services/backup/test/backupFileService.test.ts rename to src/vs/workbench/services/backup/test/node/backupFileService.test.ts diff --git a/src/vs/workbench/services/keybinding/test/keybindingEditing.test.ts b/src/vs/workbench/services/keybinding/test/node/keybindingEditing.test.ts similarity index 100% rename from src/vs/workbench/services/keybinding/test/keybindingEditing.test.ts rename to src/vs/workbench/services/keybinding/test/node/keybindingEditing.test.ts -- GitLab