Skip to content

Allow import.meta in non-module <script> tags in HTML - #19590

Closed
Socialpranker wants to merge 2 commits into
prettier:mainfrom
Socialpranker:fix/15999-script-tag-formatting
Closed

Socialpranker wants to merge 2 commits into
prettier:mainfrom
Socialpranker:fix/15999-script-tag-formatting

Conversation

@Socialpranker

Copy link
Copy Markdown

Prettier formats a <script> tag's content as sourceType "script" unless it has type="module". import.meta inside such a script makes Babel raise ImportMetaOutsideModule, 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.meta shows 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 added ImportMetaOutsideModule to the existing allowedReasonCodesArray list in src/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 for import.meta, so nothing else changes.

Added a fixture at tests/format/html/script/import-meta.html covering both a plain <script> and a <script type="module"> with import.meta, and confirmed the snapshot now shows both fully formatted instead of the module-less one passing through untouched. Ran the full tests/format/html, tests/format/js, tests/format/vue, and tests/format/misc suites locally and everything passes.

Fixes #15999

Written with Claude Code; I reviewed and tested the change before submitting.

Socialpranker and others added 2 commits July 10, 2026 21:08
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]>
@netlify

netlify Bot commented Jul 10, 2026

Copy link
Copy Markdown

✅ Deploy Preview for prettier ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 96f5bda
🔍 Latest deploy log https://app.netlify.com/projects/prettier/deploys/6a514349278c48000826fa4e
😎 Deploy Preview https://deploy-preview-19590--prettier.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Jul 10, 2026

Copy link
Copy Markdown

✅ Deploy Preview for prettier ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 81192cd
🔍 Latest deploy log https://app.netlify.com/projects/prettier/deploys/6a514361e5bac6000878c5f9
😎 Deploy Preview https://deploy-preview-19590--prettier.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@fisker fisker closed this Jul 10, 2026
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.

incorrect formatting of JS code inside <script> tag in HTML file

2 participants