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,353 @@
# at-rule-empty-line-before
Require or disallow an empty line before at-rules.
```css
a {}
/* ← */
@media {} /* ↑ */
/** ↑
* This line */
```
If the at-rule is the very first node in a stylesheet then it is ignored.
## Options
`string`: `"always"|"never"`
### `"always"`
There *must always* be an empty line before at-rules.
The following patterns are considered warnings:
```css
a {} @media {}
```
```css
a {}
@media {}
```
The following patterns are *not* considered warnings:
```css
a {}
@media {}
```
### `"never"`
There *must never* be an empty line before at-rules.
The following patterns are considered warnings:
```css
a {}
@media {}
```
The following patterns are *not* considered warnings:
```css
a {} @media {}
```
```css
a {}
@media {}
```
## Optional secondary options
### `except: ["after-same-name", "inside-block", "blockless-after-same-name-blockless", "blockless-after-blockless", "first-nested"]`
#### `"after-same-name"`
Reverse the primary option for at-rules that follow another at-rule with the same name.
This means that you can group your at-rules by name.
For example, with `"always"`:
The following patterns are *not* considered warnings:
```css
@charset "UTF-8";
@import url(x.css);
@import url(y.css);
@media (min-width: 100px) {}
@media (min-width: 200px) {}
```
```css
a {
@extends .foo;
@extends .bar;
@include x;
@include y {}
}
```
#### `"inside-block"`
***Note: This option was previously called `all-nested`.***
Reverse the primary option for at-rules that are nested.
For example, with `"always"`:
The following patterns are considered warnings:
```css
a {
@extend foo;
color: pink;
}
b {
color: pink;
@extend foo;
}
```
The following patterns are *not* considered warnings:
```css
a {
@extend foo;
color: pink;
}
b {
color: pink;
@extend foo;
}
```
#### `"blockless-after-same-name-blockless"`
Reverse the primary option for blockless at-rules that follow another blockless at-rule with the same name.
This means that you can group your blockless at-rules by name.
For example, with `"always"`:
The following patterns are *not* considered warnings:
```css
@charset "UTF-8";
@import url(x.css);
@import url(y.css);
```
```css
a {
@extends .foo;
@extends .bar;
@include loop;
@include doo;
}
```
#### `"blockless-after-blockless"`
***Note: This option was previously called `blockless-group`.***
Reverse the primary option for at-rules within a blockless group.
For example, with `"always"`:
The following patterns are considered warnings:
```css
@import url(x.css);
@import url(y.css);
@media print {}
```
The following patterns are *not* considered warnings:
```css
@import url(x.css);
@import url(y.css);
@media print {}
```
#### `"first-nested"`
Reverse the primary option for at-rules that are nested and the first child of their parent node.
For example, with `"always"`:
The following patterns are considered warnings:
```css
a {
@extend foo;
color: pink;
}
b {
color: pink;
@extend foo;
}
```
The following patterns are *not* considered warnings:
```css
a {
@extend foo;
color: pink;
}
b {
color: pink;
@extend foo;
}
```
### `ignore: ["after-comment", "inside-block", "blockless-after-same-name-blockless", "blockless-after-blockless"]`
#### `"after-comment"`
Ignore at-rules that come after a comment.
The following patterns are *not* considered warnings:
```css
/* comment */
@media {}
```
```css
/* comment */
@media {}
```
#### `"inside-block"`
***Note: This option was previously called `all-nested`.***
Ignore at-rules that are inside a declaration block.
For example, with `"always"`:
The following patterns are *not* considered warnings:
```css
a {
@extend foo;
color: pink;
}
a {
@extend foo;
color: pink;
}
b {
color: pink;
@extend foo;
}
b {
color: pink;
@extend foo;
}
```
#### `"blockless-after-same-name-blockless"`
Ignore blockless at-rules that follow another blockless at-rule with the same name.
This means that you can group your blockless at-rules by name.
For example, with `"always"`:
The following patterns are *not* considered warnings:
```css
@charset "UTF-8";
@import url(x.css);
@import url(y.css);
```
```css
a {
@extends .foo;
@extends .bar;
@include loop;
@include doo;
}
```
#### `"blockless-after-blockless"`
***Note: This option was previously called `blockless-group`.***
Ignore blockless at-rules that follow another blockless at-rule.
For example, with `"always"`:
The following patterns are *not* considered warnings:
```css
@import url(x.css);
@import url(y.css);
@media print {}
```
```css
@import url(x.css);
@import url(y.css);
@media print {}
```
### `ignoreAtRules: ["array", "of", "at-rules"]`
Ignore specified at-rules.
For example, with `"always"`.
Given:
```js
["import"]
```
The following patterns are *not* considered warnings:
```css
@charset "UTF-8";
@import {}
```

View File

@@ -0,0 +1,203 @@
"use strict"
const _ = require("lodash")
const hasBlock = require("../../utils/hasBlock")
const hasEmptyLine = require("../../utils/hasEmptyLine")
const optionsMatches = require("../../utils/optionsMatches")
const report = require("../../utils/report")
const ruleMessages = require("../../utils/ruleMessages")
const validateOptions = require("../../utils/validateOptions")
const ruleName = "at-rule-empty-line-before"
const messages = ruleMessages(ruleName, {
expected: "Expected empty line before at-rule",
rejected: "Unexpected empty line before at-rule",
})
const rule = function (expectation, options) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
possible: [
"always",
"never",
],
}, {
actual: options,
possible: {
except: [
"after-same-name",
"all-nested",
"inside-block",
"blockless-after-same-name-blockless",
"blockless-group",
"blockless-after-blockless",
"first-nested",
],
ignore: [
"after-comment",
"all-nested",
"inside-block",
"blockless-after-same-name-blockless",
"blockless-group",
"blockless-after-blockless",
],
ignoreAtRules: [_.isString],
},
optional: true,
})
if (!validOptions) {
return
}
if (
optionsMatches(options, "ignore", "all-nested")
|| optionsMatches(options, "except", "all-nested")
) {
result.warn((
"'at-rule-empty-line-before\'s' \"all-nested\" option has been deprecated and in 8.0 will be removed. " +
"Instead use the \"inside-block\" option."
), {
stylelintType: "deprecation",
stylelintReference: "https://stylelint.io/user-guide/rules/at-rule-empty-line-before/",
})
}
if (
optionsMatches(options, "ignore", "blockless-group")
|| optionsMatches(options, "except", "blockless-group")
) {
result.warn((
"'at-rule-empty-line-before\'s' \"blockless-group\" option has been deprecated and in 8.0 will be removed. " +
"Instead use the \"blockless-after-blockless\" option."
), {
stylelintType: "deprecation",
stylelintReference: "https://stylelint.io/user-guide/rules/at-rule-empty-line-before/",
})
}
root.walkAtRules(atRule => {
// Ignore the first node
if (atRule === root.first) {
return
}
// Return early if at-rule is to be ignored
if (optionsMatches(options, "ignoreAtRules", atRule.name)) {
return
}
// Optionally ignore the expectation if the node is blockless
if (
optionsMatches(options, "ignore", "blockless-group")
&& !hasBlock(atRule)
|| optionsMatches(options, "ignore", "blockless-after-blockless")
&& !hasBlock(atRule)
) {
return
}
const isNested = atRule.parent !== root
const previousNode = atRule.prev()
// Optionally ignore the expection if the node is blockless
// and following another blockless at-rule with the same name
if (
optionsMatches(options, "ignore", "blockless-after-same-name-blockless")
&& isBlocklessAfterSameNameBlockless()
) {
return
}
// Optionally ignore the expectation if the node is inside a block
if (
optionsMatches(options, "ignore", "all-nested")
&& isNested
|| optionsMatches(options, "ignore", "inside-block")
&& isNested
) {
return
}
// Optionally ignore the expectation if a comment precedes this node
if (
optionsMatches(options, "ignore", "after-comment")
&& isAfterComment()
) {
return
}
const hasEmptyLineBefore = hasEmptyLine(atRule.raws.before)
let expectEmptyLineBefore = expectation === "always"
? true
: false
// Optionally reverse the expectation if any exceptions apply
if (
optionsMatches(options, "except", "after-same-name")
&& isAfterSameName()
|| optionsMatches(options, "except", "all-nested")
&& isNested
|| optionsMatches(options, "except", "inside-block")
&& isNested
|| optionsMatches(options, "except", "first-nested")
&& isFirstNested()
|| optionsMatches(options, "except", "blockless-group")
&& isBlocklessAfterBlockless()
|| optionsMatches(options, "except", "blockless-after-blockless")
&& isBlocklessAfterBlockless()
|| optionsMatches(options, "except", "blockless-after-same-name-blockless")
&& isBlocklessAfterSameNameBlockless()
) {
expectEmptyLineBefore = !expectEmptyLineBefore
}
// Return if the expectation is met
if (expectEmptyLineBefore === hasEmptyLineBefore) {
return
}
const message = expectEmptyLineBefore
? messages.expected
: messages.rejected
report({ message, node: atRule, result, ruleName })
function isAfterComment() {
return previousNode
&& previousNode.type === "comment"
}
function isBlocklessAfterBlockless() {
return previousNode
&& previousNode.type === "atrule"
&& !hasBlock(previousNode)
&& !hasBlock(atRule)
}
function isBlocklessAfterSameNameBlockless() {
return !hasBlock(atRule)
&& previousNode
&& !hasBlock(previousNode)
&& previousNode.type === "atrule"
&& previousNode.name == atRule.name
}
function isAfterSameName() {
return previousNode
&& previousNode.type === "atrule"
&& previousNode.name === atRule.name
}
function isFirstNested() {
return isNested
&& atRule === atRule.parent.first
}
})
}
}
rule.ruleName = ruleName
rule.messages = messages
module.exports = rule