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

prefer-object-spread: add parens in shorthand arrow function #3026

Merged
merged 3 commits into from Aug 7, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/rules/preferObjectSpreadRule.ts
Expand Up @@ -64,9 +64,10 @@ function walk(ctx: Lint.WalkContext<void>) {

function createFix(node: ts.CallExpression, sourceFile: ts.SourceFile): Lint.Fix {
const args = node.arguments;
const objectNeedsParens = node.parent!.kind === ts.SyntaxKind.ArrowFunction;
const fix = [
Lint.Replacement.replaceFromTo(node.getStart(sourceFile), args[0].getStart(sourceFile), "{"),
new Lint.Replacement(node.end - 1, 1, "}"),
Lint.Replacement.replaceFromTo(node.getStart(sourceFile), args[0].getStart(sourceFile), `${objectNeedsParens ? "(" : ""}{`),
new Lint.Replacement(node.end - 1, 1, `}${objectNeedsParens ? ")" : ""}`),
];
for (let i = 0; i < args.length; ++i) {
const arg = args[i];
Expand Down
3 changes: 3 additions & 0 deletions test/rules/prefer-object-spread/test.ts.fix
Expand Up @@ -21,3 +21,6 @@ foo({...original, c: 3});
// allowed
result = Object.assign(new Foo(), {});
result = Object.assign(createFoo(), {});

let fn = () => ({...original});

5 changes: 4 additions & 1 deletion test/rules/prefer-object-spread/test.ts.lint
Expand Up @@ -35,5 +35,8 @@ Object.assign({}, {},);
result = Object.assign(new Foo(), {});
result = Object.assign(createFoo(), {});

let fn = () => Object.assign({}, original);
~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

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