1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = function (d, b) {
|
4 | extendStatics = Object.setPrototypeOf ||
|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
7 | return extendStatics(d, b);
|
8 | }
|
9 | return function (d, b) {
|
10 | extendStatics(d, b);
|
11 | function __() { this.constructor = d; }
|
12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
13 | };
|
14 | })();
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | var ts = require("typescript");
|
17 | var Lint = require("tslint");
|
18 | var tsutils = require("tsutils");
|
19 | var OPTION_ALLOW_SIBLINGS = 'allow-siblings';
|
20 | var FAILURE_BODY_RELATIVE = 'module is being loaded from a relative path. Please use an absolute path';
|
21 | var FAILURE_BODY_SIBLINGS = 'module path starts with reference to parent directory. Please use an absolute path or sibling files/folders';
|
22 | var FAILURE_BODY_INSIDE = 'module path should not contain reference to current or parent directory inside';
|
23 | var illegalInsideRegex = /(\/|\\)\.\.?\1/;
|
24 | var Rule = (function (_super) {
|
25 | __extends(Rule, _super);
|
26 | function Rule() {
|
27 | return _super !== null && _super.apply(this, arguments) || this;
|
28 | }
|
29 | Rule.prototype.apply = function (sourceFile) {
|
30 | return this.applyWithFunction(sourceFile, walk, this.parseOptions(this.getOptions()));
|
31 | };
|
32 | Rule.prototype.parseOptions = function (options) {
|
33 | return {
|
34 | allowSiblings: options.ruleArguments.indexOf(OPTION_ALLOW_SIBLINGS) > -1
|
35 | };
|
36 | };
|
37 | Rule.metadata = {
|
38 | ruleName: 'no-relative-imports',
|
39 | type: 'maintainability',
|
40 | description: 'Do not use relative paths when importing external modules or ES6 import declarations',
|
41 | options: {
|
42 | type: 'array',
|
43 | items: {
|
44 | type: 'string',
|
45 | enum: [OPTION_ALLOW_SIBLINGS]
|
46 | },
|
47 | minLength: 0,
|
48 | maxLength: 1
|
49 | },
|
50 | optionsDescription: "One argument may be optionally provided: \n\n' +\n '* `" + OPTION_ALLOW_SIBLINGS + "` allows relative imports for files in the same or nested folders.",
|
51 | typescriptOnly: false,
|
52 | issueClass: 'Ignored',
|
53 | issueType: 'Warning',
|
54 | severity: 'Low',
|
55 | level: 'Opportunity for Excellence',
|
56 | group: 'Clarity',
|
57 | commonWeaknessEnumeration: '710'
|
58 | };
|
59 | return Rule;
|
60 | }(Lint.Rules.AbstractRule));
|
61 | exports.Rule = Rule;
|
62 | function walk(ctx) {
|
63 | var allowSiblings = ctx.options.allowSiblings;
|
64 | function getValidationErrorBody(expression) {
|
65 | if (tsutils.isStringLiteral(expression)) {
|
66 | var path = expression.text;
|
67 | if (!allowSiblings && path[0] === '.') {
|
68 | return FAILURE_BODY_RELATIVE;
|
69 | }
|
70 | if (allowSiblings && path.indexOf('..') === 0) {
|
71 | return FAILURE_BODY_SIBLINGS;
|
72 | }
|
73 | if (illegalInsideRegex.test(path)) {
|
74 | return FAILURE_BODY_INSIDE;
|
75 | }
|
76 | }
|
77 | return undefined;
|
78 | }
|
79 | function cb(node) {
|
80 | if (tsutils.isExternalModuleReference(node)) {
|
81 | var errorBody = getValidationErrorBody(node.expression);
|
82 | if (errorBody !== undefined) {
|
83 | ctx.addFailureAt(node.getStart(), node.getWidth(), "External " + errorBody + ": " + node.getText());
|
84 | }
|
85 | }
|
86 | else if (tsutils.isImportDeclaration(node)) {
|
87 | var errorBody = getValidationErrorBody(node.moduleSpecifier);
|
88 | if (errorBody !== undefined) {
|
89 | ctx.addFailureAt(node.getStart(), node.getWidth(), "Imported " + errorBody + ": " + node.getText());
|
90 | }
|
91 | }
|
92 | return ts.forEachChild(node, cb);
|
93 | }
|
94 | return ts.forEachChild(ctx.sourceFile, cb);
|
95 | }
|
96 |
|
\ | No newline at end of file |