Skip to content

Commit

Permalink
feat(linter): support bind function case for compatibility with `prom…
Browse files Browse the repository at this point in the history
…ise/no-return-wrap` (#7232)

part of #4655

### Summary

Logic has been added to handle bind functions, unifying the
implementations of `unicorn/no-useless-promise-resolve-reject` and
`promise/no-return-wrap`.
This enables detection of the following code:

```javascript
foo().then((function() { return Promise.resolve(4) }).bind(this))
```

Merging this PR will allow this rule to pass all test cases of
promise/no-return-wrap without options. Additionally, merging #7274 will
ensure that all test cases are passed.
  • Loading branch information
no-yan authored and Dunqing committed Nov 17, 2024
1 parent 0761989 commit 0165545
Show file tree
Hide file tree
Showing 3 changed files with 324 additions and 2 deletions.
11 changes: 11 additions & 0 deletions THIRD-PARTY-LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,16 @@ SOFTWARE.

--------------------------------------------------------------------------------

eslint-plugin-promise

Copyright (c) 2020, Jamund Ferguson

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

--------------------------------------------------------------------------------

jsparagus

Copyright (c) 2020 Mozilla Foundation
Expand Down Expand Up @@ -486,3 +496,4 @@ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ fn get_function_like_node<'a, 'b>(
}

fn is_promise_callback<'a, 'b>(node: &'a AstNode<'b>, ctx: &'a LintContext<'b>) -> bool {
let Some(parent) = outermost_paren_parent(node, ctx) else {
let function_node = traverse_bind_calls(node, ctx);
let Some(parent) = outermost_paren_parent(function_node, ctx) else {
return false;
};

let Some(parent) = outermost_paren_parent(parent, ctx) else {
return false;
};
Expand Down Expand Up @@ -218,6 +218,36 @@ fn is_promise_callback<'a, 'b>(node: &'a AstNode<'b>, ctx: &'a LintContext<'b>)
false
}

// Traverse bind functions and return outer call expression
fn traverse_bind_calls<'a, 'b>(node: &'a AstNode<'b>, ctx: &'a LintContext<'b>) -> &'a AstNode<'b> {
let mut current_node = node;
loop {
let Some(parent) = outermost_paren_parent(current_node, ctx) else {
return current_node;
};
if !is_bind_member_expression(parent) {
return current_node;
}

let Some(grand_parent) = outermost_paren_parent(parent, ctx) else {
return current_node;
};
let AstKind::CallExpression(_) = grand_parent.kind() else {
return current_node;
};

current_node = grand_parent;
}
}

fn is_bind_member_expression(node: &AstNode) -> bool {
let AstKind::MemberExpression(member_expr) = node.kind() else {
return false;
};

member_expr.static_property_name() == Some("bind")
}

fn match_arrow_function_body<'a>(ctx: &LintContext<'a>, parent: &AstNode<'a>) -> bool {
match ctx.nodes().parent_node(parent.id()) {
Some(arrow_function_body) => match arrow_function_body.kind() {
Expand Down Expand Up @@ -466,6 +496,39 @@ fn test() {
r"(async () => { Promise.resolve().then(() => console.log('foo')); })();",
// TODO: enhance to report this case?
r#"fs.promises.readFile("foo", 'utf8').then(undefined, err => err.code === 'ENOENT' ? Promise.resolve('{}') : Promise.reject(err))"#,
r"Promise.resolve(4).then(function(x) { return x })",
r"Promise.reject(4).then(function(x) { return x })",
r"Promise.resolve(4).then(function() {})",
r"Promise.reject(4).then(function() {})",
r"doThing().then(function() { return 4 })",
r"doThing().then(function() { throw 4 })",
r"doThing().then(null, function() { return 4 })",
r"doThing().then(null, function() { throw 4 })",
r"doThing().catch(null, function() { return 4 })",
r"doThing().catch(null, function() { throw 4 })",
r"doThing().then(function() { return Promise.all([a,b,c]) })",
r"doThing().then(() => 4)",
r"doThing().then(() => { throw 4 })",
r"doThing().then(()=>{}, () => 4)",
r"doThing().then(()=>{}, () => { throw 4 })",
r"doThing().catch(() => 4)",
r"doThing().catch(() => { throw 4 })",
r"var x = function() { return Promise.resolve(4) }",
r"function y() { return Promise.resolve(4) }",
r"function then() { return Promise.reject() }",
r"doThing(function(x) { return Promise.reject(x) })",
r"doThing().then(function() { return })",
// TODO: support `allow_reject` option
// "doThing().then(function() { return Promise.reject(4) })",
r"doThing().then((function() { return Promise.resolve(4) }).toString())",
// TODO: support `allow_reject` option
// "doThing().then(() => Promise.reject(4))",
r"doThing().then(function() { return a() })",
r"doThing().then(function() { return Promise.a() })",
r"doThing().then(() => { return a() })",
r"doThing().then(() => { return Promise.a() })",
r"doThing().then(() => a())",
r"doThing().then(() => Promise.a())",
];

let fail = vec![
Expand Down Expand Up @@ -642,6 +705,77 @@ fn test() {
r"promise.then(() => {}, () => { return Promise.resolve(bar); })",
r"promise.then(() => {}, async () => Promise.reject(bar))",
r"promise.then(() => {}, async () => { return Promise.reject(bar); })",
r"doThing().then(function() { return Promise.resolve(4) })",
r"doThing().then(null, function() { return Promise.resolve(4) })",
r"doThing().catch(function() { return Promise.resolve(4) })",
r"doThing().then(function() { return Promise.reject(4) })",
r"doThing().then(null, function() { return Promise.reject(4) })",
r"doThing().catch(function() { return Promise.reject(4) })",
r#"doThing().then(function(x) { if (x>1) { return Promise.resolve(4) } else { throw "bad" } })"#,
r"doThing().then(function(x) { if (x>1) { return Promise.reject(4) } })",
r"doThing().then(null, function() { if (true && false) { return Promise.resolve() } })",
r"doThing().catch(function(x) {if (x) { return Promise.resolve(4) } else { return Promise.reject() } })",
"
fn(function() {
doThing().then(function() {
return Promise.resolve(4)
})
return
})",
"
fn(function() {
doThing().then(function nm() {
return Promise.resolve(4)
})
return
})",
"
fn(function() {
fn2(function() {
doThing().then(function() {
return Promise.resolve(4)
})
})
})",
"
fn(function() {
fn2(function() {
doThing().then(function() {
fn3(function() {
return Promise.resolve(4)
})
return Promise.resolve(4)
})
})
})",
"
const o = {
fn: function() {
return doThing().then(function() {
return Promise.resolve(5);
});
},
}
",
"
fn(
doThing().then(function() {
return Promise.resolve(5);
})
);
",
r"doThing().then((function() { return Promise.resolve(4) }).bind(this))",
r"doThing().then((function() { return Promise.resolve(4) }).bind(this).bind(this))",
r"doThing().then(() => { return Promise.resolve(4) })",
"
function a () {
return p.then(function(val) {
return Promise.resolve(val * 4)
})
}
",
r"doThing().then(() => Promise.resolve(4))",
r"doThing().then(() => Promise.reject(4))",
];

let fix = vec![
Expand Down Expand Up @@ -1042,6 +1176,7 @@ fn test() {
None,
),
];

Tester::new(NoUselessPromiseResolveReject::NAME, pass, fail)
.expect_fix(fix)
.test_and_snapshot();
Expand Down
Loading

0 comments on commit 0165545

Please sign in to comment.