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

Commit

Permalink
Merge pull request #220 from chinesedfan/filter_warnings
Browse files Browse the repository at this point in the history
�Fix warnings caused by 1.2.0
  • Loading branch information
chinesedfan committed Sep 27, 2018
2 parents aa8cec0 + ec93c42 commit 1c16989
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
22 changes: 17 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ const DEFAULT_OPTIONS = {
importName: 'default'
}

function isCausedBySubstitution(warning, line, interpolationLines) {
return interpolationLines.some(({ start, end }) => {
if (line > start && line < end) {
// Inner interpolation lines must be
return true
} else if (line === start) {
return ['value-list-max-empty-lines', 'comment-empty-line-before'].indexOf(warning.rule) >= 0
} else if (line === end) {
return ['comment-empty-line-before', 'indentation'].indexOf(warning.rule) >= 0
} else {
return false
}
})
}

module.exports = options => ({
// Get string for stylelint to lint
code(input, filepath) {
Expand Down Expand Up @@ -79,11 +94,8 @@ module.exports = options => ({
const warnings = stylelintResult.warnings
.filter(
warning =>
// Filter indentation warnings generated by interpolations substitution
!(
warning.rule === 'indentation' &&
interpolationLines.indexOf(lineCorrection[warning.line]) >= 0
)
// Filter false-positive warnings generated by interpolations substitution
!isCausedBySubstitution(warning, lineCorrection[warning.line], interpolationLines)
)
.map(warning =>
Object.assign({}, warning, {
Expand Down
11 changes: 4 additions & 7 deletions src/parsers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,10 @@ const processStyledComponentsFile = (ast, absolutePath, options) => {
)
// Save dummy interpolation lines
node.quasi.expressions.forEach((expression, i) => {
// Skip the end line of previous text
// because the indentation is not generated by substitution
let l = node.quasi.quasis[i].loc.end.line + 1
while (l <= node.quasi.quasis[i + 1].loc.start.line) {
interpolationLines.push(l)
l += 1
}
interpolationLines.push({
start: node.quasi.quasis[i].loc.end.line,
end: node.quasi.quasis[i + 1].loc.start.line
})
})

/**
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/interpolations/complex.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,18 @@ const Div = styled.div`
const Button = styled.button`
${props => props.isHovering && interpolatedStyle}
`

// Multi-line interpolations
const MultiLineDiv = styled.div`
color: ${
'red'
};
${
'long values'
}
`;
3 changes: 3 additions & 0 deletions test/interpolations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ const path = require('path')
const processor = path.join(__dirname, '../lib/index.js')
const rules = {
'block-no-empty': true,
'comment-empty-line-before': 'always',
'declaration-block-no-duplicate-properties': true,
'value-list-max-empty-lines': 0,
'max-empty-lines': 1,
indentation: 2
}

Expand Down

0 comments on commit 1c16989

Please sign in to comment.