Skip to content

Adopt additional modern JS patterns and ESLint rules. NFC - #28138

Merged
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:modern_js
Nov 5, 2026
Merged

sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:modern_js

Conversation

@sbc100

@sbc100 sbc100 commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-up to #28129 and #28139

  • Enable prefer-exponentiation-operator, prefer-object-spread, prefer-rest-params, prefer-spread, and logical-assignment-operators in eslint.config.mjs.
  • Use Object.values, direct integer indexing, or ??= in places where $hasOwn is not needed.
  • Use Map instead of plain {} objects for lookup tables in acorn-optimizer.mjs and libglfw.js.
  • Replace legacy Array.prototype.slice.call, .apply, .indexOf, Math.pow, and arr[arr.length - 1] idioms with .slice, Array.from, spread syntax, .includes, ** / Math.hypot, and .at(-1).

Note: We are not yet using .at(-1) in runtime code since we still support engines that do not yet support its (and even we we re-introduced babel, its not able to lower this one away).

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modernizes the codebase by adopting several ES6+ features and enforcing them via new ESLint rules. Key changes include replacing Math.pow with the exponentiation operator (**), utilizing Array.from and .slice() instead of Array.prototype.slice.call, adopting .at(-1) for accessing the last element of arrays, replacing indexOf checks with .includes(), and using logical assignment operators (??=, ||=). Additionally, some plain objects were refactored to use Map. Feedback on these changes highlights potential issues with the new logical assignment operators. Specifically, using ??= on objects or constructors (such as derivedClass.constructor and SDL.joystickNamePool) can bypass assignments if the property name collides with inherited prototype properties (e.g., toString or constructor). Reverting to Object.hasOwn checks is recommended in these cases. Furthermore, converting a typed array to a standard array via Array.from(arg).slice(0, 400) in src/runtime_debug.js is highly inefficient for large arrays; slicing the typed array first with .subarray(0, 400) before calling Array.from is suggested to avoid unnecessary memory copying.

Comment thread src/lib/libembind.js Resolved
Comment thread src/runtime_debug.js Resolved
Comment thread src/lib/libsdl.js Resolved
@sbc100
sbc100 force-pushed the modern_js branch 3 times, most recently from 439eacc to 57d00f3 Compare November 3, 2026 19:34
@sbc100
sbc100 requested review from dschuff and kripken November 3, 2026 19:35
Comment thread src/lib/libembind_shared.js Resolved
@sbc100 sbc100 changed the title [JS] Adopt additional modern JS patterns and ESLint rules. NFC Adopt additional modern JS patterns and ESLint rules. NFC Nov 3, 2026
Comment thread src/lib/libglfw.js Resolved
sbc100 added a commit to sbc100/emscripten that referenced this pull request Nov 4, 2026
Rename `rawType` and related `raw*Type` variables/parameters in
embind JS code to `typeID` and `*TypeID`.  This matches the existing
`// typeID -> ...` comments and C++ `TYPEID` typedef and makes it clear
that these hold numeric `TYPEID` pointers rather than type names or
registered type objects.

Inspired by the question/confusion in:
emscripten-core#28138 (comment)
sbc100 added a commit to sbc100/emscripten that referenced this pull request Nov 4, 2026
Rename `rawType` and related `raw*Type` variables/parameters in
embind JS code to `typeID` and `*TypeID`.  This matches the existing
`// typeID -> ...` comments and C++ `TYPEID` typedef and makes it clear
that these hold numeric `TYPEID` pointers rather than type names or
registered type objects.

Inspired by the question/confusion in:
emscripten-core#28138 (comment)
sbc100 added a commit that referenced this pull request Nov 4, 2026
Rename `rawType` and related `raw*Type` variables/parameters in embind
JS code to `typeID` and `*TypeID`. This matches the existing `// typeID
-> ...` comments and C++ `TYPEID` typedef and makes it clear that these
hold numeric `TYPEID` pointers rather than type names or registered type
objects.

Inspired by the question/confusion in:

#28138 (comment)
@sbc100
sbc100 requested a review from kripken November 4, 2026 22:11
Comment thread src/lib/libsdl.js Resolved
Follow-up to emscripten-core#28129:

- Enable `prefer-exponentiation-operator`, `prefer-object-spread`,
  `prefer-rest-params`, `prefer-spread`, and
  `logical-assignment-operators` in `eslint.config.mjs`.
- Use `Object.values`, direct integer indexing, or `??=` in places
  where `$hasOwn` is not needed.
- Use `Map` instead of plain `{}` objects for lookup tables in
  `acorn-optimizer.mjs` and `libglfw.js`.
- Replace legacy `.apply`, `.indexOf`, `Math.pow`, and
  `arr[arr.length - 1]` idioms with `.call` / spread syntax,
  `.includes`, `**` / `Math.hypot`, and `.at(-1)`.
@sbc100
sbc100 merged commit fc8f667 into emscripten-core:main Nov 5, 2026
19 of 25 checks passed
@sbc100
sbc100 deleted the modern_js branch November 5, 2026 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants