Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Nov 13, 2023
1 parent 0e79b65 commit 19251fc
Show file tree
Hide file tree
Showing 148 changed files with 1,349 additions and 852 deletions.
2 changes: 1 addition & 1 deletion doc/create-a-custom-rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ npm install remark-lint remark-cli
We will also use some utilities:

```sh
npm install unified-lint-rule unist-util-generated unist-util-visit
npm install unified-lint-rule unist-util-visit
```

These will help us creating and managing our custom rules.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
"type-coverage": "^2.0.0",
"type-fest": "^4.0.0",
"typescript": "^5.0.0",
"unified": "^11.0.0",
"unist-builder": "^4.0.0",
"unist-util-remove-position": "^5.0.0",
"vfile": "^6.0.0",
Expand Down
37 changes: 20 additions & 17 deletions packages/remark-lint-blockquote-indentation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,39 +79,42 @@
*/

/**
* @typedef {import('mdast').Blockquote} Blockquote
* @typedef {import('mdast').Root} Root
*/

/**
* @typedef {'consistent' | number} Options
* Options.
* @typedef {number | 'consistent'} Options
* Configuration.
*/

import pluralize from 'pluralize'
import {lintRule} from 'unified-lint-rule'
import plural from 'pluralize'
import {visit} from 'unist-util-visit'
import {pointStart} from 'unist-util-position'
import {generated} from 'unist-util-generated'
import {visit} from 'unist-util-visit'

const remarkLintBlockquoteIndentation = lintRule(
{
origin: 'remark-lint:blockquote-indentation',
url: 'https://github.com/remarkjs/remark-lint/tree/main/packages/remark-lint-blockquote-indentation#readme'
},
/** @type {import('unified-lint-rule').Rule<Root, Options>} */
(tree, file, option = 'consistent') => {
visit(tree, 'blockquote', (node) => {
if (generated(node) || node.children.length === 0) {
return
}
/**
* @param {Root} tree
* Tree.
* @param {Options | null | undefined} [options='consistent']
* Configuration (default: `'consistent'`).
* @returns {undefined}
* Nothing.
*/
function (tree, file, options) {
let option = options || 'consistent'

visit(tree, 'blockquote', function (node) {
const start = pointStart(node)
const head = pointStart(node.children[0])
/* c8 ignore next -- we get here if we have offsets. */
const count = head && start ? head.column - start.column : undefined

if (typeof count === 'number') {
if (head && start) {
const count = head.column - start.column

if (option === 'consistent') {
option = count
} else {
Expand All @@ -125,9 +128,9 @@ const remarkLintBlockquoteIndentation = lintRule(
' ' +
abs +
' ' +
plural('space', abs) +
pluralize('space', abs) +
' between block quote and content',
pointStart(node.children[0])
head
)
}
}
Expand Down
5 changes: 2 additions & 3 deletions packages/remark-lint-blockquote-indentation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
"dependencies": {
"@types/mdast": "^4.0.0",
"pluralize": "^8.0.0",
"unified": "^11.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-generated": "^3.0.0",
"unist-util-position": "^5.0.0",
"unist-util-visit": "^5.0.0"
},
Expand All @@ -54,7 +52,8 @@
"xo": {
"prettier": true,
"rules": {
"capitalized-comments": "off"
"capitalized-comments": "off",
"unicorn/prefer-default-parameters": "off"
}
}
}
37 changes: 22 additions & 15 deletions packages/remark-lint-checkbox-character-style/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,37 +92,44 @@
*/

/**
* @typedef {Styles | 'consistent'} Options
* Configuration.
*
* @typedef Styles
* Styles.
* @property {'x' | 'X' | 'consistent'} [checked='consistent']
* @property {'X' | 'x' | 'consistent' | null | undefined} [checked='consistent']
* Preferred style to use for checked checkboxes (default: `'consistent'`).
* @property {' ' | '\t' | 'consistent'} [unchecked='consistent']
* @property {'\t' | ' ' | 'consistent' | null | undefined} [unchecked='consistent']
* Preferred style to use for unchecked checkboxes (default: `'consistent'`).
*
* @typedef {'consistent' | Styles} Options
* Options.
*/

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

const remarkLintCheckboxCharacterStyle = lintRule(
{
origin: 'remark-lint:checkbox-character-style',
url: 'https://github.com/remarkjs/remark-lint/tree/main/packages/remark-lint-checkbox-character-style#readme'
},
/** @type {import('unified-lint-rule').Rule<Root, Options>} */
(tree, file, option = 'consistent') => {
/**
* @param {Root} tree
* Tree.
* @param {Options | null | undefined} [options]
* Configuration (optional).
* @returns {undefined}
* Nothing.
*/
function (tree, file, options) {
const value = String(file)
/** @type {'x' | 'X' | 'consistent'} */
/** @type {'X' | 'x' | 'consistent'} */
let checked = 'consistent'
/** @type {' ' | '\x09' | 'consistent'} */
/** @type {'\x09' | ' ' | 'consistent'} */
let unchecked = 'consistent'

if (typeof option === 'object') {
checked = option.checked || 'consistent'
unchecked = option.unchecked || 'consistent'
if (options && typeof options === 'object') {
checked = options.checked || 'consistent'
unchecked = options.unchecked || 'consistent'
}

if (unchecked !== 'consistent' && unchecked !== ' ' && unchecked !== '\t') {
Expand All @@ -141,16 +148,16 @@ const remarkLintCheckboxCharacterStyle = lintRule(
)
}

visit(tree, 'listItem', (node) => {
visit(tree, 'listItem', function (node) {
const head = node.children[0]
const point = pointStart(head)

// Exit early for items without checkbox.
// A list item cannot be checked and empty, according to GFM.
if (
!point ||
typeof node.checked !== 'boolean' ||
!head ||
typeof node.checked !== 'boolean' ||
typeof point.offset !== 'number'
) {
return
Expand Down
1 change: 0 additions & 1 deletion packages/remark-lint-checkbox-character-style/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"unified": "^11.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",
"unist-util-visit": "^5.0.0"
Expand Down
20 changes: 12 additions & 8 deletions packages/remark-lint-checkbox-content-indent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,35 @@
*/

import {lintRule} from 'unified-lint-rule'
import {location} from 'vfile-location'
import {visit} from 'unist-util-visit'
import {pointStart} from 'unist-util-position'
import {visit} from 'unist-util-visit'
import {location} from 'vfile-location'

const remarkLintCheckboxContentIndent = lintRule(
{
origin: 'remark-lint:checkbox-content-indent',
url: 'https://github.com/remarkjs/remark-lint/tree/main/packages/remark-lint-checkbox-content-indent#readme'
},
/** @type {import('unified-lint-rule').Rule<Root, void>} */
(tree, file) => {
/**
* @param {Root} tree
* Tree.
* @returns {undefined}
* Nothing.
*/
function (tree, file) {
const value = String(file)
const loc = location(file)

visit(tree, 'listItem', (node) => {
visit(tree, 'listItem', function (node) {
const head = node.children[0]
const point = pointStart(head)

// Exit early for items without checkbox.
// A list item cannot be checked and empty, according to GFM.
if (
!point ||
typeof node.checked !== 'boolean' ||
!head ||
typeof node.checked !== 'boolean' ||
typeof point.offset !== 'number'
) {
return
Expand All @@ -100,8 +105,7 @@ const remarkLintCheckboxContentIndent = lintRule(
value.slice(point.offset - 4, point.offset + 1)
)

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

// Move past checkbox.
Expand Down
1 change: 0 additions & 1 deletion packages/remark-lint-checkbox-content-indent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"unified": "^11.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",
"unist-util-visit": "^5.0.0",
Expand Down
41 changes: 25 additions & 16 deletions packages/remark-lint-code-block-style/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,29 +131,38 @@
*/

/**
* @typedef {'fenced' | 'indented'} Style
* @typedef {Style | 'consistent'} Options
* Configuration.
*
* @typedef {'indented' | 'fenced'} Style
* Styles.
* @typedef {'consistent' | Style} Options
* Options.
*/

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

const remarkLintCodeBlockStyle = lintRule(
{
origin: 'remark-lint:code-block-style',
url: 'https://github.com/remarkjs/remark-lint/tree/main/packages/remark-lint-code-block-style#readme'
},
/** @type {import('unified-lint-rule').Rule<Root, Options>} */
(tree, file, option = 'consistent') => {
/**
* @param {Root} tree
* Tree.
* @param {Options | null | undefined} [options='consistent']
* Configuration (default: `'consistent'`).
* @returns {undefined}
* Nothing.
*/
function (tree, file, options) {
let option = options || 'consistent'
const value = String(file)

if (
option !== 'consistent' &&
option !== 'fenced' &&
option !== 'indented'
option !== 'indented' &&
option !== 'fenced'
) {
file.fail(
'Incorrect code block style `' +
Expand All @@ -162,22 +171,22 @@ const remarkLintCodeBlockStyle = lintRule(
)
}

visit(tree, 'code', (node) => {
const initial = pointStart(node)
const final = pointEnd(node)
visit(tree, 'code', function (node) {
const end = pointEnd(node)
const start = pointStart(node)

if (
!initial ||
!final ||
typeof initial.offset !== 'number' ||
typeof final.offset !== 'number'
!start ||
!end ||
typeof start.offset !== 'number' ||
typeof end.offset !== 'number'
) {
return
}

const current =
node.lang ||
/^\s*([~`])\1{2,}/.test(value.slice(initial.offset, final.offset))
/^\s*([~`])\1{2,}/.test(value.slice(start.offset, end.offset))
? 'fenced'
: 'indented'

Expand Down
4 changes: 2 additions & 2 deletions packages/remark-lint-code-block-style/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"unified": "^11.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",
"unist-util-visit": "^5.0.0"
Expand All @@ -51,7 +50,8 @@
"xo": {
"prettier": true,
"rules": {
"capitalized-comments": "off"
"capitalized-comments": "off",
"unicorn/prefer-default-parameters": "off"
}
}
}
21 changes: 13 additions & 8 deletions packages/remark-lint-definition-case/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
*/

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

const label = /^\s*\[((?:\\[\s\S]|[^[\]])+)]/

Expand All @@ -52,20 +52,25 @@ const remarkLintDefinitionCase = lintRule(
origin: 'remark-lint:definition-case',
url: 'https://github.com/remarkjs/remark-lint/tree/main/packages/remark-lint-definition-case#readme'
},
/** @type {import('unified-lint-rule').Rule<Root, void>} */
(tree, file) => {
/**
* @param {Root} tree
* Tree.
* @returns {undefined}
* Nothing.
*/
function (tree, file) {
const value = String(file)

visit(tree, (node) => {
visit(tree, function (node) {
if (node.type === 'definition' || node.type === 'footnoteDefinition') {
const start = pointStart(node)
const end = pointEnd(node)
const start = pointStart(node)

if (
start &&
end &&
typeof start.offset === 'number' &&
typeof end.offset === 'number'
start &&
typeof end.offset === 'number' &&
typeof start.offset === 'number'
) {
const match = value.slice(start.offset, end.offset).match(label)

Expand Down
1 change: 0 additions & 1 deletion packages/remark-lint-definition-case/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
],
"dependencies": {
"@types/mdast": "^4.0.0",
"unified": "^11.0.0",
"unified-lint-rule": "^2.0.0",
"unist-util-position": "^5.0.0",
"unist-util-visit": "^5.0.0"
Expand Down

0 comments on commit 19251fc

Please sign in to comment.