Skip to content

Commit

Permalink
Merging fix for stylelint#2481 to the right branch
Browse files Browse the repository at this point in the history
  • Loading branch information
pfructuoso committed Apr 8, 2017
1 parent 0726962 commit ddf1682
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,52 +50,145 @@ testRule(rule, {
code: ".foo { background: -moz-linear-gradient(top, #fff, #000); }",
}, {
code: ".foo { background: -o-linear-gradient(top, #fff, #000); }",
}, {
code: ".foo { background: url(foo.png), -webkit-linear-gradient(bottom, #fff, #000 ); }",
}, {
code: ".foo { background: -webkit-linear-gradient(bottom, #fff, #000 ), url(foo.png); }",
}, {
code: ".foo { background: url(foo.png), -moz-linear-gradient(bottom, #fff, #000 ); }",
}, {
code: ".foo { background: -moz-linear-gradient(bottom, #fff, #000 ), url(foo.png); }",
}, {
code: ".foo { background: url(foo.png), -o-linear-gradient(bottom, #fff, #000 ); }",
}, {
code: ".foo { background: -o-linear-gradient(bottom, #fff, #000 ), url(foo.png); }",
}, {
code: ".foo { background: url(foo.png), -webkit-linear-gradient(bottom, #fff, #000 ), url(bar.png); }",
}, {
code: ".foo { background: url(foo.png), -moz-linear-gradient(bottom, #fff, #000 ), url(bar.png); }",
}, {
code: ".foo { background: url(foo.png), -o-linear-gradient(bottom, #fff, #000 ), url(bar.png); }",
} ],

reject: [ {
code: ".foo { background: linear-gradient(bottom, #fff, #000; )}",
message: messages.rejected,
line: 1,
column: 36,
}, {
code: ".foo { background: lInEaR-gRaDiEnT(bottom, #fff, #000; )}",
message: messages.rejected,
line: 1,
column: 36,
}, {
code: ".foo { background: LINEAR-GRADIENT(bottom, #fff, #000; )}",
message: messages.rejected,
line: 1,
column: 36,
}, {
code: ".foo { background: linear-gradient(bOtToM, #fff, #000; )}",
message: messages.rejected,
line: 1,
column: 36,
}, {
code: ".foo { background: linear-gradient(BOTTOM, #fff, #000; )}",
message: messages.rejected,
line: 1,
column: 36,
}, {
code: ".foo { background: linear-gradient(top, #fff, #000; )}",
message: messages.rejected,
line: 1,
column: 36,
}, {
code: ".foo { background: linear-gradient(left, #fff, #000; )}",
message: messages.rejected,
line: 1,
column: 36,
}, {
code: ".foo { background: linear-gradient(right, #fff, #000; )}",
message: messages.rejected,
line: 1,
column: 36,
}, {
code: ".foo { background: linear-gradient(to top top, #fff, #000; )}",
message: messages.rejected,
line: 1,
column: 36,
}, {
code: ".foo { background: linear-gradient(45, #fff, #000; )}",
message: messages.rejected,
line: 1,
column: 36,
}, {
code: ".foo { background: linear-gradient(0.25, #fff, #000; )}",
message: messages.rejected,
line: 1,
column: 36,
}, {
code: ".foo { background: linear-gradient(1.577, #fff, #000; )}",
message: messages.rejected,
line: 1,
column: 36,
}, {
code: ".foo { background: -webkit-linear-gradient(to bottom, #fff, #000 ); }",
message: messages.rejected,
line: 1,
column: 44,
}, {
code: ".foo { background: -moz-linear-gradient(to bottom, #fff, #000 ); }",
message: messages.rejected,
line: 1,
column: 41,
}, {
code: ".foo { background: -o-linear-gradient(to bottom, #fff, #000 ); }",
message: messages.rejected,
line: 1,
column: 39,
}, {
code: ".foo { background: url(foo.png), -webkit-linear-gradient(to bottom, #fff, #000); }",
message: messages.rejected,
line: 1,
column: 58,
}, {
code: ".foo { background: url(foo.png), -moz-linear-gradient(to bottom, #fff, #000); }",
message: messages.rejected,
line: 1,
column: 55,
}, {
code: ".foo { background: url(foo.png), -o-linear-gradient(to bottom, #fff, #000); }",
message: messages.rejected,
line: 1,
column: 53,
}, {
code: ".foo { background: -webkit-linear-gradient(to bottom, #fff, #000), url(foo.png); }",
message: messages.rejected,
line: 1,
column: 44,
}, {
code: ".foo { background: url(foo.png), -moz-linear-gradient(to bottom, #fff, #000), url(foo.png); }",
message: messages.rejected,
line: 1,
column: 55,
}, {
code: ".foo { background: -o-linear-gradient(to bottom, #fff, #000 ), url(foo.png); }",
message: messages.rejected,
line: 1,
column: 39,
}, {
code: ".foo { background: url(foo.png), -webkit-linear-gradient(to bottom, #fff, #000), url(bar.png); }",
message: messages.rejected,
line: 1,
column: 58,
}, {
code: ".foo { background: url(foo.png), -moz-linear-gradient(to bottom, #fff, #000), url(bar.png); }",
message: messages.rejected,
line: 1,
column: 55,
}, {
code: ".foo { background: url(foo.png), -o-linear-gradient(to bottom, #fff, #000), url(bar.png); }",
message: messages.rejected,
line: 1,
column: 53,
} ],
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const report = require("../../utils/report")
const ruleMessages = require("../../utils/ruleMessages")
const validateOptions = require("../../utils/validateOptions")
const postcss = require("postcss")
const valueParser = require("postcss-value-parser")
const declarationValueIndex = require("../../utils/declarationValueIndex")

const ruleName = "function-linear-gradient-no-nonstandard-direction"

Expand Down Expand Up @@ -37,40 +39,45 @@ const rule = function (actual) {
}

root.walkDecls(decl => {
functionArgumentsSearch(decl.toString().toLowerCase(), "linear-gradient", (expression, expressionIndex) => {
const firstArg = expression.split(",")[0].trim()
valueParser(decl.value).walk(valueNode => {
if (valueNode.type !== "function") {
return
}
functionArgumentsSearch(valueParser.stringify(valueNode).toLowerCase(), "linear-gradient", (expression, expressionIndex) => {
const firstArg = expression.split(",")[0].trim()

// If the first character is a number, we can assume the user intends an angle
if (/[\d\.]/.test(firstArg[0])) {
if (/^[\d\.]+(?:deg|grad|rad|turn)$/.test(firstArg)) {
// If the first character is a number, we can assume the user intends an angle
if (/[\d\.]/.test(firstArg[0])) {
if (/^[\d\.]+(?:deg|grad|rad|turn)$/.test(firstArg)) {
return
}
complain()
return
}
complain()
return
}

// The first argument may not be a direction: it may be an angle,
// or a color stop (in which case user gets default direction, "to bottom")
// cf. https://drafts.csswg.org/css-images-3/#linear-gradient-syntax
if (!/left|right|top|bottom/.test(firstArg)) {
return
}
// The first argument may not be a direction: it may be an angle,
// or a color stop (in which case user gets default direction, "to bottom")
// cf. https://drafts.csswg.org/css-images-3/#linear-gradient-syntax
if (!/left|right|top|bottom/.test(firstArg)) {
return
}

const withToPrefix = !postcss.vendor.prefix(decl.value)
if (!isStandardDirection(firstArg, withToPrefix)) {
complain()
return
}
const withToPrefix = !postcss.vendor.prefix(valueNode.value)
if (!isStandardDirection(firstArg, withToPrefix)) {
complain()
return
}

function complain() {
report({
message: messages.rejected,
node: decl,
index: expressionIndex,
result,
ruleName,
})
}
function complain() {
report({
message: messages.rejected,
node: decl,
index: declarationValueIndex(decl) + valueNode.sourceIndex + expressionIndex,
result,
ruleName,
})
}
})
})
})
}
Expand Down

0 comments on commit ddf1682

Please sign in to comment.