Skip to content

Commit

Permalink
feat(content): Declaration, DeclarationMap
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 53190ff commit 310368d
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 2 deletions.
10 changes: 9 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@
*/
const config = {
extends: ['./.eslintrc.base.cjs'],
overrides: [...require('./.eslintrc.base.cjs').overrides],
overrides: [
...require('./.eslintrc.base.cjs').overrides,
{
files: ['src/content/declaration.ts'],
rules: {
'unicorn/no-keyword-prefix': 0
}
}
],
root: true
}

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

import type { NodeObject } from '#tests/types'
import type {
FunctionDeclaration,
VariableDeclaration
} from '@flex-development/esast'
import type * as TestSubject from '../declaration'

describe('unit-d:content/declaration', () => {
describe('Declaration', () => {
it('should equal DeclarationMap[keyof DeclarationMap]', () => {
// Arrange
type K = keyof TestSubject.DeclarationMap
type Expect = TestSubject.DeclarationMap[K]

// Expect
expectTypeOf<TestSubject.Declaration>().toEqualTypeOf<Expect>
})
})

describe('DeclarationMap', () => {
it('should match NodeObject<FunctionDeclaration>', () => {
expectTypeOf<TestSubject.DeclarationMap>()
.toMatchTypeOf<NodeObject<FunctionDeclaration>>()
})

it('should match NodeObject<VariableDeclaration>', () => {
expectTypeOf<TestSubject.DeclarationMap>()
.toMatchTypeOf<NodeObject<VariableDeclaration>>()
})
})
})
5 changes: 5 additions & 0 deletions src/content/__tests__/statement.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
BreakStatement,
ContinueStatement,
DebuggerStatement,
DeclarationMap,
DoWhileStatement,
EmptyStatement,
ExpressionStatement,
Expand All @@ -26,6 +27,10 @@ describe('unit-d:content/statement', () => {
})

describe('StatementMap', () => {
it('should extend DeclarationMap', () => {
expectTypeOf<TestSubject.StatementMap>().toMatchTypeOf<DeclarationMap>()
})

it('should match NodeObject<BlockStatement>', () => {
expectTypeOf<TestSubject.StatementMap>()
.toMatchTypeOf<NodeObject<BlockStatement>>()
Expand Down
37 changes: 37 additions & 0 deletions src/content/declaration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @file Content - declaration
* @module esast/content/declaration
*/

import type {
FunctionDeclaration,
VariableDeclaration
} from '@flex-development/esast'

/**
* Union of registered esast nodes that can occur where a non-module declaration
* is expected.
*
* To register custom esast nodes, augment {@linkcode DeclarationMap}.
* They will be added to this union automatically.
*/
type Declaration = DeclarationMap[keyof DeclarationMap]

/**
* Registry of nodes that can occur where a {@linkcode Declaration} is expected.
*
* This interface can be augmented to register custom node types.
*
* @example
* declare module '@flex-development/esast' {
* interface DeclarationMap {
* customDeclaration: CustomDeclaration
* }
* }
*/
interface DeclarationMap {
functionDeclaration: FunctionDeclaration
variableDeclaration: VariableDeclaration
}

export type { Declaration, DeclarationMap }
1 change: 1 addition & 0 deletions src/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @module esast/content
*/

export type * from './declaration'
export type * from './expression'
export type * from './literal'
export type * from './pattern'
Expand Down
5 changes: 4 additions & 1 deletion src/content/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
BreakStatement,
ContinueStatement,
DebuggerStatement,
DeclarationMap,
DoWhileStatement,
EmptyStatement,
ExpressionStatement,
Expand All @@ -34,8 +35,10 @@ type Statement = StatementMap[keyof StatementMap]
* customStatement: CustomStatement
* }
* }
*
* @extends {DeclarationMap}
*/
interface StatementMap {
interface StatementMap extends DeclarationMap {
blockStatement: BlockStatement
breakStatement: BreakStatement
continueStatement: ContinueStatement
Expand Down

0 comments on commit 310368d

Please sign in to comment.