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

Commit 09cf61b

Browse files
committed
Filter indentation warnings generated by interpolations substitution
1 parent 0760875 commit 09cf61b

File tree

6 files changed

+46
-27
lines changed

6 files changed

+46
-27
lines changed

src/index.js

+27-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'
@@ -11,11 +12,15 @@ module.exports = options => ({
1112
code(input, filepath) {
1213
const absolutePath = path.resolve(process.cwd(), filepath)
1314
sourceMapsCorrections[absolutePath] = {}
14-
const { extractedCSS, sourceMap } = parse(
15+
const { extractedCSS, interpolationLines, sourceMap } = parse(
1516
input,
1617
absolutePath,
1718
Object.assign({}, DEFAULT_OPTIONS, options)
1819
)
20+
// Save interpolation lines
21+
interpolationLinesMap[absolutePath] = interpolationLines.concat(
22+
interpolationLinesMap[absolutePath] || []
23+
)
1924
// Save source location, merging existing corrections with current corrections
2025
sourceMapsCorrections[absolutePath] = Object.assign(
2126
sourceMapsCorrections[absolutePath],
@@ -25,16 +30,28 @@ module.exports = options => ({
2530
},
2631
// Fix sourcemaps
2732
result(stylelintResult, filepath) {
33+
const interpolationLines = interpolationLinesMap[filepath] || []
2834
const lineCorrection = sourceMapsCorrections[filepath]
29-
const warnings = stylelintResult.warnings.map(warning =>
30-
Object.assign({}, warning, {
31-
// Replace "brace" with "backtick" in warnings, e.g.
32-
// "Unexpected empty line before closing backtick" (instead of "brace")
33-
text: warning.text.replace(/brace/, 'backtick'),
34-
line: lineCorrection[warning.line]
35-
})
36-
)
35+
const warnings = stylelintResult.warnings
36+
.filter(
37+
warning =>
38+
// Filter indentation warnings generated by interpolations substitution
39+
warning.rule !== 'indentation' ||
40+
interpolationLines.indexOf(lineCorrection[warning.line]) < 0
41+
)
42+
.map(warning =>
43+
Object.assign({}, warning, {
44+
// Replace "brace" with "backtick" in warnings, e.g.
45+
// "Unexpected empty line before closing backtick" (instead of "brace")
46+
text: warning.text.replace(/brace/, 'backtick'),
47+
line: lineCorrection[warning.line]
48+
})
49+
)
3750

38-
return Object.assign({}, stylelintResult, { warnings })
51+
return Object.assign({}, stylelintResult, {
52+
warnings,
53+
// Remember to fix
54+
errored: warnings.length ? true : undefined
55+
})
3956
}
4057
})

src/parsers/index.js

+11
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,15 @@ const processStyledComponentsFile = (ast, absolutePath, options) => {
7677
sourceMap,
7778
getSourceMap(extractedCSS.join('\n'), wrappedContent, processedNode.quasi.loc.start.line)
7879
)
80+
// Save interpolation lines
81+
node.quasi.expressions.forEach((expression, i) => {
82+
let l = node.quasi.quasis[i].loc.end.line + 1
83+
while (l <= node.quasi.quasis[i + 1].loc.start.line) {
84+
interpolationLines.push(l)
85+
l += 1
86+
}
87+
})
88+
7989
/**
8090
* All queued comments have been added to the file so we don't need to, and actually shouldn't
8191
* add them to the file more than once
@@ -86,6 +96,7 @@ const processStyledComponentsFile = (ast, absolutePath, options) => {
8696

8797
return {
8898
extractedCSS: extractedCSS.join('\n'),
99+
interpolationLines,
89100
sourceMap
90101
}
91102
}

src/utils/tagged-template-literal.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,10 @@ const interleave = (quasis, expressions, absolutePath) => {
142142
substitute = '$dummyValue'
143143
}
144144
// Make sure substituted by same count of lines
145-
const intentCount = prevText.split('\n').pop().length
146-
const intent = new Array(intentCount).fill(' ').join('')
147145
const targetLines = quasis[i + 1].loc.start.line - quasis[i].loc.end.line + 1
148146
let currentLines = substitute.split('\n').length
149147
while (currentLines < targetLines) {
150-
substitute += `\n${intent}-styled-mixin${count}: dummyValue;`
148+
substitute += '\n/* dummyComment */'
151149
count += 1
152150
currentLines += 1
153151
}

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)