1
0
mirror of https://github.com/thangisme/notes.git synced 2025-11-23 13:12:25 -05:00

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,153 @@
# comment-empty-line-before
Require or disallow an empty line before comments.
```css
a {}
/* ← */
/* comment */ /* ↑ */
/** ↑
* This line */
```
If the comment is the very first node in a stylesheet then it is ignored. Shared-line comments are also ignored.
If you're using a custom syntax which support single-line comments with `//`, those are ignored as well.
**Caveat:** Comments within *selector and value lists* are currently ignored.
## Options
`string`: `"always"|"never"`
### `"always"`
There *must always* be an empty line before comments.
The following patterns are considered warnings:
```css
a {}
/* comment */
```
The following patterns are *not* considered warnings:
```css
a {}
/* comment */
```
```css
a {} /* comment */
```
### `"never"`
There *must never* be an empty line before comments.
The following patterns are considered warnings:
```css
a {}
/* comment */
```
The following patterns are *not* considered warnings:
```css
a {}
/* comment */
```
```css
a {} /* comment */
```
## Optional secondary options
### `except: ["first-nested"]`
Reverse the primary option for comments that are nested and the first child of their parent node.
For example, with `"always"`:
The following patterns are considered warnings:
```css
a {
/* comment */
color: pink;
}
```
The following patterns are *not* considered warnings:
```css
a {
/* comment */
color: pink;
}
```
### `ignore: ["after-comment", "stylelint-commands"]`
#### `"after-comment"`
***Note: This option was previously called `between-comments`.***
Don't require an empty line after a comment.
For example, with `"always"`:
The following patterns are *not* considered warnings:
```css
a {
background: pink;
/* comment */
/* comment */
color: #eee;
}
```
```css
a {
background: pink;
/* comment */
/* comment */
color: #eee;
}
```
#### `"stylelint-commands"`
Ignore comments that deliver commands to stylelint, e.g. `/* stylelint-disable color-no-hex */`.
For example, with `"always"`:
The following patterns are considered warnings:
```css
a {
background: pink;
/* not a stylelint command */
color: #eee;
}
```
The following patterns are *not* considered warnings:
```css
a {
background: pink;
/* stylelint-disable color-no-hex */
color: pink;
}
```

View File

@@ -0,0 +1,135 @@
"use strict"
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 = "comment-empty-line-before"
const messages = ruleMessages(ruleName, {
expected: "Expected empty line before comment",
rejected: "Unexpected empty line before comment",
})
const stylelintCommandPrefix = "stylelint-"
const rule = function (expectation, options) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: expectation,
possible: [
"always",
"never",
],
}, {
actual: options,
possible: {
except: ["first-nested"],
ignore: [
"stylelint-commands",
"stylelint-command",
"between-comments",
"after-comment",
],
},
optional: true,
})
if (!validOptions) {
return
}
if (
optionsMatches(options, "ignore", "between-comments")
) {
result.warn((
"'comment-empty-line-before\'s' \"between-comments\" option has been deprecated and in 8.0 will be removed. " +
"Instead use the \"after-comment\" option."
), {
stylelintType: "deprecation",
stylelintReference: "https://stylelint.io/user-guide/rules/comment-empty-line-before/",
})
}
root.walkComments(comment => {
// Ignore the first node
if (comment === root.first) {
return
}
// Optionally ignore stylelint commands
if (
comment.text.indexOf(stylelintCommandPrefix) === 0
&& optionsMatches(options, "ignore", "stylelint-commands")
) {
return
}
// Optionally ignore newlines between comments
const prev = comment.prev()
if (
prev
&& prev.type === "comment"
&& optionsMatches(options, "ignore", "between-comments")
) {
return
}
if (
prev
&& prev.type === "comment"
&& optionsMatches(options, "ignore", "after-comment")
) {
return
}
if (
comment.raws.inline
|| comment.inline
) {
return
}
const before = (comment.raws.before || "")
// Ignore shared-line comments
if (before.indexOf("\n") === -1) {
return
}
const expectEmptyLineBefore = (() => {
if (
optionsMatches(options, "except", "first-nested")
&& comment.parent !== root
&& comment === comment.parent.first
) {
return false
}
return expectation === "always"
})()
const hasEmptyLineBefore = hasEmptyLine(before)
// Return if the expectation is met
if (expectEmptyLineBefore === hasEmptyLineBefore) {
return
}
const message = expectEmptyLineBefore
? messages.expected
: messages.rejected
report({
message,
node: comment,
result,
ruleName,
})
})
}
}
rule.ruleName = ruleName
rule.messages = messages
module.exports = rule