4 * Copyright 2017 Palantir Technologies, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
21 var tsutils_1 = require("tsutils");
22 var ts = require("typescript");
23 var Lint = require("../index");
24 var singeLineConfigOptionName = "singleLine";
25 var Rule = /** @class */ (function (_super) {
26 tslib_1.__extends(Rule, _super);
28 return _super !== null && _super.apply(this, arguments) || this;
30 Rule.prototype.apply = function (sourceFile) {
31 return this.applyWithFunction(sourceFile, walk, this.getRuleOptions());
33 Rule.prototype.getRuleOptions = function () {
34 if (this.ruleArguments[0] === undefined) {
38 return this.ruleArguments[0];
41 /* tslint:disable:object-literal-sort-keys */
43 ruleName: "type-literal-delimiter",
44 description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Checks that type literal members are separated by semicolons.\n Enforces a trailing semicolon for multiline type literals."], ["\n Checks that type literal members are separated by semicolons.\n Enforces a trailing semicolon for multiline type literals."]))),
45 optionsDescription: "`{" + singeLineConfigOptionName + ": \"always\"}` enforces semicolon for one liners",
49 _a[singeLineConfigOptionName] = {
51 enum: ["always", "never"],
56 optionExamples: [true],
60 /* tslint:enable:object-literal-sort-keys */
61 Rule.FAILURE_STRING_MISSING = "Expected type literal to use ';' to separate members.";
62 Rule.FAILURE_STRING_COMMA = "Expected type literal to use ';' instead of ','.";
63 Rule.FAILURE_STRING_TRAILING = "Did not expect single-line type literal to have a trailing ';'.";
65 }(Lint.Rules.AbstractRule));
68 var sourceFile = ctx.sourceFile, options = ctx.options;
69 ts.forEachChild(sourceFile, function cb(node) {
70 if (tsutils_1.isTypeLiteralNode(node)) {
73 ts.forEachChild(node, cb);
75 function check(node) {
76 node.members.forEach(function (member, idx) {
77 var end = member.end - 1;
78 // Check if delimiter should be ommitted for a single-line type literal.
79 var shouldOmit = options.singleLine === "always"
81 : idx === node.members.length - 1 &&
82 tsutils_1.isSameLine(sourceFile, node.getStart(sourceFile), node.getEnd());
83 var delimiter = sourceFile.text[end];
87 ctx.addFailureAt(end, 1, Rule.FAILURE_STRING_TRAILING, Lint.Replacement.replaceFromTo(end, end + 1, ""));
91 ctx.addFailureAt(end, 1, Rule.FAILURE_STRING_COMMA, Lint.Replacement.replaceFromTo(end, end + 1, ";"));
95 ctx.addFailureAt(end, 1, Rule.FAILURE_STRING_MISSING, Lint.Replacement.replaceFromTo(end + 1, end + 1, ";"));
101 var templateObject_1;