From 1a935d8dded024c06a0cb2a6a65ad34b5ec32096 Mon Sep 17 00:00:00 2001 From: Jason Dent Date: Thu, 22 Sep 2022 03:09:09 +0200 Subject: [PATCH] fix: Hidden should not cause `additionalProperties` to be `true`. (#1417) --- factory/formatter.ts | 10 ++-- src/NodeParser/HiddenTypeNodeParser.ts | 4 +- src/Type/HiddenType.ts | 7 +++ src/TypeFormatter/HiddenTypeFormatter.ts | 16 +++++++ test/config.test.ts | 14 +++++- .../jsdoc-hidden-types-intersection/main.ts | 24 ++++++++++ .../schema.json | 46 +++++++++++++++++++ test/unit/Type/HiddenType.test.ts | 10 ++++ 8 files changed, 123 insertions(+), 8 deletions(-) create mode 100644 src/Type/HiddenType.ts create mode 100644 src/TypeFormatter/HiddenTypeFormatter.ts create mode 100644 test/config/jsdoc-hidden-types-intersection/main.ts create mode 100644 test/config/jsdoc-hidden-types-intersection/schema.json create mode 100644 test/unit/Type/HiddenType.test.ts diff --git a/factory/formatter.ts b/factory/formatter.ts index 5bf8753c7..b030885ee 100644 --- a/factory/formatter.ts +++ b/factory/formatter.ts @@ -1,18 +1,20 @@ -import { Config } from "../src/Config"; import { ChainTypeFormatter } from "../src/ChainTypeFormatter"; import { CircularReferenceTypeFormatter } from "../src/CircularReferenceTypeFormatter"; +import { Config } from "../src/Config"; +import { MutableTypeFormatter } from "../src/MutableTypeFormatter"; import { TypeFormatter } from "../src/TypeFormatter"; import { AliasTypeFormatter } from "../src/TypeFormatter/AliasTypeFormatter"; import { AnnotatedTypeFormatter } from "../src/TypeFormatter/AnnotatedTypeFormatter"; import { AnyTypeFormatter } from "../src/TypeFormatter/AnyTypeFormatter"; -import { SymbolTypeFormatter } from "../src/TypeFormatter/SymbolTypeFormatter"; import { ArrayTypeFormatter } from "../src/TypeFormatter/ArrayTypeFormatter"; import { BooleanTypeFormatter } from "../src/TypeFormatter/BooleanTypeFormatter"; import { DefinitionTypeFormatter } from "../src/TypeFormatter/DefinitionTypeFormatter"; import { EnumTypeFormatter } from "../src/TypeFormatter/EnumTypeFormatter"; +import { HiddenTypeFormatter } from "../src/TypeFormatter/HiddenTypeFormatter"; import { IntersectionTypeFormatter } from "../src/TypeFormatter/IntersectionTypeFormatter"; import { LiteralTypeFormatter } from "../src/TypeFormatter/LiteralTypeFormatter"; import { LiteralUnionTypeFormatter } from "../src/TypeFormatter/LiteralUnionTypeFormatter"; +import { NeverTypeFormatter } from "../src/TypeFormatter/NeverTypeFormatter"; import { NullTypeFormatter } from "../src/TypeFormatter/NullTypeFormatter"; import { NumberTypeFormatter } from "../src/TypeFormatter/NumberTypeFormatter"; import { ObjectTypeFormatter } from "../src/TypeFormatter/ObjectTypeFormatter"; @@ -21,13 +23,12 @@ import { PrimitiveUnionTypeFormatter } from "../src/TypeFormatter/PrimitiveUnion import { ReferenceTypeFormatter } from "../src/TypeFormatter/ReferenceTypeFormatter"; import { RestTypeFormatter } from "../src/TypeFormatter/RestTypeFormatter"; import { StringTypeFormatter } from "../src/TypeFormatter/StringTypeFormatter"; +import { SymbolTypeFormatter } from "../src/TypeFormatter/SymbolTypeFormatter"; import { TupleTypeFormatter } from "../src/TypeFormatter/TupleTypeFormatter"; import { UndefinedTypeFormatter } from "../src/TypeFormatter/UndefinedTypeFormatter"; import { UnionTypeFormatter } from "../src/TypeFormatter/UnionTypeFormatter"; import { UnknownTypeFormatter } from "../src/TypeFormatter/UnknownTypeFormatter"; import { VoidTypeFormatter } from "../src/TypeFormatter/VoidTypeFormatter"; -import { MutableTypeFormatter } from "../src/MutableTypeFormatter"; -import { NeverTypeFormatter } from "../src/TypeFormatter/NeverTypeFormatter"; export type FormatterAugmentor = ( formatter: MutableTypeFormatter, @@ -55,6 +56,7 @@ export function createFormatter(config: Config, augmentor?: FormatterAugmentor): .addTypeFormatter(new UndefinedTypeFormatter()) .addTypeFormatter(new UnknownTypeFormatter()) .addTypeFormatter(new VoidTypeFormatter()) + .addTypeFormatter(new HiddenTypeFormatter()) .addTypeFormatter(new NeverTypeFormatter()) .addTypeFormatter(new LiteralTypeFormatter()) diff --git a/src/NodeParser/HiddenTypeNodeParser.ts b/src/NodeParser/HiddenTypeNodeParser.ts index 6fc7a2f17..42a514278 100644 --- a/src/NodeParser/HiddenTypeNodeParser.ts +++ b/src/NodeParser/HiddenTypeNodeParser.ts @@ -2,7 +2,7 @@ import ts from "typescript"; import { Context } from "../NodeParser"; import { SubNodeParser } from "../SubNodeParser"; import { BaseType } from "../Type/BaseType"; -import { NeverType } from "../Type/NeverType"; +import { HiddenType } from "../Type/HiddenType"; import { isNodeHidden } from "../Utils/isHidden"; export class HiddenNodeParser implements SubNodeParser { @@ -13,6 +13,6 @@ export class HiddenNodeParser implements SubNodeParser { } public createType(_node: ts.KeywordTypeNode, _context: Context): BaseType { - return new NeverType(); + return new HiddenType(); } } diff --git a/src/Type/HiddenType.ts b/src/Type/HiddenType.ts new file mode 100644 index 000000000..07fa68034 --- /dev/null +++ b/src/Type/HiddenType.ts @@ -0,0 +1,7 @@ +import { NeverType } from "./NeverType"; + +export class HiddenType extends NeverType { + public getId(): string { + return "hidden"; + } +} diff --git a/src/TypeFormatter/HiddenTypeFormatter.ts b/src/TypeFormatter/HiddenTypeFormatter.ts new file mode 100644 index 000000000..2a2fdc5e7 --- /dev/null +++ b/src/TypeFormatter/HiddenTypeFormatter.ts @@ -0,0 +1,16 @@ +import { Definition } from "../Schema/Definition"; +import { SubTypeFormatter } from "../SubTypeFormatter"; +import { BaseType } from "../Type/BaseType"; +import { HiddenType } from "../Type/HiddenType"; + +export class HiddenTypeFormatter implements SubTypeFormatter { + public supportsType(type: HiddenType): boolean { + return type instanceof HiddenType; + } + public getDefinition(type: HiddenType): Definition { + return { additionalProperties: false }; + } + public getChildren(type: HiddenType): BaseType[] { + return []; + } +} diff --git a/test/config.test.ts b/test/config.test.ts index 4cdce5f38..2af2b8a34 100644 --- a/test/config.test.ts +++ b/test/config.test.ts @@ -269,8 +269,18 @@ describe("config", () => { it( "jsdoc-hidden-types", - assertSchema("jsdoc-hidden", { - type: "MyObject", + assertSchema("jsdoc-hidden-types", { + type: "MyType", + expose: "export", + topRef: true, + jsDoc: "extended", + }) + ); + + it( + "jsdoc-hidden-types-intersection", + assertSchema("jsdoc-hidden-types-intersection", { + type: "MyType", expose: "export", topRef: true, jsDoc: "extended", diff --git a/test/config/jsdoc-hidden-types-intersection/main.ts b/test/config/jsdoc-hidden-types-intersection/main.ts new file mode 100644 index 000000000..780d9a01f --- /dev/null +++ b/test/config/jsdoc-hidden-types-intersection/main.ts @@ -0,0 +1,24 @@ +/** + * @hidden + */ +export interface Hidden { + hidden?: number; +} + +export type Hidden2 = Hidden; + +export interface Visible { + visible: string; +} + +export interface Intersection extends Visible, Hidden {} + +export type MyType = { + /** + * @hidden + */ + hidden: Visible; + hidden2?: Hidden; + visible: Visible; + intersection: Intersection; +}; diff --git a/test/config/jsdoc-hidden-types-intersection/schema.json b/test/config/jsdoc-hidden-types-intersection/schema.json new file mode 100644 index 000000000..6a19c8331 --- /dev/null +++ b/test/config/jsdoc-hidden-types-intersection/schema.json @@ -0,0 +1,46 @@ +{ + "$ref": "#/definitions/MyType", + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "Intersection": { + "additionalProperties": false, + "properties": { + "visible": { + "type": "string" + } + }, + "required": [ + "visible" + ], + "type": "object" + }, + "MyType": { + "additionalProperties": false, + "properties": { + "intersection": { + "$ref": "#/definitions/Intersection" + }, + "visible": { + "$ref": "#/definitions/Visible" + } + }, + "required": [ + "visible", + "intersection" + ], + "type": "object" + }, + "Visible": { + "additionalProperties": false, + "properties": { + "visible": { + "type": "string" + } + }, + "required": [ + "visible" + ], + "type": "object" + } + } +} diff --git a/test/unit/Type/HiddenType.test.ts b/test/unit/Type/HiddenType.test.ts new file mode 100644 index 000000000..271d97d5e --- /dev/null +++ b/test/unit/Type/HiddenType.test.ts @@ -0,0 +1,10 @@ +import { HiddenType } from "../../../src/Type/HiddenType"; +import { NeverType } from "../../../src/Type/NeverType"; + +describe("HiddenType", () => { + it("creates a HiddenType", () => { + const hidden = new HiddenType(); + expect(hidden instanceof NeverType).toBe(true); + expect(hidden.getId()).toBe("hidden"); + }); +});