Skip to content

Commit 55aecaa

Browse files
committed
[heft-json-schema-typings-plugin] Strip $schema from generated typings
1 parent 3b74694 commit 55aecaa

5 files changed

Lines changed: 54 additions & 3 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/heft-json-schema-typings-plugin",
5+
"comment": "Strip the `$schema` metadata property from JSON schema files before generating TypeScript typings, so it does not appear as a property in the generated `.d.ts` declarations.",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@rushstack/heft-json-schema-typings-plugin"
10+
}

heft-plugins/heft-json-schema-typings-plugin/src/JsonSchemaTypingsGenerator.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,13 @@ export class JsonSchemaTypingsGenerator extends TypingsGenerator {
4343
relativePath: string
4444
): Promise<string> => {
4545
const parsedFileContents: IExtendedJson4Schema = JSON.parse(fileContents);
46-
const { [X_TSDOC_RELEASE_TAG_KEY]: tsdocReleaseTag, ...jsonSchemaWithoutReleaseTag } =
47-
parsedFileContents;
46+
const {
47+
[X_TSDOC_RELEASE_TAG_KEY]: tsdocReleaseTag,
48+
// Strip $schema so it doesn't appear as a property in the generated types
49+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
50+
$schema: _schema,
51+
...jsonSchemaWithoutMetadata
52+
} = parsedFileContents;
4853

4954
// Use the absolute directory of the schema file so that cross-file $ref
5055
// (e.g. { "$ref": "./other.schema.json" }) resolves correctly.
@@ -53,7 +58,7 @@ export class JsonSchemaTypingsGenerator extends TypingsGenerator {
5358
dirname.length + 1,
5459
-SCHEMA_FILE_EXTENSION.length
5560
);
56-
let typings: string = await compile(jsonSchemaWithoutReleaseTag, filenameWithoutExtension, {
61+
let typings: string = await compile(jsonSchemaWithoutMetadata, filenameWithoutExtension, {
5762
// The typings generator adds its own banner comment
5863
bannerComment: '',
5964
cwd: dirname,

heft-plugins/heft-json-schema-typings-plugin/src/test/JsonSchemaTypingsGenerator.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,16 @@ describe('JsonSchemaTypingsGenerator', () => {
6060
// The parent typings should reference the child type
6161
expect(parentTypings).toContain('ChildType');
6262
});
63+
64+
it('strips the $schema property from generated typings', async () => {
65+
const generator = new JsonSchemaTypingsGenerator({
66+
srcFolder: schemasFolder,
67+
generatedTsFolder: outputFolder
68+
});
69+
70+
await generator.generateTypingsAsync(['with-schema-field.schema.json']);
71+
const typings: string = await readGeneratedTypings('with-schema-field.schema.json');
72+
expect(typings).toMatchSnapshot();
73+
expect(typings).not.toContain('$schema');
74+
});
6375
});

heft-plugins/heft-json-schema-typings-plugin/src/test/__snapshots__/JsonSchemaTypingsGenerator.test.ts.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,15 @@ childValue?: number
8383
}
8484
"
8585
`;
86+
87+
exports[`JsonSchemaTypingsGenerator strips the $schema property from generated typings 1`] = `
88+
"// This file was generated by a tool. Modifying it will produce unexpected behavior
89+
90+
export interface ConfigWithSchemaField {
91+
/**
92+
* The name.
93+
*/
94+
name?: string
95+
}
96+
"
97+
`;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "Config With Schema Field",
4+
"type": "object",
5+
"properties": {
6+
"name": {
7+
"description": "The name.",
8+
"type": "string"
9+
}
10+
},
11+
"additionalProperties": false
12+
}

0 commit comments

Comments
 (0)