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,96 @@
# unit-blacklist
Specify a blacklist of disallowed units.
```css
a { width: 100px; }
/** ↑
* These units */
```
## Options
`array|string`: `["array", "of", "units"]|"unit"`
Given:
```js
["px", "em", "deg"]
```
The following patterns are considered warnings:
```css
a { width: 100px; }
```
```css
a { font-size: 10em; }
```
```css
a { transform: rotate(30deg); }
```
The following patterns are *not* considered warnings:
```css
a { font-size: 1.2rem; }
```
```css
a { line-height: 1.2; }
```
```css
a { height: 100vmin; }
```
```css
a { animation: animation-name 5s ease; }
```
## Optional secondary options
### `ignoreProperties: { unit: ["property", "/regex/"] }`
Ignore units in the values of declarations with the specified properties.
For example, with `["px", "vmin"]`.
Given:
```js
{
"px": [ "font-size", "/^border/" ],
"vmin": [ "width" ]
}
```
The following patterns are *not* considered warnings:
```css
a { font-size: 13px; }
```
```css
a { border-bottom-width: 6px; }
```
```css
a { width: 100vmin; }
```
The following patterns are considered warnings:
```css
a { line-height: 12px; }
```
```css
a { -moz-border-radius-topright: 40px; }
```
```css
a { height: 100vmin; }
```

View File

@@ -0,0 +1,73 @@
"use strict"
const atRuleParamIndex = require("../../utils/atRuleParamIndex")
const declarationValueIndex = require("../../utils/declarationValueIndex")
const getUnitFromValueNode = require("../../utils/getUnitFromValueNode")
const optionsMatches = require("../../utils/optionsMatches")
const report = require("../../utils/report")
const ruleMessages = require("../../utils/ruleMessages")
const validateOptions = require("../../utils/validateOptions")
const _ = require("lodash")
const validateObjectWithStringArrayProps = require("../../utils/validateObjectWithStringArrayProps")
const valueParser = require("postcss-value-parser")
const ruleName = "unit-blacklist"
const messages = ruleMessages(ruleName, {
rejected: unit => `Unexpected unit "${unit}"`,
})
const rule = function (blacklistInput, options) {
const blacklist = [].concat(blacklistInput)
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: blacklist,
possible: [_.isString],
}, {
optional: true,
actual: options,
possible: {
ignoreProperties: validateObjectWithStringArrayProps,
},
})
if (!validOptions) {
return
}
function check(node, value, getIndex) {
valueParser(value).walk(function (valueNode) {
// Ignore wrong units within `url` function
if (valueNode.type === "function" && valueNode.value.toLowerCase() === "url") {
return false
}
const unit = getUnitFromValueNode(valueNode)
if (!unit || unit && blacklist.indexOf(unit.toLowerCase()) === -1) {
return
}
if (options && optionsMatches(options.ignoreProperties, unit.toLowerCase(), node.prop)) {
return
}
report({
index: getIndex(node) + valueNode.sourceIndex,
message: messages.rejected(unit),
node,
result,
ruleName,
})
})
}
root.walkAtRules(/^media$/i, atRule => check(atRule, atRule.params, atRuleParamIndex))
root.walkDecls(decl => check(decl, decl.value, declarationValueIndex))
}
}
rule.primaryOptionArray = true
rule.ruleName = ruleName
rule.messages = messages
module.exports = rule