Skip to content

linter: unicorn/prefer-string-replace-all ignores regex flags and suggests a replacement with a different behaviour #21188

@absidue

Description

@absidue

What version of Oxlint are you using?

1.59.0

What command did you run?

No response

What does your .oxlintrc.json (or oxlint.config.ts) config file look like?

{
  "$schema": "./node_modules/oxlint/configuration_schema.json",
  "plugins": [
    "unicorn"
  ],
  "env": {
    "builtin": true
  },
  "options": {
    "reportUnusedDisableDirectives": "off",
    "typeAware": false,
    "typeCheck": false
  },
  "categories": {
    "correctness": "off",
    "nursery": "off",
    "pedantic": "off",
    "perf": "off",
    "restriction": "off",
    "style": "off",
    "suspicious": "off"
  },
  "rules": {
    "unicorn/prefer-string-replace-all": "error"
  }
}

What happened?

Given the code below:

someVar.replaceAll(/foo/gi, 'bar')

oxlint reports an error saying the regex should be replaced with a plain string 'foo' but that would result in different runtime behaviour as the replace would no longer be case-insensitive.

This works correctly in the original/JavaScript version of the unicorn/prefer-string-replace-all rule. Looking at the code the issues seems to be the difference between the following lines, the JavaScript version will only suggest replacing with a string if the only regex flags are g, u or v whereas the rust implementation in oxlint only checks if the g flag is set:

https://github.com/sindresorhus/eslint-plugin-unicorn/blob/add2138bd8852592d3f75bb55cf0c1de8a63633a/rules/prefer-string-replace-all.js#L21

if !reg_exp_literal.regex.flags.contains(RegExpFlags::G) {

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Priority

    None yet

    Effort

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions