Skip to content

Commit

Permalink
Fix parsing of the fires jsdoc tag when className and event are inclu…
Browse files Browse the repository at this point in the history
…ded in the tag #139
  • Loading branch information
runem committed Feb 12, 2020
1 parent 9e2679f commit a31d1a8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/analyze/types/js-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export interface JsDocTagParsed {
optional?: boolean;
default?: string;
description?: string;
className?: string;
namespace?: string;
}

export interface JsDocTag {
Expand Down
15 changes: 15 additions & 0 deletions src/analyze/util/js-doc-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,21 @@ function parseJsDocTagString(str: string): JsDocTagParsed {
jsDocTag.description = str.replace(/^\s*-\s*/, "").trim() || undefined;
}

// Expand the name based on namespace and classname
if (jsDocTag.name != null) {
/**
* The name could look like this, so we need to parse and the remove the class name and namespace from the name
* InputSwitch#[CustomEvent]input-switch-check-changed
* InputSwitch#input-switch-check-changed
*/
const match = jsDocTag.name.match(/(.*)#(\[.*\])?(.*)/);
if (match != null) {
jsDocTag.className = match[1];
jsDocTag.namespace = match[2];
jsDocTag.name = match[3];
}
}

return jsDocTag;
}

Expand Down
22 changes: 21 additions & 1 deletion test/flavors/jsdoc/event-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isAssignableToSimpleTypeKind, SimpleType, SimpleTypeKind } from "ts-simple-type";
import { tsTest } from "../../helpers/ts-test";
import { analyzeTextWithCurrentTsModule } from "../../helpers/analyze-text-with-current-ts-module";
import { tsTest } from "../../helpers/ts-test";

tsTest("jsdoc: Discovers custom events with @fires", t => {
const {
Expand Down Expand Up @@ -37,3 +37,23 @@ tsTest("jsdoc: Discovers the detail type of custom events with @fires", t => {
const { events } = result.componentDefinitions[0].declaration();
t.truthy(isAssignableToSimpleTypeKind(events[0].type() as SimpleType, SimpleTypeKind.STRING));
});

tsTest("jsdoc: Discovers events declared with @fires that includes extra jsdoc information", t => {
const {
results: [result]
} = analyzeTextWithCurrentTsModule(`
/**
* @element
* @fires InputSwitch#[CustomEvent]input-switch-check-changed Fires when check property changes
*/
class MyElement extends HTMLElement {
}
`);

const { events } = result.componentDefinitions[0].declaration();

t.is(events.length, 1);
t.is(events[0].name, "input-switch-check-changed");
t.is(events[0].jsDoc?.description, "Fires when check property changes");
t.truthy(isAssignableToSimpleTypeKind(events[0].type() as SimpleType, SimpleTypeKind.ANY));
});

0 comments on commit a31d1a8

Please sign in to comment.