-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for named tupple members (#1236)
- Loading branch information
Showing
10 changed files
with
196 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import ts from "typescript"; | ||
import { Context, NodeParser } from "../NodeParser"; | ||
import { SubNodeParser } from "../SubNodeParser"; | ||
import { AnnotatedType } from "../Type/AnnotatedType"; | ||
import { ArrayType } from "../Type/ArrayType"; | ||
import { BaseType } from "../Type/BaseType"; | ||
import { ReferenceType } from "../Type/ReferenceType"; | ||
import { RestType } from "../Type/RestType"; | ||
|
||
export class NamedTupleMemberNodeParser implements SubNodeParser { | ||
public constructor(protected childNodeParser: NodeParser) {} | ||
|
||
public supportsNode(node: ts.TypeNode): boolean { | ||
return node.kind === ts.SyntaxKind.NamedTupleMember; | ||
} | ||
|
||
public createType(node: ts.NamedTupleMember, context: Context, reference?: ReferenceType): BaseType | undefined { | ||
const baseType = this.childNodeParser.createType(node.type, context, reference); | ||
|
||
if (baseType instanceof ArrayType && node.getChildAt(0).kind === ts.SyntaxKind.DotDotDotToken) { | ||
return new RestType(baseType, node.name.text); | ||
} | ||
|
||
return baseType && new AnnotatedType(baseType, { title: node.name.text }, false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export type MyNamedUniformTuple = [first: string, second: string]; | ||
|
||
export type MyNamedTuple = [first: string, second: number]; | ||
|
||
export type MyNamedUniformTupleWithRest = [first: number, second: number, ...third: number[]]; | ||
|
||
export type MyNamedTupleWithRest = [first: string, second: number, ...third: string[]]; | ||
|
||
export type MyNamedNestedArrayWithinTuple = [first: string, second: number, third: string[]]; | ||
|
||
export type MyNamedNestedArrayWithinTupleWithRest = [first: string, second: number, ...third: string[][]]; | ||
|
||
export type MyNamedTupleWithOnlyRest = [...first: number[]]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"definitions": { | ||
"MyNamedUniformTuple": { | ||
"items": [ | ||
{ | ||
"title": "first", | ||
"type": "string" | ||
}, | ||
{ | ||
"title": "second", | ||
"type": "string" | ||
} | ||
], | ||
"minItems": 2, | ||
"maxItems": 2, | ||
"type": "array" | ||
}, | ||
"MyNamedTuple": { | ||
"items": [ | ||
{ | ||
"title": "first", | ||
"type": "string" | ||
}, | ||
{ | ||
"title": "second", | ||
"type": "number" | ||
} | ||
], | ||
"maxItems": 2, | ||
"minItems": 2, | ||
"type": "array" | ||
}, | ||
"MyNamedUniformTupleWithRest": { | ||
"additionalItems": { | ||
"title": "third", | ||
"type": "number" | ||
}, | ||
"items": [ | ||
{ | ||
"title": "first", | ||
"type": "number" | ||
}, | ||
{ | ||
"title": "second", | ||
"type": "number" | ||
} | ||
], | ||
"minItems": 2, | ||
"type": "array" | ||
}, | ||
"MyNamedTupleWithRest": { | ||
"additionalItems": { | ||
"title": "third", | ||
"type": "string" | ||
}, | ||
"items": [ | ||
{ | ||
"title": "first", | ||
"type": "string" | ||
}, | ||
{ | ||
"title": "second", | ||
"type": "number" | ||
} | ||
], | ||
"minItems": 2, | ||
"type": "array" | ||
}, | ||
"MyNamedNestedArrayWithinTuple": { | ||
"items": [ | ||
{ | ||
"title": "first", | ||
"type": "string" | ||
}, | ||
{ | ||
"title": "second", | ||
"type": "number" | ||
}, | ||
{ | ||
"items": { | ||
"type": "string" | ||
}, | ||
"title": "third", | ||
"type": "array" | ||
} | ||
], | ||
"maxItems": 3, | ||
"minItems": 3, | ||
"type": "array" | ||
}, | ||
"MyNamedNestedArrayWithinTupleWithRest": { | ||
"additionalItems": { | ||
"title": "third", | ||
"items": { | ||
"type": "string" | ||
}, | ||
"type": "array" | ||
}, | ||
"items": [ | ||
{ | ||
"title": "first", | ||
"type": "string" | ||
}, | ||
{ | ||
"title": "second", | ||
"type": "number" | ||
} | ||
], | ||
"minItems": 2, | ||
"type": "array" | ||
}, | ||
"MyNamedTupleWithOnlyRest": { | ||
"items": { | ||
"title": "first", | ||
"type": "number" | ||
}, | ||
"minItems": 0, | ||
"type": "array" | ||
} | ||
} | ||
} |