Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support non-literal types in template literal strings #1509

Merged
merged 11 commits into from
Dec 22, 2022
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"
flugg marked this conversation as resolved.
Show resolved Hide resolved
}
},
"required": [
"foo",
"_foo",
"ok"
"ok",
"num",
"bool"
],
"type": "object"
}
Expand Down