From 4c2b073b04abf47df8ebde21f65a9a5d40e11aaa Mon Sep 17 00:00:00 2001 From: runem Date: Fri, 29 Nov 2019 14:58:04 +0100 Subject: [PATCH] Add visibility unit test --- .../json/custom-elements-json-data.ts | 10 ++++-- src/transformers/json/json-transformer.ts | 25 ++++++------- .../flavors/custom-element/visibility-test.ts | 35 +++++++++++++++++++ 3 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 test/flavors/custom-element/visibility-test.ts diff --git a/src/transformers/json/custom-elements-json-data.ts b/src/transformers/json/custom-elements-json-data.ts index 13010407..ad3897b2 100644 --- a/src/transformers/json/custom-elements-json-data.ts +++ b/src/transformers/json/custom-elements-json-data.ts @@ -20,9 +20,15 @@ export interface HtmlDataMember { attribute?: string; } -export interface HtmlDataAttribute extends HtmlDataMember {} +export interface HtmlDataAttribute extends HtmlDataMember { + // Suggested fields: + default?: string; +} -export interface HtmlDataProperty extends HtmlDataMember {} +export interface HtmlDataProperty extends HtmlDataMember { + // Suggested fields: + default?: string; +} export interface HtmlDataSlot extends HtmlDataMember {} diff --git a/src/transformers/json/json-transformer.ts b/src/transformers/json/json-transformer.ts index f5c5a759..29e9fbc4 100644 --- a/src/transformers/json/json-transformer.ts +++ b/src/transformers/json/json-transformer.ts @@ -10,8 +10,8 @@ import { ComponentSlot } from "../../analyze/types/features/component-slot"; import { JsDoc } from "../../analyze/types/js-doc"; import { arrayFlat } from "../../util/array-util"; import { filterVisibility } from "../../util/model-util"; -import { TransformerFunction } from "../transformer-function"; import { TransformerConfig } from "../transformer-config"; +import { TransformerFunction } from "../transformer-function"; import { HtmlData, HtmlDataAttribute, @@ -126,23 +126,24 @@ function componentMemberToHtmlDataAttribute(member: ComponentMember, checker: Ty name: member.attrName, description: getDescriptionFromJsDoc(member.jsDoc), jsDoc: getJsDocTextFromJsDoc(member.jsDoc), - type: member.typeHint ?? (member.type != null ? getTypeHintFromType(member.type(), checker) : undefined) + type: member.typeHint ?? (member.type != null ? getTypeHintFromType(member.type(), checker) : undefined), + default: member.default != null ? JSON.stringify(member.default) : undefined }; } function componentMemberToHtmlDataProperty(member: ComponentMember, checker: TypeChecker): HtmlDataProperty | undefined { - switch (member.kind) { - case "property": - return { - name: member.propName, - attribute: member.attrName, - description: getDescriptionFromJsDoc(member.jsDoc), - jsDoc: getJsDocTextFromJsDoc(member.jsDoc), - type: member.typeHint ?? (member.type != null ? getTypeHintFromType(member.type(), checker) : undefined) - }; + if (member.propName == null) { + return undefined; } - return undefined; + return { + name: member.propName, + attribute: member.attrName, + description: getDescriptionFromJsDoc(member.jsDoc), + jsDoc: getJsDocTextFromJsDoc(member.jsDoc), + type: member.typeHint ?? (member.type != null ? getTypeHintFromType(member.type(), checker) : undefined), + default: member.default != null ? JSON.stringify(member.default) : undefined + }; } function getDescriptionFromJsDoc(jsDoc: JsDoc | undefined): string | undefined { diff --git a/test/flavors/custom-element/visibility-test.ts b/test/flavors/custom-element/visibility-test.ts new file mode 100644 index 00000000..872c0f2c --- /dev/null +++ b/test/flavors/custom-element/visibility-test.ts @@ -0,0 +1,35 @@ +import test from "ava"; +import { analyzeText } from "../../../src/analyze/analyze-text"; +import { assertHasMembers } from "../../helpers/util"; + +test("Handle Typescript visibility modifiers", t => { + const { result, checker } = analyzeText(` + /** + * @element + */ + class MyElement extends HTMLElement { + private myProp1; + protected myProp2; + } +`); + + const { members } = result.componentDefinitions[0]?.declaration(); + + assertHasMembers( + members, + [ + { + kind: "property", + propName: "myProp1", + visibility: "private" + }, + { + kind: "property", + propName: "myProp2", + visibility: "protected" + } + ], + t, + checker + ); +});