importPatternsRule.js 2.7 KB
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5 6
"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
7 8 9 10 11 12 13 14
const ts = require("typescript");
const Lint = require("tslint");
const minimatch = require("minimatch");
const path_1 = require("path");
class Rule extends Lint.Rules.AbstractRule {
    apply(sourceFile) {
        const configs = this.getOptions().ruleArguments;
        for (const config of configs) {
J
Joao Moreno 已提交
15 16 17 18 19
            if (minimatch(sourceFile.fileName, config.target)) {
                return this.applyWithWalker(new ImportPatterns(sourceFile, this.getOptions(), config));
            }
        }
        return [];
20 21
    }
}
J
Joao Moreno 已提交
22
exports.Rule = Rule;
23 24 25 26
class ImportPatterns extends Lint.RuleWalker {
    constructor(file, opts, _config) {
        super(file, opts);
        this._config = _config;
J
Joao Moreno 已提交
27
    }
28
    visitImportEqualsDeclaration(node) {
29 30 31
        if (node.moduleReference.kind === ts.SyntaxKind.ExternalModuleReference) {
            this._validateImport(node.moduleReference.expression.getText(), node);
        }
32 33
    }
    visitImportDeclaration(node) {
34
        this._validateImport(node.moduleSpecifier.getText(), node);
35 36 37
    }
    visitCallExpression(node) {
        super.visitCallExpression(node);
38 39
        // import('foo') statements inside the code
        if (node.expression.kind === ts.SyntaxKind.ImportKeyword) {
40
            const [path] = node.arguments;
41 42
            this._validateImport(path.getText(), node);
        }
43 44
    }
    _validateImport(path, node) {
J
Joao Moreno 已提交
45 46
        // remove quotes
        path = path.slice(1, -1);
47
        // resolve relative paths
J
Joao Moreno 已提交
48
        if (path[0] === '.') {
49
            path = path_1.join(this.getSourceFile().fileName, path);
J
Joao Moreno 已提交
50
        }
51
        let restrictions;
52 53 54 55 56 57
        if (typeof this._config.restrictions === 'string') {
            restrictions = [this._config.restrictions];
        }
        else {
            restrictions = this._config.restrictions;
        }
58 59
        let matched = false;
        for (const pattern of restrictions) {
60 61 62 63 64 65 66
            if (minimatch(path, pattern)) {
                matched = true;
                break;
            }
        }
        if (!matched) {
            // None of the restrictions matched
67
            this.addFailure(this.createFailure(node.getStart(), node.getWidth(), `Imports violates '${restrictions.join(' or ')}' restrictions. See https://github.com/Microsoft/vscode/wiki/Code-Organization`));
J
Joao Moreno 已提交
68
        }
69 70
    }
}