-
Notifications
You must be signed in to change notification settings - Fork 54
CSS tree validator with custom processor #1184
Changes from 15 commits
6e3617b
7a28d7d
180e5f5
7debac2
d680511
eca6133
889bd5c
7cf32df
92a35f2
f7c86c7
32a6e55
86ce13e
37313b6
fc2f8f5
f45a8d1
483c344
904ef58
f95c08d
e1a1c86
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,24 @@ | ||
{ | ||
"rules": { | ||
"declaration-no-important": true | ||
"declaration-no-important": true, | ||
"csstree/validator": true | ||
}, | ||
"processors": [["stylelint-processor-styled-components", { | ||
"parserPlugins": [ | ||
"jsx", | ||
"exportDefaultFrom" | ||
] | ||
}]], | ||
"processors": [ | ||
[ | ||
"stylelint-processor-styled-components", { | ||
"parserPlugins": [ | ||
"jsx", | ||
"exportDefaultFrom" | ||
] | ||
} | ||
], | ||
"./lib/stylelint/processorRemoveMixins.js" | ||
], | ||
"extends": [ | ||
"stylelint-config-recommended", | ||
"stylelint-config-styled-components" | ||
], | ||
"plugins": [ | ||
"stylelint-csstree-validator" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// If this proposed functionality ever gets added https://github.com/styled-components/stylelint-processor-styled-components/issues/271 | ||
// then please delete this processor and any use of it. | ||
|
||
module.exports = () => ({ | ||
code: input => | ||
input.replace(/-styled-mixin\d+:\s.+(?!{|,).$/gm, `/* styled-mixin */`), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was there any particular reason/preference why we are commenting out rather than removing the styled-mixin? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeh there is. By removing the line with the styled-mixin it can open a can of worms of linting errors such as having a selector with no styles e.g. const example = css`
${({ height }) => height ? `height: ${height};` : ''}
`; would produce .selector1 {
} and also having a semi-colon where you don't need one e.g. const example = css`
width: 100%;
${({ height }) => height ? `height: ${height};` : ''}
`; would produce .selector1 {
width: 100%; <--- semi colon not needed
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The empty selector makes sense, that's not ideal. On the semi-colon is that suggesting the semi-colon's are only needed in CSS if there is a trailing statement? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's true but after investigation I realised it's bad practice to do that and the semi-colon error must have been related to something else. |
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
const processorRemoveMixins = require('./processorRemoveMixins'); | ||
|
||
it('should comment out styled-components mixin styles', () => { | ||
const input = ` | ||
.selector78 { | ||
list-style-type: none; | ||
margin: $dummyValue 0 0 0; | ||
-styled-mixin1: dummyValue; | ||
-styled-mixin2: dummyValue; | ||
padding: 0; | ||
} | ||
`; | ||
const actual = processorRemoveMixins().code(input); | ||
const expected = ` | ||
.selector78 { | ||
list-style-type: none; | ||
margin: $dummyValue 0 0 0; | ||
/* styled-mixin */ | ||
/* styled-mixin */ | ||
padding: 0; | ||
} | ||
`; | ||
|
||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
it('should comment out styled-components mixins styles in media queries', () => { | ||
const input = ` | ||
.selector78 { | ||
list-style-type: none; | ||
margin: $dummyValue 0 0 0; | ||
-styled-mixin1: dummyValue; | ||
-styled-mixin2: dummyValue; | ||
padding: 0; | ||
|
||
@media (min-width: $dummyValue) { | ||
margin: $dummyValue 0 0 0; | ||
-styled-mixin3: dummyValue; | ||
} | ||
} | ||
`; | ||
const actual = processorRemoveMixins().code(input); | ||
const expected = ` | ||
.selector78 { | ||
list-style-type: none; | ||
margin: $dummyValue 0 0 0; | ||
/* styled-mixin */ | ||
/* styled-mixin */ | ||
padding: 0; | ||
|
||
@media (min-width: $dummyValue) { | ||
margin: $dummyValue 0 0 0; | ||
/* styled-mixin */ | ||
} | ||
} | ||
`; | ||
|
||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
it('should comment out styled-components mixins styles without semi-colons', () => { | ||
const input = ` | ||
.selector78 { | ||
list-style-type: none; | ||
margin: $dummyValue 0 0 0; | ||
-styled-mixin1: dummyValue; | ||
-styled-mixin2: dummyValue; | ||
-styled-mixin3: dummyValue | ||
padding: 0; | ||
-styled-mixin4: dummyValue | ||
} | ||
`; | ||
const actual = processorRemoveMixins().code(input); | ||
const expected = ` | ||
.selector78 { | ||
list-style-type: none; | ||
margin: $dummyValue 0 0 0; | ||
/* styled-mixin */ | ||
/* styled-mixin */ | ||
/* styled-mixin */ | ||
padding: 0; | ||
/* styled-mixin */ | ||
} | ||
`; | ||
|
||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
it('should not comment out styled-components mixins selectors', () => { | ||
const input = ` | ||
-styled-mixin1:hover &, | ||
-styled-mixin4:focus & { | ||
-styled-mixin5: dummyValue | ||
text-decoration: none; | ||
border-bottom: $dummyValue solid $dummyValue; | ||
-styled-mixin1: 0; | ||
} | ||
`; | ||
const actual = processorRemoveMixins().code(input); | ||
const expected = ` | ||
-styled-mixin1:hover &, | ||
-styled-mixin4:focus & { | ||
/* styled-mixin */ | ||
text-decoration: none; | ||
border-bottom: $dummyValue solid $dummyValue; | ||
/* styled-mixin */ | ||
} | ||
`; | ||
|
||
expect(actual).toEqual(expected); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,7 +101,7 @@ const StyledListItem = styled.li` | |
` | ||
: css` | ||
right: 0; | ||
`}; | ||
`} | ||
} | ||
`; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does styled-components/stylelint-processor-styled-components#271 (comment) not work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately it doesn't tell the CSS validator plugin to ignore those custom properties and rules. That's why the plugin has it's own ignore option which doesn't support regular expressions like stylelint's rules do. I have suggested this functionality in csstree/stylelint-validator