Skip to content

Commit

Permalink
feat(nodes): IfStatement
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 8, 2024
1 parent b832ad1 commit 17ab0b9
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/content/__tests__/statement.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import type {
DebuggerStatement,
DoWhileStatement,
EmptyStatement,
ExpressionStatement
ExpressionStatement,
IfStatement
} from '@flex-development/esast'
import type * as TestSubject from '../statement'

Expand Down Expand Up @@ -58,5 +59,10 @@ describe('unit-d:content/statement', () => {
expectTypeOf<TestSubject.StatementMap>()
.toMatchTypeOf<NodeObject<ExpressionStatement>>()
})

it('should match NodeObject<IfStatement>', () => {
expectTypeOf<TestSubject.StatementMap>()
.toMatchTypeOf<NodeObject<IfStatement>>()
})
})
})
4 changes: 3 additions & 1 deletion src/content/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import type {
DebuggerStatement,
DoWhileStatement,
EmptyStatement,
ExpressionStatement
ExpressionStatement,
IfStatement
} from '@flex-development/esast'

/**
Expand Down Expand Up @@ -41,6 +42,7 @@ interface StatementMap {
doWhileStatement: DoWhileStatement
emptyStatement: EmptyStatement
expressionStatement: ExpressionStatement
ifStatement: IfStatement
}

export type { Statement, StatementMap }
48 changes: 48 additions & 0 deletions src/nodes/__tests__/statement-if.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @file Type Tests - IfStatement
* @module esast/nodes/tests/unit-d/IfStatement
*/

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

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

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

it('should match [children: [Expression, Statement, Statement] | [Expression, Statement]]', () => {
// Arrange
type Expect = [Expression, Statement, Statement] | [Expression, Statement]

// Expect
expectTypeOf<Subject>().toHaveProperty('children').toEqualTypeOf<Expect>()
})

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

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

describe('IfStatementData', () => {
it('should extend Data', () => {
expectTypeOf<SubjectData>().toMatchTypeOf<Data>()
})
})
})
1 change: 1 addition & 0 deletions src/nodes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ export type {
default as ExpressionStatement,
ExpressionStatementData
} from './statement-expression'
export type { default as IfStatement, IfStatementData } from './statement-if'
54 changes: 54 additions & 0 deletions src/nodes/statement-if.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @file Nodes - IfStatement
* @module esast/nodes/IfStatement
*/

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

/**
* Info associated with `if` statements.
*
* @see {@linkcode Data}
*
* @extends {Data}
*/
interface IfStatementData extends Data {}

/**
* An `if` statement.
*
* @see {@linkcode Parent}
*
* @extends {Parent}
*/
interface IfStatement extends Parent {
/**
* List of children.
*
* @see {@linkcode Expression}
* @see {@linkcode Statement}
*/
children:
| [test: Expression, consequent: Statement, alternate: Statement]
| [test: Expression, consequent: Statement]

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

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

export type { IfStatementData, IfStatement as default }

0 comments on commit 17ab0b9

Please sign in to comment.