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,252 @@
# rule-empty-line-before
Require or disallow an empty line before rules.
```css
a {}
/* ← */
b {} /* ↑ */
/** ↑
* This line */
```
If the rule is the very first node in a stylesheet then it is ignored.
## Options
`string`: `"always"|"never"|"always-multi-line"|"never-multi-line"`
### `"always"`
There *must always* be an empty line before rules.
The following patterns are considered warnings:
```css
a {} b {}
```
```css
a {}
b {}
```
The following patterns are *not* considered warnings:
```css
a {}
b {}
```
### `"never"`
There *must never* be an empty line before rules.
The following patterns are considered warnings:
```css
a {}
b {}
```
The following patterns are *not* considered warnings:
```css
a {} b {}
```
```css
a {}
b {}
```
### `"always-multi-line"`
There *must always* be an empty line before multi-line rules.
The following patterns are considered warnings:
```css
a {
color: red;
}
b {
color: blue;
}
```
The following patterns are *not* considered warnings:
```css
a {
color: red;
}
b {
color: blue;
}
```
### `"never-multi-line"`
There *must never* be an empty line before multi-line rules.
The following patterns are considered warnings:
```css
a {
color: red;
}
b {
color: blue;
}
```
The following patterns are *not* considered warnings:
```css
a {
color: red;
}
b {
color: blue;
}
```
## Optional secondary options
### `except: ["after-rule", "after-single-line-comment", "inside-block-and-after-rule", "first-nested"]`
#### `"after-rule"`
Reverse the primary option if the rule comes after another rule.
For example, with `"always"`:
The following patterns are considered warnings:
```css
a {}
b {}
```
The following patterns are *not* considered warnings:
```css
a {}
b {}
```
#### `"after-single-line-comment"`
Reverse the primary option if the rule comes after a single-line comment.
For example, with `"always"`:
The following patterns are considered warnings:
```css
/* comment */
a {}
```
The following patterns are *not* considered warnings:
```css
/* comment */
a {}
```
#### `"inside-block-and-after-rule"`
Reverse the primary option if the rule is inside a block and comes after another rule.
For example, with `"always"`:
The following patterns are considered warnings:
```css
@media {
a {}
b {}
}
```
The following patterns are *not* considered warnings:
```css
@media {
a {}
b {}
}
```
#### `"first-nested"`
Reverse the primary option if the rule is the first in a block.
For example, with `"always"`:
The following patterns are considered warnings:
```css
@media {
a {}
b {}
}
```
The following patterns are *not* considered warnings:
```css
@media {
a {}
b {}
}
```
### `ignore: ["after-comment", "inside-block"]`
#### `"after-comment"`
Ignore rules that come after a comment.
For example, with `"always"`:
The following patterns are *not* considered warnings:
```css
/* comment */
a {}
```
#### `"inside-block"`
Ignore rules that are inside a block.
For example, with `"always"`:
The following patterns are *not* considered warnings:
```css
@media {
a {}
}
```
```css
@media {
a {}
b {}
}
```

View File

@@ -0,0 +1,144 @@
"use strict"
const hasEmptyLine = require("../../utils/hasEmptyLine")
const isSingleLineString = require("../../utils/isSingleLineString")
const isStandardSyntaxRule = require("../../utils/isStandardSyntaxRule")
const optionsMatches = require("../../utils/optionsMatches")
const report = require("../../utils/report")
const ruleMessages = require("../../utils/ruleMessages")
const validateOptions = require("../../utils/validateOptions")
const ruleName = "rule-empty-line-before"
const messages = ruleMessages(ruleName, {
expected: "Expected empty line before rule",
rejected: "Unexpected empty line before rule",
})
const rule = function (expectation, options) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
possible: [
"always",
"never",
"always-multi-line",
"never-multi-line",
],
}, {
actual: options,
possible: {
ignore: [
"after-comment",
"inside-block",
],
except: [
"after-rule",
"after-single-line-comment",
"first-nested",
"inside-block-and-after-rule",
],
},
optional: true,
})
if (!validOptions) {
return
}
root.walkRules(rule => {
if (!isStandardSyntaxRule(rule)) {
return
}
// Ignore the first node
if (rule === root.first) {
return
}
let expectEmptyLineBefore = expectation.indexOf("always") !== -1 ? true : false
// Optionally ignore the expectation if a comment precedes this node
if (
optionsMatches(options, "ignore", "after-comment")
&& rule.prev()
&& rule.prev().type === "comment"
) {
return
}
// Optionally ignore the expectation if inside a block
if (
optionsMatches(options, "ignore", "inside-block")
&& rule.parent !== root
) {
return
}
// Ignore if the expectation is for multiple and the rule is single-line
if (
expectation.indexOf("multi-line") !== -1
&& isSingleLineString(rule.toString())
) {
return
}
// Optionally reverse the expectation for the first nested node
if (
optionsMatches(options, "except", "first-nested")
&& rule === rule.parent.first
) {
expectEmptyLineBefore = !expectEmptyLineBefore
}
// Optionally reverse the expectation if a rule precedes this node
if (
optionsMatches(options, "except", "after-rule")
&& rule.prev()
&& rule.prev().type === "rule"
) {
expectEmptyLineBefore = !expectEmptyLineBefore
}
// Optionally reverse the expectation if a rule precedes this node and is inside a block
if (
optionsMatches(options, "except", "inside-block-and-after-rule")
&& rule.prev()
&& rule.prev().type === "rule"
&& rule.parent !== root
) {
expectEmptyLineBefore = !expectEmptyLineBefore
}
// Optionally reverse the expectation for single line comments
if (
optionsMatches(options, "except", "after-single-line-comment")
&& rule.prev()
&& rule.prev().type === "comment"
&& isSingleLineString(rule.prev().toString())
) {
expectEmptyLineBefore = !expectEmptyLineBefore
}
const hasEmptyLineBefore = hasEmptyLine(rule.raws.before)
// Return if the expectation is met
if (expectEmptyLineBefore === hasEmptyLineBefore) {
return
}
const message = expectEmptyLineBefore ? messages.expected : messages.rejected
report({
message,
node: rule,
result,
ruleName,
})
})
}
}
rule.ruleName = ruleName
rule.messages = messages
module.exports = rule