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");
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);
26 return _super !== null && _super.apply(this, arguments) || this;
28 Rule.prototype.apply = function (sourceFile) {
29 return this.applyWithFunction(sourceFile, walk);
31 /* tslint:disable:object-literal-sort-keys */
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.",
38 optionExamples: [true],
39 type: "functionality",
40 typescriptOnly: false,
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.";
47 }(Lint.Rules.AbstractRule));
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) &&
62 * Remove !node.arguments.some(isThisKeyword) when typescript get's
63 * support for spread types.
64 * PR: https://github.com/Microsoft/TypeScript/issues/10727
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));
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));
75 return ts.forEachChild(node, cb);
78 function createFix(node, sourceFile) {
79 var args = node.arguments;
80 var objectNeedsParens = node.parent.kind === ts.SyntaxKind.ArrowFunction;
82 Lint.Replacement.replaceFromTo(node.getStart(sourceFile), args[0].getStart(sourceFile), (objectNeedsParens ? "(" : "") + "{"),
83 new Lint.Replacement(node.end - 1, 1, "}" + (objectNeedsParens ? ")" : "")),
85 for (var i = 0; i < args.length; ++i) {
87 if (tsutils_1.isObjectLiteralExpression(arg)) {
88 if (arg.properties.length === 0) {
90 if (i !== args.length - 1) {
91 end = args[i + 1].getStart(sourceFile);
93 else if (args.hasTrailingComma) {
96 // remove empty object iteral and the following comma if exists
97 fix.push(Lint.Replacement.deleteFromTo(arg.getStart(sourceFile), end));
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));
108 var parens = needsParens(arg);
109 fix.push(Lint.Replacement.appendText(arg.getStart(sourceFile), parens ? "...(" : "..."));
111 fix.push(Lint.Replacement.appendText(arg.end, ")"));
117 function isThisKeyword(node) {
118 return node.kind === ts.SyntaxKind.ThisKeyword;
120 function needsParens(node) {
122 case ts.SyntaxKind.ConditionalExpression:
123 case ts.SyntaxKind.BinaryExpression: