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_FOR_LOOPS = 'allow-for-loops';
|
20 | var Rule = (function (_super) {
|
21 | __extends(Rule, _super);
|
22 | function Rule() {
|
23 | return _super !== null && _super.apply(this, arguments) || this;
|
24 | }
|
25 | Rule.prototype.apply = function (sourceFile) {
|
26 | if (Rule.isWarningShown === false) {
|
27 | console.warn('Warning: no-increment-decrement rule is deprecated. Replace your usage with the TSLint increment-decrement rule.');
|
28 | Rule.isWarningShown = true;
|
29 | }
|
30 | return this.applyWithFunction(sourceFile, walk, this.parseOptions(this.getOptions()));
|
31 | };
|
32 | Rule.prototype.parseOptions = function (options) {
|
33 | return {
|
34 | allowForLoops: options.ruleArguments.indexOf(OPTION_ALLOW_FOR_LOOPS) > -1
|
35 | };
|
36 | };
|
37 | Rule.metadata = {
|
38 | ruleName: 'no-increment-decrement',
|
39 | type: 'maintainability',
|
40 | description: 'Avoid use of increment and decrement operators particularly as part of complicated expressions',
|
41 | options: {
|
42 | type: 'array',
|
43 | items: {
|
44 | type: 'string',
|
45 | enum: [OPTION_ALLOW_FOR_LOOPS]
|
46 | },
|
47 | minLength: 0,
|
48 | maxLength: 1
|
49 | },
|
50 | optionsDescription: "One argument may be optionally provided: \n\n' +\n '* `" + OPTION_ALLOW_FOR_LOOPS + "` allows increments and decrement operators to be used in for loop headers.",
|
51 | typescriptOnly: true,
|
52 | issueClass: 'Non-SDL',
|
53 | issueType: 'Warning',
|
54 | severity: 'Low',
|
55 | level: 'Opportunity for Excellence',
|
56 | recommendation: 'false',
|
57 | group: 'Correctness',
|
58 | commonWeaknessEnumeration: '398, 710'
|
59 | };
|
60 | Rule.isWarningShown = false;
|
61 | return Rule;
|
62 | }(Lint.Rules.AbstractRule));
|
63 | exports.Rule = Rule;
|
64 | function walk(ctx) {
|
65 | function validateUnaryExpression(node) {
|
66 | if (node.operator === ts.SyntaxKind.PlusPlusToken) {
|
67 | ctx.addFailureAt(node.getStart(), node.getWidth(), 'Forbidden ++ operator');
|
68 | }
|
69 | else if (node.operator === ts.SyntaxKind.MinusMinusToken) {
|
70 | ctx.addFailureAt(node.getStart(), node.getWidth(), 'Forbidden -- operator');
|
71 | }
|
72 | }
|
73 | function cb(node) {
|
74 | if (tsutils.isForStatement(node)) {
|
75 | if (ctx.options.allowForLoops) {
|
76 | cb(node.statement);
|
77 | if (node.initializer) {
|
78 | cb(node.initializer);
|
79 | }
|
80 | if (node.condition) {
|
81 | cb(node.condition);
|
82 | }
|
83 | }
|
84 | else {
|
85 | ts.forEachChild(node, cb);
|
86 | }
|
87 | return;
|
88 | }
|
89 | if (tsutils.isPostfixUnaryExpression(node)) {
|
90 | validateUnaryExpression(node);
|
91 | }
|
92 | else if (tsutils.isPrefixUnaryExpression(node)) {
|
93 | validateUnaryExpression(node);
|
94 | }
|
95 | return ts.forEachChild(node, cb);
|
96 | }
|
97 | return ts.forEachChild(ctx.sourceFile, cb);
|
98 | }
|
99 |
|
\ | No newline at end of file |