This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 887
New rule prefer-object-spread #2624
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* @license | ||
* Copyright 2017 Palantir Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { | ||
isBinaryExpression, isCallExpression, isIdentifier, isObjectLiteralExpression, | ||
isPropertyAccessExpression, isSpreadElement, | ||
} from "tsutils"; | ||
import * as ts from "typescript"; | ||
|
||
import * as Lint from "../index"; | ||
export class Rule extends Lint.Rules.AbstractRule { | ||
/* tslint:disable:object-literal-sort-keys */ | ||
public static metadata: Lint.IRuleMetadata = { | ||
ruleName: "prefer-object-spread", | ||
description: "Enforces the use of the ES2015 object spread operator over `Object.assign()` where appropriate.", | ||
rationale: "Object spread allows for better type checking and inference.", | ||
optionsDescription: "Not configurable.", | ||
options: null, | ||
optionExamples: [true], | ||
type: "functionality", | ||
typescriptOnly: false, | ||
hasFix: true, | ||
}; | ||
/* tslint:enable:object-literal-sort-keys */ | ||
|
||
public static FAILURE_STRING = "Use the object spread operator instead."; | ||
public static ASSIGNMENT_FAILURE_STRING = "'Object.assign' returns the first argument. Prefer object spread if you want a new object."; | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithFunction(sourceFile, walk); | ||
} | ||
} | ||
|
||
function walk(ctx: Lint.WalkContext<void>) { | ||
return ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): 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" && | ||
// Object.assign(...someArray) cannot be written as object spread | ||
!node.arguments.some(isSpreadElement)) { | ||
if (node.arguments[0].kind === ts.SyntaxKind.ObjectLiteralExpression) { | ||
ctx.addFailureAtNode(node, Rule.FAILURE_STRING, createFix(node, ctx.sourceFile)); | ||
} else { | ||
const parent = node.parent!; | ||
if (parent.kind === ts.SyntaxKind.VariableDeclaration || | ||
isBinaryExpression(parent) && parent.operatorToken.kind === ts.SyntaxKind.EqualsToken) { | ||
ctx.addFailureAtNode(node, Rule.ASSIGNMENT_FAILURE_STRING, createFix(node, ctx.sourceFile)); | ||
} | ||
} | ||
} | ||
return ts.forEachChild(node, cb); | ||
}); | ||
} | ||
|
||
function createFix(node: ts.CallExpression, sourceFile: ts.SourceFile): Lint.Fix { | ||
const args = node.arguments; | ||
const fix = [ | ||
Lint.Replacement.replaceFromTo(node.getStart(sourceFile), args[0].getStart(sourceFile), "{"), | ||
new Lint.Replacement(node.end - 1, 1, "}"), | ||
]; | ||
for (let i = 0; i < args.length; ++i) { | ||
const arg = args[i]; | ||
if (isObjectLiteralExpression(arg)) { | ||
if (arg.properties.length === 0) { | ||
let end = arg.end; | ||
if (i !== args.length - 1) { | ||
end = args[i + 1].getStart(sourceFile); | ||
} else if (args.hasTrailingComma) { | ||
end = args.end; | ||
} | ||
// remove empty object iteral and the following comma if exists | ||
fix.push(Lint.Replacement.deleteFromTo(arg.getStart(sourceFile), end)); | ||
} else { | ||
fix.push( | ||
// remove open brace | ||
Lint.Replacement.deleteText(arg.getStart(sourceFile), 1), | ||
// remove trailing comma if exists and close brace | ||
Lint.Replacement.deleteFromTo(arg.properties[arg.properties.length - 1].end, arg.end), | ||
); | ||
} | ||
} else { | ||
const parens = needsParens(arg); | ||
fix.push(Lint.Replacement.appendText(arg.getStart(sourceFile), parens ? "...(" : "...")); | ||
if (parens) { | ||
fix.push(Lint.Replacement.appendText(arg.end, ")")); | ||
} | ||
} | ||
} | ||
|
||
return fix; | ||
} | ||
|
||
function needsParens(node: ts.Node): boolean { | ||
switch (node.kind) { | ||
case ts.SyntaxKind.ConditionalExpression: | ||
case ts.SyntaxKind.BinaryExpression: | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const original = {a: 1, b:2}; | ||
{...original, c: 3}; | ||
{a: 1, b: 2, c: 3}; | ||
Object.assign({a:1}, ...[{b: 2}, {c: 3}]) | ||
Object.assign(original, {c: 3}); | ||
var result = {...original, c: 3}; | ||
result = {...original, c: 3}; | ||
var result = {...original, c: 3}; | ||
|
||
{a: 1,}; | ||
{a: 1, ...(foo ? {b: 2} : {c: 3})}; | ||
{a: 1, ...{b: 2}}; | ||
{}; | ||
{}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const original = {a: 1, b:2}; | ||
Object.assign({}, original, {c: 3}); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
Object.assign({a: 1, b: 2}, {c: 3}); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
Object.assign({a:1}, ...[{b: 2}, {c: 3}]) | ||
Object.assign(original, {c: 3}); | ||
var result = Object.assign(original, {c: 3}); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [1] | ||
result = Object.assign(original, {c: 3}); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [1] | ||
var result = Object.assign({}, original, {c: 3}); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
Object.assign({}, {}, {a: 1,},); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
Object.assign({a: 1}, foo ? {b: 2} : {c: 3}); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
Object.assign({a: 1, ...{b: 2}}); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
Object.assign({}, {}); | ||
~~~~~~~~~~~~~~~~~~~~~ [0] | ||
Object.assign({}, {},); | ||
~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
[0]: Use the object spread operator instead. | ||
[1]: 'Object.assign' returns the first argument. Prefer object spread if you want a new object. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"prefer-object-spread": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we shouldn't even be applying overlapping fixes