Skip to content

Commit

Permalink
feat: Children
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Mar 13, 2024
1 parent 4ce8664 commit 9e988ea
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 7 deletions.
70 changes: 63 additions & 7 deletions .eslintrc.base.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -619,13 +619,7 @@ const config = {
'node/no-unpublished-require': 0,
'node/no-unsupported-features/es-builtins': 2,
'node/no-unsupported-features/es-syntax': 0,
'node/no-unsupported-features/node-builtins': [
2,
{
version: require('./package.json').engines?.node ??
'>=' + fs.readFileSync('.nvmrc', 'utf8')
}
],
'node/no-unsupported-features/node-builtins': 2,
'node/prefer-global/buffer': 2,
'node/prefer-global/console': 2,
'node/prefer-global/process': 2,
Expand Down Expand Up @@ -918,6 +912,68 @@ const config = {
'@typescript-eslint/explicit-member-accessibility': 0
}
},
{
files: ['**/*.html'],
parser: '@html-eslint/parser',
plugins: ['@html-eslint', 'html'],
rules: {
'@html-eslint/element-newline': 2,
'@html-eslint/id-naming-convention': 2,
'@html-eslint/indent': [2, 2],
'@html-eslint/lowercase': 2,
'@html-eslint/no-abstract-roles': 2,
'@html-eslint/no-accesskey-attrs': 2,
'@html-eslint/no-aria-hidden-body': 2,
'@html-eslint/no-duplicate-attrs': 2,
'@html-eslint/no-duplicate-id': 2,
'@html-eslint/no-extra-spacing-attrs': 2,
'@html-eslint/no-inline-styles': 2,
'@html-eslint/no-multiple-empty-lines': 2,
'@html-eslint/no-multiple-h1': 2,
'@html-eslint/no-non-scalable-viewport': 2,
'@html-eslint/no-obsolete-tags': 2,
'@html-eslint/no-positive-tabindex': 2,
'@html-eslint/no-restricted-attr-values': [
2,
{
attrPatterns: [],
attrValuePatterns: []
}
],
'@html-eslint/no-restricted-attrs': [
2,
{
attrPatterns: [],
tagPatterns: []
}
],
'@html-eslint/no-script-style-type': 2,
'@html-eslint/no-skip-heading-levels': 2,
'@html-eslint/no-target-blank': 2,
'@html-eslint/no-trailing-spaces': 2,
'@html-eslint/quotes': 2,
'@html-eslint/require-attrs': [2],
'@html-eslint/require-button-type': 2,
'@html-eslint/require-closing-tags': [2, { selfClosing: 'always' }],
'@html-eslint/require-doctype': 2,
'@html-eslint/require-frame-title': 2,
'@html-eslint/require-img-alt': 2,
'@html-eslint/require-lang': 2,
'@html-eslint/require-li-container': 2,
'@html-eslint/require-meta-charset': 2,
'@html-eslint/require-meta-description': 2,
'@html-eslint/require-meta-viewport': 2,
'@html-eslint/require-open-graph-protocol': 0,
'@html-eslint/require-title': 2,
'@html-eslint/sort-attrs': 2
},
settings: {
html: {
extensions: ['.html'],
indent: '+2'
}
}
},
{
files: '**/*.+(json|json5|jsonc)',
parser: 'jsonc-eslint-parser',
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@
"resolutions": {
"@types/unist": "3.0.2"
},
"engines": {
"node": ">=18.18.2"
},
"packageManager": "[email protected]",
"sideEffects": false
}
22 changes: 22 additions & 0 deletions src/__tests__/children.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @file Type Tests - Children
* @module unist-util-types/tests/unit-d/Children
*/

import type * as docast from '@flex-development/docast'
import type * as mdast from 'mdast'
import type TestSubject from '../children'

describe('unit-d:Children', () => {
it('should equal T["children"] if T extends Parent', () => {
// Arrange
type T = docast.Comment

// Expect
expectTypeOf<TestSubject<T>>().toEqualTypeOf<T['children']>()
})

it('should equal never if T does not extend Parent', () => {
expectTypeOf<TestSubject<mdast.InlineCode>>().toEqualTypeOf<never>()
})
})
28 changes: 28 additions & 0 deletions src/children.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @file Children
* @module unist-util-types/Children
*/

import type { Node, Parent } from 'unist'

/**
* Extract [*children*][1] from [*tree*][2] `T`.
*
* [1]: https://github.com/syntax-tree/unist#child
* [2]: https://github.com/syntax-tree/unist#tree
*
* @see {@linkcode Node}
* @see {@linkcode Parent}
*
* @example
* type X = Children<Parent> // Node[]
* @example
* type X = Children<Node> // never
* @example
* type X = Children<Literal> // never
*
* @template {Node} T - Tree to try extracting children from
*/
type Children<T extends Node> = T extends Parent ? T['children'] : never

export type { Children as default }
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @module unist-util-types
*/

export type { default as Children } from './children'
export type { default as Decrement } from './decrement'
export type { default as InclusiveDescendant } from './descendant-inclusive'
export type { default as Increment } from './increment'
Expand Down

0 comments on commit 9e988ea

Please sign in to comment.