mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 13:00:58 +00:00
add rule for bad node_module imports, #15293
This commit is contained in:
parent
7f7c5703ba
commit
fe219220de
50
build/lib/tslint/importPatternsRule.js
Normal file
50
build/lib/tslint/importPatternsRule.js
Normal file
|
@ -0,0 +1,50 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || function (d, b) {
|
||||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
var Lint = require('tslint/lib/lint');
|
||||
var minimatch = require('minimatch');
|
||||
var Rule = (function (_super) {
|
||||
__extends(Rule, _super);
|
||||
function Rule() {
|
||||
_super.apply(this, arguments);
|
||||
}
|
||||
Rule.prototype.apply = function (sourceFile) {
|
||||
var configs = this.getOptions().ruleArguments;
|
||||
for (var _i = 0, configs_1 = configs; _i < configs_1.length; _i++) {
|
||||
var config = configs_1[_i];
|
||||
if (minimatch(sourceFile.fileName, config.target)) {
|
||||
return this.applyWithWalker(new ImportPatterns(sourceFile, this.getOptions(), config));
|
||||
}
|
||||
}
|
||||
return [];
|
||||
};
|
||||
return Rule;
|
||||
}(Lint.Rules.AbstractRule));
|
||||
exports.Rule = Rule;
|
||||
var ImportPatterns = (function (_super) {
|
||||
__extends(ImportPatterns, _super);
|
||||
function ImportPatterns(file, opts, _config) {
|
||||
_super.call(this, file, opts);
|
||||
this._config = _config;
|
||||
}
|
||||
ImportPatterns.prototype.visitImportDeclaration = function (node) {
|
||||
var path = node.moduleSpecifier.getText();
|
||||
// remove quotes
|
||||
path = path.slice(1, -1);
|
||||
// ignore relative paths
|
||||
if (path[0] === '.') {
|
||||
return;
|
||||
}
|
||||
if (!minimatch(path, this._config.restrictions)) {
|
||||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), "Imports violates '" + this._config.restrictions + "'-restriction."));
|
||||
}
|
||||
};
|
||||
return ImportPatterns;
|
||||
}(Lint.RuleWalker));
|
53
build/lib/tslint/importPatternsRule.ts
Normal file
53
build/lib/tslint/importPatternsRule.ts
Normal file
|
@ -0,0 +1,53 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as ts from 'typescript';
|
||||
import * as Lint from 'tslint/lib/lint';
|
||||
import * as minimatch from 'minimatch';
|
||||
|
||||
interface ImportPatternsConfig {
|
||||
target: string;
|
||||
restrictions: string;
|
||||
}
|
||||
|
||||
|
||||
export class Rule extends Lint.Rules.AbstractRule {
|
||||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
|
||||
|
||||
const configs = <ImportPatternsConfig[]>this.getOptions().ruleArguments;
|
||||
|
||||
|
||||
for (const config of configs) {
|
||||
if (minimatch(sourceFile.fileName, config.target)) {
|
||||
return this.applyWithWalker(new ImportPatterns(sourceFile, this.getOptions(), config));
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
class ImportPatterns extends Lint.RuleWalker {
|
||||
|
||||
constructor(file: ts.SourceFile, opts: Lint.IOptions, private _config: ImportPatternsConfig) {
|
||||
super(file, opts);
|
||||
}
|
||||
|
||||
protected visitImportDeclaration(node: ts.ImportDeclaration): void {
|
||||
let path = node.moduleSpecifier.getText();
|
||||
|
||||
// remove quotes
|
||||
path = path.slice(1, -1);
|
||||
|
||||
// ignore relative paths
|
||||
if (path[0] === '.') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!minimatch(path, this._config.restrictions)) {
|
||||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), `Imports violates '${this._config.restrictions}'-restriction.`));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,7 +3,7 @@
|
|||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import 'vs/css!./../browser/media/repl';
|
||||
import 'vs/css!vs/workbench/parts/debug/browser/media/repl';
|
||||
import * as nls from 'vs/nls';
|
||||
import uri from 'vs/base/common/uri';
|
||||
import { wireCancellationToken } from 'vs/base/common/async';
|
||||
|
|
11
tslint.json
11
tslint.json
|
@ -46,6 +46,17 @@
|
|||
"node"
|
||||
]
|
||||
}
|
||||
],
|
||||
"import-patterns": [
|
||||
false,
|
||||
{
|
||||
"target": "**/{node,electron-browser,electron-main}/**",
|
||||
"restrictions": "**/*"
|
||||
},
|
||||
{
|
||||
"target": "**/{common,browser,workbench}/**",
|
||||
"restrictions": "{**/vs/**,assert}"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue