Skip to content

Commit 7a0e931

Browse files
committed
fix(linter): Update the unicorn/prefer-add-event-listener rule with new JavaScript APIs (#15581)
This will cause new violations for some users if they're using particularly new APIs, but that should be fine. For users migrating from the ESLint plugin, I wonder if we should consider calling out in the docs that this rule (and other rules of this nature) will potentially differ from the original Unicorn rule due to data updates over time? I ran into problems with the jsx-a11y rule in my work codebase due to this kind of improvement, so it may be worth calling out. This also renames `animationStart` and `animationEnd` to be lowercase, as I believe this discrepancy was a mistake in the porting process. The original rule undercases these when processing them with vendor prefixes: https://github.com/sindresorhus/eslint-plugin-unicorn/blob/609d4870f3731d39bd5b5f184628e2cf06578dba/rules/shared/dom-events.js Sources: - https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes#list_of_global_event_handler_attributes - https://github.com/mdn/browser-compat-data/blob/d5d5f2e21ef3f798784d1f5f75bde7c7f10f250e/api/Element.json - https://github.com/microsoft/TypeScript-DOM-lib-generator/blob/f915ac0c987300d75af41bfe4a34bb29a0fb941f/baselines/dom.generated.d.ts I used AI to compare the lists since they're in such different formats and then checked against MDN to ensure the additions were actually real. Prompt for future reference, since I think the pattern is useful: ``` Are there any onX methods defined here: \`\`\` declare var onabort: ((this: Window, ev: UIEvent) => any) | null; // and so on from here... \`\`\` That are not defined in this Rust array?: \`\`\` paste the DOM_EVENT_TYPE_NAMES array from the Rust file here \`\`\` Note that the list in rust strips out the `on` at the start. Capitalization differences matter. Link the MDN page for each function if it isn't found in the Rust list, please. ``` Will have to grab the first list from one of the sources above, I used the TS DOM lib generator because it tends to be quite up-to-date. Also remove the backslashes, obviously. I also added a comment about sourcing this data for future reference.
1 parent 65764fd commit 7a0e931

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

crates/oxc_linter/src/rules/unicorn/prefer_add_event_listener.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ impl Rule for PreferAddEventListener {
7171
}
7272
}
7373

74+
// Can refer to the following sources for the list of event handler names, compare
75+
// this array against any new `onx` functions introduced in browsers:
76+
// - https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes#list_of_global_event_handler_attributes
77+
// - https://github.com/mdn/browser-compat-data/blob/d5d5f2e21ef3f798784d1f5f75bde7c7f10f250e/api/Element.json
78+
// - https://github.com/microsoft/TypeScript-DOM-lib-generator/blob/f915ac0c987300d75af41bfe4a34bb29a0fb941f/baselines/dom.generated.d.ts
79+
//
80+
// Please avoid adding new events that are not implemented in at least two major browser engines!
81+
// Last updated: Nov 2025
7482
const DOM_EVENT_TYPE_NAMES: phf::Set<&'static str> = phf::phf_set![
7583
"AnimationEnd",
7684
"AnimationIteration",
@@ -104,18 +112,21 @@ const DOM_EVENT_TYPE_NAMES: phf::Set<&'static str> = phf::phf_set![
104112
"activate",
105113
"afterblur",
106114
"afterprint",
107-
"animationEnd",
108-
"animationStart",
115+
"animationcancel",
116+
"animationend",
109117
"animationiteration",
118+
"animationstart",
110119
"appinstalled",
111120
"auxclick",
112121
"beforeblur",
113122
"beforecopy",
114123
"beforecut",
115124
"beforeinput",
116125
"beforeinstallprompt",
126+
"beforematch",
117127
"beforepaste",
118128
"beforeprint",
129+
"beforetoggle",
119130
"beforeunload",
120131
"blur",
121132
"cancel",
@@ -129,9 +140,12 @@ const DOM_EVENT_TYPE_NAMES: phf::Set<&'static str> = phf::phf_set![
129140
"compositionupdate",
130141
"connect",
131142
"consolemessage",
143+
"contextlost",
132144
"contextmenu",
145+
"contextrestored",
133146
"controllerchange",
134147
"copy",
148+
"cuechange",
135149
"cut",
136150
"dblclick",
137151
"deactivate",
@@ -157,6 +171,7 @@ const DOM_EVENT_TYPE_NAMES: phf::Set<&'static str> = phf::phf_set![
157171
"focusin",
158172
"focusout",
159173
"foreignfetch",
174+
"formdata",
160175
"fullscreenchange",
161176
"gotpointercapture",
162177
"hashchange",
@@ -208,6 +223,7 @@ const DOM_EVENT_TYPE_NAMES: phf::Set<&'static str> = phf::phf_set![
208223
"pointermove",
209224
"pointerout",
210225
"pointerover",
226+
"pointerrawupdate",
211227
"pointerup",
212228
"popstate",
213229
"progress",
@@ -219,14 +235,17 @@ const DOM_EVENT_TYPE_NAMES: phf::Set<&'static str> = phf::phf_set![
219235
"responsive",
220236
"rightclick",
221237
"scroll",
238+
"scrollend",
222239
"search",
240+
"securitypolicyviolation",
223241
"seeked",
224242
"seeking",
225243
"select",
226244
"selectionchange",
227245
"selectstart",
228246
"show",
229247
"sizechanged",
248+
"slotchange",
230249
"sourceclosed",
231250
"sourceended",
232251
"sourceopen",
@@ -236,15 +255,18 @@ const DOM_EVENT_TYPE_NAMES: phf::Set<&'static str> = phf::phf_set![
236255
"submit",
237256
"suspend",
238257
"text",
239-
"textInput",
240258
"textinput",
259+
"textInput",
241260
"timeupdate",
242261
"toggle",
243262
"touchcancel",
244263
"touchend",
245264
"touchmove",
246265
"touchstart",
266+
"transitioncancel",
247267
"transitionend",
268+
"transitionrun",
269+
"transitionstart",
248270
"unload",
249271
"unresponsive",
250272
"update",

0 commit comments

Comments
 (0)