{"record":{"id":"23d560d5141b0868","repo":"oxc-project/oxc","slug":"prefer-good-method-over-bad-method-23d560","errorCode":null,"errorMessage":"Prefer `.{good_method}()` over `.{bad_method}()`.","messagePattern":"Prefer `\\.(.+?)\\(\\)` over `\\.(.+?)\\(\\)`\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_query_selector.rs","lineNumber":13,"sourceCode":"use oxc_ast::{AstKind, ast::Expression};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{AstNode, context::LintContext, rule::Rule, utils::is_node_value_not_dom_node};\n\nfn prefer_query_selector_diagnostic(\n    good_method: &str,\n    bad_method: &str,\n    span: Span,\n) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"Prefer `.{good_method}()` over `.{bad_method}()`.\"))\n        .with_help(\"It's better to use the same method to query DOM elements. This helps keep consistency and it lends itself to future improvements (e.g. more specific selectors).\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferQuerySelector;\n\nfn get_preferred_identifier_name(ident_name: &str) -> Option<&'static str> {\n    match ident_name {\n        \"getElementById\" => Some(\"querySelector\"),\n        \"getElementsByClassName\" | \"getElementsByTagName\" | \"getElementsByName\" => {\n            Some(\"querySelectorAll\")\n        }\n        _ => None,\n    }\n}\n\ndeclare_oxc_lint!(","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_query_selector.rs#L1-L31","documentation":"Emitted by the unicorn/prefer-query-selector rule when it finds a DOM query call using `getElementById`, `getElementsByClassName`, `getElementsByTagName`, or `getElementsBy*Name`-style methods. It fires at the call-expression span because those legacy DOM query methods are less flexible and less consistent than the modern `querySelector`/`querySelectorAll` API, which supports full CSS selectors.","triggerScenarios":"Thrown at crates/oxc_linter/src/rules/unicorn/prefer_query_selector.rs:13 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Replace `document.getElementById('id')` with `document.querySelector('#id')`","Replace `document.getElementsByClassName('c')` with `document.querySelectorAll('.c')`","Replace `document.getElementsByTagName('div')` with `document.querySelectorAll('div')`"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"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"}