diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-12-10 21:51:33 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-12-10 21:51:33 +0100 |
commit | 0469abd4a9c9270a1fdc962969e36e63699af8b4 (patch) | |
tree | f9864d4a4148621378958794cbbfdc2393733283 /node_modules/tslint/lib/rules/trailingCommaRule.js | |
parent | 6947e79bbc258f7bc96af424ddb71a511f0c15a3 (diff) |
upgrade dependencies
Diffstat (limited to 'node_modules/tslint/lib/rules/trailingCommaRule.js')
-rw-r--r-- | node_modules/tslint/lib/rules/trailingCommaRule.js | 77 |
1 files changed, 50 insertions, 27 deletions
diff --git a/node_modules/tslint/lib/rules/trailingCommaRule.js b/node_modules/tslint/lib/rules/trailingCommaRule.js index e183e4ea4..784bc9a58 100644 --- a/node_modules/tslint/lib/rules/trailingCommaRule.js +++ b/node_modules/tslint/lib/rules/trailingCommaRule.js @@ -20,7 +20,7 @@ var tslib_1 = require("tslib"); var tsutils_1 = require("tsutils"); var ts = require("typescript"); var Lint = require("../index"); -var defaultOptions = fillOptions("ignore"); // tslint:disable-line no-unnecessary-type-assertion +var defaultOptions = fillOptions("ignore"); function fillOptions(value) { return { arrays: value, @@ -32,10 +32,10 @@ function fillOptions(value) { }; } function normalizeOptions(options) { - return { multiline: normalize(options.multiline), singleline: normalize(options.singleline) }; - function normalize(value) { - return typeof value === "string" ? fillOptions(value) : tslib_1.__assign({}, defaultOptions, value); - } + return { multiline: normalize(options.multiline), singleline: normalize(options.singleline), specCompliant: !!options.esSpecCompliant }; +} +function normalize(value) { + return typeof value === "string" ? fillOptions(value) : tslib_1.__assign({}, defaultOptions, value); } /* tslint:disable:object-literal-sort-keys */ var metadataOptionShape = { @@ -71,12 +71,13 @@ var Rule = /** @class */ (function (_super) { ruleName: "trailing-comma", description: (_a = ["\n Requires or disallows trailing commas in array and object literals, destructuring assignments, function typings,\n named imports and exports and function parameters."], _a.raw = ["\n Requires or disallows trailing commas in array and object literals, destructuring assignments, function typings,\n named imports and exports and function parameters."], Lint.Utils.dedent(_a)), hasFix: true, - optionsDescription: (_b = ["\n One argument which is an object with the keys `multiline` and `singleline`.\n Both can be set to a string (`\"always\"` or `\"never\"`) or an object.\n\n The object can contain any of the following keys: `\"arrays\"`, `\"objects\"`, `\"functions\"`,\n `\"imports\"`, `\"exports\"`, and `\"typeLiterals\"`; each key can have one of the following\n values: `\"always\"`, `\"never\"`, and `\"ignore\"`. Any missing keys will default to `\"ignore\"`.\n\n * `\"multiline\"` checks multi-line object literals.\n * `\"singleline\"` checks single-line object literals.\n\n An array is considered \"multiline\" if its closing bracket is on a line\n after the last array element. The same general logic is followed for\n object literals, function typings, named import statements\n and function parameters."], _b.raw = ["\n One argument which is an object with the keys \\`multiline\\` and \\`singleline\\`.\n Both can be set to a string (\\`\"always\"\\` or \\`\"never\"\\`) or an object.\n\n The object can contain any of the following keys: \\`\"arrays\"\\`, \\`\"objects\"\\`, \\`\"functions\"\\`,\n \\`\"imports\"\\`, \\`\"exports\"\\`, and \\`\"typeLiterals\"\\`; each key can have one of the following\n values: \\`\"always\"\\`, \\`\"never\"\\`, and \\`\"ignore\"\\`. Any missing keys will default to \\`\"ignore\"\\`.\n\n * \\`\"multiline\"\\` checks multi-line object literals.\n * \\`\"singleline\"\\` checks single-line object literals.\n\n An array is considered \"multiline\" if its closing bracket is on a line\n after the last array element. The same general logic is followed for\n object literals, function typings, named import statements\n and function parameters."], Lint.Utils.dedent(_b)), + optionsDescription: (_b = ["\n One argument which is an object with the keys `multiline` and `singleline`.\n Both can be set to a string (`\"always\"` or `\"never\"`) or an object.\n\n The object can contain any of the following keys: `\"arrays\"`, `\"objects\"`, `\"functions\"`,\n `\"imports\"`, `\"exports\"`, and `\"typeLiterals\"`; each key can have one of the following\n values: `\"always\"`, `\"never\"`, and `\"ignore\"`. Any missing keys will default to `\"ignore\"`.\n\n * `\"multiline\"` checks multi-line object literals.\n * `\"singleline\"` checks single-line object literals.\n\n An array is considered \"multiline\" if its closing bracket is on a line\n after the last array element. The same general logic is followed for\n object literals, function typings, named import statements\n and function parameters.\n\n To align this rule with the ECMAScript specification that is implemented in modern JavaScript VMs,\n there is a third option `esSpecCompliant`. Set this option to `true` to disallow trailing comma on\n object and array rest and rest parameters.\n "], _b.raw = ["\n One argument which is an object with the keys \\`multiline\\` and \\`singleline\\`.\n Both can be set to a string (\\`\"always\"\\` or \\`\"never\"\\`) or an object.\n\n The object can contain any of the following keys: \\`\"arrays\"\\`, \\`\"objects\"\\`, \\`\"functions\"\\`,\n \\`\"imports\"\\`, \\`\"exports\"\\`, and \\`\"typeLiterals\"\\`; each key can have one of the following\n values: \\`\"always\"\\`, \\`\"never\"\\`, and \\`\"ignore\"\\`. Any missing keys will default to \\`\"ignore\"\\`.\n\n * \\`\"multiline\"\\` checks multi-line object literals.\n * \\`\"singleline\"\\` checks single-line object literals.\n\n An array is considered \"multiline\" if its closing bracket is on a line\n after the last array element. The same general logic is followed for\n object literals, function typings, named import statements\n and function parameters.\n\n To align this rule with the ECMAScript specification that is implemented in modern JavaScript VMs,\n there is a third option \\`esSpecCompliant\\`. Set this option to \\`true\\` to disallow trailing comma on\n object and array rest and rest parameters.\n "], Lint.Utils.dedent(_b)), options: { type: "object", properties: { multiline: metadataOptionShape, singleline: metadataOptionShape, + esSpecCompliant: { type: "boolean" }, }, additionalProperties: false, }, @@ -91,6 +92,7 @@ var Rule = /** @class */ (function (_super) { functions: "never", typeLiterals: "ignore", }, + esSpecCompliant: true, }, ], ], @@ -99,6 +101,7 @@ var Rule = /** @class */ (function (_super) { }; /* tslint:enable:object-literal-sort-keys */ Rule.FAILURE_STRING_NEVER = "Unnecessary trailing comma"; + Rule.FAILURE_STRING_FORBIDDEN = "Forbidden trailing comma"; Rule.FAILURE_STRING_ALWAYS = "Missing trailing comma"; return Rule; }(Lint.Rules.AbstractRule)); @@ -113,23 +116,25 @@ var TrailingCommaWalker = /** @class */ (function (_super) { var cb = function (node) { switch (node.kind) { case ts.SyntaxKind.ArrayLiteralExpression: + _this.checkList(node.elements, node.end, "arrays", isArrayRest); + break; case ts.SyntaxKind.ArrayBindingPattern: - _this.checkList(node.elements, node.end, "arrays"); + _this.checkList(node.elements, node.end, "arrays", isDestructuringRest); break; case ts.SyntaxKind.ObjectBindingPattern: - _this.checkList(node.elements, node.end, "objects"); + _this.checkList(node.elements, node.end, "objects", isDestructuringRest); break; case ts.SyntaxKind.NamedImports: - _this.checkList(node.elements, node.end, "imports"); + _this.checkList(node.elements, node.end, "imports", noRest); break; case ts.SyntaxKind.NamedExports: - _this.checkList(node.elements, node.end, "exports"); + _this.checkList(node.elements, node.end, "exports", noRest); break; case ts.SyntaxKind.ObjectLiteralExpression: - _this.checkList(node.properties, node.end, "objects"); + _this.checkList(node.properties, node.end, "objects", isObjectRest); break; case ts.SyntaxKind.EnumDeclaration: - _this.checkList(node.members, node.end, "objects"); + _this.checkList(node.members, node.end, "objects", noRest); break; case ts.SyntaxKind.NewExpression: if (node.arguments === undefined) { @@ -137,9 +142,14 @@ var TrailingCommaWalker = /** @class */ (function (_super) { } // falls through case ts.SyntaxKind.CallExpression: - _this.checkList(node.arguments, node.end, "functions"); + _this.checkList(node.arguments, node.end, "functions", noRest); break; case ts.SyntaxKind.ArrowFunction: + // don't check arrow functions without parens around the parameter + if (tsutils_1.getChildOfKind(node, ts.SyntaxKind.OpenParenToken, _this.sourceFile) === undefined) { + break; + } + // falls through case ts.SyntaxKind.Constructor: case ts.SyntaxKind.FunctionDeclaration: case ts.SyntaxKind.FunctionExpression: @@ -150,7 +160,7 @@ var TrailingCommaWalker = /** @class */ (function (_super) { case ts.SyntaxKind.ConstructorType: case ts.SyntaxKind.FunctionType: case ts.SyntaxKind.CallSignature: - _this.checkListWithEndToken(node, node.parameters, ts.SyntaxKind.CloseParenToken, "functions"); + _this.checkList(node.parameters, tsutils_1.getChildOfKind(node, ts.SyntaxKind.CloseParenToken, _this.sourceFile).end, "functions", isRestParameter); break; case ts.SyntaxKind.TypeLiteral: _this.checkTypeLiteral(node); @@ -176,26 +186,24 @@ var TrailingCommaWalker = /** @class */ (function (_super) { } // The trailing comma is part of the last member and therefore not present as hasTrailingComma on the NodeArray var hasTrailingComma = sourceText[members.end - 1] === ","; - return this.checkComma(hasTrailingComma, members, node.end, "typeLiterals"); + return this.checkComma(hasTrailingComma, members, node.end, "typeLiterals", noRest); }; - TrailingCommaWalker.prototype.checkListWithEndToken = function (node, list, closeTokenKind, optionKey) { + TrailingCommaWalker.prototype.checkList = function (list, closeElementPos, optionKey, isRest) { if (list.length === 0) { return; } - var token = tsutils_1.getChildOfKind(node, closeTokenKind, this.sourceFile); - if (token !== undefined) { - return this.checkComma(list.hasTrailingComma, list, token.end, optionKey); - } + return this.checkComma(list.hasTrailingComma, list, closeElementPos, optionKey, isRest); }; - TrailingCommaWalker.prototype.checkList = function (list, closeElementPos, optionKey) { - if (list.length === 0) { + /* Expects `list.length !== 0` */ + TrailingCommaWalker.prototype.checkComma = function (hasTrailingComma, list, closeTokenPos, optionKey, isRest) { + var last = list[list.length - 1]; + if (this.options.specCompliant && isRest(last)) { + if (hasTrailingComma) { + this.addFailureAt(list.end - 1, 1, Rule.FAILURE_STRING_FORBIDDEN, Lint.Replacement.deleteText(list.end - 1, 1)); + } return; } - return this.checkComma(list.hasTrailingComma, list, closeElementPos, optionKey); - }; - /* Expects `list.length !== 0` */ - TrailingCommaWalker.prototype.checkComma = function (hasTrailingComma, list, closeTokenPos, optionKey) { - var options = tsutils_1.isSameLine(this.sourceFile, list[list.length - 1].end, closeTokenPos) + var options = tsutils_1.isSameLine(this.sourceFile, last.end, closeTokenPos) ? this.options.singleline : this.options.multiline; var option = options[optionKey]; @@ -208,4 +216,19 @@ var TrailingCommaWalker = /** @class */ (function (_super) { }; return TrailingCommaWalker; }(Lint.AbstractWalker)); +function isRestParameter(node) { + return node.dotDotDotToken !== undefined; +} +function isDestructuringRest(node) { + return node.kind === ts.SyntaxKind.BindingElement && node.dotDotDotToken !== undefined; +} +function isObjectRest(node) { + return node.kind === ts.SyntaxKind.SpreadAssignment && tsutils_1.isReassignmentTarget(node.expression); +} +function isArrayRest(node) { + return node.kind === ts.SyntaxKind.SpreadElement && tsutils_1.isReassignmentTarget(node); +} +function noRest() { + return false; +} var _a, _b; |