From 01655451c1acd16e83ddbca41d57bf4ec851beef Mon Sep 17 00:00:00 2001 From: no-yan <63000297+no-yan@users.noreply.github.com> Date: Thu, 14 Nov 2024 20:33:42 +0900 Subject: [PATCH] feat(linter): support bind function case for compatibility with `promise/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. --- THIRD-PARTY-LICENSE | 11 ++ .../no_useless_promise_resolve_reject.rs | 139 +++++++++++++- .../no_useless_promise_resolve_reject.snap | 176 ++++++++++++++++++ 3 files changed, 324 insertions(+), 2 deletions(-) diff --git a/THIRD-PARTY-LICENSE b/THIRD-PARTY-LICENSE index 4156033e29bf56..8d7c17b67540c9 100644 --- a/THIRD-PARTY-LICENSE +++ b/THIRD-PARTY-LICENSE @@ -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 @@ -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. + diff --git a/crates/oxc_linter/src/rules/unicorn/no_useless_promise_resolve_reject.rs b/crates/oxc_linter/src/rules/unicorn/no_useless_promise_resolve_reject.rs index 7a3e301f15d73b..2b02cb8438d84f 100644 --- a/crates/oxc_linter/src/rules/unicorn/no_useless_promise_resolve_reject.rs +++ b/crates/oxc_linter/src/rules/unicorn/no_useless_promise_resolve_reject.rs @@ -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; }; @@ -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() { @@ -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![ @@ -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![ @@ -1042,6 +1176,7 @@ fn test() { None, ), ]; + Tester::new(NoUselessPromiseResolveReject::NAME, pass, fail) .expect_fix(fix) .test_and_snapshot(); diff --git a/crates/oxc_linter/src/snapshots/no_useless_promise_resolve_reject.snap b/crates/oxc_linter/src/snapshots/no_useless_promise_resolve_reject.snap index 0b129224766ceb..e918f09b5ce593 100644 --- a/crates/oxc_linter/src/snapshots/no_useless_promise_resolve_reject.snap +++ b/crates/oxc_linter/src/snapshots/no_useless_promise_resolve_reject.snap @@ -1,5 +1,6 @@ --- source: crates/oxc_linter/src/tester.rs +snapshot_kind: text --- ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `throw error` over `return Promise.reject(error)`. ╭─[no_useless_promise_resolve_reject.tsx:4:28] @@ -451,3 +452,178 @@ source: crates/oxc_linter/src/tester.rs · ─────────────────── ╰──── help: Wrapping the error in `Promise.reject` is needlessly verbose. All errors thrown in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `return value` over `return Promise.resolve(value)`. + ╭─[no_useless_promise_resolve_reject.tsx:1:36] + 1 │ doThing().then(function() { return Promise.resolve(4) }) + · ────────────────── + ╰──── + help: Wrapping the return value in `Promise.Resolve` is needlessly verbose. All return values in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `return value` over `return Promise.resolve(value)`. + ╭─[no_useless_promise_resolve_reject.tsx:1:42] + 1 │ doThing().then(null, function() { return Promise.resolve(4) }) + · ────────────────── + ╰──── + help: Wrapping the return value in `Promise.Resolve` is needlessly verbose. All return values in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `return value` over `return Promise.resolve(value)`. + ╭─[no_useless_promise_resolve_reject.tsx:1:37] + 1 │ doThing().catch(function() { return Promise.resolve(4) }) + · ────────────────── + ╰──── + help: Wrapping the return value in `Promise.Resolve` is needlessly verbose. All return values in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `throw error` over `return Promise.reject(error)`. + ╭─[no_useless_promise_resolve_reject.tsx:1:36] + 1 │ doThing().then(function() { return Promise.reject(4) }) + · ───────────────── + ╰──── + help: Wrapping the error in `Promise.reject` is needlessly verbose. All errors thrown in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `throw error` over `return Promise.reject(error)`. + ╭─[no_useless_promise_resolve_reject.tsx:1:42] + 1 │ doThing().then(null, function() { return Promise.reject(4) }) + · ───────────────── + ╰──── + help: Wrapping the error in `Promise.reject` is needlessly verbose. All errors thrown in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `throw error` over `return Promise.reject(error)`. + ╭─[no_useless_promise_resolve_reject.tsx:1:37] + 1 │ doThing().catch(function() { return Promise.reject(4) }) + · ───────────────── + ╰──── + help: Wrapping the error in `Promise.reject` is needlessly verbose. All errors thrown in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `return value` over `return Promise.resolve(value)`. + ╭─[no_useless_promise_resolve_reject.tsx:1:48] + 1 │ doThing().then(function(x) { if (x>1) { return Promise.resolve(4) } else { throw "bad" } }) + · ────────────────── + ╰──── + help: Wrapping the return value in `Promise.Resolve` is needlessly verbose. All return values in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `throw error` over `return Promise.reject(error)`. + ╭─[no_useless_promise_resolve_reject.tsx:1:48] + 1 │ doThing().then(function(x) { if (x>1) { return Promise.reject(4) } }) + · ───────────────── + ╰──── + help: Wrapping the error in `Promise.reject` is needlessly verbose. All errors thrown in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `return value` over `return Promise.resolve(value)`. + ╭─[no_useless_promise_resolve_reject.tsx:1:63] + 1 │ doThing().then(null, function() { if (true && false) { return Promise.resolve() } }) + · ───────────────── + ╰──── + help: Wrapping the return value in `Promise.Resolve` is needlessly verbose. All return values in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `return value` over `return Promise.resolve(value)`. + ╭─[no_useless_promise_resolve_reject.tsx:1:46] + 1 │ doThing().catch(function(x) {if (x) { return Promise.resolve(4) } else { return Promise.reject() } }) + · ────────────────── + ╰──── + help: Wrapping the return value in `Promise.Resolve` is needlessly verbose. All return values in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `throw error` over `return Promise.reject(error)`. + ╭─[no_useless_promise_resolve_reject.tsx:1:81] + 1 │ doThing().catch(function(x) {if (x) { return Promise.resolve(4) } else { return Promise.reject() } }) + · ──────────────── + ╰──── + help: Wrapping the error in `Promise.reject` is needlessly verbose. All errors thrown in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `return value` over `return Promise.resolve(value)`. + ╭─[no_useless_promise_resolve_reject.tsx:4:38] + 3 │ doThing().then(function() { + 4 │ return Promise.resolve(4) + · ────────────────── + 5 │ }) + ╰──── + help: Wrapping the return value in `Promise.Resolve` is needlessly verbose. All return values in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `return value` over `return Promise.resolve(value)`. + ╭─[no_useless_promise_resolve_reject.tsx:4:38] + 3 │ doThing().then(function nm() { + 4 │ return Promise.resolve(4) + · ────────────────── + 5 │ }) + ╰──── + help: Wrapping the return value in `Promise.Resolve` is needlessly verbose. All return values in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `return value` over `return Promise.resolve(value)`. + ╭─[no_useless_promise_resolve_reject.tsx:5:40] + 4 │ doThing().then(function() { + 5 │ return Promise.resolve(4) + · ────────────────── + 6 │ }) + ╰──── + help: Wrapping the return value in `Promise.Resolve` is needlessly verbose. All return values in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `return value` over `return Promise.resolve(value)`. + ╭─[no_useless_promise_resolve_reject.tsx:8:40] + 7 │ }) + 8 │ return Promise.resolve(4) + · ────────────────── + 9 │ }) + ╰──── + help: Wrapping the return value in `Promise.Resolve` is needlessly verbose. All return values in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `return value` over `return Promise.resolve(value)`. + ╭─[no_useless_promise_resolve_reject.tsx:5:40] + 4 │ return doThing().then(function() { + 5 │ return Promise.resolve(5); + · ────────────────── + 6 │ }); + ╰──── + help: Wrapping the return value in `Promise.Resolve` is needlessly verbose. All return values in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `return value` over `return Promise.resolve(value)`. + ╭─[no_useless_promise_resolve_reject.tsx:4:38] + 3 │ doThing().then(function() { + 4 │ return Promise.resolve(5); + · ────────────────── + 5 │ }) + ╰──── + help: Wrapping the return value in `Promise.Resolve` is needlessly verbose. All return values in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `return value` over `return Promise.resolve(value)`. + ╭─[no_useless_promise_resolve_reject.tsx:1:37] + 1 │ doThing().then((function() { return Promise.resolve(4) }).bind(this)) + · ────────────────── + ╰──── + help: Wrapping the return value in `Promise.Resolve` is needlessly verbose. All return values in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `return value` over `return Promise.resolve(value)`. + ╭─[no_useless_promise_resolve_reject.tsx:1:37] + 1 │ doThing().then((function() { return Promise.resolve(4) }).bind(this).bind(this)) + · ────────────────── + ╰──── + help: Wrapping the return value in `Promise.Resolve` is needlessly verbose. All return values in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `return value` over `return Promise.resolve(value)`. + ╭─[no_useless_promise_resolve_reject.tsx:1:31] + 1 │ doThing().then(() => { return Promise.resolve(4) }) + · ────────────────── + ╰──── + help: Wrapping the return value in `Promise.Resolve` is needlessly verbose. All return values in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `return value` over `return Promise.resolve(value)`. + ╭─[no_useless_promise_resolve_reject.tsx:4:38] + 3 │ return p.then(function(val) { + 4 │ return Promise.resolve(val * 4) + · ──────────────────────── + 5 │ }) + ╰──── + help: Wrapping the return value in `Promise.Resolve` is needlessly verbose. All return values in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `return value` over `return Promise.resolve(value)`. + ╭─[no_useless_promise_resolve_reject.tsx:1:22] + 1 │ doThing().then(() => Promise.resolve(4)) + · ────────────────── + ╰──── + help: Wrapping the return value in `Promise.Resolve` is needlessly verbose. All return values in async functions are already wrapped in a `Promise`. + + ⚠ eslint-plugin-unicorn(no-useless-promise-resolve-reject): Prefer `throw error` over `return Promise.reject(error)`. + ╭─[no_useless_promise_resolve_reject.tsx:1:22] + 1 │ doThing().then(() => Promise.reject(4)) + · ───────────────── + ╰──── + help: Wrapping the error in `Promise.reject` is needlessly verbose. All errors thrown in async functions are already wrapped in a `Promise`.