{"record":{"id":"24450369aff63e9a","repo":"oxc-project/oxc","slug":"prefer-eventtarget-over-eventemitter","errorCode":null,"errorMessage":"Prefer `EventTarget` over `EventEmitter`.","messagePattern":"Prefer `EventTarget` over `EventEmitter`\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_event_target.rs","lineNumber":17,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{Argument, BindingPattern, Expression, IdentifierReference},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\n\nuse crate::{\n    AstNode, ast_util::get_declaration_of_variable, context::LintContext, rule::Rule,\n    utils::is_import_from_module,\n};\n\nconst IGNORED_PACKAGES: [&str; 2] = [\"@angular/core\", \"eventemitter3\"];\n\nfn prefer_event_target_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Prefer `EventTarget` over `EventEmitter`.\")\n        .with_help(\"Change `EventEmitter` to `EventTarget`. EventEmitters are only available in Node.js, while EventTargets are also available in browsers.\")\n        .with_note(\"https://developer.mozilla.org/en-US/docs/Web/API/EventTarget\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferEventTarget;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Prefers `EventTarget` over `EventEmitter`.\n    ///\n    /// This rule reduces the bundle size and makes your code more cross-platform friendly.\n    ///\n    /// See the [differences](https://nodejs.org/api/events.html#eventtarget-and-event-api) between `EventEmitter` and `EventTarget`.\n    ///\n    /// ### Why is this bad?","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_event_target.rs#L1-L35","documentation":"Diagnostic from the unicorn/prefer-event-target lint rule. It fires when `EventEmitter` (from `events` or a non-ignored package) is instantiated or used as a base class in code that is not Node-specific; the DOM-standard `EventTarget` is cross-environment and preferred. Packages in the rule's ignore list (e.g. `@angular/core`, `eventemitter3`) are exempt. It is a lint suggestion, not a runtime error.","triggerScenarios":"Thrown at crates/oxc_linter/src/rules/unicorn/prefer_event_target.rs:17 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Change `class X extends EventEmitter` to `extends EventTarget`","Switch `new EventEmitter()` to `new EventTarget()` and use `addEventListener`/`dispatchEvent` instead of `on`/`emit`","Remove the now-unused `events` import","Add a package to the rule's ignore list if `EventEmitter` is genuinely required"],"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"}