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,84 @@
# font-weight-notation
Require numeric or named (where possible) `font-weight` values. Also, when named values are expected, require only valid names.
```css
a { font-weight: bold }
/** ↑
* This notation */
a { font: italic small-caps 600 16px/3 cursive; }
/** ↑
* And this notation, too */
```
Valid font-weight names are `normal`, `bold`, `bolder`, and `lighter`.
This rule ignores `$sass`, `@less`, and `var(--custom-property)` variable syntaxes.
## Options
`string`: `"numeric"|"named-where-possible"`
### `"numeric"`
`font-weight` values *must always* be numbers.
The following patterns are considered warnings:
```css
a { font-weight: bold; }
```
```css
a { font: italic normal 20px; }
```
The following patterns are *not* considered warnings:
```css
a { font-weight: 700; }
```
```css
a { font: italic 900 20px; }
```
### `"named-where-possible"`
`font-weight` values *must always* be keywords when an appropriate keyword is available.
This means that only `400` and `700` will be rejected, because those are the only numbers with keyword equivalents (`normal` and `bold`).
The following patterns are considered warnings:
```css
a { font-weight: 700; }
```
```css
a { font: italic 400 20px; }
```
The following patterns are *not* considered warnings:
```css
a { font-weight: bold; }
```
```css
a { font: italic normal 20px; }
```
## Optional secondary options
### `ignore: ["relative"]`
Ignore the [*relative*](https://drafts.csswg.org/css-fonts/#font-weight-prop) keyword names of `bolder` and `lighter`.
The following patterns are *not* considered warnings:
```css
a { font-weight: 400; }
a b { font-weight: lighter; }
```

View File

@@ -0,0 +1,125 @@
"use strict"
const declarationValueIndex = require("../../utils/declarationValueIndex")
const isNumbery = require("../../utils/isNumbery")
const isStandardSyntaxValue = require("../../utils/isStandardSyntaxValue")
const isVariable = require("../../utils/isVariable")
const optionsMatches = require("../../utils/optionsMatches")
const report = require("../../utils/report")
const ruleMessages = require("../../utils/ruleMessages")
const validateOptions = require("../../utils/validateOptions")
const keywordSets = require("../../reference/keywordSets")
const _ = require("lodash")
const postcss = require("postcss")
const ruleName = "font-weight-notation"
const messages = ruleMessages(ruleName, {
expected: type => `Expected ${type} font-weight notation`,
invalidNamed: name => `Unexpected invalid font-weight name "${name}"`,
})
const INHERIT_KEYWORD = "inherit"
const INITIAL_KEYWORD = "initial"
const NORMAL_KEYWORD = "normal"
const WEIGHTS_WITH_KEYWORD_EQUIVALENTS = [
"400",
"700",
]
const rule = function (expectation, options) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
possible: [
"numeric",
"named-where-possible",
],
}, {
actual: options,
possible: {
ignore: ["relative"],
},
optional: true,
})
if (!validOptions) {
return
}
root.walkDecls(decl => {
if (decl.prop.toLowerCase() === "font-weight") {
checkWeight(decl.value, decl)
}
if (decl.prop.toLowerCase() === "font") {
checkFont(decl)
}
})
function checkFont(decl) {
const valueList = postcss.list.space(decl.value)
// We do not need to more carefully distinguish font-weight
// numbers from unitless line-heights because line-heights in
// `font` values need to be part of a font-size/line-height pair
const hasNumericFontWeight = valueList.some(isNumbery)
for (const value of postcss.list.space(decl.value)) {
if (value.toLowerCase() === NORMAL_KEYWORD && !hasNumericFontWeight || isNumbery(value) || value.toLowerCase() !== NORMAL_KEYWORD && keywordSets.fontWeightKeywords.has(value.toLowerCase())) {
checkWeight(value, decl)
return
}
}
}
function checkWeight(weightValue, decl) {
if (!isStandardSyntaxValue(weightValue)) {
return
}
if (isVariable(weightValue)) {
return
}
if (weightValue.toLowerCase() === INHERIT_KEYWORD || weightValue.toLowerCase() === INITIAL_KEYWORD) {
return
}
if (optionsMatches(options, "ignore", "relative") && keywordSets.fontWeightRelativeKeywords.has(weightValue.toLowerCase())) {
return
}
const weightValueOffset = decl.value.indexOf(weightValue)
if (expectation === "numeric") {
if (!isNumbery(weightValue)) {
return complain(messages.expected("numeric"))
}
}
if (expectation === "named-where-possible") {
if (isNumbery(weightValue)) {
if (_.includes(WEIGHTS_WITH_KEYWORD_EQUIVALENTS, weightValue)) {
complain(messages.expected("named"))
}
return
}
if (!keywordSets.fontWeightKeywords.has(weightValue.toLowerCase()) && weightValue.toLowerCase() !== NORMAL_KEYWORD) {
return complain(messages.invalidNamed(weightValue))
}
return
}
function complain(message) {
report({
ruleName,
result,
message,
node: decl,
index: declarationValueIndex(decl) + weightValueOffset,
})
}
}
}
}
rule.ruleName = ruleName
rule.messages = messages
module.exports = rule