Skip to content

Commit

Permalink
prefer-object-spread: add parens in shorthand arrow function (palanti…
Browse files Browse the repository at this point in the history
  • Loading branch information
ajafff authored and HyphnKnight committed Apr 9, 2018
1 parent 94b1d16 commit 3237e69
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/rules/preferObjectSpreadRule.ts
Expand Up @@ -65,9 +65,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
2 changes: 2 additions & 0 deletions test/rules/prefer-object-spread/test.ts.fix
Expand Up @@ -23,3 +23,5 @@ result = Object.assign(new Foo(), {});
result = Object.assign(createFoo(), {});
result = Object.assign(function() {}, {});
result = Object.assign(() => {}, {});

let fn = () => ({...original});
4 changes: 3 additions & 1 deletion test/rules/prefer-object-spread/test.ts.lint
Expand Up @@ -37,5 +37,7 @@ result = Object.assign(createFoo(), {});
result = Object.assign(function() {}, {});
result = Object.assign(() => {}, {});

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.

0 comments on commit 3237e69

Please sign in to comment.