Skip to content

Commit

Permalink
fix: support comments on extracted literals (#1908)
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz authored Apr 8, 2024
1 parent 0020ba0 commit 9353127
Show file tree
Hide file tree
Showing 5 changed files with 29 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"));
});
4 changes: 4 additions & 0 deletions test/valid-data/lowercase/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** The built-in color schemes, cased. */
type Foo = "Accent";

export type MyType = Lowercase<Foo>;
10 changes: 10 additions & 0 deletions test/valid-data/lowercase/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$ref": "#/definitions/MyType",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyType": {
"const": "accent",
"type": "string"
}
}
}

0 comments on commit 9353127

Please sign in to comment.