Skip to content

Commit

Permalink
Add 'default' to json output for attributes and properties
Browse files Browse the repository at this point in the history
  • Loading branch information
runem committed Nov 29, 2019
1 parent ab15f3e commit 52f3082
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
10 changes: 8 additions & 2 deletions src/transformers/json/custom-elements-json-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down
25 changes: 13 additions & 12 deletions src/transformers/json/json-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
35 changes: 35 additions & 0 deletions test/flavors/custom-element/visibility-test.ts
Original file line number Diff line number Diff line change
@@ -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
);
});

0 comments on commit 52f3082

Please sign in to comment.