Initial commit

This commit is contained in:
Patrick Marsceill
2017-03-09 13:16:08 -05:00
commit b7b0d0d7bf
4147 changed files with 401224 additions and 0 deletions

View File

@@ -0,0 +1,103 @@
# function-url-quotes
Require or disallow quotes for urls.
```css
a { background: url("x.jpg") }
/** ↑ ↑
* These quotes */
```
## Options
`string`: `"always"|"never"`
### `"always"`
Urls *must always* be quoted.
The following patterns are considered warnings:
```css
@import url(foo.css);
```
```css
@document domain(http://www.w3.org/);
```
```css
@font-face { font-family: 'foo'; src: url(foo.ttf); }
```
```css
@-moz-document url-prefix() {}
```
The following patterns are *not* considered warnings:
```css
a { background: url('x.jpg'); }
```
```css
@import url("foo.css");
```
```css
@document domain('http://www.w3.org/');
```
```css
@font-face { font-family: "foo"; src: url("foo.ttf"); }
```
```css
@-moz-document url-prefix('') {}
```
### `"never"`
Urls *must never* be quoted.
The following patterns are considered warnings:
```css
a { background: url('x.jpg'); }
```
```css
@import url("foo.css");
```
```css
@font-face { font-family: "foo"; src: url('foo.ttf'); }
```
The following patterns are *not* considered warnings:
```css
a { background: url(x.jpg); }
```
```css
@import url(foo.css);
```
```css
@font-face { font-family: 'foo'; src: url(foo.ttf); }
```
## Optional secondary options
### `except: ["empty"]`
Reverse the primary option if the function has no arguments.
For example, with `"always"`.
The following pattern is *not* considered warnings:
```css
@-moz-document url-prefix() {}
```

View File

@@ -0,0 +1,113 @@
"use strict"
const atRuleParamIndex = require("../../utils/atRuleParamIndex")
const functionArgumentsSearch = require("../../utils/functionArgumentsSearch")
const isStandardSyntaxUrl = require("../../utils/isStandardSyntaxUrl")
const optionsMatches = require("../../utils/optionsMatches")
const report = require("../../utils/report")
const ruleMessages = require("../../utils/ruleMessages")
const validateOptions = require("../../utils/validateOptions")
const _ = require("lodash")
const ruleName = "function-url-quotes"
const messages = ruleMessages(ruleName, {
expected: () => "Expected quotes",
rejected: () => "Unexpected quotes",
})
const rule = function (expectation, options) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
possible: [
"always",
"never",
],
}, {
actual: options,
possible: {
except: ["empty"],
},
optional: true,
})
if (!validOptions) {
return
}
root.walkAtRules(checkStatement)
root.walkRules(checkStatement)
function checkStatement(statement) {
if (statement.type === "atrule") {
checkAtRuleParams(statement)
}
statement.walkDecls(function (decl) {
functionArgumentsSearch(decl.toString().toLowerCase(), "url", (args, index) => {
checkArgs(args, decl, index, "url")
})
})
}
function checkAtRuleParams(atRule) {
const atRuleParamsLowerCase = atRule.params.toLowerCase()
functionArgumentsSearch(atRuleParamsLowerCase, "url", (args, index) => {
checkArgs(args, atRule, index + atRuleParamIndex(atRule), "url")
})
functionArgumentsSearch(atRuleParamsLowerCase, "url-prefix", (args, index) => {
checkArgs(args, atRule, index + atRuleParamIndex(atRule), "url-prefix")
})
functionArgumentsSearch(atRuleParamsLowerCase, "domain", (args, index) => {
checkArgs(args, atRule, index + atRuleParamIndex(atRule), "domain")
})
}
function checkArgs(args, node, index, functionName) {
let shouldHasQuotes = expectation === "always"
const leftTrimmedArgs = args.trimLeft()
if (!isStandardSyntaxUrl(leftTrimmedArgs)) {
return
}
const complaintIndex = index + args.length - leftTrimmedArgs.length
const hasQuotes = leftTrimmedArgs[0] === "'" || leftTrimmedArgs[0] === "\""
const trimmedArg = args.trim()
const isEmptyArgument = _.includes([
"",
"''",
"\"\"",
], trimmedArg)
if (optionsMatches(options, "except", "empty") && isEmptyArgument) {
shouldHasQuotes = !shouldHasQuotes
}
if (shouldHasQuotes) {
if (hasQuotes) {
return
}
complain(messages.expected(functionName), node, complaintIndex)
} else {
if (!hasQuotes) {
return
}
complain(messages.rejected(functionName), node, complaintIndex)
}
}
function complain(message, node, index) {
report({
message,
node,
index,
result,
ruleName,
})
}
}
}
rule.ruleName = ruleName
rule.messages = messages
module.exports = rule