.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / preferObjectSpreadRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2017 Palantir Technologies, Inc.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
20 var tsutils_1 = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var Rule = /** @class */ (function (_super) {
24     tslib_1.__extends(Rule, _super);
25     function Rule() {
26         return _super !== null && _super.apply(this, arguments) || this;
27     }
28     Rule.prototype.apply = function (sourceFile) {
29         return this.applyWithFunction(sourceFile, walk);
30     };
31     /* tslint:disable:object-literal-sort-keys */
32     Rule.metadata = {
33         ruleName: "prefer-object-spread",
34         description: "Enforces the use of the ES2018 object spread operator over `Object.assign()` where appropriate.",
35         rationale: "Object spread allows for better type checking and inference.",
36         optionsDescription: "Not configurable.",
37         options: null,
38         optionExamples: [true],
39         type: "functionality",
40         typescriptOnly: false,
41         hasFix: true,
42     };
43     /* tslint:enable:object-literal-sort-keys */
44     Rule.FAILURE_STRING = "Use the object spread operator instead.";
45     Rule.ASSIGNMENT_FAILURE_STRING = "'Object.assign' returns the first argument. Prefer object spread if you want a new object.";
46     return Rule;
47 }(Lint.Rules.AbstractRule));
48 exports.Rule = Rule;
49 function walk(ctx) {
50     return ts.forEachChild(ctx.sourceFile, function cb(node) {
51         if (tsutils_1.isCallExpression(node) &&
52             node.arguments.length !== 0 &&
53             tsutils_1.isPropertyAccessExpression(node.expression) &&
54             node.expression.name.text === "assign" &&
55             tsutils_1.isIdentifier(node.expression.expression) &&
56             node.expression.expression.text === "Object" &&
57             !ts.isFunctionLike(node.arguments[0]) &&
58             // Object.assign(...someArray) cannot be written as object spread
59             !node.arguments.some(tsutils_1.isSpreadElement) &&
60             /**
61              * @TODO
62              * Remove !node.arguments.some(isThisKeyword) when typescript get's
63              * support for spread types.
64              * PR: https://github.com/Microsoft/TypeScript/issues/10727
65              */
66             !node.arguments.some(isThisKeyword)) {
67             if (node.arguments[0].kind === ts.SyntaxKind.ObjectLiteralExpression) {
68                 ctx.addFailureAtNode(node, Rule.FAILURE_STRING, createFix(node, ctx.sourceFile));
69             }
70             else if (tsutils_1.isExpressionValueUsed(node) &&
71                 !tsutils_1.hasSideEffects(node.arguments[0], 2 /* Constructor */)) {
72                 ctx.addFailureAtNode(node, Rule.ASSIGNMENT_FAILURE_STRING, createFix(node, ctx.sourceFile));
73             }
74         }
75         return ts.forEachChild(node, cb);
76     });
77 }
78 function createFix(node, sourceFile) {
79     var args = node.arguments;
80     var objectNeedsParens = node.parent.kind === ts.SyntaxKind.ArrowFunction;
81     var fix = [
82         Lint.Replacement.replaceFromTo(node.getStart(sourceFile), args[0].getStart(sourceFile), (objectNeedsParens ? "(" : "") + "{"),
83         new Lint.Replacement(node.end - 1, 1, "}" + (objectNeedsParens ? ")" : "")),
84     ];
85     for (var i = 0; i < args.length; ++i) {
86         var arg = args[i];
87         if (tsutils_1.isObjectLiteralExpression(arg)) {
88             if (arg.properties.length === 0) {
89                 var end = arg.end;
90                 if (i !== args.length - 1) {
91                     end = args[i + 1].getStart(sourceFile);
92                 }
93                 else if (args.hasTrailingComma) {
94                     end = args.end;
95                 }
96                 // remove empty object iteral and the following comma if exists
97                 fix.push(Lint.Replacement.deleteFromTo(arg.getStart(sourceFile), end));
98             }
99             else {
100                 fix.push(
101                 // remove open brace
102                 Lint.Replacement.deleteText(arg.getStart(sourceFile), 1), 
103                 // remove trailing comma if exists and close brace
104                 Lint.Replacement.deleteFromTo(arg.properties[arg.properties.length - 1].end, arg.end));
105             }
106         }
107         else {
108             var parens = needsParens(arg);
109             fix.push(Lint.Replacement.appendText(arg.getStart(sourceFile), parens ? "...(" : "..."));
110             if (parens) {
111                 fix.push(Lint.Replacement.appendText(arg.end, ")"));
112             }
113         }
114     }
115     return fix;
116 }
117 function isThisKeyword(node) {
118     return node.kind === ts.SyntaxKind.ThisKeyword;
119 }
120 function needsParens(node) {
121     switch (node.kind) {
122         case ts.SyntaxKind.ConditionalExpression:
123         case ts.SyntaxKind.BinaryExpression:
124             return true;
125         default:
126             return false;
127     }
128 }