Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 11, 2021
1 parent e0ad120 commit 28d4e50
Show file tree
Hide file tree
Showing 79 changed files with 2,748 additions and 3,148 deletions.
35 changes: 14 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"to-vfile": "^7.0.0",
"unist-builder": "^3.0.0",
"unist-util-remove-position": "^4.0.0",
"xo": "^0.38.0"
"xo": "^0.39.0"
},
"scripts": {
"postinstall": "lerna bootstrap --no-ci",
Expand All @@ -73,27 +73,20 @@
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"unicorn/explicit-length-check": "off",
"unicorn/no-array-callback-reference": "off",
"unicorn/no-array-for-each": "off",
"unicorn/no-new-array": "off",
"unicorn/prefer-default-parameters": "off",
"unicorn/prefer-includes": "off",
"unicorn/prefer-number-properties": "off",
"unicorn/prefer-optional-catch-binding": "off",
"unicorn/string-content": "off",
"guard-for-in": "off",
"max-depth": "off",
"no-eq-null": "off",
"eqeqeq": [
"error",
"always",
{
"null": "ignore"
"unicorn/prefer-switch": "off"
},
"overrides": [
{
"files": [
"test.js",
"script/**/*.js"
],
"rules": {
"max-depth": "off",
"no-await-in-loop": "off"
}
]
}
}
]
}
}
33 changes: 14 additions & 19 deletions packages/remark-lint-blockquote-indentation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,38 +59,33 @@ const remarkLintBlockquoteIndentation = lintRule(
export default remarkLintBlockquoteIndentation

function blockquoteIndentation(tree, file, option) {
var preferred = typeof option === 'number' && !isNaN(option) ? option : null

visit(tree, 'blockquote', visitor)

function visitor(node) {
var abs
var diff
var reason
let preferred = typeof option === 'number' ? option : null

visit(tree, 'blockquote', (node) => {
if (generated(node) || node.children.length === 0) {
return
}

if (preferred) {
diff = preferred - check(node)
const diff = preferred - check(node)

if (diff !== 0) {
abs = Math.abs(diff)
reason =
(diff > 0 ? 'Add' : 'Remove') +
' ' +
abs +
' ' +
plural('space', abs) +
' between block quote and content'
const abs = Math.abs(diff)

file.message(reason, pointStart(node.children[0]))
file.message(
(diff > 0 ? 'Add' : 'Remove') +
' ' +
abs +
' ' +
plural('space', abs) +
' between block quote and content',
pointStart(node.children[0])
)
}
} else {
preferred = check(node)
}
}
})
}

function check(node) {
Expand Down
140 changes: 66 additions & 74 deletions packages/remark-lint-checkbox-character-style/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,86 +74,78 @@ import {visit} from 'unist-util-visit'
import {pointStart, pointEnd} from 'unist-util-position'
import {generated} from 'unist-util-generated'

const checked = {x: true, X: true}
const unchecked = {' ': true, '\t': true}
const types = {true: 'checked', false: 'unchecked'}

const remarkLintCheckboxCharacterStyle = lintRule(
'remark-lint:checkbox-character-style',
checkboxCharacterStyle
)

export default remarkLintCheckboxCharacterStyle

var checked = {x: true, X: true}
var unchecked = {' ': true, '\t': true}
var types = {true: 'checked', false: 'unchecked'}

function checkboxCharacterStyle(tree, file, option) {
var contents = String(file)
var preferred = typeof option === 'object' ? option : {}

if (preferred.unchecked && unchecked[preferred.unchecked] !== true) {
file.fail(
'Incorrect unchecked checkbox marker `' +
preferred.unchecked +
"`: use either `'\\t'`, or `' '`"
)
}

if (preferred.checked && checked[preferred.checked] !== true) {
file.fail(
'Incorrect checked checkbox marker `' +
preferred.checked +
"`: use either `'x'`, or `'X'`"
)
}

visit(tree, 'listItem', visitor)

function visitor(node) {
var type
var point
var value
var style
var reason
(tree, file, option) => {
const value = String(file)
const preferred = typeof option === 'object' ? option : {}

if (preferred.unchecked && unchecked[preferred.unchecked] !== true) {
file.fail(
'Incorrect unchecked checkbox marker `' +
preferred.unchecked +
"`: use either `'\\t'`, or `' '`"
)
}

// Exit early for items without checkbox.
if (typeof node.checked !== 'boolean' || generated(node)) {
return
if (preferred.checked && checked[preferred.checked] !== true) {
file.fail(
'Incorrect checked checkbox marker `' +
preferred.checked +
"`: use either `'x'`, or `'X'`"
)
}

type = types[node.checked]
visit(tree, 'listItem', (node) => {
// Exit early for items without checkbox.
if (typeof node.checked !== 'boolean' || generated(node)) {
return
}

// A list item cannot be checked and empty, according to GFM, but
// theoretically it makes sense to get the end if that were possible.
point =
const type = types[node.checked]

// A list item cannot be checked and empty, according to GFM, but
// theoretically it makes sense to get the end if that were possible.
const point =
/* c8 ignore next 2 */
node.children.length === 0
? pointEnd(node)
: pointStart(node.children[0])
// Move back to before `] `.
point.offset -= 2
point.column -= 2

// Assume we start with a checkbox, because well, `checked` is set.
const match = /\[([\t Xx])]/.exec(
value.slice(point.offset - 2, point.offset + 1)
)

// Failsafe to make sure we don‘t crash if there actually isn’t a checkbox.
/* c8 ignore next */
node.children.length === 0 ? pointEnd(node) : pointStart(node.children[0])
// Move back to before `] `.
point.offset -= 2
point.column -= 2

// Assume we start with a checkbox, because well, `checked` is set.
value = /\[([\t Xx])]/.exec(
contents.slice(point.offset - 2, point.offset + 1)
)

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

style = preferred[type]

if (style) {
if (value[1] !== style) {
reason =
type.charAt(0).toUpperCase() +
type.slice(1) +
' checkboxes should use `' +
style +
'` as a marker'

file.message(reason, point)
if (!match) return

const style = preferred[type]

if (style) {
if (match[1] !== style) {
file.message(
type.charAt(0).toUpperCase() +
type.slice(1) +
' checkboxes should use `' +
style +
'` as a marker',
point
)
}
} else {
preferred[type] = match[1]
}
} else {
preferred[type] = value[1]
}
})
}
}
)

export default remarkLintCheckboxCharacterStyle
79 changes: 35 additions & 44 deletions packages/remark-lint-checkbox-content-indent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,56 +35,47 @@ import {generated} from 'unist-util-generated'

const remarkLintCheckboxContentIndent = lintRule(
'remark-lint:checkbox-content-indent',
checkboxContentIndent
)

export default remarkLintCheckboxContentIndent

var reason = 'Checkboxes should be followed by a single character'

function checkboxContentIndent(tree, file) {
var contents = String(file)
var loc = location(file)
(tree, file) => {
const value = String(file)
const loc = location(file)

visit(tree, 'listItem', visitor)
visit(tree, 'listItem', (node) => {
// Exit early for items without checkbox.
if (typeof node.checked !== 'boolean' || generated(node)) {
return
}

function visitor(node) {
var initial
var final
var value
var point
// A list item cannot be checked and empty, according to GFM, but
// theoretically it makes sense to get the end if that were possible.
const point =
/* c8 ignore next 2 */
node.children.length === 0
? pointEnd(node)
: pointStart(node.children[0])

// Exit early for items without checkbox.
if (typeof node.checked !== 'boolean' || generated(node)) {
return
}
// Assume we start with a checkbox, because well, `checked` is set.
const match = /\[([\t xX])]/.exec(
value.slice(point.offset - 4, point.offset + 1)
)

// A list item cannot be checked and empty, according to GFM, but
// theoretically it makes sense to get the end if that were possible.
point =
// Failsafe to make sure we don‘t crash if there actually isn’t a checkbox.
/* c8 ignore next */
node.children.length === 0 ? pointEnd(node) : pointStart(node.children[0])
if (!match) return

// Assume we start with a checkbox, because well, `checked` is set.
value = /\[([\t xX])]/.exec(
contents.slice(point.offset - 4, point.offset + 1)
)
// Move past checkbox.
const initial = point.offset
let final = initial

// Failsafe to make sure we don‘t crash if there actually isn’t a checkbox.
/* c8 ignore next */
if (!value) return
while (/[\t ]/.test(value.charAt(final))) final++

// Move past checkbox.
initial = point.offset
final = initial

while (/[\t ]/.test(contents.charAt(final))) final++

if (final - initial > 0) {
file.message(reason, {
start: loc.toPoint(initial),
end: loc.toPoint(final)
})
}
if (final - initial > 0) {
file.message('Checkboxes should be followed by a single character', {
start: loc.toPoint(initial),
end: loc.toPoint(final)
})
}
})
}
}
)

export default remarkLintCheckboxContentIndent
Loading

0 comments on commit 28d4e50

Please sign in to comment.