From 17ab0b95f069070323a767143374e683d2a08f8f Mon Sep 17 00:00:00 2001 From: Lexus Drumgold Date: Thu, 7 Mar 2024 21:50:38 -0500 Subject: [PATCH] feat(nodes): `IfStatement` Signed-off-by: Lexus Drumgold --- src/content/__tests__/statement.spec-d.ts | 8 +++- src/content/statement.ts | 4 +- src/nodes/__tests__/statement-if.spec-d.ts | 48 +++++++++++++++++++ src/nodes/index.ts | 1 + src/nodes/statement-if.ts | 54 ++++++++++++++++++++++ 5 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 src/nodes/__tests__/statement-if.spec-d.ts create mode 100644 src/nodes/statement-if.ts diff --git a/src/content/__tests__/statement.spec-d.ts b/src/content/__tests__/statement.spec-d.ts index e4a5021..3e3a068 100644 --- a/src/content/__tests__/statement.spec-d.ts +++ b/src/content/__tests__/statement.spec-d.ts @@ -11,7 +11,8 @@ import type { DebuggerStatement, DoWhileStatement, EmptyStatement, - ExpressionStatement + ExpressionStatement, + IfStatement } from '@flex-development/esast' import type * as TestSubject from '../statement' @@ -58,5 +59,10 @@ describe('unit-d:content/statement', () => { expectTypeOf() .toMatchTypeOf>() }) + + it('should match NodeObject', () => { + expectTypeOf() + .toMatchTypeOf>() + }) }) }) diff --git a/src/content/statement.ts b/src/content/statement.ts index 3798eda..f0ebf8a 100644 --- a/src/content/statement.ts +++ b/src/content/statement.ts @@ -10,7 +10,8 @@ import type { DebuggerStatement, DoWhileStatement, EmptyStatement, - ExpressionStatement + ExpressionStatement, + IfStatement } from '@flex-development/esast' /** @@ -41,6 +42,7 @@ interface StatementMap { doWhileStatement: DoWhileStatement emptyStatement: EmptyStatement expressionStatement: ExpressionStatement + ifStatement: IfStatement } export type { Statement, StatementMap } diff --git a/src/nodes/__tests__/statement-if.spec-d.ts b/src/nodes/__tests__/statement-if.spec-d.ts new file mode 100644 index 0000000..c5537dd --- /dev/null +++ b/src/nodes/__tests__/statement-if.spec-d.ts @@ -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().toMatchTypeOf() + }) + + it('should match [children: [Expression, Statement, Statement] | [Expression, Statement]]', () => { + // Arrange + type Expect = [Expression, Statement, Statement] | [Expression, Statement] + + // Expect + expectTypeOf().toHaveProperty('children').toEqualTypeOf() + }) + + it('should match [data?: Optional]', () => { + expectTypeOf() + .toHaveProperty('data') + .toEqualTypeOf>() + }) + + it('should match [type: "ifStatement"]', () => { + expectTypeOf() + .toHaveProperty('type') + .toEqualTypeOf<'ifStatement'>() + }) + + describe('IfStatementData', () => { + it('should extend Data', () => { + expectTypeOf().toMatchTypeOf() + }) + }) +}) diff --git a/src/nodes/index.ts b/src/nodes/index.ts index 63d314c..dd27042 100644 --- a/src/nodes/index.ts +++ b/src/nodes/index.ts @@ -60,3 +60,4 @@ export type { default as ExpressionStatement, ExpressionStatementData } from './statement-expression' +export type { default as IfStatement, IfStatementData } from './statement-if' diff --git a/src/nodes/statement-if.ts b/src/nodes/statement-if.ts new file mode 100644 index 0000000..0f16704 --- /dev/null +++ b/src/nodes/statement-if.ts @@ -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 + + /** + * Node type. + */ + type: 'ifStatement' +} + +export type { IfStatementData, IfStatement as default }