Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: no-extra-bind No autofix if arg may have side effect (fixes #10846) #10918

Merged
merged 2 commits into from Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 22 additions & 0 deletions lib/rules/no-extra-bind.js
Expand Up @@ -10,6 +10,12 @@

const astUtils = require("../util/ast-utils");

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

const SIDE_EFFECT_FREE_NODE_TYPES = new Set(["Literal", "Identifier", "ThisExpression", "FunctionExpression"]);

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand All @@ -35,6 +41,18 @@ module.exports = {
create(context) {
let scopeInfo = null;

/**
* Checks if a node is free of side effects.
*
* This check is stricter than it needs to be, in order to keep the implementation simple.
*
* @param {ASTNode} node A node to check.
* @returns {boolean} True if the node is known to be side-effect free, false otherwise.
*/
function isSideEffectFree(node) {
return SIDE_EFFECT_FREE_NODE_TYPES.has(node.type);
}

/**
* Reports a given function node.
*
Expand All @@ -48,6 +66,10 @@ module.exports = {
messageId: "unexpected",
loc: node.parent.property.loc.start,
fix(fixer) {
if (node.parent.parent.arguments.length && !isSideEffectFree(node.parent.parent.arguments[0])) {
return null;
}

const firstTokenToRemove = context.getSourceCode()
.getFirstTokenBetween(node.parent.object, node.parent.property, astUtils.isNotClosingParenToken);

Expand Down
22 changes: 22 additions & 0 deletions tests/lib/rules/no-extra-bind.js
Expand Up @@ -70,10 +70,32 @@ ruleTester.run("no-extra-bind", rule, {
output: "var a = function() { function c(){ this.d } }",
errors
},
{
code: "var a = function() { return 1; }.bind(this)",
output: "var a = function() { return 1; }",
errors
},
{
code: "var a = function() { (function(){ (function(){ this.d }.bind(c)) }) }.bind(b)",
output: "var a = function() { (function(){ (function(){ this.d }.bind(c)) }) }",
errors: [{ messageId: "unexpected", type: "CallExpression", column: 71 }]
},

// Should not autofix if bind expression args have side effects
{
code: "var a = function() {}.bind(b++)",
output: null,
errors
},
{
code: "var a = function() {}.bind(b())",
output: null,
errors
},
{
code: "var a = function() {}.bind(b.c)",
output: null,
errors
}
]
});