Skip to content

Fix PostCSS crash when using toJSON() #18083

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

Merged
merged 1 commit into from
May 19, 2025
Merged

Conversation

thecrypticace
Copy link
Contributor

When I added source maps to PostCSS I mistakenly assumed that .source on a node could be undefined. The comment above the property in PostCSS says that source can be undefined but this is a commentary on the value upon access not its expected value on write:

declare abstract class Node_ {
  /**
   * …
   *
   * The nodes that are created manually using the public APIs
   * provided by PostCSS will have `source` undefined and
   * will be absent in the source map.
   *
   * …
   */
  source?: Node.Source
}

Rather, what these types mean is that if the property exists it must be defined. But otherwise the property can be missing if a node has no source location metadata. This generally wasn't a problem with the string-returning APIs but the toJSON() API in PostCSS expects that source is defined if present. This caused a crash because our license comment doesn't have a source location.

I've addressed this by deleting the source property from the node if source location data is not available.

Fixes #18082

ref parcel-bundler/parcel#10161

@thecrypticace thecrypticace requested a review from a team as a code owner May 19, 2025 14:23
@thecrypticace thecrypticace merged commit 9df5ba7 into main May 19, 2025
7 checks passed
@thecrypticace thecrypticace deleted the fix/postcss-source-undefined branch May 19, 2025 15:28
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.

@tailwindcss/postcss 4.1.6 with @parcel/transformer-postcss: Cannot read properties of undefined (reading 'input')
2 participants