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

Commit ca14148

Browse files
committed
Filter indentation warnings generated by interpolations substitution
1 parent 9cce4c8 commit ca14148

File tree

6 files changed

+51
-28
lines changed

6 files changed

+51
-28
lines changed

src/index.js

+30-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require('path')
22
const parse = require('./parsers/index')
33

4+
const interpolationLinesMap = {}
45
const sourceMapsCorrections = {}
56
const DEFAULT_OPTIONS = {
67
moduleName: 'styled-components'
@@ -12,11 +13,15 @@ module.exports = options => ({
1213
try {
1314
const absolutePath = path.resolve(process.cwd(), filepath)
1415
sourceMapsCorrections[absolutePath] = {}
15-
const { extractedCSS, sourceMap } = parse(
16+
const { extractedCSS, interpolationLines, sourceMap } = parse(
1617
input,
1718
absolutePath,
1819
Object.assign({}, DEFAULT_OPTIONS, options)
1920
)
21+
// Save dummy interpolation lines
22+
interpolationLinesMap[absolutePath] = interpolationLines.concat(
23+
interpolationLinesMap[absolutePath] || []
24+
)
2025
// Save source location, merging existing corrections with current corrections
2126
sourceMapsCorrections[absolutePath] = Object.assign(
2227
sourceMapsCorrections[absolutePath],
@@ -33,16 +38,31 @@ module.exports = options => ({
3338
},
3439
// Fix sourcemaps
3540
result(stylelintResult, filepath) {
41+
const interpolationLines = interpolationLinesMap[filepath] || []
3642
const lineCorrection = sourceMapsCorrections[filepath]
37-
const warnings = stylelintResult.warnings.map(warning =>
38-
Object.assign({}, warning, {
39-
// Replace "brace" with "backtick" in warnings, e.g.
40-
// "Unexpected empty line before closing backtick" (instead of "brace")
41-
text: warning.text.replace(/brace/, 'backtick'),
42-
line: lineCorrection[warning.line]
43-
})
44-
)
43+
const warnings = stylelintResult.warnings
44+
.filter(
45+
warning =>
46+
// Filter indentation warnings generated by interpolations substitution
47+
!(
48+
warning.rule === 'indentation' &&
49+
interpolationLines.indexOf(lineCorrection[warning.line]) >= 0
50+
)
51+
)
52+
.map(warning =>
53+
Object.assign({}, warning, {
54+
// Replace "brace" with "backtick" in warnings, e.g.
55+
// "Unexpected empty line before closing backtick" (instead of "brace")
56+
text: warning.text.replace(/brace/, 'backtick'),
57+
line: lineCorrection[warning.line]
58+
})
59+
)
4560

46-
return Object.assign({}, stylelintResult, { warnings })
61+
const result = Object.assign({}, stylelintResult, { warnings })
62+
// Undo `errored` if no warnings with error severity any more
63+
if (result.errored && !warnings.some(warning => warning.severity === 'error')) {
64+
delete result.errored
65+
}
66+
return result
4767
}
4868
})

src/parsers/index.js

+13
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const processStyledComponentsFile = (ast, absolutePath, options) => {
2727
injectGlobal: 'injectGlobal'
2828
}
2929
let sourceMap = {}
30+
const interpolationLines = []
3031
traverse(ast, {
3132
noScope: true,
3233
enter({ node }) {
@@ -76,6 +77,17 @@ const processStyledComponentsFile = (ast, absolutePath, options) => {
7677
sourceMap,
7778
getSourceMap(extractedCSS.join('\n'), wrappedContent, processedNode.quasi.loc.start.line)
7879
)
80+
// Save dummy interpolation lines
81+
node.quasi.expressions.forEach((expression, i) => {
82+
// Skip the end line of previous text
83+
// because the indentation is not generated by substitution
84+
let l = node.quasi.quasis[i].loc.end.line + 1
85+
while (l <= node.quasi.quasis[i + 1].loc.start.line) {
86+
interpolationLines.push(l)
87+
l += 1
88+
}
89+
})
90+
7991
/**
8092
* All queued comments have been added to the file so we don't need to, and actually shouldn't
8193
* add them to the file more than once
@@ -86,6 +98,7 @@ const processStyledComponentsFile = (ast, absolutePath, options) => {
8698

8799
return {
88100
extractedCSS: extractedCSS.join('\n'),
101+
interpolationLines,
89102
sourceMap
90103
}
91104
}

src/utils/tagged-template-literal.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,10 @@ const interleave = (quasis, expressions, absolutePath) => {
147147
substitute = '$dummyValue'
148148
}
149149
// Make sure substituted by same count of lines
150-
const intentCount = prevText.split('\n').pop().length
151-
const intent = new Array(intentCount).fill(' ').join('')
152150
const targetLines = quasis[i + 1].loc.start.line - quasis[i].loc.end.line + 1
153151
let currentLines = substitute.split('\n').length
154152
while (currentLines < targetLines) {
155-
substitute += `\n${intent}-styled-mixin${count}: dummyValue;`
156-
count += 1
153+
substitute += '\n/* dummyComment */'
157154
currentLines += 1
158155
}
159156

test/fixtures/interpolations/invalid.js

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const Button2 = styled.button.attrs({
1010
${
1111
props => props.isHovering && interpolatedStyle
1212
}
13+
position: ${
14+
props => props.position
15+
};
1316
color: ${color};
1417
background: blue;
1518
`

test/hard.test.js

+3-13
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,16 @@ describe('hard', () => {
102102
expect(data.errored).toEqual(true)
103103
})
104104

105-
it('should have seven warning', () => {
106-
expect(data.results[0].warnings.length).toEqual(7)
105+
it('should have five warning', () => {
106+
expect(data.results[0].warnings.length).toEqual(5)
107107
})
108108

109-
it('should have seven warnings about indentation', () => {
109+
it('should have four warnings about indentation', () => {
110110
expect(data.results[0].warnings[0].rule).toEqual('indentation')
111111
expect(data.results[0].warnings[1].rule).toEqual('indentation')
112112
expect(data.results[0].warnings[2].rule).toEqual('indentation')
113113
expect(data.results[0].warnings[3].rule).toEqual('indentation')
114114
expect(data.results[0].warnings[4].rule).toEqual('indentation')
115-
expect(data.results[0].warnings[5].rule).toEqual('indentation')
116-
expect(data.results[0].warnings[6].rule).toEqual('indentation')
117115
})
118116

119117
it('should have a warning in line 5', () => {
@@ -135,13 +133,5 @@ describe('hard', () => {
135133
it('should have a warning in line 35', () => {
136134
expect(data.results[0].warnings[4].line).toEqual(35)
137135
})
138-
139-
it('should have a warning in line 36', () => {
140-
expect(data.results[0].warnings[5].line).toEqual(36)
141-
})
142-
143-
it('should have a warning in line 37', () => {
144-
expect(data.results[0].warnings[6].line).toEqual(37)
145-
})
146136
})
147137
})

test/interpolations.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe('interpolations', () => {
8080
})
8181

8282
it('should have the indentation warning in the right line', () => {
83-
expect(data.results[0].warnings[0].line).toEqual(13)
83+
expect(data.results[0].warnings[0].line).toEqual(16)
8484
})
8585
})
8686

0 commit comments

Comments
 (0)