Skip to content

Commit

Permalink
fix: support comments on extracted literals
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Apr 7, 2024
1 parent 0020ba0 commit e37758f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"editor.tabSize": 4,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"jest.autoRun": "off"
}
20 changes: 12 additions & 8 deletions src/Utils/extractLiterals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
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[] {
Expand Down
2 changes: 2 additions & 0 deletions test/valid-data-type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
});

0 comments on commit e37758f

Please sign in to comment.