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 comments on extracted literals #1908

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
}
}
}