{"record":{"id":"53b67bf41528bc20","repo":"oxc-project/oxc","slug":"prefer-string-endswith-over-a-regex-with-a-dollar","errorCode":null,"errorMessage":"Prefer String#endsWith over a regex with a dollar sign.","messagePattern":"Prefer String#endsWith over a regex with a dollar sign\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_string_starts_ends_with.rs","lineNumber":22,"sourceCode":"};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_regular_expression::ast::{BoundaryAssertionKind, Term};\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{\n    AstNode,\n    context::LintContext,\n    fixer::{RuleFix, RuleFixer},\n    rule::Rule,\n};\n\nfn starts_with(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Prefer String#startsWith over a regex with a caret.\").with_label(span)\n}\n\nfn ends_with(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Prefer String#endsWith over a regex with a dollar sign.\").with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferStringStartsEndsWith;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Prefer [`String#startsWith()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) and [`String#endsWith()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith) over using a regex with `/^foo/` or `/foo$/`.\n    ///\n    /// ::: warning\n    /// This rule is deprecated. Prefer the type-aware [`typescript/prefer-string-starts-ends-with`](https://oxc.rs/docs/guide/usage/linter/rules/typescript/prefer-string-starts-ends-with.html) rule instead.\n    /// :::\n    ///\n    /// ### Why is this bad?\n    ///\n    /// Using `String#startsWith()` and `String#endsWith()` is more readable and performant as it does not need to parse a regex.\n    ///","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_string_starts_ends_with.rs#L4-L40","documentation":"This is the `endsWith` diagnostic of oxlint's `unicorn/prefer-string-starts-ends-with` rule. It reports `/suffix$/.test(str)` — a regex anchored at the end with a dollar sign — and recommends `String#endsWith` for the same performance and clarity reasons as the caret variant. The rule is deprecated in favor of the type-aware `typescript/prefer-string-starts-ends-with`.","triggerScenarios":"A `.test()` call on a regex literal ending in `$` with only literal terms before it (boundary assertions recognized via `BoundaryAssertionKind::End`/`WordBoundary` handling in the rule's term walk); multiline (`m`) flags and trailing metacharacters disqualify the match. A fixer can rewrite to `str.endsWith('suffix')`.","commonSituations":"Extension/suffix checks like `/\\.json$/.test(filename)`; the escaped dot is fine because `.` inside the class of literal terms is unescaped by the fixer. Same migration pressure to the typescript variant applies.","solutions":["Replace `/\\.json$/.test(name)` with `name.endsWith('.json')`.","Switch to `typescript/prefer-string-starts-ends-with` and disable the deprecated unicorn rule.","Auto-fix with `oxlint --fix` and eyeball the produced string literal for correctly unescaped characters.","Keep the regex if you rely on case-insensitive (`i`) matching — `endsWith` is case-sensitive, so add `.toLowerCase()` or keep the regex."],"exampleFix":"// before\nconst isJson = /\\.json$/.test(filename);\n\n// after\nconst isJson = filename.endsWith('.json');","handlingStrategy":"validation","validationCode":"// Intent-revealing suffix checks\nif (filename.endsWith('.json')) { /* ... */ }\n// CI: npx oxlint --deny-warn unicorn/prefer-string-starts-ends-with src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use endsWith for literal suffixes like extensions and domains.","Keep the regex when you need `i` flag or multiline semantics — the string method cannot express them.","Auto-fix output unescapes metacharacters; spot-check `\\.`-style patterns after `oxlint --fix`."],"tags":["oxlint","unicorn","string","regex","endswith","deprecated-rule"],"backgroundTag":"regex-test-to-startswith","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}