Skip to content

Commit

Permalink
Fix issues with markdown transformer and documenting events
Browse files Browse the repository at this point in the history
  • Loading branch information
runem committed Dec 2, 2019
1 parent 81ffe82 commit 852352c
Show file tree
Hide file tree
Showing 11 changed files with 417 additions and 407 deletions.
2 changes: 1 addition & 1 deletion dev/src/lit-element/lit-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { customElement, LitElement, property } from "lit-element";
* This is my element
* @fires {string} change - This is a change event
* @fires my-event - This event is awesome
* @cssprop --main-bg-color - This css property is nice!
* @cssprop {Color} --main-bg-color - This css property is nice!
* @cssprop --main-color - This css property is also nice!
* @attr {on|off} switch - This is a great attribute
* @attr {String} my-attr - This is an attribute defined from jsdoc
Expand Down
2 changes: 1 addition & 1 deletion src/analyze/flavors/js-doc/discover-features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const discoverFeatures: Partial<FeatureDiscoverVisitMap<AnalyzerVisitCont
return {
name: name,
jsDoc: description != null ? { description } : undefined,
type: type || undefined,
typeHint: type || undefined,
default: def
};
}
Expand Down
5 changes: 4 additions & 1 deletion src/analyze/flavors/js-doc/refine-feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export const refineFeature: AnalyzerFlavor["refineFeature"] = {
event: (event: ComponentEvent) => {
if (event.jsDoc == null || event.jsDoc.tags == null) return event;

return [applyJsDocDeprecated, applyJsDocVisibility].reduce((event, applyFunc) => (applyFunc as Function)(event, event.jsDoc), event);
return [applyJsDocDeprecated, applyJsDocVisibility, applyJsDocType].reduce(
(event, applyFunc) => (applyFunc as Function)(event, event.jsDoc),
event
);
},
method: (method: ComponentMethod) => {
if (method.jsDoc == null || method.jsDoc.tags == null) return method;
Expand Down
11 changes: 9 additions & 2 deletions src/analyze/flavors/lit-element/exclude-node.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { Node } from "typescript";
import { AnalyzerVisitContext } from "../../analyzer-visit-context";
import { getDeclarationName } from "../../util/ast-util";

export function excludeNode(node: Node, context: AnalyzerVisitContext): boolean | undefined {
if (context.config.analyzeLib) {
return undefined;
}

const fileName = node.getSourceFile().fileName;
return fileName.includes("lit-element.d.ts") || fileName.endsWith("updating-element.d.ts");
const declName = getDeclarationName(node, context);
if (declName != null) {
return declName === "LitElement" || declName === "UpdatingElement";
} else {
const fileName = node.getSourceFile().fileName;

return fileName.includes("/lit-element.") || fileName.endsWith("/updating-element.");
}
}
13 changes: 2 additions & 11 deletions src/analyze/stages/analyze-declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Node } from "typescript";
import { AnalyzerVisitContext } from "../analyzer-visit-context";
import { AnalyzerDeclarationVisitContext, ComponentFeatureCollection } from "../flavors/analyzer-flavor";
import { ComponentDeclaration } from "../types/component-declaration";
import { getDeclarationName } from "../util/ast-util";
import { getUniqueResolvedNodeForInheritanceTree } from "../util/inheritance-tree-util";
import { getJsDoc } from "../util/js-doc-util";
import { discoverFeatures } from "./discover-features";
Expand Down Expand Up @@ -82,7 +83,7 @@ export function analyzeComponentDeclaration(initialDeclarationNodes: Node[], con
}

function shouldExcludeNode(node: Node, context: AnalyzerVisitContext): boolean {
if (!context.config.analyzeLibDom && excludeNode(node.getSourceFile(), context)) {
if (!context.config.analyzeLibDom && excludeNode(node, context)) {
return true;
}

Expand All @@ -94,13 +95,3 @@ function shouldExcludeNode(node: Node, context: AnalyzerVisitContext): boolean {

return false;
}

function getDeclarationName(node: Node, context: AnalyzerVisitContext): string | undefined {
if (context.ts.isClassLike(node) || context.ts.isInterfaceDeclaration(node)) {
return node.name?.text;
} else if (context.ts.isVariableDeclaration(node)) {
return node.name?.getText();
}

return undefined;
}
2 changes: 1 addition & 1 deletion src/analyze/types/features/component-css-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { JsDoc } from "../js-doc";
export interface ComponentCssProperty {
name: string;
jsDoc: JsDoc | undefined;
type?: string;
typeHint?: string;
default?: any;
}
10 changes: 10 additions & 0 deletions src/analyze/util/ast-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,13 @@ export function getLeadingCommentForNode(node: Node, ts: typeof tsModule): strin
export function isHTMLElementExtensionInterface(node: Node, context: AnalyzerVisitContext): node is InterfaceDeclaration {
return context.ts.isInterfaceDeclaration(node) && context.ts.isModuleBlock(node.parent) && node.name.text === "HTMLElement";
}

export function getDeclarationName(node: Node, context: AnalyzerVisitContext): string | undefined {
if (context.ts.isClassLike(node) || context.ts.isInterfaceDeclaration(node)) {
return node.name?.text;
} else if (context.ts.isVariableDeclaration(node)) {
return node.name?.getText();
}

return undefined;
}
2 changes: 1 addition & 1 deletion src/transformers/json/json-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function componentCssPropToHtmlCssProp(prop: ComponentCssProperty, checker: Type
return {
name: prop.name || "",
description: getDescriptionFromJsDoc(prop.jsDoc),
type: prop.type,
type: prop.typeHint,
default: prop.default != null ? JSON.stringify(prop.default) : undefined
};
}
Expand Down
13 changes: 6 additions & 7 deletions src/transformers/markdown/markdown-transformer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isAssignableToSimpleTypeKind, SimpleTypeKind, toTypeString } from "ts-simple-type";
import { Program, TypeChecker } from "typescript";
import { AnalyzerResult } from "../../analyze/types/analyzer-result";
import { ComponentCssPart } from "../../analyze/types/features/component-css-part";
Expand Down Expand Up @@ -97,16 +96,16 @@ export const markdownTransformer: TransformerFunction = (results: AnalyzerResult

/**
* Returns a markdown table with css props
* @param cssProperty
* @param cssProperties
* @param config
*/
function cssPropSection(cssProperty: ComponentCssProperty[], config: TransformerConfig): string {
const rows: string[][] = [["Property", "Default", "Description"]];
function cssPropSection(cssProperties: ComponentCssProperty[], config: TransformerConfig): string {
const rows: string[][] = [["Property", "Type", "Default", "Description"]];
rows.push(
...cssProperty.map(prop => {
...cssProperties.map(prop => {
const def = (prop.default !== undefined ? JSON.stringify(prop.default) : "") || "";

return [(prop.name && markdownHighlight(prop.name)) || "", def, prop.jsDoc?.description || ""];
return [(prop.name && markdownHighlight(prop.name)) || "", prop.typeHint || "", def, prop.jsDoc?.description || ""];
})
);
return markdownHeader("CSS Custom Properties", 2, config) + "\n" + markdownTable(rows);
Expand Down Expand Up @@ -156,7 +155,7 @@ function eventSection(events: ComponentEvent[], checker: TypeChecker, config: Tr
...events.map(event => [
(event.name && markdownHighlight(event.name)) || "",
...(showVisibility ? [event.visibility || "public"] : []),
isAssignableToSimpleTypeKind(event.type(), SimpleTypeKind.ANY, checker) ? "" : markdownHighlight(toTypeString(event.type(), checker)),
markdownHighlight(getTypeHintFromType(event.typeHint ?? event.type?.(), checker)),
event.jsDoc?.description || ""
])
);
Expand Down
Loading

0 comments on commit 852352c

Please sign in to comment.