UNPKG

2.42 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 Rule = (function (_super) {
20 __extends(Rule, _super);
21 function Rule() {
22 return _super !== null && _super.apply(this, arguments) || this;
23 }
24 Rule.FAILURE_STRING_FACTORY = function (initializer, expression) {
25 return "Do not use the 'for in' statement: 'for (" + initializer + " in " + expression + ")'. If this is an object, use 'Object.keys' instead. If this is an array use a standard 'for' loop instead.";
26 };
27 Rule.prototype.apply = function (sourceFile) {
28 return this.applyWithFunction(sourceFile, walk);
29 };
30 Rule.metadata = {
31 ruleName: 'no-for-in',
32 type: 'maintainability',
33 description: 'Avoid use of for-in statements. They can be replaced by Object.keys',
34 options: null,
35 optionsDescription: '',
36 typescriptOnly: true,
37 issueClass: 'Non-SDL',
38 issueType: 'Warning',
39 severity: 'Moderate',
40 level: 'Opportunity for Excellence',
41 group: 'Clarity',
42 commonWeaknessEnumeration: '398, 710'
43 };
44 return Rule;
45}(Lint.Rules.AbstractRule));
46exports.Rule = Rule;
47function walk(ctx) {
48 function cb(node) {
49 if (tsutils.isForInStatement(node)) {
50 var initializer = node.initializer.getText();
51 var expression = node.expression.getText();
52 var msg = Rule.FAILURE_STRING_FACTORY(initializer, expression);
53 ctx.addFailureAt(node.getStart(), node.getWidth(), msg);
54 }
55 return ts.forEachChild(node, cb);
56 }
57 return ts.forEachChild(ctx.sourceFile, cb);
58}
59//# sourceMappingURL=noForInRule.js.map
\No newline at end of file