importPatternsRule.js 3.9 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.
 *--------------------------------------------------------------------------------------------*/
var __extends = (this && this.__extends) || (function () {
M
Matt Bierner 已提交
7 8 9 10 11 12
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    }
J
Joao Moreno 已提交
13 14 15 16 17 18 19
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
20
var ts = require("typescript");
J
Joao Moreno 已提交
21 22
var Lint = require("tslint");
var minimatch = require("minimatch");
23
var path_1 = require("path");
J
Joao 已提交
24
var Rule = /** @class */ (function (_super) {
J
Joao Moreno 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    __extends(Rule, _super);
    function Rule() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    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;
J
Joao 已提交
42
var ImportPatterns = /** @class */ (function (_super) {
J
Joao Moreno 已提交
43 44 45 46 47 48
    __extends(ImportPatterns, _super);
    function ImportPatterns(file, opts, _config) {
        var _this = _super.call(this, file, opts) || this;
        _this._config = _config;
        return _this;
    }
49 50 51 52 53
    ImportPatterns.prototype.visitImportEqualsDeclaration = function (node) {
        if (node.moduleReference.kind === ts.SyntaxKind.ExternalModuleReference) {
            this._validateImport(node.moduleReference.expression.getText(), node);
        }
    };
J
Joao Moreno 已提交
54
    ImportPatterns.prototype.visitImportDeclaration = function (node) {
55 56
        this._validateImport(node.moduleSpecifier.getText(), node);
    };
57 58 59 60 61 62 63 64
    ImportPatterns.prototype.visitCallExpression = function (node) {
        _super.prototype.visitCallExpression.call(this, node);
        // import('foo') statements inside the code
        if (node.expression.kind === ts.SyntaxKind.ImportKeyword) {
            var path = node.arguments[0];
            this._validateImport(path.getText(), node);
        }
    };
65
    ImportPatterns.prototype._validateImport = function (path, node) {
J
Joao Moreno 已提交
66 67
        // remove quotes
        path = path.slice(1, -1);
68
        // resolve relative paths
J
Joao Moreno 已提交
69
        if (path[0] === '.') {
70
            path = path_1.join(this.getSourceFile().fileName, path);
J
Joao Moreno 已提交
71
        }
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        var restrictions;
        if (typeof this._config.restrictions === 'string') {
            restrictions = [this._config.restrictions];
        }
        else {
            restrictions = this._config.restrictions;
        }
        var matched = false;
        for (var _i = 0, restrictions_1 = restrictions; _i < restrictions_1.length; _i++) {
            var pattern = restrictions_1[_i];
            if (minimatch(path, pattern)) {
                matched = true;
                break;
            }
        }
        if (!matched) {
            // None of the restrictions matched
89
            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 已提交
90 91 92 93
        }
    };
    return ImportPatterns;
}(Lint.RuleWalker));