Skip to content

Commit

Permalink
fix: support non-literal types in template literal strings (#1509)
Browse files Browse the repository at this point in the history
  • Loading branch information
flugg committed Dec 22, 2022
1 parent c5e8393 commit 0ac5df1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
39 changes: 25 additions & 14 deletions src/NodeParser/StringTemplateLiteralNodeParser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import ts from "typescript";
import { UnknownTypeError } from "../Error/UnknownTypeError";
import { Context, NodeParser } from "../NodeParser";
import { SubNodeParser } from "../SubNodeParser";
import { BaseType } from "../Type/BaseType";
import { LiteralType } from "../Type/LiteralType";
import { StringType } from "../Type/StringType";
import { UnionType } from "../Type/UnionType";
import { extractLiterals } from "../Utils/extractLiterals";

Expand All @@ -18,24 +20,33 @@ export class StringTemplateLiteralNodeParser implements SubNodeParser {
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);
try {
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 expandedTypes = expandedLiterals.map((literal) => new LiteralType(literal));
const expandedLiterals = expand(matrix);

if (expandedTypes.length === 1) {
return expandedTypes[0];
}
const expandedTypes = expandedLiterals.map((literal) => new LiteralType(literal));

if (expandedTypes.length === 1) {
return expandedTypes[0];
}

return new UnionType(expandedTypes);
return new UnionType(expandedTypes);
} catch (error) {
if (error instanceof UnknownTypeError) {
return new StringType();
}

throw error;
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/Utils/extractLiterals.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { UnknownTypeError } from "../Error/UnknownTypeError";
import { AliasType } from "../Type/AliasType";
import { BaseType } from "../Type/BaseType";
import { BooleanType } from "../Type/BooleanType";
import { LiteralType } from "../Type/LiteralType";
import { UnionType } from "../Type/UnionType";

Expand All @@ -22,6 +23,10 @@ function* _extractLiterals(type: BaseType): Iterable<string> {
yield* _extractLiterals(type.getType());
return;
}
if (type instanceof BooleanType) {
yield* _extractLiterals(new UnionType([new LiteralType("true"), new LiteralType("false")]));
return;
}

throw new UnknownTypeError(type);
}
Expand Down
4 changes: 4 additions & 0 deletions test/valid-data/string-template-expression-literals/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ type OK = "ok";
type Result = OK | "fail" | `abort`;
type PrivateResultId = `__${Result}_id`;
type OK_ID = `id_${OK}`;
type Num = `${number}%`;
type Bool = `${boolean}!`;

export interface MyObject {
foo: Result;
_foo: PrivateResultId;
ok: OK_ID;
num: Num;
bool: Bool;
}
11 changes: 10 additions & 1 deletion test/valid-data/string-template-expression-literals/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,21 @@
"ok": {
"const": "id_ok",
"type": "string"
},
"num": {
"type": "string"
},
"bool": {
"enum": ["true!", "false!"],
"type": "string"
}
},
"required": [
"foo",
"_foo",
"ok"
"ok",
"num",
"bool"
],
"type": "object"
}
Expand Down

0 comments on commit 0ac5df1

Please sign in to comment.