Skip to content

Commit

Permalink
Fix max-line-length column number for lines with urls
Browse files Browse the repository at this point in the history
Closes #2286
  • Loading branch information
David Clark committed Jan 28, 2017
1 parent 14f083d commit 5942c48
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/rules/max-line-length/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ a { color: red }

Lines that exceed the maximum length but contain no whitespace (other than at the beginning of the line) are ignored.

When evaluating the line length, `url(...)` functions are collapsed into just `url()`, because typically you have no control over the length of its argument. This means that long `url()` functions should not contribute to warnings.
When evaluating the line length, the arguments of any `url(...)` functions are excluded from the calculation, because typically you have no control over the length of these arguments. This means that long `url()` functions should not contribute to warnings.

## Options

Expand Down Expand Up @@ -103,7 +103,7 @@ a { color: pink; }
```css
/*
* comment that is too long the max length
* comment that is too long the max length
* comment that is too long the max length
*
*/
a { color: pink; }
Expand Down
16 changes: 11 additions & 5 deletions lib/rules/max-line-length/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict"

const execall = require("execall")
const optionsMatches = require("../../utils/optionsMatches")
const report = require("../../utils/report")
const ruleMessages = require("../../utils/ruleMessages")
Expand Down Expand Up @@ -32,9 +33,7 @@ const rule = function (maxLength, options) {
return
}

// Collapse all urls into something nice and short,
// so they do not throw the game
const rootString = root.toString().replace(/url\(.*\)/ig, "url()")
const rootString = root.toString()

const ignoreNonComments = optionsMatches(options, "ignore", "non-comments")
const ignoreComments = optionsMatches(options, "ignore", "comments")
Expand Down Expand Up @@ -66,9 +65,16 @@ const rule = function (maxLength, options) {
nextNewlineIndex = rootString.length
}

const rawLineLength = nextNewlineIndex - match.endIndex
const lineText = rootString.slice(match.endIndex, nextNewlineIndex)
const urlValueLengths = execall(/url\((.*)\)/ig, lineText).reduce((result, match) => {
return result + _.get(match, "sub[0].length", 0)
}, 0)

// If the line's length is less than or equal to the specified
// max, ignore it ... So anything below is liable to be complained about
if (nextNewlineIndex - match.endIndex <= maxLength) {
// max, ignore it ... So anything below is liable to be complained about.
// **Note that the length of any url values are excluded from the calculation.**
if (rawLineLength - urlValueLengths <= maxLength) {
return
}

Expand Down

0 comments on commit 5942c48

Please sign in to comment.