From 9e988eaac5365286b3bd7dbbe5ca6293828299bd Mon Sep 17 00:00:00 2001 From: Lexus Drumgold Date: Tue, 12 Mar 2024 20:06:28 -0400 Subject: [PATCH] feat: `Children` Signed-off-by: Lexus Drumgold --- .eslintrc.base.cjs | 70 ++++++++++++++++++++++++++++---- package.json | 3 ++ src/__tests__/children.spec-d.ts | 22 ++++++++++ src/children.ts | 28 +++++++++++++ src/index.ts | 1 + 5 files changed, 117 insertions(+), 7 deletions(-) create mode 100644 src/__tests__/children.spec-d.ts create mode 100644 src/children.ts diff --git a/.eslintrc.base.cjs b/.eslintrc.base.cjs index 4cfe0c0d..1ebaac9c 100644 --- a/.eslintrc.base.cjs +++ b/.eslintrc.base.cjs @@ -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, @@ -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', diff --git a/package.json b/package.json index f84ac6b4..d045ca78 100644 --- a/package.json +++ b/package.json @@ -129,6 +129,9 @@ "resolutions": { "@types/unist": "3.0.2" }, + "engines": { + "node": ">=18.18.2" + }, "packageManager": "yarn@4.1.0", "sideEffects": false } diff --git a/src/__tests__/children.spec-d.ts b/src/__tests__/children.spec-d.ts new file mode 100644 index 00000000..5f569fa4 --- /dev/null +++ b/src/__tests__/children.spec-d.ts @@ -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>().toEqualTypeOf() + }) + + it('should equal never if T does not extend Parent', () => { + expectTypeOf>().toEqualTypeOf() + }) +}) diff --git a/src/children.ts b/src/children.ts new file mode 100644 index 00000000..5cfce665 --- /dev/null +++ b/src/children.ts @@ -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 // Node[] + * @example + * type X = Children // never + * @example + * type X = Children // never + * + * @template {Node} T - Tree to try extracting children from + */ +type Children = T extends Parent ? T['children'] : never + +export type { Children as default } diff --git a/src/index.ts b/src/index.ts index a7ba5feb..ee7b42d7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'