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,556 @@
# declaration-block-properties-order
***Deprecated: instead use the community [`stylelint-order`](https://github.com/hudochenkov/stylelint-order) plugin pack.***
Specify the order of properties within declaration blocks.
```css
a {
color: pink;
top: 0;
}
/** ↑
* These properties */
```
Prefixed properties *must always* be alphabetically ordered and *must always* precede the unprefixed version.
This rule ignores variables (`$sass`, `@less`, `--custom-property`).
## Options
`string|array`: `"alphabetical"|["array", "of", "unprefixed", "property", "names", "or", "group", "objects"]`
### `"alphabetical"`
Properties *must always* be ordered alphabetically.
The following patterns are considered warnings:
```css
a {
top: 0;
color: pink;
}
```
```css
a {
-moz-transform: scale(1);
transform: scale(1);
-webkit-transform: scale(1);
}
```
The following patterns are *not* considered warnings:
```css
a {
color: pink;
top: 0;
}
```
```css
a {
-moz-transform: scale(1);
-webkit-transform: scale(1);
transform: scale(1);
}
```
### `["array", "of", "unprefixed", "property", "names", "or", "group", "objects"]`
Within an order array, you can include
- unprefixed property names
- group objects with these properties:
- `order ("strict"|"flexible")`: If `"strict"` (the default), the properties in this group must come in the order specified. If `"flexible"`, the properties can be in any order as long as they are grouped correctly.
- `properties (array of strings)`: The properties in this group.
There are some important details to keep in mind:
**By default, unlisted properties will be ignored.** So if you specify an array and do not include `display`, that means that the `display` property can be included before or after any other property. *This can be changed with the `unspecified` option* (see below).
**If an (unprefixed) property name is not included in your array and it contains a hyphen (e.g. `padding-left`), this rule will look for the string before that first hyphen in your array (e.g. `padding`) and use that position.** This means that you do not have to specify each extension of the root property; you can just specify the root property and the extensions will be accounted for.
For example, if you have included `border` in your array but not `border-top`, the rule will expect `border-top` to appear in the same relative position as `border`.
Other relevant rules include `margin`, `border`, `animation`, `transition`, etc.
Using this fallback, the order of these hyphenated relative to their peer extensions (e.g. `border-top` to `border-bottom`) will be *arbitrary*. If you would like to enforce a specific ordering (e.g. always put `border-right` before `border-left`), you should specify those particular names in your array.
Given:
```js
["transform", "top", "color"]
```
The following patterns are considered warnings:
```css
a {
color: pink;
top: 0;
}
```
```css
a {
-moz-transform: scale(1);
transform: scale(1);
-webkit-transform: scale(1);
}
```
The following patterns are *not* considered warnings:
```css
a {
top: 0;
color: pink;
}
```
```css
a {
-moz-transform: scale(1);
-webkit-transform: scale(1);
transform: scale(1);
}
```
Given:
```js
["padding", "color"]
```
The following patterns are considered warnings:
```css
a {
color: pink;
padding: 1em;
}
```
```css
a {
color: pink;
padding-top: 1em;
}
```
```css
a {
padding-left: 2em;
color: pink;
padding-top: 1em;
}
```
The following patterns are *not* considered warnings:
```css
a {
padding: 1em;
color: pink;
}
```
```css
a {
padding-top: 1em;
color: pink;
}
```
```css
a {
padding-left: 2em;
padding-top: 1em;
color: pink;
}
```
```css
a {
padding-top: 1em;
padding-left: 2em;
color: pink;
}
```
Given:
```js
["my", "font-smoothing", "color"]
```
Where `font-smoothing` is the unprefixed version of proprietary browser property `-webkit-font-smoothing` and `my` is a user-defined shorthand property.
The following patterns are considered warnings:
```css
a {
color: pink;
-webkit-font-smoothing: antialiased;
}
```
```css
a {
-webkit-font-smoothing: antialiased;
my-property: 2em;
}
```
```css
a {
my-property: 2em;
color: pink;
my-other-property: 1em;
}
```
The following patterns are *not* considered warnings:
```css
a {
-webkit-font-smoothing: antialiased;
color: pink;
}
```
```css
a {
my-property: 2em;
-webkit-font-smoothing: antialiased;
}
```
```css
a {
my-property: 2em;
my-other-property: 1em;
color: pink;
}
```
```css
a {
my-other-property: 1em;
my-property: 2em;
color: pink;
}
```
Given:
```js
["padding", "padding-top", "padding-right", "padding-bottom", "padding-left", "color"]
```
The following patterns are considered warnings:
```css
a {
padding-left: 2em;
padding-top: 1em;
padding: 1em;
color: pink;
}
```
The following patterns are *not* considered warnings:
```css
a {
padding-top: 1em;
padding-right: 1em;
padding-bottom: 0.5em;
padding-left: 0.5em;
color: pink;
}
```
```css
a {
padding: 1em;
padding-right: 2em;
padding-left: 2.5em;
color: pink;
}
```
Given:
```js
[
{
properties: [
"height",
"width",
],
},
{
properties: [
"font-size",
"font-weight",
],
},
]
```
The following patterns are considered warnings:
```css
a {
width: 2px;
height: 1px;
font-size: 2px;
font-weight: bold;
}
```
```css
a {
width: 2px;
height: 1px;
font-size: 2px;
font-weight: bold;
}
```
The following patterns are *not* considered warnings:
```css
a {
height: 1px;
width: 2px;
font-size: 2px;
font-weight: bold;
}
```
```css
a {
height: 1px;
width: 2px;
font-size: 2px;
font-weight: bold;
}
```
Given:
```js
[
"height",
"width",
{
order: "flexible",
properties: [
"color",
"font-size",
"font-weight",
],
},
]
```
The following patterns are considered warnings:
```css
a {
height: 1px;
font-weight: bold;
width: 2px;
}
```
```css
a {
width: 2px;
height: 1px;
font-weight: bold;
}
```
```css
a {
height: 1px;
color: pink;
width: 2px;
font-weight: bold;
}
```
The following patterns are *not* considered warnings:
```css
a {
height: 1px;
width: 2px;
color: pink;
font-size: 2px;
font-weight: bold;
}
```
```css
a {
height: 1px;
width: 2px;
font-size: 2px;
color: pink;
font-weight: bold;
}
```
## Optional secondary options
### `unspecified: "top"|"bottom"|"bottomAlphabetical"|"ignore"`
These options only apply if you've defined your own array of properties.
Default behavior is the same as `"ignore"`: an unspecified property can appear before or after any other property.
With `"top"`, unspecified properties are expected *before* any specified properties. With `"bottom"`, unspecified properties are expected *after* any specified properties. With `"bottomAlphabetical"`, unspecified properties are expected *after* any specified properties, and the unspecified properties are expected to be in alphabetical order.
Given:
```js
[["color", "background"], { unspecified: "ignore" }]
```
The following patterns are *not* considered warnings:
```css
a {
color: pink;
background: orange;
left: 0;
}
```
```css
a {
left: 0;
color: pink;
background: orange;
}
```
```css
a {
color: pink;
left: 0;
background: orange;
}
```
Given:
```js
[["color", "background"], { unspecified: "top" }]
```
The following patterns are considered warnings:
```css
a {
color: pink;
background: orange;
left: 0;
}
```
```css
a {
color: pink;
left: 0;
background: orange;
}
```
The following patterns are *not* considered warnings:
```css
a {
left: 0;
color: pink;
background: orange;
}
```
Given:
```js
[["color", "background"], { unspecified: "bottom" }]
```
The following patterns are considered warnings:
```css
a {
left: 0;
color: pink;
background: orange;
}
```
```css
a {
color: pink;
left: 0;
background: orange;
}
```
The following patterns are *not* considered warnings:
```css
a {
color: pink;
background: orange;
left: 0;
}
```
Given:
```js
[["composes"], { unspecified: "bottomAlphabetical" }]
```
The following patterns are considered warnings:
```css
a {
align-items: flex-end;
composes: b;
left: 0;
}
```
```css
a {
composes: b;
left: 0;
align-items: flex-end;
}
```
The following patterns are *not* considered warnings:
```css
a {
composes: b;
align-items: flex-end;
left: 0;
}
```

View File

@@ -0,0 +1,299 @@
"use strict"
const isCustomProperty = require("../../utils/isCustomProperty")
const isStandardSyntaxProperty = require("../../utils/isStandardSyntaxProperty")
const report = require("../../utils/report")
const ruleMessages = require("../../utils/ruleMessages")
const validateOptions = require("../../utils/validateOptions")
const _ = require("lodash")
const postcss = require("postcss")
const ruleName = "declaration-block-properties-order"
const messages = ruleMessages(ruleName, {
expected: (first, second) => `Expected "${first}" to come before "${second}"`,
})
const rule = function (expectation, options) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
possible: validatePrimaryOption,
}, {
actual: options,
possible: {
unspecified: [
"top",
"bottom",
"ignore",
"bottomAlphabetical",
],
},
optional: true,
})
if (!validOptions) {
return
}
result.warn((
"'declaration-block-properties-order'has been deprecated and in 8.0 will be removed. " +
"Instead use the community 'stylelint-order' plugin pack."
), {
stylelintType: "deprecation",
stylelintReference: "https://stylelint.io/user-guide/rules/declaration-block-properties-order/",
})
const alphabetical = expectation === "alphabetical"
const expectedOrder = alphabetical ? null : createExpectedOrder(expectation)
// By default, ignore unspecified properties
const unspecified = _.get(options, ["unspecified"], "ignore")
// Shallow loop
root.each(node => {
if (node.type === "rule" || node.type === "atrule") {
checkNode(node)
}
})
function checkNode(node) {
const allPropData = []
node.each(child => {
// If the child has nested nodes with child
// (e.g. a rule nested within a rule), make
// sure to check the children
if (child.nodes && child.nodes.length) {
checkNode(child)
}
if (child.type !== "decl") {
return
}
const prop = child.prop
if (!isStandardSyntaxProperty(prop)) {
return
}
if (isCustomProperty(prop)) {
return
}
let unprefixedPropName = postcss.vendor.unprefixed(prop)
// Hack to allow -moz-osx-font-smoothing to be understood
// just like -webkit-font-smoothing
if (unprefixedPropName.indexOf("osx-") === 0) {
unprefixedPropName = unprefixedPropName.slice(4)
}
const propData = {
name: prop,
unprefixedName: unprefixedPropName,
orderData: alphabetical ? null : getOrderData(expectedOrder, unprefixedPropName),
before: child.raws.before,
index: allPropData.length,
node: child,
}
const previousPropData = _.last(allPropData)
allPropData.push(propData)
// Skip first decl
if (!previousPropData) {
return
}
const isCorrectOrder = alphabetical ? checkAlpabeticalOrder(previousPropData, propData) : checkOrder(previousPropData, propData)
if (isCorrectOrder) {
return
}
complain({
message: messages.expected(propData.name, previousPropData.name),
node: child,
})
})
function checkOrder(firstPropData, secondPropData) {
// If the unprefixed property names are the same, resort to alphabetical ordering
if (firstPropData.unprefixedName === secondPropData.unprefixedName) {
return firstPropData.name <= secondPropData.name
}
const firstPropIsUnspecified = !firstPropData.orderData
const secondPropIsUnspecified = !secondPropData.orderData
// Now check actual known properties ...
if (!firstPropIsUnspecified && !secondPropIsUnspecified) {
return firstPropData.orderData.expectedPosition <= secondPropData.orderData.expectedPosition
}
if (firstPropIsUnspecified && !secondPropIsUnspecified) {
// If first prop is unspecified, look for a specified prop before it to
// compare to the current prop
const priorSpecifiedPropData = _.findLast(allPropData.slice(0, -1), d => !!d.orderData)
if (priorSpecifiedPropData && priorSpecifiedPropData.orderData && priorSpecifiedPropData.orderData.expectedPosition > secondPropData.orderData.expectedPosition) {
complain({
message: messages.expected(secondPropData.name, priorSpecifiedPropData.name),
node: secondPropData.node,
})
return true // avoid logging another warning
}
}
// Now deal with unspecified props ...
// Starting with bottomAlphabetical as it requires more specific conditionals
if (unspecified === "bottomAlphabetical" && !firstPropIsUnspecified && secondPropIsUnspecified) {
return true
}
if (unspecified === "bottomAlphabetical" && secondPropIsUnspecified && firstPropIsUnspecified) {
if (checkAlpabeticalOrder(firstPropData, secondPropData)) {
return true
} else {
return false
}
}
if (unspecified === "bottomAlphabetical" && firstPropIsUnspecified) {
return false
}
if (firstPropIsUnspecified && secondPropIsUnspecified) {
return true
}
if (unspecified === "ignore" && (firstPropIsUnspecified || secondPropIsUnspecified)) {
return true
}
if (unspecified === "top" && firstPropIsUnspecified) {
return true
}
if (unspecified === "top" && secondPropIsUnspecified) {
return false
}
if (unspecified === "bottom" && secondPropIsUnspecified) {
return true
}
if (unspecified === "bottom" && firstPropIsUnspecified) {
return false
}
}
}
function complain(opts) {
report({
message: opts.message,
node: opts.node,
result,
ruleName,
})
}
}
}
rule.primaryOptionArray = true
rule.ruleName = ruleName
rule.messages = messages
module.exports = rule
function createExpectedOrder(input) {
const order = {}
let expectedPosition = 0
appendGroup(input)
function appendGroup(items) {
items.forEach(item => appendItem(item, false))
}
function appendItem(item, inFlexibleGroup) {
if (_.isString(item)) {
// In flexible groups, the expectedPosition does not ascend
// to make that flexibility work;
// otherwise, it will always ascend
if (!inFlexibleGroup) {
expectedPosition += 1
}
order[item] = { expectedPosition }
return
}
if (!item.order || item.order === "strict") {
appendGroup(item.properties)
return
} else if (item.order === "flexible") {
expectedPosition += 1
item.properties.forEach(property => {
appendItem(property, true)
})
}
}
return order
}
function getOrderData(expectedOrder, propName) {
let orderData = expectedOrder[propName]
// If prop was not specified but has a hyphen
// (e.g. `padding-top`), try looking for the segment preceding the hyphen
// and use that index
if (!orderData && propName.lastIndexOf("-") !== -1) {
const propNamePreHyphen = propName.slice(0, propName.lastIndexOf("-"))
orderData = getOrderData(expectedOrder, propNamePreHyphen)
}
return orderData
}
function checkAlpabeticalOrder(firstPropData, secondPropData) {
// If unprefixed prop names are the same, compare the prefixed versions
if (firstPropData.unprefixedName === secondPropData.unprefixedName) {
return firstPropData.name <= secondPropData.name
}
return firstPropData.unprefixedName < secondPropData.unprefixedName
}
function validatePrimaryOption(actualOptions) {
// Return true early if alphabetical
if (actualOptions === "alphabetical") {
return true
}
// Otherwise, begin checking array options
if (!Array.isArray(actualOptions)) {
return false
}
// Every item in the array must be a string or an object
// with a "properties" property
if (!actualOptions.every(item => {
if (_.isString(item)) {
return true
}
return _.isPlainObject(item) && !_.isUndefined(item.properties)
})) {
return false
}
const objectItems = actualOptions.filter(_.isPlainObject)
// Every object-item's "order" property must be "strict" or "flexible"
if (!objectItems.every(item => {
if (_.isUndefined(item.order)) {
return true
}
return _.includes([
"strict",
"flexible",
], item.order)
})) {
return false
}
return true
}