Skip to content

Commit fe21922

Browse files
committed
add rule for bad node_module imports, microsoft#15293
1 parent 7f7c570 commit fe21922

4 files changed

Lines changed: 115 additions & 1 deletion

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
"use strict";
6+
var __extends = (this && this.__extends) || function (d, b) {
7+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
8+
function __() { this.constructor = d; }
9+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10+
};
11+
var Lint = require('tslint/lib/lint');
12+
var minimatch = require('minimatch');
13+
var Rule = (function (_super) {
14+
__extends(Rule, _super);
15+
function Rule() {
16+
_super.apply(this, arguments);
17+
}
18+
Rule.prototype.apply = function (sourceFile) {
19+
var configs = this.getOptions().ruleArguments;
20+
for (var _i = 0, configs_1 = configs; _i < configs_1.length; _i++) {
21+
var config = configs_1[_i];
22+
if (minimatch(sourceFile.fileName, config.target)) {
23+
return this.applyWithWalker(new ImportPatterns(sourceFile, this.getOptions(), config));
24+
}
25+
}
26+
return [];
27+
};
28+
return Rule;
29+
}(Lint.Rules.AbstractRule));
30+
exports.Rule = Rule;
31+
var ImportPatterns = (function (_super) {
32+
__extends(ImportPatterns, _super);
33+
function ImportPatterns(file, opts, _config) {
34+
_super.call(this, file, opts);
35+
this._config = _config;
36+
}
37+
ImportPatterns.prototype.visitImportDeclaration = function (node) {
38+
var path = node.moduleSpecifier.getText();
39+
// remove quotes
40+
path = path.slice(1, -1);
41+
// ignore relative paths
42+
if (path[0] === '.') {
43+
return;
44+
}
45+
if (!minimatch(path, this._config.restrictions)) {
46+
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), "Imports violates '" + this._config.restrictions + "'-restriction."));
47+
}
48+
};
49+
return ImportPatterns;
50+
}(Lint.RuleWalker));
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import * as ts from 'typescript';
7+
import * as Lint from 'tslint/lib/lint';
8+
import * as minimatch from 'minimatch';
9+
10+
interface ImportPatternsConfig {
11+
target: string;
12+
restrictions: string;
13+
}
14+
15+
16+
export class Rule extends Lint.Rules.AbstractRule {
17+
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
18+
19+
const configs = <ImportPatternsConfig[]>this.getOptions().ruleArguments;
20+
21+
22+
for (const config of configs) {
23+
if (minimatch(sourceFile.fileName, config.target)) {
24+
return this.applyWithWalker(new ImportPatterns(sourceFile, this.getOptions(), config));
25+
}
26+
}
27+
28+
return [];
29+
}
30+
}
31+
32+
class ImportPatterns extends Lint.RuleWalker {
33+
34+
constructor(file: ts.SourceFile, opts: Lint.IOptions, private _config: ImportPatternsConfig) {
35+
super(file, opts);
36+
}
37+
38+
protected visitImportDeclaration(node: ts.ImportDeclaration): void {
39+
let path = node.moduleSpecifier.getText();
40+
41+
// remove quotes
42+
path = path.slice(1, -1);
43+
44+
// ignore relative paths
45+
if (path[0] === '.') {
46+
return;
47+
}
48+
49+
if (!minimatch(path, this._config.restrictions)) {
50+
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), `Imports violates '${this._config.restrictions}'-restriction.`));
51+
}
52+
}
53+
}

src/vs/workbench/parts/debug/electron-browser/repl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import 'vs/css!./../browser/media/repl';
6+
import 'vs/css!vs/workbench/parts/debug/browser/media/repl';
77
import * as nls from 'vs/nls';
88
import uri from 'vs/base/common/uri';
99
import { wireCancellationToken } from 'vs/base/common/async';

tslint.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@
4646
"node"
4747
]
4848
}
49+
],
50+
"import-patterns": [
51+
false,
52+
{
53+
"target": "**/{node,electron-browser,electron-main}/**",
54+
"restrictions": "**/*"
55+
},
56+
{
57+
"target": "**/{common,browser,workbench}/**",
58+
"restrictions": "{**/vs/**,assert}"
59+
}
4960
]
5061
}
5162
}

0 commit comments

Comments
 (0)