Skip to content

Commit 2ed78b5

Browse files
committed
fixup: account for typeof + parentheses
1 parent 51c9f6a commit 2ed78b5

File tree

1 file changed

+16
-4
lines changed
  • packages/async-rewriter3/src

1 file changed

+16
-4
lines changed

packages/async-rewriter3/src/lib.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{borrow::Borrow, collections::VecDeque, fmt::Debug};
22
use wasm_bindgen::prelude::*;
3-
use rslint_parser::{ast::{ArrowExpr, AssignExpr, CallExpr, ClassDecl, Constructor, Expr, ExprOrBlock, ExprStmt, FnDecl, FnExpr, Literal, Method, NameRef, ObjectPatternProp, ParameterList, Pattern, PropName, ReturnStmt, ThisExpr, UnaryExpr, VarDecl}, parse_text, AstNode, SyntaxKind, SyntaxNode, TextSize};
3+
use rslint_parser::{ast::{ArrowExpr, AssignExpr, CallExpr, ClassDecl, Constructor, Expr, ExprOrBlock, ExprStmt, FnDecl, FnExpr, GroupingExpr, Literal, Method, NameRef, ObjectPatternProp, ParameterList, Pattern, PropName, ReturnStmt, ThisExpr, UnaryExpr, VarDecl}, parse_text, AstNode, SyntaxKind, SyntaxNode, TextSize};
44

55
#[derive(Debug)]
66
enum InsertionText {
@@ -232,6 +232,19 @@ fn add_all_variables_from_declaration(patterns: impl Iterator<Item = impl Borrow
232232
ret
233233
}
234234

235+
fn is_name_ref(syntax_node: &SyntaxNode, names: Option<&'static[&'static str]>) -> bool {
236+
if !NameRef::can_cast(syntax_node.kind()) {
237+
if GroupingExpr::can_cast(syntax_node.kind()) {
238+
return is_name_ref(GroupingExpr::cast(syntax_node.clone()).unwrap().inner().unwrap().syntax(), names);
239+
}
240+
return false;
241+
}
242+
if names.is_none() {
243+
return true;
244+
}
245+
return names.unwrap().iter().any(|t| *t == syntax_node.text().to_string().as_str())
246+
}
247+
235248
fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
236249
let has_function_parent = nesting_depth > 0;
237250
let mut insertions = InsertionList::new();
@@ -342,8 +355,7 @@ fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
342355
insertions.append(child_insertions);
343356
}
344357
Some(as_expr) => {
345-
let is_eval_this_super_reference = (NameRef::can_cast(as_expr.syntax().kind()) &&
346-
["eval", "this", "super"].iter().any(|t| *t == as_expr.syntax().text().to_string().as_str())) ||
358+
let is_eval_this_super_reference = is_name_ref(as_expr.syntax(), Some(&["eval", "this", "super"])) ||
347359
ThisExpr::can_cast(as_expr.syntax().kind());
348360

349361
let is_returned_expression = ReturnStmt::can_cast(as_expr.syntax().parent().unwrap().kind());
@@ -359,7 +371,7 @@ fn collect_insertions(node: &SyntaxNode, nesting_depth: u32) -> InsertionList {
359371

360372
let is_unary_rhs = UnaryExpr::can_cast(as_expr.syntax().parent().unwrap().kind());
361373
let is_typeof_rhs = is_unary_rhs && UnaryExpr::cast(as_expr.syntax().parent().unwrap()).unwrap().text().starts_with("typeof");
362-
let is_named_typeof_rhs = is_typeof_rhs && NameRef::can_cast(as_expr.syntax().kind());
374+
let is_named_typeof_rhs = is_typeof_rhs && is_name_ref(as_expr.syntax(), None);
363375
let is_lhs_of_assign_expr = (AssignExpr::can_cast(as_expr.syntax().parent().unwrap().kind()) &&
364376
AssignExpr::cast(as_expr.syntax().parent().unwrap()).unwrap().lhs().unwrap().syntax().text_range() ==
365377
as_expr.syntax().text_range()) ||

0 commit comments

Comments
 (0)