aboutsummaryrefslogtreecommitdiff
path: root/node_modules/tslint/lib/rules/semicolonRule.js
blob: 747a2acd2e886f1170b920c3c3d096f8ffd5aca6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
"use strict";
/**
 * @license
 * Copyright 2013 Palantir Technologies, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var utils = require("tsutils");
var ts = require("typescript");
var Lint = require("../index");
var OPTION_ALWAYS = "always";
var OPTION_NEVER = "never";
var OPTION_IGNORE_BOUND_CLASS_METHODS = "ignore-bound-class-methods";
var OPTION_IGNORE_INTERFACES = "ignore-interfaces";
var Rule = (function (_super) {
    tslib_1.__extends(Rule, _super);
    function Rule() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    Rule.prototype.apply = function (sourceFile) {
        var options = {
            always: this.ruleArguments.indexOf(OPTION_NEVER) === -1,
            boundClassMethods: this.ruleArguments.indexOf(OPTION_IGNORE_BOUND_CLASS_METHODS) === -1,
            interfaces: this.ruleArguments.indexOf(OPTION_IGNORE_INTERFACES) === -1,
        };
        return this.applyWithWalker(new SemicolonWalker(sourceFile, this.ruleName, options));
    };
    return Rule;
}(Lint.Rules.AbstractRule));
/* tslint:disable:object-literal-sort-keys */
Rule.metadata = {
    ruleName: "semicolon",
    description: "Enforces consistent semicolon usage at the end of every statement.",
    hasFix: true,
    optionsDescription: (_a = ["\n            One of the following arguments must be provided:\n\n            * `\"", "\"` enforces semicolons at the end of every statement.\n            * `\"", "\"` disallows semicolons at the end of every statement except for when they are necessary.\n\n            The following arguments may be optionally provided:\n\n            * `\"", "\"` skips checking semicolons at the end of interface members.\n            * `\"", "\"` skips checking semicolons at the end of bound class methods."], _a.raw = ["\n            One of the following arguments must be provided:\n\n            * \\`\"", "\"\\` enforces semicolons at the end of every statement.\n            * \\`\"", "\"\\` disallows semicolons at the end of every statement except for when they are necessary.\n\n            The following arguments may be optionally provided:\n\n            * \\`\"", "\"\\` skips checking semicolons at the end of interface members.\n            * \\`\"", "\"\\` skips checking semicolons at the end of bound class methods."], Lint.Utils.dedent(_a, OPTION_ALWAYS, OPTION_NEVER, OPTION_IGNORE_INTERFACES, OPTION_IGNORE_BOUND_CLASS_METHODS)),
    options: {
        type: "array",
        items: [
            {
                type: "string",
                enum: [OPTION_ALWAYS, OPTION_NEVER],
            },
            {
                type: "string",
                enum: [OPTION_IGNORE_INTERFACES],
            },
        ],
        additionalItems: false,
    },
    optionExamples: [
        [true, OPTION_ALWAYS],
        [true, OPTION_NEVER],
        [true, OPTION_ALWAYS, OPTION_IGNORE_INTERFACES],
        [true, OPTION_ALWAYS, OPTION_IGNORE_BOUND_CLASS_METHODS],
    ],
    type: "style",
    typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */
Rule.FAILURE_STRING_MISSING = "Missing semicolon";
Rule.FAILURE_STRING_COMMA = "Properties should be separated by semicolons";
Rule.FAILURE_STRING_UNNECESSARY = "Unnecessary semicolon";
exports.Rule = Rule;
var SemicolonWalker = (function (_super) {
    tslib_1.__extends(SemicolonWalker, _super);
    function SemicolonWalker() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.scanner = undefined;
        return _this;
    }
    SemicolonWalker.prototype.walk = function (sourceFile) {
        var _this = this;
        var cb = function (node) {
            switch (node.kind) {
                case ts.SyntaxKind.VariableStatement:
                case ts.SyntaxKind.ExpressionStatement:
                case ts.SyntaxKind.ReturnStatement:
                case ts.SyntaxKind.BreakStatement:
                case ts.SyntaxKind.ContinueStatement:
                case ts.SyntaxKind.ThrowStatement:
                case ts.SyntaxKind.ImportEqualsDeclaration:
                case ts.SyntaxKind.DoStatement:
                case ts.SyntaxKind.ExportAssignment:
                    _this.checkSemicolonAt(node);
                    break;
                case ts.SyntaxKind.TypeAliasDeclaration:
                case ts.SyntaxKind.ImportDeclaration:
                case ts.SyntaxKind.ExportDeclaration:
                case ts.SyntaxKind.DebuggerStatement:
                    _this.checkSemicolonOrLineBreak(node);
                    break;
                case ts.SyntaxKind.ModuleDeclaration:
                    // shorthand module declaration
                    if (node.body === undefined) {
                        _this.checkSemicolonOrLineBreak(node);
                    }
                    break;
                case ts.SyntaxKind.PropertyDeclaration:
                    _this.visitPropertyDeclaration(node);
                    break;
                case ts.SyntaxKind.MethodDeclaration:
                case ts.SyntaxKind.FunctionDeclaration:
                    if (node.body === undefined) {
                        _this.checkSemicolonOrLineBreak(node);
                    }
                    break;
                case ts.SyntaxKind.InterfaceDeclaration:
                    if (_this.options.interfaces) {
                        _this.checkInterface(node);
                    }
                    break;
                case ts.SyntaxKind.SemicolonClassElement:
                    return _this.reportUnnecessary(node.end - 1);
                case ts.SyntaxKind.EmptyStatement:
                    return _this.checkEmptyStatement(node);
                default:
            }
            return ts.forEachChild(node, cb);
        };
        return ts.forEachChild(sourceFile, cb);
    };
    SemicolonWalker.prototype.visitPropertyDeclaration = function (node) {
        // check if this is a multi-line arrow function
        if (node.initializer !== undefined &&
            node.initializer.kind === ts.SyntaxKind.ArrowFunction &&
            !utils.isSameLine(this.sourceFile, node.getStart(this.sourceFile), node.end)) {
            if (this.options.boundClassMethods) {
                if (this.sourceFile.text[node.end - 1] === ";" &&
                    this.isFollowedByLineBreak(node.end)) {
                    this.reportUnnecessary(node.end - 1);
                }
            }
        }
        else {
            this.checkSemicolonOrLineBreak(node);
        }
    };
    SemicolonWalker.prototype.isFollowedByLineBreak = function (pos) {
        var scanner = this.scanner !== undefined ? this.scanner :
            (this.scanner = ts.createScanner(this.sourceFile.languageVersion, true, this.sourceFile.languageVariant, this.sourceFile.text));
        scanner.setTextPos(pos);
        return scanner.scan() === ts.SyntaxKind.EndOfFileToken || scanner.hasPrecedingLineBreak();
    };
    SemicolonWalker.prototype.checkSemicolonOrLineBreak = function (node) {
        var hasSemicolon = this.sourceFile.text[node.end - 1] === ";";
        if (this.options.always && !hasSemicolon) {
            this.reportMissing(node.end);
        }
        else if (!this.options.always && hasSemicolon && this.isFollowedByLineBreak(node.end)) {
            // semicolon can be removed if followed by line break;
            this.reportUnnecessary(node.end - 1);
        }
    };
    SemicolonWalker.prototype.checkEmptyStatement = function (node) {
        // An empty statement is only ever useful when it is the only statement inside a loop
        if (!utils.isIterationStatement(node.parent)) {
            var parentKind = node.parent.kind;
            // don't remove empty statement if it is a direct child of if, with or a LabeledStatement
            // otherwise this would unintentionally change control flow
            var noFix = parentKind === ts.SyntaxKind.IfStatement ||
                parentKind === ts.SyntaxKind.LabeledStatement ||
                parentKind === ts.SyntaxKind.WithStatement;
            this.reportUnnecessary(node.end - 1, noFix);
        }
    };
    SemicolonWalker.prototype.checkInterface = function (node) {
        for (var _i = 0, _a = node.members; _i < _a.length; _i++) {
            var member = _a[_i];
            var lastChar = this.sourceFile.text[member.end - 1];
            var hasSemicolon = lastChar === ";";
            if (this.options.always && !hasSemicolon) {
                if (lastChar === ",") {
                    this.addFailureAt(member.end - 1, 1, Rule.FAILURE_STRING_COMMA, new Lint.Replacement(member.end - 1, 1, ";"));
                }
                else {
                    this.reportMissing(member.end);
                }
            }
            else if (!this.options.always && hasSemicolon &&
                (member === node.members[node.members.length - 1] || this.isFollowedByLineBreak(member.end))) {
                this.reportUnnecessary(member.end - 1);
            }
        }
    };
    SemicolonWalker.prototype.reportMissing = function (pos) {
        this.addFailureAt(pos, 0, Rule.FAILURE_STRING_MISSING, Lint.Replacement.appendText(pos, ";"));
    };
    SemicolonWalker.prototype.reportUnnecessary = function (pos, noFix) {
        this.addFailureAt(pos, 1, Rule.FAILURE_STRING_UNNECESSARY, noFix === true ? undefined : Lint.Replacement.deleteText(pos, 1));
    };
    SemicolonWalker.prototype.checkSemicolonAt = function (node) {
        var hasSemicolon = this.sourceFile.text[node.end - 1] === ";";
        if (this.options.always && !hasSemicolon) {
            this.reportMissing(node.end);
        }
        else if (!this.options.always && hasSemicolon) {
            switch (utils.getNextToken(node, this.sourceFile).kind) {
                case ts.SyntaxKind.OpenParenToken:
                case ts.SyntaxKind.OpenBracketToken:
                case ts.SyntaxKind.PlusToken:
                case ts.SyntaxKind.MinusToken:
                case ts.SyntaxKind.RegularExpressionLiteral:
                    break;
                default:
                    if (!this.isFollowedByStatement(node)) {
                        this.reportUnnecessary(node.end - 1);
                    }
            }
        }
    };
    SemicolonWalker.prototype.isFollowedByStatement = function (node) {
        var nextStatement = utils.getNextStatement(node);
        if (nextStatement === undefined) {
            return false;
        }
        return utils.isSameLine(this.sourceFile, node.end, nextStatement.getStart(this.sourceFile));
    };
    return SemicolonWalker;
}(Lint.AbstractWalker));
var _a;