Skip to content

Commit

Permalink
refactor: unist-util-types integration
Browse files Browse the repository at this point in the history
  • Loading branch information
unicornware committed Mar 13, 2024
1 parent 0c95017 commit 80a80bd
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 123 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ permissions:
packages: read
env:
CACHE_PATH: node_modules
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GH_REGISTRY_TOKEN }}
HUSKY: 0
REF: ${{ github.head_ref || github.ref }}
REF_NAME: ${{ github.head_ref || github.ref_name }}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ extended to support other JavaScript-based languages, like [TypeScript][typescri
TypeScript users can integrate `esast` type definitions into their project by installing the appropriate packages:

```sh
yarn add @flex-development/esast @flex-development/docast @types/mdast @types/unist
yarn add @flex-development/esast @flex-development/docast @flex-development/unist-util-types @types/mdast @types/unist
```

<blockquote>
Expand Down
45 changes: 0 additions & 45 deletions __tests__/types/any-node-helper.ts

This file was deleted.

1 change: 0 additions & 1 deletion __tests__/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
* @module tests/types
*/

export type { default as AnyNodeHelper } from './any-node-helper'
export type { default as NodeObject } from './node-object'
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"@flex-development/mkbuild": "1.0.0-alpha.23",
"@flex-development/mlly": "1.0.0-alpha.18",
"@flex-development/pathe": "2.0.0",
"@flex-development/unist-util-types": "1.0.0",
"@types/eslint": "8.56.5",
"@types/estree": "1.0.5",
"@types/estree-jsx": "1.0.5",
Expand Down Expand Up @@ -134,6 +135,7 @@
},
"peerDependencies": {
"@flex-development/docast": ">=1.0.0-alpha.14",
"@flex-development/unist-util-types": ">=1.0.0",
"@types/mdast": ">=4.0.3",
"@types/unist": ">=3.0.2"
},
Expand Down
45 changes: 4 additions & 41 deletions src/content/__tests__/node.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
* @module esast/content/tests/unit-d/node
*/

import type { AnyNodeHelper } from '#tests/types'
import * as docast from '@flex-development/docast'
import type {
CatchClause,
ClassBody,
Expand All @@ -17,7 +15,6 @@ import type {
MethodDefinition,
Nothing,
ParameterList,
Parent,
PatternMap,
Property,
PropertyDefinition,
Expand All @@ -30,30 +27,10 @@ import type {
TemplateElement,
VariableDeclarator
} from '@flex-development/esast'
import type { InclusiveDescendant } from '@flex-development/unist-util-types'
import type * as TestSubject from '../node'

describe('unit-d:content/node', () => {
describe('AnyNode', () => {
it('should equal NodeMap[keyof NodeMap]', () => {
expectTypeOf<TestSubject.AnyNode>()
.toEqualTypeOf<TestSubject.NodeMap[keyof TestSubject.NodeMap]>
})
})

describe('AnyParent', () => {
it('should equal Extract<AnyNode, Parent>', () => {
expectTypeOf<TestSubject.AnyParent>()
.toEqualTypeOf<Extract<TestSubject.AnyNode, Parent>>
})
})

describe('Child', () => {
it('should equal Exclude<AnyNode, { type: "root" }>', () => {
expectTypeOf<TestSubject.Child>()
.toEqualTypeOf<Exclude<TestSubject.AnyNode, { type: 'root' }>>
})
})

describe('NodeMap', () => {
it('should extend HeritageMap', () => {
expectTypeOf<TestSubject.NodeMap>().toMatchTypeOf<HeritageMap>()
Expand All @@ -71,21 +48,6 @@ describe('unit-d:content/node', () => {
expectTypeOf<TestSubject.NodeMap>().toMatchTypeOf<StatementMap>()
})

it('should extend docast.BlockTagContentMap', () => {
expectTypeOf<TestSubject.NodeMap>()
.toMatchTypeOf<docast.BlockTagContentMap>()
})

it('should extend docast.BlockTagContentMap', () => {
expectTypeOf<TestSubject.NodeMap>()
.toMatchTypeOf<docast.BlockTagContentMap>()
})

it('should extend docast.FlowContentMap', () => {
expectTypeOf<TestSubject.NodeMap>()
.toMatchTypeOf<docast.FlowContentMap>()
})

it('should match [catchClause: CatchClause]', () => {
expectTypeOf<TestSubject.NodeMap>()
.toHaveProperty('catchClause')
Expand Down Expand Up @@ -196,10 +158,11 @@ describe('unit-d:content/node', () => {

it('should register all nodes', () => {
// Arrange
type Registry = TestSubject.NodeMap[keyof TestSubject.NodeMap]
type Nodes = InclusiveDescendant<Root>
type Subject = TestSubject.NodeMap[keyof TestSubject.NodeMap]

// Expect
expectTypeOf<Exclude<AnyNodeHelper, Registry>>().toEqualTypeOf<never>()
expectTypeOf<Exclude<Subject, Nodes>>().toEqualTypeOf<never>()
})
})
})
36 changes: 2 additions & 34 deletions src/content/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* @module esast/content/node
*/

import * as docast from '@flex-development/docast'
import type {
CatchClause,
ClassBody,
Expand All @@ -16,7 +15,6 @@ import type {
MethodDefinition,
Nothing,
ParameterList,
Parent,
PatternMap,
Property,
PropertyDefinition,
Expand All @@ -30,30 +28,6 @@ import type {
VariableDeclarator
} from '@flex-development/esast'

/**
* Union of registered esast nodes.
*
* To register custom esast nodes, augment {@linkcode NodeMap}. They will be
* added to this union automatically.
*/
type AnyNode = NodeMap[keyof NodeMap]

/**
* Union of registered esast parent nodes.
*
* To register custom esast parent nodes, augment {@linkcode NodeMap}. They will
* be added to this union automatically.
*/
type AnyParent = Extract<AnyNode, Parent>

/**
* Union of registered esast child nodes.
*
* To register custom esast child nodes, augment {@linkcode NodeMap}. They will
* be added to this union automatically.
*/
type Child = Exclude<AnyNode, { type: 'root' }>

/**
* Registry of esast nodes.
*
Expand All @@ -70,14 +44,8 @@ type Child = Exclude<AnyNode, { type: 'root' }>
* @extends {PatternMap}
* @extends {RootMap}
* @extends {StatementMap}
* @extends {docast.BlockTagContentMap}
* @extends {docast.DescriptionContentMap}
* @extends {docast.FlowContentMap}
*/
interface NodeMap
extends HeritageMap, PatternMap, RootMap, StatementMap,
docast.BlockTagContentMap, docast.DescriptionContentMap,
docast.FlowContentMap {
interface NodeMap extends HeritageMap, PatternMap, RootMap, StatementMap {
catchClause: CatchClause
classBody: ClassBody
classHeritage: ClassHeritage
Expand All @@ -98,4 +66,4 @@ interface NodeMap
variableDeclarator: VariableDeclarator
}

export type { AnyNode, AnyParent, Child, NodeMap }
export type { NodeMap }
14 changes: 14 additions & 0 deletions src/types/__tests__/any-node.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @file Type Tests - AnyNode
* @module esast/types/tests/unit-d/AnyNode
*/

import type { Root } from '@flex-development/esast'
import type { InclusiveDescendant } from '@flex-development/unist-util-types'
import type TestSubject from '../any-node'

describe('unit-d:types/AnyNode', () => {
it('should equal InclusiveDescendant<Root>', () => {
expectTypeOf<TestSubject>().toEqualTypeOf<InclusiveDescendant<Root>>()
})
})
14 changes: 14 additions & 0 deletions src/types/__tests__/any-parent.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @file Type Tests - AnyParent
* @module esast/types/tests/unit-d/AnyParent
*/

import type { Root } from '@flex-development/esast'
import type { Parents } from '@flex-development/unist-util-types'
import type TestSubject from '../any-parent'

describe('unit-d:types/AnyParent', () => {
it('should equal Parents<Root>', () => {
expectTypeOf<TestSubject>().toEqualTypeOf<Parents<Root>>()
})
})
14 changes: 14 additions & 0 deletions src/types/__tests__/child.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @file Type Tests - Child
* @module esast/types/tests/unit-d/Child
*/

import type { AnyParent } from '@flex-development/esast'
import type { Children } from '@flex-development/unist-util-types'
import type TestSubject from '../child'

describe('unit-d:types/Child', () => {
it('should equal Children<AnyParent>[number]', () => {
expectTypeOf<TestSubject>().toEqualTypeOf<Children<AnyParent>[number]>()
})
})
14 changes: 14 additions & 0 deletions src/types/any-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @file Type Definitions - AnyNode
* @module esast/types/AnyNode
*/

import type { Root } from '@flex-development/esast'
import type { InclusiveDescendant } from '@flex-development/unist-util-types'

/**
* Union of nodes that can occur in esast.
*/
type AnyNode = InclusiveDescendant<Root>

export type { AnyNode as default }
18 changes: 18 additions & 0 deletions src/types/any-parent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @file Type Definitions - AnyParent
* @module esast/types/AnyParent
*/

import type { Root } from '@flex-development/esast'
import type { Parents } from '@flex-development/unist-util-types'

/**
* Union of [*parents*][1] that are [*inclusive descendants*][2] of
* {@linkcode Root}.
*
* [1]: https://github.com/syntax-tree/unist#parent
* [2]: https://github.com/syntax-tree/unist#descendant
*/
type AnyParent = Parents<Root>

export type { AnyParent as default }
18 changes: 18 additions & 0 deletions src/types/child.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @file Type Definitions - Child
* @module esast/types/Child
*/

import type { AnyParent } from '@flex-development/esast'
import type { Children } from '@flex-development/unist-util-types'

/**
* Union of [*child*][1] nodes that are [*inclusive descendants*][2] of
* {@linkcode Root}.
*
* [1]: https://github.com/syntax-tree/unist#child
* [2]: https://github.com/syntax-tree/unist#descendant
*/
type Child = Children<AnyParent>[number]

export type { Child as default }
3 changes: 3 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

export type { default as AnyClass } from './any-class'
export type { default as AnyFunction } from './any-function'
export type { default as AnyNode } from './any-node'
export type { default as AnyParent } from './any-parent'
export type { default as Child } from './child'
export type { default as EcmaVersion } from './ecma-version'
export type { default as CommentKind } from './kind-comment'
export type { default as ImportSpecifierKind } from './kind-import-specifier'
Expand Down
11 changes: 11 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,7 @@ __metadata:
"@flex-development/mlly": "npm:1.0.0-alpha.18"
"@flex-development/pathe": "npm:2.0.0"
"@flex-development/tutils": "npm:6.0.0-alpha.25"
"@flex-development/unist-util-types": "npm:1.0.0"
"@types/eslint": "npm:8.56.5"
"@types/estree": "npm:1.0.5"
"@types/estree-jsx": "npm:1.0.5"
Expand Down Expand Up @@ -1513,6 +1514,7 @@ __metadata:
yaml-eslint-parser: "npm:1.2.2"
peerDependencies:
"@flex-development/docast": ">=1.0.0-alpha.14"
"@flex-development/unist-util-types": ">=1.0.0"
"@types/mdast": ">=4.0.3"
"@types/unist": ">=3.0.2"
languageName: unknown
Expand Down Expand Up @@ -1848,6 +1850,15 @@ __metadata:
languageName: node
linkType: hard

"@flex-development/unist-util-types@npm:1.0.0":
version: 1.0.0
resolution: "@flex-development/unist-util-types@npm:1.0.0::__archiveUrl=https%3A%2F%2Fnpm.pkg.github.com%2Fdownload%2F%40flex-development%2Funist-util-types%2F1.0.0%2Ff66f57aa436e3ab5d054e6a4190b597bb4632f35"
peerDependencies:
"@types/unist": ">=3.0.2"
checksum: 10/c714ef87b53eed2a42fa0bb94abf5be41234e96081d0cfeeb9eb09fcedccad0019d525365533f1e060ce26b9836e02e4d025a845afdf8ddb7d2f3dbe847bb480
languageName: node
linkType: hard

"@gar/promisify@npm:^1.1.3":
version: 1.1.3
resolution: "@gar/promisify@npm:1.1.3"
Expand Down

0 comments on commit 80a80bd

Please sign in to comment.