[Web] Add way to ESLint Emscripten libraries #106607
Draft
+752
−230
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Important
This PR adds a
package.json
file to the root. Don't worry. It is only required bypre-commit
(which will run the package.json filein its own custom environmentinside the root dir, but no Node/npm is needed), so nonpm i
required to build Godot and contribute.Note
This is my first step to modernize the Web stack. Having
package.json
andeslint.config.mts
at the root are needed to properly have access to tools for files through out the whole project. Especiallly since the Web files are disseminated throughout the whole project, and aren't contained entirely inplatform/web
. (I'm trying my best to not spread out as much as possible, though.) In another PR, I will remove the existingpackage.json
inplatform/web
.Note
This PR is neither a bug nor an enhancement. It will be required to upgrade emscripten especially regarding WebXR (cc. @dsnopek). And it will be required to "revert" #105833 (see #105833 (comment)).
This PR replaces the current way we call ESLint in pre-commit. Instead of calling
eslint
directly, it runsnode misc/scripts/run_eslint.mjs
in order to preprocess some emscripten library files.It fixes a huge issue, the fact that it was impossible to lint emscripten libraries with macros.
This is quite "simple". Imagine that you have
platform/web/js/libs/library_godot_with_macros.js
.To make it lintable with eslint, you just have now to add
platform/web/js/libs/library_godot_with_macros.lint_settings.json
with all the macros in the file defined to a value. The file will be preprocessed to remove the macros according to the settings file values and eslint will parse the preprocessed file afterwards.You have branching macro paths? No problem. The system in my PR supports multiple
lint_settings.json
files.library_godot_with_macros.lint_settings.webgl_enabled.json
andlibrary_godot_with_macros.lint_settings.webgl_disabled.json
are valid setting files forlibrary_godot_with_macros.js
.Demo
platform/web/js/libs/library_godot_with_macros.js
platform/web/js/libs/library_godot_with_macros.lint_settings.hello.json
platform/web/js/libs/library_godot_with_macros.lint_settings.world.json
platform/web/js/libs/library_godot_with_macros.lint_settings.huh.json
eslint will complain because of the unindentified symbol
HUH
.Conclusion
I cannot stress enough about how much I tried to wrap the problem. I thought initially to just use a ESLint preprocessor in order to strip the macros before linting, but this is quite a naive approach. If variables are defined and used only in some contexts, or if a function is closed early, then ESLint breaks.
This method is not perfect, as it will require multiple
lint_settings.json
files to properly lint a complex library file with multiple macros used, but it's better than nothing. Emscripten itself doesn't seem to know how to handle their own macros. It leads to obvious syntax errors getting through. They are thinking for a way to fix that issue, though.