Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

prefer-object-spread: ignore Object.assign({}, this) #3126

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/rules/preferObjectSpreadRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
*/

import {
hasSideEffects, isCallExpression, isExpressionValueUsed, isIdentifier, isObjectLiteralExpression,
isPropertyAccessExpression, isSpreadElement, SideEffectOptions,
hasSideEffects,
isCallExpression,
isExpressionValueUsed,
isIdentifier,
isObjectLiteralExpression,
isPropertyAccessExpression,
isSpreadElement,
SideEffectOptions,
} from "tsutils";
import * as ts from "typescript";

Expand Down Expand Up @@ -52,12 +58,20 @@ function walk(ctx: Lint.WalkContext<void>) {
isIdentifier(node.expression.expression) && node.expression.expression.text === "Object" &&
!ts.isFunctionLike(node.arguments[0]) &&
// Object.assign(...someArray) cannot be written as object spread
!node.arguments.some(isSpreadElement)) {
!node.arguments.some(isSpreadElement) &&
/**
* @TODO
* Remove !node.arguments.some(isThisKeyword) when typescript get's
* support for spread types.
* PR: https://github.com/Microsoft/TypeScript/issues/10727
*/
!node.arguments.some(isThisKeyword)) {
if (node.arguments[0].kind === ts.SyntaxKind.ObjectLiteralExpression) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING, createFix(node, ctx.sourceFile));
} else if (isExpressionValueUsed(node) && !hasSideEffects(node.arguments[0], SideEffectOptions.Constructor)) {
ctx.addFailureAtNode(node, Rule.ASSIGNMENT_FAILURE_STRING, createFix(node, ctx.sourceFile));
}

}
return ts.forEachChild(node, cb);
});
Expand Down Expand Up @@ -102,6 +116,10 @@ function createFix(node: ts.CallExpression, sourceFile: ts.SourceFile): Lint.Fix
return fix;
}

function isThisKeyword(node: ts.Expression): boolean {
return node.kind === ts.SyntaxKind.ThisKeyword;
}

function needsParens(node: ts.Node): boolean {
switch (node.kind) {
case ts.SyntaxKind.ConditionalExpression:
Expand Down
3 changes: 3 additions & 0 deletions test/rules/prefer-object-spread/test.ts.fix
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const form = Object.assign({}, this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a test for const foo = Object.assign({}, bar, baz, this);

const foo = Object.assign(bar, this);
const foo = Object.assign({}, bar, baz, this);
const original = {a: 1, b:2};
{...original, c: 3};
{a: 1, b: 2, c: 3};
Expand Down
3 changes: 3 additions & 0 deletions test/rules/prefer-object-spread/test.ts.lint
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const form = Object.assign({}, this);
const foo = Object.assign(bar, this);
const foo = Object.assign({}, bar, baz, this);
const original = {a: 1, b:2};
Object.assign({}, original, {c: 3});
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]
Expand Down