Skip to content
This repository has been archived by the owner on May 14, 2021. It is now read-only.

Move to shared config #49

Merged
merged 10 commits into from
Aug 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ You need:

- `stylelint` (duh)
- This processor (to add `styled-components` support)
- The [stylelint-config-styled-components-processor](https://github.com/styled-components/stylelint-config-styled-components-processor) shareable config
- The standard config for stylelint (or any config you like)

```
npm install --save-dev stylelint-processor-styled-components stylelint stylelint-config-standard
npm install --save-dev stylelint-processor-styled-components stylelint-config-styled-components-processor stylelint stylelint-config-standard
```

### Setup
Expand All @@ -32,7 +33,10 @@ Add a `.stylelintrc` file to the root of your project:
```JSON
{
"processors": ["stylelint-processor-styled-components"],
"extends": "stylelint-config-standard",
"extends": [
"stylelint-config-standard",
"stylelint-config-styled-components-processor"
],
"syntax": "scss"
}
```
Expand All @@ -59,6 +63,19 @@ Now you can lint your CSS by running this script! 🎉
npm run lint:css
```

### Processor specific stylelint rules

When using this processor a couple of stylelint rules throw errors that you cannot prevent. Like
'[no-empty-source](https://stylelint.io/user-guide/rules/no-empty-source)' or
'[no-missing-end-of-source-newline](https://stylelint.io/user-guide/rules/no-missing-end-of-source-newline)'.

The [stylelint-config-styled-components-processor](https://github.com/styled-components/stylelint-config-styled-components-processor)
shareable config will automatically disable rules that cause unresolvable conflicts. Besides those
rules vendor prefixed [properties](https://stylelint.io/user-guide/rules/property-no-vendor-prefix)
and [values](https://stylelint.io/user-guide/rules/value-no-vendor-prefix) will throw an error since
styled-components automatically generates vendor prefixes for your css. Note that if you want to
change any of these rules you can always override them in your stylelint config.

### Webpack

For use with Webpack you can use the [`stylelint-custom-processor-loader`](https://github.com/emilgoldsmith/stylelint-custom-processor-loader).
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
"prettier": "^1.4.4",
"stylelint": "^8.0.0"
},
"peerDependency": {
"stylelint-config-styled-components-processor": "^0.1.0"
},
"dependencies": {
"babel-traverse": "^6.16.0",
"babylon": "^6.12.0",
Expand Down
27 changes: 4 additions & 23 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
const path = require('path')
const parse = require('./parsers/index')
// TODO Fix ampersand in selectors
// TODO ENFORCE THESE RULES
// value-no-vendor-prefix – don't allow vendor prefixes
// property-no-vendor-prefix – don't allow vendor prefixes

const ignoredRules = [
// Don't throw if there's no styled-components in a file
'no-empty-source',
// We don't care about end-of-source newlines, users cannot control them
'no-missing-end-of-source-newline'
]

const sourceMapsCorrections = {}

Expand All @@ -30,23 +19,15 @@ module.exports = (/* options */) => ({
// Fix sourcemaps
result(stylelintResult, filepath) {
const lineCorrection = sourceMapsCorrections[filepath]
const newWarnings = stylelintResult.warnings.reduce((prevWarnings, warning) => {
if (ignoredRules.includes(warning.rule)) return prevWarnings
const correctedWarning = Object.assign(warning, {
const warnings = stylelintResult.warnings.map(warning =>
Object.assign({}, warning, {
// Replace "brace" with "backtick" in warnings, e.g.
// "Unexpected empty line before closing backtick" (instead of "brace")
text: warning.text.replace(/brace/, 'backtick'),
line: lineCorrection[warning.line]
})
prevWarnings.push(correctedWarning)
return prevWarnings
}, [])

if (newWarnings.length === 0) {
// eslint-disable-next-line no-param-reassign
stylelintResult.errored = false
}
)

return Object.assign(stylelintResult, { warnings: newWarnings })
return Object.assign({}, stylelintResult, { warnings })
}
})