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

feat(linter): implement unicorn/no-unnecessary-await #856

Merged
merged 2 commits into from
Sep 4, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
more unfixable cases
  • Loading branch information
Devin-Yeung committed Sep 4, 2023
commit 60bf61baa2311cf33f07945c566e6725f6979c0b
29 changes: 22 additions & 7 deletions crates/oxc_linter/src/rules/unicorn/no_unnecessary_await.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,24 @@ impl Rule for NoUnnecessaryAwait {
if !not_promise(&expr.argument) {
return;
}
if
// Removing `await` may change them to a declaration, if there is no `id` will cause SyntaxError
matches!(expr.argument, Expression::FunctionExpression(_))
|| matches!(expr.argument, Expression::ClassExpression(_))
// TODO: `+await +1` -> `++1`
{
if {
// Removing `await` may change them to a declaration, if there is no `id` will cause SyntaxError
matches!(expr.argument, Expression::FunctionExpression(_))
|| matches!(expr.argument, Expression::ClassExpression(_))
} || {
// `+await +1` -> `++1`
ctx.nodes().parent_node(node.id()).map_or(false, |parent| {
if let (
AstKind::UnaryExpression(parent_unary),
Expression::UnaryExpression(inner_unary),
) = (parent.kind(), &expr.argument)
{
parent_unary.operator == inner_unary.operator
} else {
false
}
})
} {
ctx.diagnostic(NoUnnecessaryAwaitDiagnostic(expr.span));
} else {
ctx.diagnostic_with_fix(NoUnnecessaryAwaitDiagnostic(expr.span), || {
Expand Down Expand Up @@ -138,9 +150,12 @@ fn test() {
let fix = vec![
("await []", "[]", None),
("await (a == b)", "(a == b)", None),
Devin-Yeung marked this conversation as resolved.
Show resolved Hide resolved
("+await -1", "+ -1", None),
("-await +1", "- +1", None),
("await function() {}", "await function() {}", None), // no autofix
("await class {}", "await class {}", None), // no autofix
// ("+await +1", "+await +1", None), // no autofix
("+await +1", "+await +1", None), // no autofix
("-await -1", "-await -1", None), // no autofix
];

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