UNPKG

3.32 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12Object.defineProperty(exports, "__esModule", { value: true });
13var Lint = require("tslint");
14var ErrorTolerantWalker_1 = require("./utils/ErrorTolerantWalker");
15var AstUtils_1 = require("./utils/AstUtils");
16var Rule = (function (_super) {
17 __extends(Rule, _super);
18 function Rule() {
19 return _super !== null && _super.apply(this, arguments) || this;
20 }
21 Rule.prototype.apply = function (sourceFile) {
22 return this.applyWithWalker(new NoGenericArrayWalker(sourceFile, this.getOptions()));
23 };
24 Rule.metadata = {
25 ruleName: 'prefer-array-literal',
26 type: 'maintainability',
27 description: 'Use array literal syntax when declaring or instantiating array types.',
28 options: null,
29 optionsDescription: '',
30 typescriptOnly: true,
31 issueClass: 'Non-SDL',
32 issueType: 'Warning',
33 severity: 'Moderate',
34 level: 'Opportunity for Excellence',
35 group: 'Clarity',
36 commonWeaknessEnumeration: '398, 710'
37 };
38 Rule.GENERICS_FAILURE_STRING = 'Replace generic-typed Array with array literal: ';
39 Rule.CONSTRUCTOR_FAILURE_STRING = 'Replace Array constructor with an array literal: ';
40 return Rule;
41}(Lint.Rules.AbstractRule));
42exports.Rule = Rule;
43var NoGenericArrayWalker = (function (_super) {
44 __extends(NoGenericArrayWalker, _super);
45 function NoGenericArrayWalker(sourceFile, options) {
46 var _this = _super.call(this, sourceFile, options) || this;
47 _this.allowTypeParameters = false;
48 _this.getOptions().forEach(function (opt) {
49 if (typeof (opt) === 'object') {
50 _this.allowTypeParameters = opt['allow-type-parameters'] === true;
51 }
52 });
53 return _this;
54 }
55 NoGenericArrayWalker.prototype.visitTypeReference = function (node) {
56 if (this.allowTypeParameters === false) {
57 if (node.typeName.text === 'Array') {
58 var failureString = Rule.GENERICS_FAILURE_STRING + node.getText();
59 this.addFailureAt(node.getStart(), node.getWidth(), failureString);
60 }
61 }
62 _super.prototype.visitTypeReference.call(this, node);
63 };
64 NoGenericArrayWalker.prototype.visitNewExpression = function (node) {
65 var functionName = AstUtils_1.AstUtils.getFunctionName(node);
66 if (functionName === 'Array') {
67 var failureString = Rule.CONSTRUCTOR_FAILURE_STRING + node.getText();
68 this.addFailureAt(node.getStart(), node.getWidth(), failureString);
69 }
70 _super.prototype.visitNewExpression.call(this, node);
71 };
72 return NoGenericArrayWalker;
73}(ErrorTolerantWalker_1.ErrorTolerantWalker));
74//# sourceMappingURL=preferArrayLiteralRule.js.map
\No newline at end of file