Skip to content

Commit

Permalink
Refactor more code, improve small things
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Dec 15, 2023
1 parent 547188e commit 627363b
Show file tree
Hide file tree
Showing 35 changed files with 344 additions and 143 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
"remark-comment-config": "^8.0.0",
"remark-gfm": "^4.0.0",
"remark-github": "^12.0.0",
"remark-mdx": "^3.0.0",
"remark-toc": "^9.0.0",
"remark-validate-links": "^13.0.0",
"strip-indent": "^4.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/remark-lint-checkbox-character-style/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ const remarkLintCheckboxCharacterStyle = lintRule(
value.slice(point.offset - 2, point.offset + 1)
)

// Failsafe to make sure we dont crash if there actually isn’t a checkbox.
/* c8 ignore next */
/* c8 ignore next 2 -- failsafe so we dont crash if there actually isn’t
* a checkbox. */
if (!match) return

const style = node.checked ? checked : unchecked
Expand Down
15 changes: 10 additions & 5 deletions packages/remark-lint-checkbox-character-style/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ When configured with `{ checked: 'x' }`.
###### In
> 👉 **Note**: this example uses GFM ([`remark-gfm`][github-remark-gfm]).
> 👉 **Note**: this example uses
> GFM ([`remark-gfm`][github-remark-gfm]).
```markdown
- [x] List item
Expand All @@ -199,7 +200,8 @@ When configured with `{ checked: 'X' }`.
###### In
> 👉 **Note**: this example uses GFM ([`remark-gfm`][github-remark-gfm]).
> 👉 **Note**: this example uses
> GFM ([`remark-gfm`][github-remark-gfm]).
```markdown
- [X] List item
Expand All @@ -216,7 +218,8 @@ When configured with `{ unchecked: ' ' }`.
###### In
> 👉 **Note**: this example uses GFM ([`remark-gfm`][github-remark-gfm]).
> 👉 **Note**: this example uses
> GFM ([`remark-gfm`][github-remark-gfm]).
```markdown
- [ ] List item
Expand All @@ -235,7 +238,8 @@ When configured with `{ unchecked: '\t' }`.
###### In
> 👉 **Note**: this example uses GFM ([`remark-gfm`][github-remark-gfm]).
> 👉 **Note**: this example uses
> GFM ([`remark-gfm`][github-remark-gfm]).
```markdown
- [␉] List item
Expand All @@ -250,7 +254,8 @@ No messages.
###### In
> 👉 **Note**: this example uses GFM ([`remark-gfm`][github-remark-gfm]).
> 👉 **Note**: this example uses
> GFM ([`remark-gfm`][github-remark-gfm]).
```markdown
- [x] List item
Expand Down
6 changes: 4 additions & 2 deletions packages/remark-lint-checkbox-content-indent/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ content after them with a single space between.

###### In

> 👉 **Note**: this example uses GFM ([`remark-gfm`][github-remark-gfm]).
> 👉 **Note**: this example uses
> GFM ([`remark-gfm`][github-remark-gfm]).
```markdown
- [ ] List item
Expand All @@ -176,7 +177,8 @@ No messages.

###### In

> 👉 **Note**: this example uses GFM ([`remark-gfm`][github-remark-gfm]).
> 👉 **Note**: this example uses
> GFM ([`remark-gfm`][github-remark-gfm]).
```markdown
- [ ] List item
Expand Down
91 changes: 58 additions & 33 deletions packages/remark-lint-final-definition/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,44 @@
* @example
* {"name": "not-ok.md", "label": "output"}
*
* 3:1-3:47: Move definitions to the end of the file (after the node at line `5`)
* 3:1-3:47: Move definitions to the end of the file (after `5:19`)
*
* @example
* {"name": "ok-comments.md"}
* {"name": "ok-html-comments.md"}
*
* Paragraph.
*
* [example-1]: http://example.com/one/
*
* <!-- Comments are fine between and after definitions -->
* <!-- Comments are fine between and after definitions. -->
*
* [example-2]: http://example.com/two/
*
* @example
* {"name": "ok-mdx-comments.mdx", "mdx": true}
*
* Paragraph.
*
* [example-1]: http://example.com/one/
*
* {/* Comments are fine in MDX. *␀/}
*
* [example-2]: http://example.com/two/
*/

/**
* @typedef {import('mdast').Definition} Definition
* @typedef {import('mdast').FootnoteDefinition} FootnoteDefinition
* @typedef {import('mdast').Root} Root
*
* @typedef {import('unist').Point} Point
*/

/// <reference types="mdast-util-mdx" />

import {lintRule} from 'unified-lint-rule'
import {pointStart} from 'unist-util-position'
import {pointEnd, pointStart} from 'unist-util-position'
import {stringifyPosition} from 'unist-util-stringify-position'
import {visit} from 'unist-util-visit'

const remarkLintFinalDefinition = lintRule(
Expand All @@ -91,40 +109,47 @@ const remarkLintFinalDefinition = lintRule(
* Nothing.
*/
function (tree, file) {
let last = 0
/** @type {Array<Definition | FootnoteDefinition>} */
const definitions = []
/** @type {Point | undefined} */
let last

visit(
tree,
function (node) {
const start = pointStart(node)
visit(tree, function (node) {
if (node.type === 'definition' || node.type === 'footnoteDefinition') {
definitions.push(node)
} else if (
node.type === 'root' ||
node.type === 'blockquote' ||
node.type === 'listItem' ||
// Ignore HTML comments.
(node.type === 'html' && /^[\t ]*<!--/.test(node.value)) ||
// Ignore MDX comments.
((node.type === 'mdxFlowExpression' ||
node.type === 'mdxTextExpression') &&
/^\s*\/\*/.test(node.value))
) {
// Empty.
} else {
const place = pointEnd(node)

// To do: ignore MDX comments?
// Ignore generated and HTML comment nodes.
if (
!start ||
node.type === 'root' ||
(node.type === 'html' && /^\s*<!--/.test(node.value))
) {
return
if (place) {
last = place
}
}
})

const line = start.line
for (const node of definitions) {
const point = pointStart(node)

if (node.type === 'definition') {
if (last && last > line) {
file.message(
'Move definitions to the end of the file (after the node at line `' +
last +
'`)',
node
)
}
} else if (last === 0) {
last = line
}
},
true
)
if (point && last && point.line < last.line) {
file.message(
'Move definitions to the end of the file (after `' +
stringifyPosition(last) +
'`)',
node
)
}
}
}
)

Expand Down
3 changes: 3 additions & 0 deletions packages/remark-lint-final-definition/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"@types/unist": "^3.0.0",
"mdast-util-mdx": "^3.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",
"unist-util-stringify-position": "^4.0.0",
"unist-util-visit": "^5.0.0"
},
"scripts": {},
Expand Down
29 changes: 26 additions & 3 deletions packages/remark-lint-final-definition/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ Another paragraph.
###### Out

```text
3:1-3:47: Move definitions to the end of the file (after the node at line `5`)
3:1-3:47: Move definitions to the end of the file (after `5:19`)
```

##### `ok-comments.md`
##### `ok-html-comments.md`

###### In

Expand All @@ -183,7 +183,28 @@ Paragraph.

[example-1]: http://example.com/one/

<!-- Comments are fine between and after definitions -->
<!-- Comments are fine between and after definitions. -->

[example-2]: http://example.com/two/
```

###### Out

No messages.

##### `ok-mdx-comments.mdx`

###### In

> 👉 **Note**: this example uses
> MDX ([`remark-mdx`][github-remark-mdx]).
```mdx
Paragraph.

[example-1]: http://example.com/one/

{/* Comments are fine in MDX. */}

[example-2]: http://example.com/two/
```
Expand Down Expand Up @@ -263,6 +284,8 @@ abide by its terms.

[github-remark-lint]: https://github.com/remarkjs/remark-lint

[github-remark-mdx]: https://mdxjs.com/packages/remark-mdx/

[github-unified-transformer]: https://github.com/unifiedjs/unified#transformer

[npm-install]: https://docs.npmjs.com/cli/install
Expand Down
7 changes: 4 additions & 3 deletions packages/remark-lint-final-newline/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
*/

import {lintRule} from 'unified-lint-rule'
import {location} from 'vfile-location'

const remarkLintFinalNewline = lintRule(
{
Expand All @@ -95,11 +96,11 @@ const remarkLintFinalNewline = lintRule(
*/
function (_, file) {
const value = String(file)
const end = location(file).toPoint(value.length)
const last = value.length - 1

if (last > -1 && value.charAt(last) !== '\n') {
// To do: warn at last character.
file.message('Missing newline character at end of file')
if (end && last > -1 && value.charAt(last) !== '\n') {
file.message('Missing newline character at end of file', end)
}
}
)
Expand Down
3 changes: 2 additions & 1 deletion packages/remark-lint-final-newline/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"unified-lint-rule": "^2.0.0"
"unified-lint-rule": "^2.0.0",
"vfile-location": "^5.0.0"
},
"scripts": {},
"typeCoverage": {
Expand Down
22 changes: 19 additions & 3 deletions packages/remark-lint-first-heading-level/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@
* {"name": "not-ok-html.md", "config": 2, "label": "output"}
*
* 1:1-1:14: First heading level should be `2`
*
* @example
* {"name": "ok.mdx", "mdx": true}
*
* In <b>MDX</b>, JSX is supported.
*
* <h1>First heading</h1>
*/

/**
Expand All @@ -158,11 +165,14 @@
* Configuration.
*/

/// <reference types="mdast-util-mdx" />

import {lintRule} from 'unified-lint-rule'
import {position} from 'unist-util-position'
import {EXIT, visit} from 'unist-util-visit'

const re = /<h([1-6])/
const htmlRe = /<h([1-6])/
const jsxNameRe = /h([1-6])/

const remarkLintFirstHeadingLevel = lintRule(
{
Expand All @@ -187,11 +197,17 @@ const remarkLintFirstHeadingLevel = lintRule(
/** @type {Depth | undefined} */
let rank

// To do: MDX?
if (node.type === 'heading') {
rank = node.depth
} else if (node.type === 'html') {
const results = node.value.match(re)
const results = node.value.match(htmlRe)
rank = results ? /** @type {Depth} */ (Number(results[1])) : undefined
} else if (
(node.type === 'mdxJsxFlowElement' ||
node.type === 'mdxJsxTextElement') &&
node.name
) {
const results = node.name.match(jsxNameRe)
rank = results ? /** @type {Depth} */ (Number(results[1])) : undefined
}

Expand Down
1 change: 1 addition & 0 deletions packages/remark-lint-first-heading-level/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-mdx": "^3.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",
"unist-util-visit": "^5.0.0"
Expand Down
Loading

0 comments on commit 627363b

Please sign in to comment.