matype / stylefmt

stylefmt is a tool that automatically formats stylesheets.
Other
2.1k stars 89 forks source link

Fails when part of a postcss-cli chain including postcss-nesting or postcss-nested #276

Closed callahad closed 7 years ago

callahad commented 7 years ago

Stylefmt throws "Cannot read property 'split' of undefined" when used as part of a postcss-cli chain that includes postcss-nesting or postcss-nested.

Steps to reproduce:

  1. npm install postcss-cli postcss-nesting stylefmt
  2. echo ".x{&.y{color:red}}" | postcss --no-map -u postcss-nesting stylefmt

What should happen:

.x {
}

.x.y {
    color: red;
}

What actually happens:

Processing stdinTypeError: Cannot read property 'split' of undefined
    at declarationEmptyLineBefore (/tmp/tmp.xgEO6ZTxPM/node_modules/stylefmt/lib/formatDecls.js:65:33)
    at /tmp/tmp.xgEO6ZTxPM/node_modules/stylefmt/lib/formatDecls.js:26:26
    at /tmp/tmp.xgEO6ZTxPM/node_modules/postcss/lib/container.js:192:28
    at /tmp/tmp.xgEO6ZTxPM/node_modules/postcss/lib/container.js:148:26
    at Rule.each (/tmp/tmp.xgEO6ZTxPM/node_modules/postcss/lib/container.js:114:22)
    at Rule.walk (/tmp/tmp.xgEO6ZTxPM/node_modules/postcss/lib/container.js:147:21)
    at Rule.walkDecls (/tmp/tmp.xgEO6ZTxPM/node_modules/postcss/lib/container.js:190:25)
    at formatDecls (/tmp/tmp.xgEO6ZTxPM/node_modules/stylefmt/lib/formatDecls.js:14:10)
    at /tmp/tmp.xgEO6ZTxPM/node_modules/stylefmt/lib/formatRules.js:64:12
    at /tmp/tmp.xgEO6ZTxPM/node_modules/postcss/lib/container.js:241:28
    at /tmp/tmp.xgEO6ZTxPM/node_modules/postcss/lib/container.js:148:26
    at Root.each (/tmp/tmp.xgEO6ZTxPM/node_modules/postcss/lib/container.js:114:22)
    at Root.walk (/tmp/tmp.xgEO6ZTxPM/node_modules/postcss/lib/container.js:147:21)
    at Root.walkRules (/tmp/tmp.xgEO6ZTxPM/node_modules/postcss/lib/container.js:239:25)
    at formatRules (/tmp/tmp.xgEO6ZTxPM/node_modules/stylefmt/lib/formatRules.js:11:8)
    at /tmp/tmp.xgEO6ZTxPM/node_modules/stylefmt/index.js:18:9

Workaround:

If stylefmt is run separately, it works correctly:

echo ".x{&.y{color:red}}" | postcss --no-map -u postcss-nesting | postcss --no-map -u stylefmt

Further investigation:

callahad commented 7 years ago

When running stylefmt against the expanded CSS, before is an empty string. When running it against the nested CSS, it's not present in decl.raws:

Input decl.raws
.x{&.y{color:red}} { between: ': ' }
.x{}x.y{color:red} { before: '', between: ': ' }

It seems like a potential solution might be to treat an undefined before as the empty string.