Refactor to improve performance of deep trees

This commit is contained in:
Titus Wormer 2024-02-01 12:27:07 +01:00
parent 45aeac273a
commit e55606ec99
No known key found for this signature in database
GPG Key ID: E6E581152ED04E2E
69 changed files with 364 additions and 70 deletions

View File

@ -122,10 +122,11 @@
* Configuration.
*/
import {phrasing} from 'mdast-util-phrasing'
import pluralize from 'pluralize'
import {lintRule} from 'unified-lint-rule'
import {pointStart} from 'unist-util-position'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
const remarkLintBlockquoteIndentation = lintRule(
{
@ -156,7 +157,14 @@ const remarkLintBlockquoteIndentation = lintRule(
)
}
visitParents(tree, 'blockquote', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'blockquote') return
const start = pointStart(node)
const headStart = pointStart(node.children[0])

View File

@ -33,6 +33,7 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"mdast-util-phrasing": "^4.0.0",
"pluralize": "^8.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",

View File

@ -145,9 +145,10 @@
* Preferred style to use for unchecked checkboxes (default: `'consistent'`).
*/
import {phrasing} from 'mdast-util-phrasing'
import {lintRule} from 'unified-lint-rule'
import {pointStart} from 'unist-util-position'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
import {VFileMessage} from 'vfile-message'
const remarkLintCheckboxCharacterStyle = lintRule(
@ -204,7 +205,14 @@ const remarkLintCheckboxCharacterStyle = lintRule(
)
}
visitParents(tree, 'listItem', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'listItem') return
const head = node.children[0]
const headStart = pointStart(head)

View File

@ -34,6 +34,7 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"mdast-util-phrasing": "^4.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",
"unist-util-visit-parents": "^6.0.0",

View File

@ -93,10 +93,11 @@
* @typedef {import('mdast').Root} Root
*/
import {phrasing} from 'mdast-util-phrasing'
import pluralize from 'pluralize'
import {lintRule} from 'unified-lint-rule'
import {pointStart} from 'unist-util-position'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
const remarkLintCheckboxContentIndent = lintRule(
{
@ -112,7 +113,14 @@ const remarkLintCheckboxContentIndent = lintRule(
function (tree, file) {
const value = String(file)
visitParents(tree, 'listItem', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'listItem') return
const head = node.children[0]
const headStart = pointStart(head)

View File

@ -34,6 +34,7 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"mdast-util-phrasing": "^4.0.0",
"pluralize": "^8.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",

View File

@ -162,9 +162,10 @@
* Styles.
*/
import {phrasing} from 'mdast-util-phrasing'
import {lintRule} from 'unified-lint-rule'
import {pointEnd, pointStart} from 'unist-util-position'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
import {VFileMessage} from 'vfile-message'
const remarkLintCodeBlockStyle = lintRule(
@ -199,7 +200,14 @@ const remarkLintCodeBlockStyle = lintRule(
)
}
visitParents(tree, 'code', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'code') return
const end = pointEnd(node)
const start = pointStart(node)

View File

@ -32,6 +32,7 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"mdast-util-phrasing": "^4.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",
"unist-util-visit-parents": "^6.0.0",

View File

@ -68,8 +68,9 @@
* @typedef {import('mdast').Root} Root
*/
import {phrasing} from 'mdast-util-phrasing'
import {lintRule} from 'unified-lint-rule'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
const remarkLintDefinitionCase = lintRule(
{
@ -84,6 +85,11 @@ const remarkLintDefinitionCase = lintRule(
*/
function (tree, file) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (
(node.type === 'definition' || node.type === 'footnoteDefinition') &&
node.position &&

View File

@ -32,6 +32,7 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"mdast-util-phrasing": "^4.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-visit-parents": "^6.0.0"
},

View File

@ -45,6 +45,8 @@
* @example
* {"name": "ok.md"}
*
* The first planet is [planet mercury][].
*
* [planet mercury]: http://example.com
*
* @example
@ -73,9 +75,10 @@
*/
import {longestStreak} from 'longest-streak'
import {phrasing} from 'mdast-util-phrasing'
import pluralize from 'pluralize'
import {lintRule} from 'unified-lint-rule'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
const remarkLintDefinitionSpacing = lintRule(
{
@ -90,6 +93,11 @@ const remarkLintDefinitionSpacing = lintRule(
*/
function (tree, file) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type === 'definition' && node.position && node.label) {
const size = longestStreak(node.label, ' ')

View File

@ -33,6 +33,7 @@
"dependencies": {
"@types/mdast": "^4.0.0",
"longest-streak": "^3.0.0",
"mdast-util-phrasing": "^4.0.0",
"pluralize": "^8.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-visit-parents": "^6.0.0"

View File

@ -150,6 +150,8 @@ Due to this, its recommended to use one space and turn this rule on.
###### In
```markdown
The first planet is [planet mercury][].
[planet mercury]: http://example.com
```

View File

@ -59,6 +59,8 @@
* @example
* {"name": "ok.md"}
*
* Some markdown:
*
* ```markdown
* # Mercury
* ```
@ -149,9 +151,10 @@
*/
import {quotation} from 'quotation'
import {phrasing} from 'mdast-util-phrasing'
import {lintRule} from 'unified-lint-rule'
import {pointEnd, pointStart} from 'unist-util-position'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
const fence = /^ {0,3}([~`])\1{2,}/
@ -212,7 +215,14 @@ const remarkLintFencedCodeFlag = lintRule(
allowedDisplay = 'keyword'
}
visitParents(tree, 'code', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'code') return
const end = pointEnd(node)
const start = pointStart(node)

View File

@ -34,6 +34,7 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"mdast-util-phrasing": "^4.0.0",
"quotation": "^2.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",

View File

@ -165,6 +165,8 @@ Its recommended to instead use a certain flag for plain text (such as
###### In
````markdown
Some markdown:
```markdown
# Mercury
```

View File

@ -146,9 +146,10 @@
* Configuration.
*/
import {phrasing} from 'mdast-util-phrasing'
import {lintRule} from 'unified-lint-rule'
import {pointStart} from 'unist-util-position'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
import {VFileMessage} from 'vfile-message'
const remarkLintFencedCodeMarker = lintRule(
@ -183,7 +184,14 @@ const remarkLintFencedCodeMarker = lintRule(
)
}
visitParents(tree, 'code', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'code') return
const start = pointStart(node)
if (start && typeof start.offset === 'number') {

View File

@ -33,6 +33,7 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"mdast-util-phrasing": "^4.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",
"unist-util-visit-parents": "^6.0.0",

View File

@ -111,9 +111,10 @@
/// <reference types="mdast-util-mdx" />
import {ok as assert} from 'devlop'
import {phrasing} from 'mdast-util-phrasing'
import {lintRule} from 'unified-lint-rule'
import {pointEnd, pointStart} from 'unist-util-position'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
import {VFileMessage} from 'vfile-message'
const remarkLintFinalDefinition = lintRule(
@ -134,6 +135,11 @@ const remarkLintFinalDefinition = lintRule(
let contentAncestors
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type === 'definition' || node.type === 'footnoteDefinition') {
definitionStacks.push([...parents, node])
} else if (
@ -141,9 +147,7 @@ const remarkLintFinalDefinition = lintRule(
// Ignore HTML comments.
(node.type === 'html' && /^[\t ]*<!--/.test(node.value)) ||
// Ignore MDX comments.
((node.type === 'mdxFlowExpression' ||
node.type === 'mdxTextExpression') &&
/^\s*\/\*/.test(node.value))
(node.type === 'mdxFlowExpression' && /^\s*\/\*/.test(node.value))
) {
// Empty.
} else {

View File

@ -35,6 +35,7 @@
"@types/mdast": "^4.0.0",
"devlop": "^1.0.0",
"mdast-util-mdx": "^3.0.0",
"mdast-util-phrasing": "^4.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",
"unist-util-visit-parents": "^6.0.0",

View File

@ -145,9 +145,10 @@
*/
import {headingStyle} from 'mdast-util-heading-style'
import {phrasing} from 'mdast-util-phrasing'
import {lintRule} from 'unified-lint-rule'
import {position} from 'unist-util-position'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
import {VFileMessage} from 'vfile-message'
const remarkLintHeadingStyle = lintRule(
@ -185,7 +186,14 @@ const remarkLintHeadingStyle = lintRule(
)
}
visitParents(tree, 'heading', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'heading') return
const place = position(node)
const actual = headingStyle(node, expected)

View File

@ -35,6 +35,7 @@
"dependencies": {
"@types/mdast": "^4.0.0",
"mdast-util-heading-style": "^3.0.0",
"mdast-util-phrasing": "^4.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",
"unist-util-visit-parents": "^6.0.0",

View File

@ -109,10 +109,11 @@
* @typedef {import('mdast').Root} Root
*/
import {phrasing} from 'mdast-util-phrasing'
import pluralize from 'pluralize'
import {lintRule} from 'unified-lint-rule'
import {pointStart} from 'unist-util-position'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
import {VFileMessage} from 'vfile-message'
const remarkLintListItemContentIndent = lintRule(
@ -131,7 +132,14 @@ const remarkLintListItemContentIndent = lintRule(
/** @type {VFileMessage | undefined} */
let cause
visitParents(tree, 'listItem', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'listItem') return
let index = -1
/** @type {number | undefined} */
let expected

View File

@ -34,6 +34,7 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"mdast-util-phrasing": "^4.0.0",
"pluralize": "^8.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",

View File

@ -301,10 +301,11 @@
* Configuration.
*/
import {phrasing} from 'mdast-util-phrasing'
import pluralize from 'pluralize'
import {lintRule} from 'unified-lint-rule'
import {pointStart} from 'unist-util-position'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
const remarkLintListItemIndent = lintRule(
{
@ -347,21 +348,28 @@ const remarkLintListItemIndent = lintRule(
)
}
visitParents(tree, 'list', function (list, parents) {
let loose = list.spread
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'list') return
let loose = node.spread
if (!loose) {
for (const item of list.children) {
if (item.spread) {
for (const child of node.children) {
if (child.spread) {
loose = true
break
}
}
}
for (const item of list.children) {
const head = item.children[0]
const itemStart = pointStart(item)
for (const child of node.children) {
const head = child.children[0]
const itemStart = pointStart(child)
const headStart = pointStart(head)
if (
@ -420,7 +428,7 @@ const remarkLintListItemIndent = lintRule(
differenceAbsolute +
'` ' +
pluralize('space', differenceAbsolute),
{ancestors: [...parents, list, item], place: headStart}
{ancestors: [...parents, node, child], place: headStart}
)
}
}

View File

@ -33,6 +33,7 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"mdast-util-phrasing": "^4.0.0",
"pluralize": "^8.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",
@ -49,6 +50,7 @@
"prettier": true,
"rules": {
"capitalized-comments": "off",
"complexity": "off",
"unicorn/prefer-code-point": "off",
"unicorn/prefer-switch": "off"
}

View File

@ -151,10 +151,11 @@
* preference (default: `false`).
*/
import {phrasing} from 'mdast-util-phrasing'
import pluralize from 'pluralize'
import {lintRule} from 'unified-lint-rule'
import {pointEnd, pointStart} from 'unist-util-position'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
import {VFileMessage} from 'vfile-message'
/** @type {Readonly<Options>} */
@ -178,7 +179,13 @@ const remarkLintListItemSpacing = lintRule(
// To do: change options. Maybe to `Style = 'markdown' | 'markdown-style-guide'`?
const checkBlanks = settings.checkBlanks || false
visitParents(tree, 'list', function (list, parents) {
visitParents(tree, function (list, parents) {
// Do not walk into phrasing.
if (phrasing(list)) {
return SKIP
}
if (list.type !== 'list') return
/** @type {VFileMessage | undefined} */
let spacedCause

View File

@ -34,6 +34,7 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"mdast-util-phrasing": "^4.0.0",
"pluralize": "^8.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",

View File

@ -117,6 +117,7 @@
/// <reference types="mdast-util-directive" />
import {ok as assert} from 'devlop'
import {phrasing} from 'mdast-util-phrasing'
import pluralize from 'pluralize'
import {lintRule} from 'unified-lint-rule'
import {pointEnd, pointStart} from 'unist-util-position'
@ -139,7 +140,14 @@ const remarkLintNoBlockquoteWithoutMarker = lintRule(
const loc = location(file)
// Only paragraphs can be lazy.
visitParents(tree, 'paragraph', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'paragraph') return
let expected = 0
for (const parent of parents) {

View File

@ -35,6 +35,7 @@
"@types/mdast": "^4.0.0",
"devlop": "^1.0.0",
"mdast-util-directive": "^3.0.0",
"mdast-util-phrasing": "^4.0.0",
"pluralize": "^8.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",

View File

@ -260,7 +260,11 @@ const remarkLintNoConsecutiveBlankLines = lintRule(
// Ignore phrasing nodes and non-parents.
if (!parent) return
if (phrasing(node)) return SKIP
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
const siblings = /** @type {Array<Nodes>} */ (parent.children)
const index = siblings.indexOf(node)

View File

@ -39,6 +39,8 @@
* @example
* {"name": "ok.md"}
*
* The first planet is [mercury][].
*
* [mercury]: https://example.com/mercury/
* [venus]: https://example.com/venus/
*
@ -59,8 +61,9 @@
*/
import {ok as assert} from 'devlop'
import {phrasing} from 'mdast-util-phrasing'
import {lintRule} from 'unified-lint-rule'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
import {VFileMessage} from 'vfile-message'
const remarkLintNoDuplicateDefinedUrls = lintRule(
@ -78,7 +81,13 @@ const remarkLintNoDuplicateDefinedUrls = lintRule(
/** @type {Map<string, Array<Nodes>>} */
const map = new Map()
visitParents(tree, 'definition', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'definition') return
const ancestors = [...parents, node]
if (node.position && node.url) {

View File

@ -34,6 +34,7 @@
"dependencies": {
"@types/mdast": "^4.0.0",
"devlop": "^1.0.0",
"mdast-util-phrasing": "^4.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-visit-parents": "^6.0.0",
"vfile-message": "4.0.0"

View File

@ -140,6 +140,8 @@ identifiers.
###### In
```markdown
The first planet is [mercury][].
[mercury]: https://example.com/mercury/
[venus]: https://example.com/venus/
```

View File

@ -75,8 +75,9 @@
*/
import {ok as assert} from 'devlop'
import {phrasing} from 'mdast-util-phrasing'
import {lintRule} from 'unified-lint-rule'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
import {VFileMessage} from 'vfile-message'
/** @type {ReadonlyArray<never>} */
@ -100,6 +101,11 @@ const remarkLintNoDuplicateDefinitions = lintRule(
const footnoteDefinitions = new Map()
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
const [map, identifier] =
node.type === 'definition'
? [definitions, node.identifier]

View File

@ -33,6 +33,7 @@
"dependencies": {
"@types/mdast": "^4.0.0",
"devlop": "^1.0.0",
"mdast-util-phrasing": "^4.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-visit-parents": "^6.0.0",
"vfile-message": "^4.0.0"

View File

@ -70,8 +70,9 @@
* @typedef {import('mdast').RootContent} RootContent
*/
import {phrasing} from 'mdast-util-phrasing'
import {lintRule} from 'unified-lint-rule'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
const remarkLintNoEmphasisAsHeading = lintRule(
{
@ -85,7 +86,14 @@ const remarkLintNoEmphasisAsHeading = lintRule(
* Nothing.
*/
function (tree, file) {
visitParents(tree, 'paragraph', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'paragraph') return
const parent = parents.at(-1)
if (!node.position || !parent) {

View File

@ -32,6 +32,7 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"mdast-util-phrasing": "^4.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-visit-parents": "^6.0.0"
},

View File

@ -88,10 +88,11 @@
* @typedef {import('mdast').Root} Root
*/
import {phrasing} from 'mdast-util-phrasing'
import pluralize from 'pluralize'
import {lintRule} from 'unified-lint-rule'
import {pointEnd, pointStart} from 'unist-util-position'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
const remarkLintNoHeadingContentIndent = lintRule(
{
@ -107,7 +108,13 @@ const remarkLintNoHeadingContentIndent = lintRule(
function (tree, file) {
const value = String(file)
visitParents(tree, 'heading', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'heading') return
const start = pointStart(node)
const end = pointEnd(node)

View File

@ -33,6 +33,7 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"mdast-util-phrasing": "^4.0.0",
"pluralize": "^8.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",

View File

@ -93,10 +93,11 @@
* @typedef {import('mdast').Root} Root
*/
import {phrasing} from 'mdast-util-phrasing'
import pluralize from 'pluralize'
import {lintRule} from 'unified-lint-rule'
import {pointStart} from 'unist-util-position'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
const remarkLintNoHeadingIndent = lintRule(
{
@ -110,13 +111,23 @@ const remarkLintNoHeadingIndent = lintRule(
* Nothing.
*/
function (tree, file) {
visitParents(tree, 'heading', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
const parent = parents[parents.length - 1]
const start = pointStart(node)
// Note: its rather complex to detect what the expected indent is in block
// quotes and lists, so lets only do directly in root for now.
if (!start || !parent || parent.type !== 'root') {
if (
!start ||
!parent ||
node.type !== 'heading' ||
parent.type !== 'root'
) {
return
}

View File

@ -32,6 +32,7 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"mdast-util-phrasing": "^4.0.0",
"pluralize": "^8.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",

View File

@ -59,10 +59,11 @@
* @typedef {import('mdast').Root} Root
*/
import {phrasing} from 'mdast-util-phrasing'
import pluralize from 'pluralize'
import {lintRule} from 'unified-lint-rule'
import {pointStart} from 'unist-util-position'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
const max = 6
@ -78,7 +79,14 @@ const remarkLintNoHeadingLikeParagraph = lintRule(
* Nothing.
*/
function (tree, file) {
visitParents(tree, 'paragraph', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'paragraph') return
const head = node.children[0]
if (head && head.type === 'text') {

View File

@ -32,6 +32,7 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"mdast-util-phrasing": "^4.0.0",
"pluralize": "^8.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",

View File

@ -234,10 +234,13 @@ const remarkLintNoMissingBlankLines = lintRule(
visitParents(tree, function (node, parents) {
const parent = parents[parents.length - 1]
// Ignore phrasing nodes and non-parents.
if (phrasing(node)) return SKIP
if (!parent) return
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (
// Children of list items are normally checked.
(!exceptTightLists || parent.type !== 'listItem') &&

View File

@ -88,6 +88,7 @@
*/
import {ok as assert} from 'devlop'
import {phrasing} from 'mdast-util-phrasing'
import pluralize from 'pluralize'
import {lintRule} from 'unified-lint-rule'
import {pointEnd, pointStart} from 'unist-util-position'
@ -110,7 +111,14 @@ const remarkLintNoParagraphContentIndent = lintRule(
const locations = location(value)
// Note: this code is very similar to `remark-lint-no-table-indentation`.
visitParents(tree, 'paragraph', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'paragraph') return
const parent = parents.at(-1)
const end = pointEnd(node)
const start = pointStart(node)

View File

@ -34,6 +34,7 @@
"dependencies": {
"@types/mdast": "^4.0.0",
"devlop": "^1.0.0",
"mdast-util-phrasing": "^4.0.0",
"pluralize": "^8.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",

View File

@ -51,6 +51,8 @@
* echo "Earth and Mars" > file
* ```
*
* Mixed dollars for input lines and without for output is also OK:
*
* ```zsh
* $ echo "Mercury and Venus"
* Mercury and Venus
@ -88,8 +90,9 @@
*/
import {collapseWhiteSpace} from 'collapse-white-space'
import {phrasing} from 'mdast-util-phrasing'
import {lintRule} from 'unified-lint-rule'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
// See: <https://github.com/wooorm/starry-night/blob/a3e35db/lang/source.shell.js#L8>
const flags = new Set([
@ -132,8 +135,14 @@ const remarkLintNoShellDollars = lintRule(
* Nothing.
*/
function (tree, file) {
visitParents(tree, 'code', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (
node.type === 'code' &&
node.position &&
// Check known shell code.
node.lang &&

View File

@ -33,6 +33,7 @@
"dependencies": {
"@types/mdast": "^4.0.0",
"collapse-white-space": "^2.0.0",
"mdast-util-phrasing": "^4.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-visit-parents": "^6.0.0"
},

View File

@ -156,6 +156,8 @@ echo "Mercury and Venus"
echo "Earth and Mars" > file
```
Mixed dollars for input lines and without for output is also OK:
```zsh
$ echo "Mercury and Venus"
Mercury and Venus

View File

@ -95,6 +95,7 @@
*/
import {ok as assert} from 'devlop'
import {phrasing} from 'mdast-util-phrasing'
import pluralize from 'pluralize'
import {lintRule} from 'unified-lint-rule'
import {pointEnd, pointStart} from 'unist-util-position'
@ -117,7 +118,14 @@ const remarkLintNoTableIndentation = lintRule(
const locations = location(value)
// Note: this code is very similar to `remark-lint-no-paragraph-content-indent`.
visitParents(tree, 'table', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'table') return
const parent = parents.at(-1)
const end = pointEnd(node)
const start = pointStart(node)

View File

@ -34,6 +34,7 @@
"@types/mdast": "^4.0.0",
"devlop": "^1.0.0",
"pluralize": "^8.0.0",
"mdast-util-phrasing": "^4.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",
"unist-util-visit-parents": "^6.0.0",

View File

@ -118,10 +118,11 @@
* Style.
*/
import {phrasing} from 'mdast-util-phrasing'
import {asciiDigit} from 'micromark-util-character'
import {lintRule} from 'unified-lint-rule'
import {pointStart} from 'unist-util-position'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
import {VFileMessage} from 'vfile-message'
const remarkLintOrderedListMarkerStyle = lintRule(
@ -156,7 +157,14 @@ const remarkLintOrderedListMarkerStyle = lintRule(
)
}
visitParents(tree, 'listItem', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'listItem') return
const parent = parents.at(-1)
if (!parent || parent.type !== 'list' || !parent.ordered) return

View File

@ -33,6 +33,7 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"mdast-util-phrasing": "^4.0.0",
"micromark-util-character": "^2.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",

View File

@ -237,10 +237,11 @@
*/
import {ok as assert} from 'devlop'
import {phrasing} from 'mdast-util-phrasing'
import {asciiDigit} from 'micromark-util-character'
import {lintRule} from 'unified-lint-rule'
import {pointStart} from 'unist-util-position'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
import {VFileMessage} from 'vfile-message'
const remarkLintOrderedListMarkerValue = lintRule(
@ -282,7 +283,14 @@ const remarkLintOrderedListMarkerValue = lintRule(
/** @type {Array<{ancestors: Array<Nodes>, counters: Array<string | undefined>}>} */
const lists = []
visitParents(tree, 'list', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'list') return
if (!node.ordered) return
/** @type {Array<string | undefined>} */

View File

@ -35,6 +35,7 @@
"dependencies": {
"@types/mdast": "^4.0.0",
"devlop": "^1.0.0",
"mdast-util-phrasing": "^4.0.0",
"micromark-util-character": "^2.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",

View File

@ -69,7 +69,9 @@
* @license MIT
*
* @example
* {"config": "* * *", "name": "ok.md"}
* {"name": "ok.md"}
*
* Two rules:
*
* * * *
*
@ -108,9 +110,10 @@
* Configuration.
*/
import {phrasing} from 'mdast-util-phrasing'
import {lintRule} from 'unified-lint-rule'
import {pointEnd, pointStart} from 'unist-util-position'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
import {VFileMessage} from 'vfile-message'
const remarkLintRuleStyle = lintRule(
@ -150,7 +153,14 @@ const remarkLintRuleStyle = lintRule(
expected = options
}
visitParents(tree, 'thematicBreak', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'thematicBreak') return
const end = pointEnd(node)
const start = pointStart(node)

View File

@ -33,6 +33,7 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"mdast-util-phrasing": "^4.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",
"unist-util-visit-parents": "^6.0.0",

View File

@ -175,11 +175,11 @@ There are three settings to control rules:
##### `ok.md`
When configured with `'* * *'`.
###### In
```markdown
Two rules:
* * *
* * *

View File

@ -406,6 +406,7 @@
*/
import {ok as assert} from 'devlop'
import {phrasing} from 'mdast-util-phrasing'
import pluralize from 'pluralize'
import {lintRule} from 'unified-lint-rule'
import {pointEnd, pointStart} from 'unist-util-position'
@ -459,7 +460,14 @@ const remarkLintTableCellPadding = lintRule(
)
}
visitParents(tree, 'table', function (table, parents) {
visitParents(tree, function (table, parents) {
// Do not walk into phrasing.
if (phrasing(table)) {
return SKIP
}
if (table.type !== 'table') return
const entries = inferTable([...parents, table])
// Find max column sizes.

View File

@ -35,6 +35,7 @@
"@types/mdast": "^4.0.0",
"@types/unist": "^3.0.0",
"devlop": "^1.0.0",
"mdast-util-phrasing": "^4.0.0",
"pluralize": "^8.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",

View File

@ -57,6 +57,8 @@
* @example
* {"gfm": true, "name": "ok.md"}
*
* This rule is only about the alignment of pipes across rows:
*
* | Planet | Mean anomaly (°) |
* | ------- | ---------------: |
* | Mercury | 174 796 |
@ -202,6 +204,7 @@
*/
import {ok as assert} from 'devlop'
import {phrasing} from 'mdast-util-phrasing'
import pluralize from 'pluralize'
import {lintRule} from 'unified-lint-rule'
import {pointEnd, pointStart} from 'unist-util-position'
@ -237,7 +240,14 @@ const remarkLintTablePipeAlignment = lintRule(
const value = String(file)
visitParents(tree, 'table', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'table') return
const entries = inferTable([...parents, node])
// Find max column sizes.
/** @type {Array<number>} */

View File

@ -36,6 +36,7 @@
"@types/mdast": "^4.0.0",
"@types/unist": "^3.0.0",
"devlop": "^1.0.0",
"mdast-util-phrasing": "^4.0.0",
"pluralize": "^8.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",

View File

@ -164,6 +164,8 @@ in which case this rule must be turned off.
> GFM ([`remark-gfm`][github-remark-gfm]).
```markdown
This rule is only about the alignment of pipes across rows:
| Planet | Mean anomaly (°) |
| ------- | ---------------: |
| Mercury | 174 796 |

View File

@ -50,6 +50,8 @@
* @example
* {"name": "ok.md", "gfm": true}
*
* Small table:
*
* | Planet | Mean anomaly (°) |
* | :- | -: |
* | Mercury | 174 796 |
@ -133,9 +135,10 @@
* @typedef {import('unist').Point} Point
*/
import {phrasing} from 'mdast-util-phrasing'
import {lintRule} from 'unified-lint-rule'
import {pointEnd, pointStart} from 'unist-util-position'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
const remarkLintTablePipes = lintRule(
{
@ -151,7 +154,14 @@ const remarkLintTablePipes = lintRule(
function (tree, file) {
const value = String(file)
visitParents(tree, 'table', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'table') return
let index = -1
while (++index < node.children.length) {
@ -216,6 +226,9 @@ const remarkLintTablePipes = lintRule(
}
}
}
// No tables in tables.
return SKIP
})
/**

View File

@ -34,6 +34,7 @@
"dependencies": {
"@types/mdast": "^4.0.0",
"@types/unist": "^3.0.0",
"mdast-util-phrasing": "^4.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",
"unist-util-visit-parents": "^6.0.0"

View File

@ -157,6 +157,8 @@ delimiters.
> GFM ([`remark-gfm`][github-remark-gfm]).
```markdown
Small table:
| Planet | Mean anomaly (°) |
| :- | -: |
| Mercury | 174 796 |

View File

@ -123,9 +123,10 @@
* Styles.
*/
import {phrasing} from 'mdast-util-phrasing'
import {lintRule} from 'unified-lint-rule'
import {pointStart} from 'unist-util-position'
import {visitParents} from 'unist-util-visit-parents'
import {SKIP, visitParents} from 'unist-util-visit-parents'
import {VFileMessage} from 'vfile-message'
const remarkLintUnorderedListMarkerStyle = lintRule(
@ -148,8 +149,6 @@ const remarkLintUnorderedListMarkerStyle = lintRule(
/** @type {VFileMessage | undefined} */
let cause
console.log('check:', file.path)
if (options === null || options === undefined || options === 'consistent') {
// Empty.
} else if (options === '*' || options === '+' || options === '-') {
@ -162,7 +161,14 @@ const remarkLintUnorderedListMarkerStyle = lintRule(
)
}
visitParents(tree, 'listItem', function (node, parents) {
visitParents(tree, function (node, parents) {
// Do not walk into phrasing.
if (phrasing(node)) {
return SKIP
}
if (node.type !== 'listItem') return
const parent = parents.at(-1)
if (!parent || parent.type !== 'list' || parent.ordered) return

View File

@ -33,6 +33,7 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"mdast-util-phrasing": "^4.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",
"unist-util-visit-parents": "^6.0.0",