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

allow functions as first argument to Object.assign #3098

Merged
merged 2 commits into from
Aug 6, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/rules/preferObjectSpreadRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function walk(ctx: Lint.WalkContext<void>) {
isPropertyAccessExpression(node.expression) && node.expression.name.text === "assign" &&
isIdentifier(node.expression.expression) && node.expression.expression.text === "Object" &&
// Object.assign(...someArray) cannot be written as object spread
!ts.isFunctionLike(node.arguments[0]) &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Please move this line above the comment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

!node.arguments.some(isSpreadElement)) {
if (node.arguments[0].kind === ts.SyntaxKind.ObjectLiteralExpression) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING, createFix(node, ctx.sourceFile));
Expand Down
2 changes: 2 additions & 0 deletions test/rules/prefer-object-spread/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ foo({...original, c: 3});
// allowed
result = Object.assign(new Foo(), {});
result = Object.assign(createFoo(), {});
result = Object.assign(function() {}, {});
result = Object.assign(() => {}, {});
2 changes: 2 additions & 0 deletions test/rules/prefer-object-spread/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Object.assign({}, {},);
// allowed
result = Object.assign(new Foo(), {});
result = Object.assign(createFoo(), {});
result = Object.assign(function() {}, {});
result = Object.assign(() => {}, {});

[0]: Use the object spread operator instead.
[1]: 'Object.assign' returns the first argument. Prefer object spread if you want a new object.