From e37758fb7f0d9bb7d50e5bd226cfca201543edc0 Mon Sep 17 00:00:00 2001 From: Dominik Moritz Date: Sun, 7 Apr 2024 19:10:29 -0400 Subject: [PATCH] fix: support comments on extracted literals --- .vscode/settings.json | 2 +- src/Utils/extractLiterals.ts | 20 ++++++++++++-------- test/valid-data-type.test.ts | 2 ++ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 58507a4ca..b568d7614 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -11,7 +11,7 @@ "editor.tabSize": 4, "editor.formatOnSave": true, "editor.codeActionsOnSave": { - "source.fixAll.eslint": true + "source.fixAll.eslint": "explicit" }, "jest.autoRun": "off" } diff --git a/src/Utils/extractLiterals.ts b/src/Utils/extractLiterals.ts index 278802976..aa10421f6 100644 --- a/src/Utils/extractLiterals.ts +++ b/src/Utils/extractLiterals.ts @@ -6,31 +6,35 @@ import { DefinitionType } from "../Type/DefinitionType"; import { EnumType } from "../Type/EnumType"; import { LiteralType } from "../Type/LiteralType"; import { UnionType } from "../Type/UnionType"; +import { derefAnnotatedType } from "./derefType"; function* _extractLiterals(type: BaseType): Iterable { if (!type) { return; } - if (type instanceof LiteralType) { - yield type.getValue().toString(); + + const dereffedType = derefAnnotatedType(type); + + if (dereffedType instanceof LiteralType) { + yield dereffedType.getValue().toString(); return; } - if (type instanceof UnionType || type instanceof EnumType) { - for (const t of type.getTypes()) { + if (dereffedType instanceof UnionType || dereffedType instanceof EnumType) { + for (const t of dereffedType.getTypes()) { yield* _extractLiterals(t); } return; } - if (type instanceof AliasType || type instanceof DefinitionType) { - yield* _extractLiterals(type.getType()); + if (dereffedType instanceof AliasType || dereffedType instanceof DefinitionType) { + yield* _extractLiterals(dereffedType.getType()); return; } - if (type instanceof BooleanType) { + if (dereffedType instanceof BooleanType) { yield* _extractLiterals(new UnionType([new LiteralType("true"), new LiteralType("false")])); return; } - throw new UnknownTypeError(type); + throw new UnknownTypeError(dereffedType); } export function extractLiterals(type: BaseType): string[] { diff --git a/test/valid-data-type.test.ts b/test/valid-data-type.test.ts index 8375ad680..1ee23a99a 100644 --- a/test/valid-data-type.test.ts +++ b/test/valid-data-type.test.ts @@ -148,4 +148,6 @@ describe("valid-data-type", () => { it("type-satisfies", assertValidSchema("type-satisfies", "MyType")); it("ignore-export", assertValidSchema("ignore-export", "*")); + + it("lowercase", assertValidSchema("lowercase", "MyType")); });