Skip to content

Commit

Permalink
feat(nodes): BlockStatement
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed Mar 7, 2024
1 parent d49b280 commit ea01ab1
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 5 deletions.
6 changes: 6 additions & 0 deletions __tests__/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @file Entry Point - Test Types
* @module tests/types
*/

export type { default as NodeObject } from './node-object'
15 changes: 15 additions & 0 deletions __tests__/types/node-object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @file Test Types - NodeObject
* @module tests/types/NodeObject
*/

import type { Node } from '@flex-development/esast'

/**
* Create an object mapping a node type to a node.
*
* @template {Node} T - Node to map
*/
type NodeObject<T extends Node> = { [K in T['type']]: T }

export type { NodeObject as default }
13 changes: 9 additions & 4 deletions src/content/__tests__/statement.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* @module esast/content/tests/unit-d/statement
*/

import type { EmptyStatement } from '@flex-development/esast'
import type { NodeObject } from '#tests/types'
import type { BlockStatement, EmptyStatement } from '@flex-development/esast'
import type * as TestSubject from '../statement'

describe('unit-d:content/statement', () => {
Expand All @@ -15,10 +16,14 @@ describe('unit-d:content/statement', () => {
})

describe('StatementMap', () => {
it('should match [emptyStatement: EmptyStatement]', () => {
it('should match NodeObject<BlockStatement>', () => {
expectTypeOf<TestSubject.StatementMap>()
.toHaveProperty('emptyStatement')
.toEqualTypeOf<EmptyStatement>
.toMatchTypeOf<NodeObject<BlockStatement>>()
})

it('should match NodeObject<EmptyStatement>', () => {
expectTypeOf<TestSubject.StatementMap>()
.toMatchTypeOf<NodeObject<EmptyStatement>>()
})
})
})
3 changes: 2 additions & 1 deletion src/content/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @module esast/content/statement
*/

import type { EmptyStatement } from '@flex-development/esast'
import type { BlockStatement, EmptyStatement } from '@flex-development/esast'

/**
* Union of registered esast nodes that can occur where a statement is expected.
Expand All @@ -26,6 +26,7 @@ type Statement = StatementMap[keyof StatementMap]
* }
*/
interface StatementMap {
blockStatement: BlockStatement
emptyStatement: EmptyStatement
}

Expand Down
47 changes: 47 additions & 0 deletions src/nodes/__tests__/statement-block.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @file Type Tests - BlockStatement
* @module esast/nodes/tests/unit-d/BlockStatement
*/

import type { Data, Parent, Statement } from '@flex-development/esast'
import type { Nilable, Optional } from '@flex-development/tutils'
import type * as TestSubject from '../statement-block'

describe('unit-d:nodes/BlockStatement', () => {
type Subject = TestSubject.default
type SubjectData = TestSubject.BlockStatementData

it('should extend Parent', () => {
expectTypeOf<Subject>().toMatchTypeOf<Parent>()
})

it('should match [children: Statement[]]', () => {
expectTypeOf<Subject>()
.toHaveProperty('children')
.toEqualTypeOf<Statement[]>()
})

it('should match [data?: Optional<BlockStatementData>]', () => {
expectTypeOf<Subject>()
.toHaveProperty('data')
.toEqualTypeOf<Optional<SubjectData>>()
})

it('should match [type: "blockStatement"]', () => {
expectTypeOf<Subject>()
.toHaveProperty('type')
.toEqualTypeOf<'blockStatement'>()
})

describe('BlockStatementData', () => {
it('should extend Data', () => {
expectTypeOf<SubjectData>().toMatchTypeOf<Data>()
})

it('should match [static?: Nilable<boolean>]', () => {
expectTypeOf<SubjectData>()
.toHaveProperty('static')
.toEqualTypeOf<Nilable<boolean>>()
})
})
})
4 changes: 4 additions & 0 deletions src/nodes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export type { default as StringLiteral } from './literal-string'
export type { default as UndefinedLiteral } from './literal-undefined'
export type { default as Node } from './node'
export type { default as Parent } from './parent'
export type {
default as BlockStatement,
BlockStatementData
} from './statement-block'
export type {
default as EmptyStatement,
EmptyStatementData
Expand Down
55 changes: 55 additions & 0 deletions src/nodes/statement-block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @file Nodes - BlockStatement
* @module esast/nodes/BlockStatement
*/

import type { Data, Parent, Statement } from '@flex-development/esast'
import type { Nilable, Optional } from '@flex-development/tutils'

/**
* Info associated with block statements.
*
* @see {@linkcode Data}
*
* @extends {Data}
*/
interface BlockStatementData extends Data {
/**
* Static block statement?
*/
static?: Nilable<boolean>
}

/**
* A block statement.
*
* Block statements group zero (`0`) or more statements. The block is delimited
* by a pair of braces ("curly braces") and contains a list of zero (`0`) or
* more statements and declarations.
*
* @see {@linkcode Parent}
*
* @extends {Parent}
*/
interface BlockStatement extends Parent {
/**
* List of children.
*
* @see {@linkcode Statement}
*/
children: Statement[]

/**
* Info from the ecosystem.
*
* @see {@linkcode BlockStatementData}
*/
data?: Optional<BlockStatementData>

/**
* Node type.
*/
type: 'blockStatement'
}

export type { BlockStatementData, BlockStatement as default }
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"outDir": "dist",
"paths": {
"#src/*": ["src/*"],
"#tests/*": ["__tests__/*"],
"@flex-development/esast": ["src/index"]
},
"preserveConstEnums": true,
Expand Down

0 comments on commit ea01ab1

Please sign in to comment.