UNPKG

3.52 kBJavaScriptView Raw
1"use strict";
2var __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})();
15Object.defineProperty(exports, "__esModule", { value: true });
16var ts = require("typescript");
17var Lint = require("tslint");
18var tsutils = require("tsutils");
19var AstUtils_1 = require("./utils/AstUtils");
20var TypeGuard_1 = require("./utils/TypeGuard");
21var Rule = (function (_super) {
22 __extends(Rule, _super);
23 function Rule() {
24 return _super !== null && _super.apply(this, arguments) || this;
25 }
26 Rule.prototype.apply = function (sourceFile) {
27 return this.applyWithFunction(sourceFile, walk, this.parseOptions(this.getOptions()));
28 };
29 Rule.prototype.parseOptions = function (options) {
30 var value = false;
31 var ruleOptions = [];
32 if (options.ruleArguments instanceof Array) {
33 ruleOptions = options.ruleArguments;
34 }
35 if (options instanceof Array) {
36 ruleOptions = options;
37 }
38 ruleOptions.forEach(function (opt) {
39 if (TypeGuard_1.isObject(opt)) {
40 value = opt['allow-type-parameters'] === true;
41 }
42 });
43 return {
44 allowTypeParameters: value
45 };
46 };
47 Rule.metadata = {
48 ruleName: 'prefer-array-literal',
49 type: 'maintainability',
50 description: 'Use array literal syntax when declaring or instantiating array types.',
51 options: null,
52 optionsDescription: '',
53 typescriptOnly: true,
54 issueClass: 'Non-SDL',
55 issueType: 'Warning',
56 severity: 'Moderate',
57 level: 'Opportunity for Excellence',
58 group: 'Clarity',
59 commonWeaknessEnumeration: '398, 710'
60 };
61 Rule.GENERICS_FAILURE_STRING = 'Replace generic-typed Array with array literal: ';
62 Rule.CONSTRUCTOR_FAILURE_STRING = 'Replace Array constructor with an array literal: ';
63 return Rule;
64}(Lint.Rules.AbstractRule));
65exports.Rule = Rule;
66function walk(ctx) {
67 var allowTypeParameters = ctx.options.allowTypeParameters;
68 function cb(node) {
69 if (tsutils.isTypeReferenceNode(node)) {
70 if (!allowTypeParameters) {
71 if (node.typeName.text === 'Array') {
72 var failureString = Rule.GENERICS_FAILURE_STRING + node.getText();
73 ctx.addFailureAt(node.getStart(), node.getWidth(), failureString);
74 }
75 }
76 }
77 if (tsutils.isNewExpression(node)) {
78 var functionName = AstUtils_1.AstUtils.getFunctionName(node);
79 if (functionName === 'Array') {
80 var failureString = Rule.CONSTRUCTOR_FAILURE_STRING + node.getText();
81 ctx.addFailureAt(node.getStart(), node.getWidth(), failureString);
82 }
83 }
84 return ts.forEachChild(node, cb);
85 }
86 return ts.forEachChild(ctx.sourceFile, cb);
87}
88//# sourceMappingURL=preferArrayLiteralRule.js.map
\No newline at end of file