-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support template literals as types. (#1171)
- Loading branch information
Showing
7 changed files
with
150 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import ts from "typescript"; | ||
import { UnknownTypeError } from "../Error/UnknownTypeError"; | ||
import { Context, NodeParser } from "../NodeParser"; | ||
import { SubNodeParser } from "../SubNodeParser"; | ||
import { AliasType } from "../Type/AliasType"; | ||
import { BaseType } from "../Type/BaseType"; | ||
import { LiteralType } from "../Type/LiteralType"; | ||
import { UnionType } from "../Type/UnionType"; | ||
|
||
export class StringTemplateLiteralNodeParser implements SubNodeParser { | ||
public constructor(protected childNodeParser: NodeParser) {} | ||
|
||
public supportsNode(node: ts.NoSubstitutionTemplateLiteral | ts.TemplateLiteralTypeNode): boolean { | ||
return ( | ||
node.kind === ts.SyntaxKind.NoSubstitutionTemplateLiteral || node.kind === ts.SyntaxKind.TemplateLiteralType | ||
); | ||
} | ||
public createType(node: ts.NoSubstitutionTemplateLiteral | ts.TemplateLiteralTypeNode, context: Context): BaseType { | ||
if (node.kind === ts.SyntaxKind.NoSubstitutionTemplateLiteral) { | ||
return new LiteralType(node.text); | ||
} | ||
const prefix = node.head.text; | ||
const matrix: string[][] = [[prefix]].concat( | ||
node.templateSpans.map((span) => { | ||
const suffix = span.literal.text; | ||
const type = this.childNodeParser.createType(span.type, context); | ||
return [...extractLiterals(type)].map((value) => value + suffix); | ||
}) | ||
); | ||
|
||
const expandedLiterals = expand(matrix); | ||
|
||
const expandedTypes = expandedLiterals.map((literal) => new LiteralType(literal)); | ||
|
||
if (expandedTypes.length === 1) { | ||
return expandedTypes[0]; | ||
} | ||
|
||
return new UnionType(expandedTypes); | ||
} | ||
} | ||
|
||
function expand(matrix: string[][]): string[] { | ||
if (matrix.length === 1) { | ||
return matrix[0]; | ||
} | ||
const head = matrix[0]; | ||
const nested = expand(matrix.slice(1)); | ||
const combined = head.map((prefix) => nested.map((suffix) => prefix + suffix)); | ||
return ([] as string[]).concat(...combined); | ||
} | ||
|
||
function* extractLiterals(type: BaseType | undefined): Iterable<string> { | ||
if (!type) return; | ||
if (type instanceof LiteralType) { | ||
yield type.getValue().toString(); | ||
return; | ||
} | ||
if (type instanceof UnionType) { | ||
for (const t of type.getTypes()) { | ||
yield* extractLiterals(t); | ||
} | ||
return; | ||
} | ||
if (type instanceof AliasType) { | ||
yield* extractLiterals(type.getType()); | ||
return; | ||
} | ||
|
||
throw new UnknownTypeError(type); | ||
} |
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
10 changes: 10 additions & 0 deletions
10
test/valid-data/string-template-expression-literals/main.ts
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,10 @@ | ||
type OK = "ok"; | ||
type Result = OK | "fail" | `abort`; | ||
type PrivateResultId = `__${Result}_id`; | ||
type OK_ID = `id_${OK}`; | ||
|
||
export interface MyObject { | ||
foo: Result; | ||
_foo: PrivateResultId; | ||
ok: OK_ID; | ||
} |
37 changes: 37 additions & 0 deletions
37
test/valid-data/string-template-expression-literals/schema.json
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,37 @@ | ||
{ | ||
"$ref": "#/definitions/MyObject", | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"definitions": { | ||
"MyObject": { | ||
"additionalProperties": false, | ||
"properties": { | ||
"_foo": { | ||
"enum": [ | ||
"__ok_id", | ||
"__fail_id", | ||
"__abort_id" | ||
], | ||
"type": "string" | ||
}, | ||
"foo": { | ||
"enum": [ | ||
"ok", | ||
"fail", | ||
"abort" | ||
], | ||
"type": "string" | ||
}, | ||
"ok": { | ||
"const": "id_ok", | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"foo", | ||
"_foo", | ||
"ok" | ||
], | ||
"type": "object" | ||
} | ||
} | ||
} |
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,5 @@ | ||
type Result = "ok" | "fail" | `abort`; | ||
|
||
export interface MyObject { | ||
foo: Result; | ||
} |
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,23 @@ | ||
{ | ||
"$ref": "#/definitions/MyObject", | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"definitions": { | ||
"MyObject": { | ||
"additionalProperties": false, | ||
"properties": { | ||
"foo": { | ||
"enum": [ | ||
"ok", | ||
"fail", | ||
"abort" | ||
], | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"foo" | ||
], | ||
"type": "object" | ||
} | ||
} | ||
} |