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

Commit a163452

Browse files
committed
Add full file support for ignoring rules by comment
1 parent aa92639 commit a163452

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/parsers/index.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ const isStyledImport = require('../utils/styled').isStyledImport
99
const wrapSelector = require('../utils/general').wrapSelector
1010
const wrapKeyframes = require('../utils/general').wrapKeyframes
1111
const fixIndentation = require('../utils/general').fixIndentation
12+
const isStylelintComment = require('../utils/general').isStylelintComment
1213

1314
const getTTLContent = require('../utils/tagged-template-literal.js').getTaggedTemplateLiteralContent
1415
const parseImports = require('../utils/parse').parseImports
1516
const getSourceMap = require('../utils/parse').getSourceMap
1617

1718
const processStyledComponentsFile = ast => {
1819
const extractedCSS = []
20+
let ignoreRuleComments = []
1921
let importedNames = {
2022
default: 'styled',
2123
css: 'css',
@@ -26,6 +28,13 @@ const processStyledComponentsFile = ast => {
2628
traverse(ast, {
2729
noScope: true,
2830
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+
}
2938
if (isStyledImport(node)) {
3039
importedNames = parseImports(node)
3140
return
@@ -36,11 +45,20 @@ const processStyledComponentsFile = ast => {
3645
const fixedContent = fixIndentation(content).text
3746
const wrapperFn = helper === 'keyframes' ? wrapKeyframes : wrapSelector
3847
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)
4053
sourceMap = Object.assign(
4154
sourceMap,
4255
getSourceMap(extractedCSS.join('\n'), wrappedContent, node.loc.start.line)
4356
)
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 = []
4462
}
4563
})
4664

src/utils/general.js

+3
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ const isEmptyOrSpaceOnly = text => {
6868
const wrapSelector = content => `.selector${(count += 1)} {${content}}\n`
6969
const wrapKeyframes = content => `@keyframes {${content}}\n`
7070

71+
const isStylelintComment = comment => /^\s*stylelint-(?:enable|disable)(?:\s.*)?$/.test(comment)
72+
7173
exports.wrapKeyframes = wrapKeyframes
7274
exports.wrapSelector = wrapSelector
7375
exports.fixIndentation = fixIndentation
7476
exports.isLastLineWhitespaceOnly = isLastLineWhitespaceOnly
7577
exports.isEmptyOrSpaceOnly = isEmptyOrSpaceOnly
78+
exports.isStylelintComment = isStylelintComment

0 commit comments

Comments
 (0)