Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check all linear-gradients in a value list #2496

Merged
merged 1 commit into from Apr 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
} ],
})
@@ -1,10 +1,12 @@
"use strict"

const declarationValueIndex = require("../../utils/declarationValueIndex")
const functionArgumentsSearch = require("../../utils/functionArgumentsSearch")
const postcss = require("postcss")
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 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