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,225 @@
# function-name-case
Specify lowercase or uppercase for function names.
```css
a { width: calc(5% - 10em); }
/** ↑
* These functions */
```
Camel case function names, e.g. `translateX`, are accounted for when the `lower` option is used.
## Options
`string`: `"lower"|"upper"`
### `"lower"`
The following patterns are considered warnings:
```css
a {
width: Calc(5% - 10em);
}
```
```css
a {
width: cAlC(5% - 10em);
}
```
```css
a {
width: CALC(5% - 10em);
}
```
```css
a {
background: -WEBKIT-RADIAL-GRADIENT(red, green, blue);
}
```
The following patterns are *not* considered warnings:
```css
a {
width: calc(5% - 10em);
}
```
```css
a {
background: -webkit-radial-gradient(red, green, blue);
}
```
### `"upper"`
The following patterns are considered warnings:
```css
a {
width: Calc(5% - 10em);
}
```
```css
a {
width: cAlC(5% - 10em);
}
```
```css
a {
width: calc(5% - 10em);
}
```
```css
a {
background: -webkit-radial-gradient(red, green, blue);
}
```
The following patterns are *not* considered warnings:
```css
a {
width: CALC(5% - 10em);
}
```
```css
a {
background: -WEBKIT-RADIAL-GRADIENT(red, green, blue);
}
```
## Optional secondary options
### `ignoreFunctions: ["/regex/", "non-regex"]`
Ignore case of function names.
For example, with `"lower"`.
Given:
```js
["SOME-FUNCTION", "/^get.*$/"]
```
The following patterns are considered warnings:
```css
a {
color: sOmE-FuNcTiOn();
}
```
```css
a {
color: OTHER-SOME-FUNCTION();
}
```
```css
a {
color: GetColor();
}
```
```css
a {
color: GET_COLOR();
}
```
The following patterns are *not* considered warnings:
```css
a {
display: some-function();
}
```
```css
a {
display: SOME-FUNCTION();
}
```
```css
a {
display: getColor();
}
```
```css
a {
display: get_color();
}
```
For example, with `"upper"`.
Given:
```js
["some-function", "/^get.*$/"]
```
The following patterns are considered warnings:
```css
a {
color: sOmE-FuNcTiOn();
}
```
```css
a {
color: other-some-function();
}
```
```css
a {
color: GetColor();
}
```
```css
a {
color: GET_COLOR();
}
```
The following patterns are *not* considered warnings:
```css
a {
display: some-function();
}
```
```css
a {
display: SOME-FUNCTION();
}
```
```css
a {
display: getColor();
}
```
```css
a {
display: get_color();
}
```

View File

@@ -0,0 +1,88 @@
"use strict"
const declarationValueIndex = require("../../utils/declarationValueIndex")
const isStandardSyntaxFunction = require("../../utils/isStandardSyntaxFunction")
const matchesStringOrRegExp = require("../../utils/matchesStringOrRegExp")
const report = require("../../utils/report")
const ruleMessages = require("../../utils/ruleMessages")
const validateOptions = require("../../utils/validateOptions")
const keywordSets = require("../../reference/keywordSets")
const _ = require("lodash")
const valueParser = require("postcss-value-parser")
const ruleName = "function-name-case"
const messages = ruleMessages(ruleName, {
expected: (actual, expected) => `Expected "${actual}" to be "${expected}"`,
})
const mapLowercaseFunctionNamesToCamelCase = new Map()
keywordSets.camelCaseFunctionNames.forEach(func => {
mapLowercaseFunctionNamesToCamelCase.set(func.toLowerCase(), func)
})
const rule = function (expectation, options) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
possible: [
"lower",
"upper",
],
}, {
actual: options,
possible: {
ignoreFunctions: [_.isString],
},
optional: true,
})
if (!validOptions) {
return
}
root.walkDecls(decl => {
const value = decl.value
valueParser(value).walk(function (node) {
if (node.type !== "function" || !isStandardSyntaxFunction(node)) {
return
}
const functionName = node.value
const functionNameLowerCase = functionName.toLowerCase()
const ignoreFunctions = options && options.ignoreFunctions || []
if (ignoreFunctions.length > 0 && matchesStringOrRegExp(functionName, ignoreFunctions)) {
return
}
let expectedFunctionName = null
if (expectation === "lower" && mapLowercaseFunctionNamesToCamelCase.has(functionNameLowerCase)) {
expectedFunctionName = mapLowercaseFunctionNamesToCamelCase.get(functionNameLowerCase)
} else if (expectation === "lower") {
expectedFunctionName = functionNameLowerCase
} else {
expectedFunctionName = functionName.toUpperCase()
}
if (functionName === expectedFunctionName) {
return
}
report({
message: messages.expected(functionName, expectedFunctionName),
node: decl,
index: declarationValueIndex(decl) + node.sourceIndex,
result,
ruleName,
})
})
})
}
}
rule.ruleName = ruleName
rule.messages = messages
module.exports = rule