Skip to content

Commit

Permalink
allow functions as first argument to Object.assign (palantir#3098)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgoli authored and ajafff committed Aug 6, 2017
1 parent d62cb31 commit d7fab55
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/rules/preferObjectSpreadRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function walk(ctx: Lint.WalkContext<void>) {
if (isCallExpression(node) && node.arguments.length !== 0 &&
isPropertyAccessExpression(node.expression) && node.expression.name.text === "assign" &&
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)) {
if (node.arguments[0].kind === ts.SyntaxKind.ObjectLiteralExpression) {
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.

0 comments on commit d7fab55

Please sign in to comment.