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 30, 2023
1 parent 555f625 commit d831bc7
Show file tree
Hide file tree
Showing 184 changed files with 3,613 additions and 3,739 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@
"typescript": "^5.0.0",
"undici": "^5.0.0",
"unified": "^11.0.0",
"unist-builder": "^4.0.0",
"unist-util-visit": "^5.0.0",
"vfile": "^6.0.0",
"vfile-find-down": "^7.0.0",
Expand All @@ -114,7 +113,7 @@
"build": "tsc --build --clean && tsc --build && type-coverage",
"format": "remark . --frail --output --quiet && prettier . --log-level warn --write && xo --fix",
"prepack": "npm run build && npm run format",
"test": "npm run generate && npm run build && npm run format && npm run test-coverage",
"test": "npm run build && npm run generate && npm run format && npm run test-coverage",
"test-api": "node --conditions development test/index.js",
"test-coverage": "c8 --100 --check-coverage --reporter lcov npm run test-api"
},
Expand Down
35 changes: 1 addition & 34 deletions packages/hast-util-from-string/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,37 +57,4 @@
* Given node (`Node`).
*/

/**
* @typedef {import('hast').Nodes} Nodes
*/

// To do: remove return result.
/**
* Set the plain-text value of a hast node.
* This is like the DOMs `Node#textContent` setter.
* The given node is returned.
*
* @template {Nodes} Thing
* Node kind.
* @param {Thing} node
* Node to change.
* @param {string | null | undefined} [value='']
* Value to use (default: `''`)
* @returns {Thing}
* Given node.
*/
export function fromString(node, value) {
const normalized = value === undefined || value === null ? '' : String(value)

if ('children' in node) {
node.children = []

if (value) {
node.children.push({type: 'text', value: normalized})
}
} else if (node.type !== 'doctype') {
node.value = normalized
}

return node
}
export {fromString} from './lib/index.js'
34 changes: 34 additions & 0 deletions packages/hast-util-from-string/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @typedef {import('hast').Nodes} Nodes
*/

// To do: next major: remove return result.
/**
* Set the plain-text value of a hast node.
* This is like the DOMs `Node#textContent` setter.
* The given node is returned.
*
* @template {Nodes} Thing
* Node kind.
* @param {Thing} node
* Node to change.
* @param {string | null | undefined} [value='']
* Value to use (default: `''`)
* @returns {Thing}
* Given node.
*/
export function fromString(node, value) {
const normalized = value === undefined || value === null ? '' : String(value)

if ('children' in node) {
node.children = []

if (value) {
node.children.push({type: 'text', value: normalized})
}
} else if (node.type !== 'doctype') {
node.value = normalized
}

return node
}
3 changes: 2 additions & 1 deletion packages/hast-util-from-string/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
"index.js",
"lib/"
],
"dependencies": {
"@types/hast": "^3.0.0"
Expand Down
56 changes: 36 additions & 20 deletions packages/hast-util-from-string/test.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,56 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import {u} from 'unist-builder'
import {fromString} from './index.js'

test('hast-util-from-string', async function (t) {
await t.test('should set text nodes', async function () {
assert.deepEqual(
// @ts-expect-error: `value` missing.
fromString(u('text'), 'foo'),
u('text', 'foo')
)
})

await t.test('should reset text nodes (1)', async function () {
assert.deepEqual(
// @ts-expect-error: `value` missing.
fromString(u('text')),
u('text', '')
)
assert.deepEqual(fromString({type: 'text', value: 'alpha'}, 'bravo'), {
type: 'text',
value: 'bravo'
})
})

await t.test('should reset text nodes (2)', async function () {
assert.deepEqual(fromString(u('text', 'foo')), u('text', ''))
await t.test('should reset text nodes', async function () {
assert.deepEqual(fromString({type: 'text', value: 'alpha'}), {
type: 'text',
value: ''
})
})

await t.test('should set parent nodes', async function () {
assert.deepEqual(
fromString(u('element', {tagName: 'p', properties: {}}, []), 'foo'),
u('element', {tagName: 'p', properties: {}}, [u('text', 'foo')])
fromString(
{
type: 'element',
tagName: 'p',
properties: {},
children: [{type: 'text', value: 'alpha'}]
},
'bravo'
),
{
type: 'element',
tagName: 'p',
properties: {},
children: [{type: 'text', value: 'bravo'}]
}
)
})

await t.test('should reset parent nodes', async function () {
assert.deepEqual(
fromString(u('element', {tagName: 'p', properties: {}}, [])),
u('element', {tagName: 'p', properties: {}}, [])
fromString({
type: 'element',
tagName: 'p',
properties: {},
children: [{type: 'text', value: 'alpha'}]
}),
{
type: 'element',
tagName: 'p',
properties: {},
children: []
}
)
})
})
41 changes: 1 addition & 40 deletions packages/hast-util-is-body-ok-link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,43 +41,4 @@
* Whether a node is a “body OK” link (`boolean`).
*/

/**
* @typedef {import('hast').Root} Root
* @typedef {Root|Root['children'][number]} Node
*/

import {isElement} from 'hast-util-is-element'
import {hasProperty} from 'hast-util-has-property'

const list = new Set(['pingback', 'prefetch', 'stylesheet'])

/**
* Checks whether a node is a “body OK” link.
*
* @param {Node} node
* @returns {boolean}
*/
export function isBodyOkLink(node) {
if (!isElement(node, 'link')) {
return false
}

if (hasProperty(node, 'itemProp')) {
return true
}

const rel = node.properties.rel || []
let index = -1

if (!Array.isArray(rel) || rel.length === 0) {
return false
}

while (++index < rel.length) {
if (!list.has(String(rel[index]))) {
return false
}
}

return true
}
export {isBodyOkLink} from './lib/index.js'
38 changes: 38 additions & 0 deletions packages/hast-util-is-body-ok-link/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @typedef {import('hast').Nodes} Nodes
*/

const list = new Set(['pingback', 'prefetch', 'stylesheet'])

/**
* Checks whether a node is a “body OK” link.
*
* @param {Nodes} node
* Node to check.
* @returns {boolean}
* Whether `node` is a “body OK” link.
*/
export function isBodyOkLink(node) {
if (node.type !== 'element' || node.tagName !== 'link') {
return false
}

if (node.properties.itemProp) {
return true
}

const rel = node.properties.rel
let index = -1

if (!Array.isArray(rel) || rel.length === 0) {
return false
}

while (++index < rel.length) {
if (!list.has(String(rel[index]))) {
return false
}
}

return true
}
7 changes: 3 additions & 4 deletions packages/hast-util-is-body-ok-link/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
"index.js",
"lib/"
],
"dependencies": {
"@types/hast": "^3.0.0",
"hast-util-has-property": "^3.0.0",
"hast-util-is-element": "^3.0.0"
"@types/hast": "^3.0.0"
},
"scripts": {},
"typeCoverage": {
Expand Down
13 changes: 1 addition & 12 deletions packages/hast-util-is-body-ok-link/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import {u} from 'unist-builder'
import {h} from 'hastscript'
import {isBodyOkLink} from './index.js'

Expand Down Expand Up @@ -65,21 +64,11 @@ test('isBodyOkLink', async function (t) {
}
)

await t.test(
'should be no for `link`s without `rel` or `itemProp` (2)',
async function () {
assert.equal(
isBodyOkLink(u('element', {tagName: 'link', properties: {}}, [])),
false
)
}
)

await t.test('should be no for non-links', async function () {
assert.equal(isBodyOkLink(h('p')), false)
})

await t.test('should be no for non-elements', async function () {
assert.equal(isBodyOkLink(u('text', 'foo')), false)
assert.equal(isBodyOkLink({type: 'text', value: 'foo'}), false)
})
})
24 changes: 4 additions & 20 deletions packages/hast-util-is-conditional-comment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
* ## Use
*
* ```js
* import {u} from 'unist-builder'
* import {isConditionalComment} from 'hast-util-is-conditional-comment'
*
* isConditionalComment(u('comment', '[if IE]>...<![endif]')) //=> true
* isConditionalComment(u('comment', '<![endif]')) //=> true
* isConditionalComment(u('comment', 'foo')) //=> false
* isConditionalComment(u({type: 'comment', value: '[if IE]>...<![endif]'})) //=> true
* isConditionalComment(u({type: 'comment', value: '<![endif]'})) //=> true
* isConditionalComment(u({type: 'comment', value: 'foo'})) //=> false
* ```
*
* ## API
Expand All @@ -41,19 +40,4 @@
* Whether a node is a conditional comment (`boolean`).
*/

const re = /^\[if[ \t\f\n\r]+[^\]]+]|<!\[endif]$/

/**
* @typedef {import('hast').Root} Root
* @typedef {Root|Root['children'][number]} Node
*/

/**
* Check if a node is a conditional comment.
*
* @param {Node} node
* @returns {boolean}
*/
export function isConditionalComment(node) {
return node && node.type === 'comment' && re.test(node.value)
}
export {isConditionalComment} from './lib/index.js'
17 changes: 17 additions & 0 deletions packages/hast-util-is-conditional-comment/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const re = /^\[if[ \t\f\n\r]+[^\]]+]|<!\[endif]$/

/**
* @typedef {import('hast').Nodes} Nodes
*/

/**
* Check if a node is a conditional comment.
*
* @param {Nodes} node
* Node to check.
* @returns {boolean}
* Whether `node` is a conditional comment.
*/
export function isConditionalComment(node) {
return node.type === 'comment' && re.test(node.value)
}
3 changes: 2 additions & 1 deletion packages/hast-util-is-conditional-comment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
"index.js",
"lib/"
],
"dependencies": {
"@types/hast": "^3.0.0"
Expand Down
7 changes: 3 additions & 4 deletions packages/hast-util-is-conditional-comment/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,11 @@ In browsers with [`esm.sh`][esm-sh]:
## Use

```js
import {u} from 'unist-builder'
import {isConditionalComment} from 'hast-util-is-conditional-comment'

isConditionalComment(u('comment', '[if IE]>...<![endif]')) //=> true
isConditionalComment(u('comment', '<![endif]')) //=> true
isConditionalComment(u('comment', 'foo')) //=> false
isConditionalComment(u({type: 'comment', value: '[if IE]>...<![endif]'})) //=> true
isConditionalComment(u({type: 'comment', value: '<![endif]'})) //=> true
isConditionalComment(u({type: 'comment', value: 'foo'})) //=> false
```

## API
Expand Down
Loading

0 comments on commit d831bc7

Please sign in to comment.