-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[[FIX]] Allow linting of inline module scripts in html file #3492
base: main
Are you sure you want to change the base?
Conversation
If esversion >= 6, extract the code from a script type=module for linting. Prior to this commit, was skipping.
Thanks for the patch! There's one high-level change I'd like to see before diving in to the details. In general, the distinction between "module code" and "script code" is provided "out of band." You can't always tell if how a JavaScript file is intended to be interpreted just from looking at its source. In Node.js, this is communicated either through the So, when JSHint extracts code from a Does that make sense? If so, I'd recommend updating the (Also, you can remove the changes to the files in the |
Hi jugglinmike, <script type=module id=a>...</script>
<script type=text/javascript id=b>...</script>
<script type=module id=c>...</script>
<script id=d>...</script> In order to properly detect undeclared and unknown variables at least two passes would need to be made through this file (one for each module script). The first pass would scan scripts b+d+a and the second through b+d+c.
FWIW I took a look at eslint-plugin-html and it treats each script block as its own sandbox. We can emulate this behavior in JSHint in the above example by making 4 passes through the file (one for each script block), but the results would not be correct. Here is the code I used to test; there are no errors in the browser: <html>
<body>
<script>
let x = 5;
</script>
<script type="module">
console.log("x+1=" + (x + 1));
console.log("u=" + u);
</script>
<script>
let u = 40;
console.log("x=" + x);
</script>
</body>
</html> How would you like to proceed? |
Thanks for looking in to this. You're right about the unexpected challenge. To accomodate this, don't think we want to lint any of the input more than once, and I expect that we should be linting each in document order. Currently, the extraction logic concatenates all the script sources together. This won't work with module code, so we'll have to process each source separately. That means invoking the This is easier said than done. As you point out, the sources are dependent on each other, most notably in the form of global variables. Another by-product of the current implementation is that linting rules are also shared--set To accomodate this, we'll need to collect the results of one invocation of Does this make sense? I'm happy to keep working on this with you, but I also know it's more than you bargained for, so no hard feelings if you want to step back. |
If esversion >= 6, extract the code from a script type=module for
linting. Prior to this commit, was skipping.