Skip to content

Commit

Permalink
feat(nodes): InferType
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Apr 16, 2024
1 parent 8385019 commit 680e3be
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.base.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const config = {
parser: '@typescript-eslint/parser',
parserOptions: {
extraFileExtensions: [],
project: ['**/tsconfig.json', '**/tsconfig.*.json'],
project: ['./tsconfig.json', './tsconfig.typecheck.json'],
tsconfigRootDir: process.cwd(),
warnOnUnsupportedTypeScriptVersion: true
},
Expand Down
10 changes: 8 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@
"editor.bracketPairColorization.enabled": true,
"editor.bracketPairColorization.independentColorPoolPerBracketType": true,
"editor.codeActionsOnSave": {
"source.fixAll": "always",
"source.fixAll.eslint": "explicit",
"source.fixAll.sortJSON": "never",
"source.organizeImports": "always"
"source.organizeImports": "explicit"
},
"editor.defaultFormatter": "dprint.dprint",
"editor.detectIndentation": false,
Expand Down Expand Up @@ -137,6 +137,7 @@
"overrideConfigFile": ".eslintrc.cjs"
},
"eslint.runtime": "node",
"eslint.trace.server": "messages",
"eslint.validate": [
"css",
"github-actions-workflow",
Expand Down Expand Up @@ -309,6 +310,11 @@
"format": "svg",
"icon": "private"
},
{
"extensions": ["namespaces"],
"format": "svg",
"icon": "module"
},
{
"extensions": ["patches"],
"format": "svg",
Expand Down
6 changes: 6 additions & 0 deletions src/content/__tests__/expression-type.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
GenericType,
Identifier,
IndexedAccessType,
InferType,
IntersectionType,
MappedType,
ObjectLiteralType,
Expand Down Expand Up @@ -81,6 +82,11 @@ describe('unit-d:content/typeExpression', () => {
.toMatchTypeOf<NodeObject<IndexedAccessType>>()
})

it('should match NodeObject<InferType>', () => {
expectTypeOf<TestSubject.TypeExpressionMap>()
.toMatchTypeOf<NodeObject<InferType>>()
})

it('should match NodeObject<IntersectionType>', () => {
expectTypeOf<TestSubject.TypeExpressionMap>()
.toMatchTypeOf<NodeObject<IntersectionType>>()
Expand Down
2 changes: 2 additions & 0 deletions src/content/expression-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
GenericType,
Identifier,
IndexedAccessType,
InferType,
IntersectionType,
MappedType,
ObjectLiteralType,
Expand Down Expand Up @@ -58,6 +59,7 @@ interface TypeExpressionMap extends PrimitiveMap {
genericType: GenericType
identifier: Identifier
indexedAccessType: IndexedAccessType
inferType: InferType
intersectionType: IntersectionType
mappedType: MappedType
objectLiteralType: ObjectLiteralType
Expand Down
35 changes: 35 additions & 0 deletions src/nodes/__tests__/type-infer.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @file Type Tests - InferType
* @module esast/nodes/tests/unit-d/InferType
*/

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

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

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

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

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

describe('InferTypeData', () => {
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 @@ -415,6 +415,7 @@ export type {
default as IndexedAccessType,
IndexedAccessTypeData
} from './type-indexed-access'
export type { default as InferType, InferTypeData } from './type-infer'
export type {
default as IntersectionType,
IntersectionTypeData
Expand Down
63 changes: 63 additions & 0 deletions src/nodes/type-infer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* @file Nodes - InferType
* @module esast/nodes/InferType
*/

import type { InternalComments } from '#internal'
import type {
Comments,
Data,
ExtendsClause,
Identifier,
Parent
} from '@flex-development/esast'
import type { Optional } from '@flex-development/tutils'

/**
* Info associated with `infer` types.
*
* @see {@linkcode Data}
*
* @extends {Data}
*/
interface InferTypeData extends Data {}

/**
* An `infer` type.
*
* @see {@linkcode Parent}
* @see https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#inferring-within-conditional-types
*
* @extends {Parent}
*/
interface InferType extends Parent {
/**
* List of children.
*
* @see {@linkcode Comments}
* @see {@linkcode ExtendsClause}
* @see {@linkcode Identifier}
*/
children:
| [
...comments: Comments,
identifier: Identifier,
...comments: InternalComments,
extend: ExtendsClause
]
| [...comments: Comments, identifier: Identifier]

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

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

export type { InferTypeData, InferType as default }
4 changes: 0 additions & 4 deletions src/types/__tests__/operator-unary-type.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ describe('unit-d:types/UnaryTypeOperator', () => {
expectTypeOf<TestSubject>().extract<'asserts'>().not.toBeNever()
})

it('should extract "infer"', () => {
expectTypeOf<TestSubject>().extract<'infer'>().not.toBeNever()
})

it('should extract "keyof"', () => {
expectTypeOf<TestSubject>().extract<'keyof'>().not.toBeNever()
})
Expand Down
3 changes: 1 addition & 2 deletions src/types/operator-unary-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
*
* A unary operation is an operation with only one operand.
*
* @see https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#inferring-within-conditional-types
* @see https://www.typescriptlang.org/docs/handbook/2/keyof-types.html
* @see https://www.typescriptlang.org/docs/handbook/2/typeof-types.html
* @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions
*/
type UnaryTypeOperator = 'asserts' | 'infer' | 'keyof' | 'readonly' | 'typeof'
type UnaryTypeOperator = 'asserts' | 'keyof' | 'readonly' | 'typeof'

export type { UnaryTypeOperator as default }
8 changes: 4 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@
"__tests__/setup/index.d.mts"
],
"include": [
"**/**/*.json",
"**/**/*.mjs",
"**/**/*.mts",
"**/**/*.ts",
"**/*.json",
"**/*.mjs",
"**/*.mts",
"**/*.ts",
"**/.*.json",
"**/.*.mjs",
"**/.*.mts",
Expand Down

0 comments on commit 680e3be

Please sign in to comment.