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_invalid_regexp): expand invalid case #1302

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Colt45s
Copy link

@Colt45s Colt45s commented Jul 28, 2024

Fixes #1269

To understand the issue better, I first modified the code to include debug output for inspecting the AST. Specifically, I added the following debug statement to the handle_call_or_new_expr function:

fn handle_call_or_new_expr(
  &mut self,
  callee: &Expr,
  args: &[ExprOrSpread],
  range: SourceRange,
) {
+  println!("{:?}", callee);
+  println!("{:?}", args);

  if let Expr::Ident(ident) = callee {
    if ident.sym != *"RegExp" || args.is_empty() {
      return;
    }
    if let Some(pattern) = &check_expr_for_string_literal(&args[0].expr) {
      if args.len() > 1 {
        if let Some(flags) = &check_expr_for_string_literal(&args[1].expr) {
          self.check_regex(pattern, flags, range);
          return;
        }
      }
      self.check_regex(pattern, "", range);
    }
  }
}

By examining the printed AST output, I thought checking for nested AST might be needed.

I ran lint on the following code snippet.

((_) => [
  /+/,
  RegExp("+"),
  new RegExp("+"),
])([
  /+/,
  RegExp("+"),
  new RegExp("+"),
]);

println output:

// callee
Paren(ParenExpr { span: 1623..1676#0, expr: Arrow(ArrowExpr { span: 1624..1675#0, params: [Ident(BindingIdent { id: Ident { span: 1625..1626#3, sym: "_", optional: false }, type_ann: None })], body: Expr(Array(ArrayLit { span: 1631..1675#0, elems: [Some(ExprOrSpread { spread: None, expr: Lit(Regex(Regex { span: 1635..1638#0, exp: "+", flags: "" })) }), Some(ExprOrSpread { spread: None, expr: Call(CallExpr { span: 1642..1653#0, callee: Expr(Ident(Ident { span: 1642..1648#1, sym: "RegExp", optional: false })), args: [ExprOrSpread { spread: None, expr: Lit(Str(Str { span: 1649..1652#0, value: "+", raw: Some("\"+\"") })) }], type_args: None }) }), Some(ExprOrSpread { spread: None, expr: New(NewExpr { span: 1657..1672#0, callee: Ident(Ident { span: 1661..1667#1, sym: "RegExp", optional: false }), args: Some([ExprOrSpread { spread: None, expr: Lit(Str(Str { span: 1668..1671#0, value: "+", raw: Some("\"+\"") })) }]), type_args: None }) })] })), is_async: false, is_generator: false, type_params: None, return_type: None }) })

// args
[ExprOrSpread { spread: None, expr: Array(ArrayLit { span: 1677..1721#0, elems: [Some(ExprOrSpread { spread: None, expr: Lit(Regex(Regex { span: 1681..1684#0, exp: "+", flags: "" })) }), Some(ExprOrSpread { spread: None, expr: Call(CallExpr { span: 1688..1699#0, callee: Expr(Ident(Ident { span: 1688..1694#1, sym: "RegExp", optional: false })), args: [ExprOrSpread { spread: None, expr: Lit(Str(Str { span: 1695..1698#0, value: "+", raw: Some("\"+\"") })) }], type_args: None }) }), Some(ExprOrSpread { spread: None, expr: New(NewExpr { span: 1703..1718#0, callee: Ident(Ident { span: 1707..1713#1, sym: "RegExp", optional: false }), args: Some([ExprOrSpread { spread: None, expr: Lit(Str(Str { span: 1714..1717#0, value: "+", raw: Some("\"+\"") })) }]), type_args: None }) })] }) }]

@CLAassistant
Copy link

CLAassistant commented Jul 28, 2024

CLA assistant check
All committers have signed the CLA.

@0f-0b
Copy link
Contributor

0f-0b commented Jul 28, 2024

The fix doesn't cover these cases.

class C extends RegExp {
  constructor() {
    super(/+/); // there should be warnings on this line…
  }
}

new C();
new function () {
  return /+/; // …and on this line.
};

@Colt45s
Copy link
Author

Colt45s commented Jul 29, 2024

Thank you! I'll try 😃

@Colt45s
Copy link
Author

Colt45s commented Aug 3, 2024

Fixed.

@Colt45s Colt45s changed the title fix(no_invalid_regexp): support inside function calls fix(no_invalid_regexp): expand invalid case Aug 3, 2024
@Colt45s
Copy link
Author

Colt45s commented Aug 28, 2024

Can anyone review this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

no-invalid-regexp does not look inside function calls
3 participants