Skip to content

Commit

Permalink
Refactor to improve performance of deep trees
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Feb 1, 2024
1 parent 45aeac2 commit e55606e
Show file tree
Hide file tree
Showing 69 changed files with 364 additions and 70 deletions.
12 changes: 10 additions & 2 deletions packages/remark-lint-blockquote-indentation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down Expand Up @@ -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])

Expand Down
1 change: 1 addition & 0 deletions packages/remark-lint-blockquote-indentation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 10 additions & 2 deletions packages/remark-lint-checkbox-character-style/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions packages/remark-lint-checkbox-character-style/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 10 additions & 2 deletions packages/remark-lint-checkbox-content-indent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand All @@ -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)

Expand Down
1 change: 1 addition & 0 deletions packages/remark-lint-checkbox-content-indent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 10 additions & 2 deletions packages/remark-lint-code-block-style/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions packages/remark-lint-code-block-style/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 7 additions & 1 deletion packages/remark-lint-definition-case/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand All @@ -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 &&
Expand Down
1 change: 1 addition & 0 deletions packages/remark-lint-definition-case/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
10 changes: 9 additions & 1 deletion packages/remark-lint-definition-spacing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
* @example
* {"name": "ok.md"}
*
* The first planet is [planet mercury][].
*
* [planet mercury]: http://example.com
*
* @example
Expand Down Expand Up @@ -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(
{
Expand All @@ -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, ' ')

Expand Down
1 change: 1 addition & 0 deletions packages/remark-lint-definition-spacing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions packages/remark-lint-definition-spacing/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ Due to this, it’s recommended to use one space and turn this rule on.
###### In

```markdown
The first planet is [planet mercury][].

[planet mercury]: http://example.com
```

Expand Down
14 changes: 12 additions & 2 deletions packages/remark-lint-fenced-code-flag/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
* @example
* {"name": "ok.md"}
*
* Some markdown:
*
* ```markdown
* # Mercury
* ```
Expand Down Expand Up @@ -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,}/

Expand Down Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions packages/remark-lint-fenced-code-flag/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions packages/remark-lint-fenced-code-flag/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ It’s recommended to instead use a certain flag for plain text (such as
###### In

````markdown
Some markdown:

```markdown
# Mercury
```
Expand Down
12 changes: 10 additions & 2 deletions packages/remark-lint-fenced-code-marker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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') {
Expand Down
1 change: 1 addition & 0 deletions packages/remark-lint-fenced-code-marker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 8 additions & 4 deletions packages/remark-lint-final-definition/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -134,16 +135,19 @@ 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 (
node.type === 'root' ||
// 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 {
Expand Down
1 change: 1 addition & 0 deletions packages/remark-lint-final-definition/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 10 additions & 2 deletions packages/remark-lint-heading-style/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions packages/remark-lint-heading-style/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading

0 comments on commit e55606e

Please sign in to comment.