Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
- json-ts-csharp
- dart,schema-dart
# - swift,schema-swift # pgp issue
- javascript-prop-types
- javascript-prop-types,schema-javascript-prop-types
- ruby
- php,schema-php
- scala3,schema-scala3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import {
type ArrayType,
type ClassProperty,
type ClassType,
type MapType,
type ObjectType,
PrimitiveType,
type Type,
} from "../../Type/index.js";
import {
directlyReachableSingleNamedType,
matchType,
removeNullFromType,
} from "../../Type/TypeUtils.js";
import { isES3IdentifierStart } from "../JavaScript/unicodeMaps.js";
import { legalizeName } from "../JavaScript/utils.js";
Expand Down Expand Up @@ -92,9 +94,16 @@ export class JavaScriptPropTypesRenderer extends ConvenienceRenderer {
return super.makeNameForProperty(c, className, p, jsonName, undefined);
}

private typeMapTypeFor(t: Type, required = true): Sourcelike {
private typeMapTypeFor(t: Type, required = false): Sourcelike {
if (["class", "object", "enum"].includes(t.kind)) {
return ["_", this.nameForNamedType(t)];
const validator: Sourcelike = [
"_",
this.nameForNamedType(t),
required ? ".isRequired" : "",
];
return t.kind === "enum"
? validator
: ["(...args) => ", validator, "(...args)"];
}

const match = matchType<Sourcelike>(
Expand Down Expand Up @@ -129,14 +138,15 @@ export class JavaScriptPropTypesRenderer extends ConvenienceRenderer {
);

if (required) {
return [match];
return [match, ".isRequired"];
}

return match;
}

private typeMapTypeForProperty(p: ClassProperty): Sourcelike {
return this.typeMapTypeFor(p.type);
const [nullType] = removeNullFromType(p.type);
return this.typeMapTypeFor(p.type, !p.isOptional && nullType === null);
}

private importStatement(
Expand Down Expand Up @@ -211,10 +221,10 @@ export class JavaScriptPropTypesRenderer extends ConvenienceRenderer {
this.forEachEnumCase(
enumType,
"none",
(name: Name, _jsonName, _position) => {
options.push("'");
options.push(name);
options.push("'");
(_name: Name, jsonName, _position) => {
options.push('"');
options.push(utf16StringEscape(jsonName));
options.push('"');
options.push(", ");
},
);
Expand All @@ -223,7 +233,7 @@ export class JavaScriptPropTypesRenderer extends ConvenienceRenderer {
this.emitLine([
"const _",
enumName,
" = PropTypes.oneOfType([",
" = PropTypes.oneOf([",
...options,
"]);",
]);
Expand Down Expand Up @@ -281,6 +291,13 @@ export class JavaScriptPropTypesRenderer extends ConvenienceRenderer {
this.typeMapTypeFor((type as ArrayType).items),
")",
]);
} else if (type.kind === "map") {
this.ensureBlankLine();
this.emitExport(name, [
"PropTypes.objectOf(",
this.typeMapTypeFor((type as MapType).values),
")",
]);
} else {
this.ensureBlankLine();
this.emitExport(name, ["_", name]);
Expand Down
24 changes: 19 additions & 5 deletions test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,23 @@ class JSONSchemaFixture extends LanguageFixture {
}
}

class JavaScriptPropTypesJSONSchemaFixture extends JSONSchemaFixture {
constructor() {
super(languages.JavaScriptPropTypesLanguage);
}

getSamples(sources: string[]): { priority: Sample[]; others: Sample[] } {
return super.getSamples(
sources.length === 0
? [
"test/inputs/schema/javascript-prop-types/javascript-prop-types-map.schema",
"test/inputs/schema/javascript-prop-types/javascript-prop-types-required.schema",
]
: sources,
);
}
}

// `leadingComments` is a quicktype-core API option, so the CLI fixture path
// cannot exercise it.
class LeadingCommentsGoFixture extends JSONSchemaFixture {
Expand Down Expand Up @@ -1641,11 +1658,7 @@ class CommandSuccessfulLanguageFixture extends LanguageFixture {
}

const command = this.language.runCommand(filename);
const results = await execAsync(command);

if (results.stdout.indexOf("Success") === -1) {
throw new Error(`Test failed:\n${results.stdout}`);
}
await execAsync(command);

return 0;
}
Expand Down Expand Up @@ -1819,6 +1832,7 @@ export const allFixtures: Fixture[] = [
new JSONSchemaFixture(languages.TypeScriptZodLanguage),
new JSONSchemaFixture(languages.FlowLanguage),
new JSONSchemaFixture(languages.JavaScriptLanguage),
new JavaScriptPropTypesJSONSchemaFixture(),
new JSONSchemaFixture(languages.KotlinLanguage),
new JSONSchemaFixture(
languages.KotlinJacksonLanguage,
Expand Down
6 changes: 4 additions & 2 deletions test/fixtures/javascript-prop-types/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { readFileSync } from "node:fs";
import { argv } from "node:process";
import PropTypes from "prop-types";
import { TopLevel } from "./toplevel.js";

const sample = argv[2];
const json = readFileSync(sample);
Expand All @@ -26,6 +25,7 @@ try {
checkerWorks = errors.length > 0;
errors.length = 0;

const { TopLevel } = await import("./toplevel.js");
PropTypes.resetWarningCache();
PropTypes.checkPropTypes({ obj: TopLevel }, { obj }, "prop", "MyComponent");
} finally {
Expand All @@ -36,8 +36,10 @@ if (!checkerWorks) {
console.log(
"Failure: prop-types checks are disabled (NODE_ENV=production?)",
);
process.exitCode = 1;
} else if (errors.length > 0) {
console.log("Failure:", errors.join("\n"));
process.exitCode = 1;
} else {
console.log("Success");
console.log(JSON.stringify(obj));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"first": 42
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"first": "value",
"second": "another value"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "object",
"additionalProperties": { "type": "string" }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"disabled": false,
"options": {
"label": "shown"
},
"tags": [
"one",
"two"
],
"kind": "primary choice"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"anything": {
"accepted": true
},
"options": {
"label": "shown"
},
"tags": [
"one",
"two"
],
"kind": "primary choice"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"anything": {
"accepted": true
},
"disabled": false,
"tags": [
"one",
"two"
],
"kind": "primary choice"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"anything": {
"accepted": true
},
"disabled": false,
"options": {
"label": "shown"
},
"kind": "primary choice"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"anything": {
"accepted": true
},
"disabled": false,
"options": {
"label": "shown"
},
"tags": [
"one",
"two"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"anything": {
"accepted": true
},
"disabled": false,
"options": {},
"tags": [
"one",
"two"
],
"kind": "primary choice"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"anything": {
"accepted": true
},
"disabled": false,
"options": {
"label": "shown"
},
"tags": [
"one",
"two"
],
"kind": "primary choice"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"type": "object",
"required": ["anything", "disabled", "options", "tags", "kind"],
"properties": {
"anything": {},
"disabled": { "type": "boolean" },
"options": {
"type": "object",
"required": ["label"],
"properties": {
"label": { "type": "string" }
}
},
"tags": {
"type": "array",
"items": { "type": "string" }
},
"kind": {
"type": "string",
"enum": ["primary choice", "secondary"]
},
"optional": { "type": "number" }
}
}
Loading