hudochenkov / postcss-sorting

PostCSS plugin to keep rules and at-rules content in order.
MIT License
516 stars 30 forks source link

Error when using with postcss-sass: Cannot read properties of undefined (reading 'includes') #107

Closed junebugfix closed 1 year ago

junebugfix commented 1 year ago

Hi! I've discovered that when trying to combine postcss-sorting with postcss-sass, I get an error.

I've made a minimal repro in this repo: https://github.com/junebugfix/postcss-sorting-repro

Here is the error:

TypeError: Cannot read properties of undefined (reading 'includes')
    at /Users/june/code/postcss-sorting-repro/styles.sass:1:1
    at /Users/june/code/postcss-sorting-repro/styles.sass:1:1
    at isAllowedToProcess (/Users/june/code/postcss-sorting-repro/node_modules/postcss-sorting/lib/isAllowedToProcess.js:13:22)
    at sortNodeProperties (/Users/june/code/postcss-sorting-repro/node_modules/postcss-sorting/lib/properties-order/sortNodeProperties.js:12:7)
    at /Users/june/code/postcss-sorting-repro/node_modules/postcss-sorting/index.js:52:4
    at /Users/june/code/postcss-sorting-repro/node_modules/postcss/lib/container.js:360:44
    at /Users/june/code/postcss-sorting-repro/node_modules/postcss/lib/container.js:55:18
    at Root.each (/Users/june/code/postcss-sorting-repro/node_modules/postcss/lib/container.js:41:16)
    at Root.walk (/Users/june/code/postcss-sorting-repro/node_modules/postcss/lib/container.js:52:17)
    at Proxy.<anonymous> (/Users/june/code/postcss-sorting-repro/node_modules/postcss/lib/container.js:357:30)
    at plugin (/Users/june/code/postcss-sorting-repro/node_modules/postcss-sorting/index.js:49:7)
    at Root (/Users/june/code/postcss-sorting-repro/node_modules/postcss-sorting/index.js:11:4) {
  postcssNode: <ref *1> Root {
    raws: { semicolon: undefined, before: '' },
    type: 'root',
    nodes: [ [Rule] ],
    source: { start: [Object], end: [Object], input: [Input] },
    proxyCache: [Circular *1],
    lastEach: 1,
    indexes: { '1': 0 },
    [Symbol(isClean)]: true,
    [Symbol(my)]: true
  }
}

Admittedly I don't fully understand all the moving parts here, but it appears this error comes from this line in postcss-sorting when the provided node.raws does not contain an after property. Making the following change fixes the error and correctly sorts the css properties:

-   if (node.raws.after.includes('${')) {
+   if (node.raws.after?.includes('${')) {

I don't understand the library or the workings of postcss to be confident in making a PR, but it seems like this might be an easy fix!

hudochenkov commented 1 year ago

Thank you for providing all the information and repro repository. Could you send a PR, please?

hudochenkov commented 1 year ago

Released in 8.0.2.