Repository navigation
Allow import.meta in non-module <script> tags in HTML - #19590
Closed
Socialpranker wants to merge 2 commits into
Closed
Socialpranker wants to merge 2 commits into
Socialpranker wants to merge 2 commits into
Conversation
Prettier parses embedded <script> content with sourceType "script" unless type="module" is set, so import.meta triggered Babel's ImportMetaOutsideModule error. Since embed.js swallows errors from failed embeds, the whole script block was left unformatted instead of erroring visibly. import.meta is valid in classic scripts loaded as ES modules by bundlers and in inline scripts inside custom elements, so treat this error the same way other technically-invalid-but-safe-to-format syntax is already allowed in allowedReasonCodesArray. Fixes #15999 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Co-Authored-By: Claude Opus 4.8 <[email protected]>
✅ Deploy Preview for prettier ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for prettier ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Prettier formats a
<script>tag's content as sourceType "script" unless it hastype="module".import.metainside such a script makes Babel raiseImportMetaOutsideModule, and since the HTML embed logic silently drops any embed that throws, the whole script block was left completely unformatted instead of showing a clear error.import.metashows up in classic scripts all the time, for example inline scripts in custom elements or code emitted by bundlers, so this isn't really invalid code Prettier needs to reject. I addedImportMetaOutsideModuleto the existingallowedReasonCodesArraylist insrc/language-js/parse/babel.js, which already treats several other technically-invalid-but-safe-to-format syntax cases the same way (DeclarationMissingInitializer,NonAbstractClassHasAbstractMethod, etc). Babel's error recovery still builds a normal AST node forimport.meta, so nothing else changes.Added a fixture at
tests/format/html/script/import-meta.htmlcovering both a plain<script>and a<script type="module">withimport.meta, and confirmed the snapshot now shows both fully formatted instead of the module-less one passing through untouched. Ran the fulltests/format/html,tests/format/js,tests/format/vue, andtests/format/miscsuites locally and everything passes.Fixes #15999
Written with Claude Code; I reviewed and tested the change before submitting.