@@ -9,13 +9,15 @@ const isStyledImport = require('../utils/styled').isStyledImport
9
9
const wrapSelector = require ( '../utils/general' ) . wrapSelector
10
10
const wrapKeyframes = require ( '../utils/general' ) . wrapKeyframes
11
11
const fixIndentation = require ( '../utils/general' ) . fixIndentation
12
+ const isStylelintComment = require ( '../utils/general' ) . isStylelintComment
12
13
13
14
const getTTLContent = require ( '../utils/tagged-template-literal.js' ) . getTaggedTemplateLiteralContent
14
15
const parseImports = require ( '../utils/parse' ) . parseImports
15
16
const getSourceMap = require ( '../utils/parse' ) . getSourceMap
16
17
17
18
const processStyledComponentsFile = ast => {
18
19
const extractedCSS = [ ]
20
+ let ignoreRuleComments = [ ]
19
21
let importedNames = {
20
22
default : 'styled' ,
21
23
css : 'css' ,
@@ -26,6 +28,13 @@ const processStyledComponentsFile = ast => {
26
28
traverse ( ast , {
27
29
noScope : true ,
28
30
enter ( { node } ) {
31
+ if ( node . type !== 'Program' && node . leadingComments ) {
32
+ node . leadingComments . forEach ( comment => {
33
+ if ( isStylelintComment ( comment . value ) ) {
34
+ ignoreRuleComments . push ( `/*${ comment . value } */` )
35
+ }
36
+ } )
37
+ }
29
38
if ( isStyledImport ( node ) ) {
30
39
importedNames = parseImports ( node )
31
40
return
@@ -36,11 +45,20 @@ const processStyledComponentsFile = ast => {
36
45
const fixedContent = fixIndentation ( content ) . text
37
46
const wrapperFn = helper === 'keyframes' ? wrapKeyframes : wrapSelector
38
47
const wrappedContent = wrapperFn ( fixedContent )
39
- extractedCSS . push ( wrappedContent )
48
+ const stylelintCommentsAdded =
49
+ ignoreRuleComments . length > 0
50
+ ? `${ ignoreRuleComments . join ( '\n' ) } \n${ wrappedContent } `
51
+ : wrappedContent
52
+ extractedCSS . push ( stylelintCommentsAdded )
40
53
sourceMap = Object . assign (
41
54
sourceMap ,
42
55
getSourceMap ( extractedCSS . join ( '\n' ) , wrappedContent , node . loc . start . line )
43
56
)
57
+ /**
58
+ * All queued comments have been added to the file so we don't need to, and actually shouldn't
59
+ * add them to the file more than once
60
+ */
61
+ ignoreRuleComments = [ ]
44
62
}
45
63
} )
46
64
0 commit comments