mirror of
https://github.com/tailwindlabs/tailwindcss.git
synced 2025-12-08 21:36:08 +00:00
Fix PostCSS crash when using toJSON() (#18083)
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**:
```ts
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 https://github.com/parcel-bundler/parcel/issues/10161
This commit is contained in:
parent
a42251cc29
commit
9df5ba78bf
@ -49,6 +49,20 @@ export function cssAstToPostCssAst(ast: AstNode[], source: PostcssSource | undef
|
||||
}
|
||||
}
|
||||
|
||||
function updateSource(astNode: PostCssChildNode, loc: SourceLocation | undefined) {
|
||||
let source = toSource(loc)
|
||||
|
||||
// The `source` property on PostCSS nodes must be defined if present because
|
||||
// `toJSON()` reads each property and tries to read from source.input if it
|
||||
// sees a `source` property. This means for a missing or otherwise absent
|
||||
// source it must be *missing* from the object rather than just `undefined`
|
||||
if (source) {
|
||||
astNode.source = source
|
||||
} else {
|
||||
delete astNode.source
|
||||
}
|
||||
}
|
||||
|
||||
function transform(node: AstNode, parent: PostCssContainerNode) {
|
||||
// Declaration
|
||||
if (node.kind === 'declaration') {
|
||||
@ -57,14 +71,14 @@ export function cssAstToPostCssAst(ast: AstNode[], source: PostcssSource | undef
|
||||
value: node.value ?? '',
|
||||
important: node.important,
|
||||
})
|
||||
astNode.source = toSource(node.src)
|
||||
updateSource(astNode, node.src)
|
||||
parent.append(astNode)
|
||||
}
|
||||
|
||||
// Rule
|
||||
else if (node.kind === 'rule') {
|
||||
let astNode = postcss.rule({ selector: node.selector })
|
||||
astNode.source = toSource(node.src)
|
||||
updateSource(astNode, node.src)
|
||||
astNode.raws.semicolon = true
|
||||
parent.append(astNode)
|
||||
for (let child of node.nodes) {
|
||||
@ -75,7 +89,7 @@ export function cssAstToPostCssAst(ast: AstNode[], source: PostcssSource | undef
|
||||
// AtRule
|
||||
else if (node.kind === 'at-rule') {
|
||||
let astNode = postcss.atRule({ name: node.name.slice(1), params: node.params })
|
||||
astNode.source = toSource(node.src)
|
||||
updateSource(astNode, node.src)
|
||||
astNode.raws.semicolon = true
|
||||
parent.append(astNode)
|
||||
for (let child of node.nodes) {
|
||||
@ -90,7 +104,7 @@ export function cssAstToPostCssAst(ast: AstNode[], source: PostcssSource | undef
|
||||
// spaces.
|
||||
astNode.raws.left = ''
|
||||
astNode.raws.right = ''
|
||||
astNode.source = toSource(node.src)
|
||||
updateSource(astNode, node.src)
|
||||
parent.append(astNode)
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user