4.0.2 • Published 6 months ago

@jackolope/web-component-analyzer v4.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Dependencies

@jackolope/web-component-analyzer is a package that makes it possible to easily analyze web components. It analyzes your code and jsdoc in order to extract properties, attributes, methods, events, slots, css shadow parts and css custom properties. Works with both javascript and typescript.

Used internally for lit-analyzer, and I would recommend looking into the @custom-elements-manifest/analyzer (or something similar) for a CLI tool that complies with the standard Custom Element Manifest instead of using this directly. The CLI functionality has been removed from this tool, and it is now focused on analyzing and generating types for web components.

Try the online playground here

In addition to vanilla web components this tool supports web components built with the following libraries:

-----------------------------------------------------

➤ Installation

$ npm install -g @jackolope/web-component-analyzer

-----------------------------------------------------

➤ How to document your components using JSDoc

In addition to analyzing the code of your components, this library also use JSDoc to construct the documentation. It's especially a good idea to use JSDoc for documenting slots, events, css custom properties and css shadow parts as these not analyzed statically by this tool as of now (except when constructing a CustomEvent within your component).

Here's an example including all supported JSDoc tags. All JSDoc tags are on the the form @tag {type} name - comment and @tag {type} [name=default] - comment.

/**
 * Here is a description of my web component.
 * 
 * @element my-element
 * 
 * @fires change - This jsdoc tag makes it possible to document events.
 * @fires submit
 * 
 * @attr {Boolean} disabled - This jsdoc tag documents an attribute.
 * @attr {on|off} switch - Here is an attribute with either the "on" or "off" value.
 * @attr [my-attr=default value]
 * 
 * @prop {String} myProp - You can use this jsdoc tag to document properties.
 * @prop value
 * 
 * @slot - This is an unnamed slot (the default slot)
 * @slot start - This is a slot named "start".
 * @slot end
 * 
 * @cssprop --main-bg-color - This jsdoc tag can be used to document css custom properties.
 * @cssprop [--main-color=red]

 * @csspart container 
 */
class MyElement extends HTMLElement {

 /**
  * This is a description of a property with an attribute with exactly the same name: "color".
  * @type {"red"|"green"|"blue"}
  * @attr
  */
  color = "red";

  /**
   * This is a description of a property with an attribute called "my-prop".
   * @type {number}
   * @deprecated
   * @attr my-prop
   */
  myProp = 10

  static get observedAttributes () {
    return [
      /**
       * The header text of this element
       */
      "header"
    ];
  }

}

Overview of supported JSDoc tags

JSDoc TagDescription
@elementGives your component a tag name. This JSDoc tag is useful if your 'customElements.define` is called dynamically eg. using a custom function.
@firesDocuments events.
@slotDocuments slots. Using an empty name here documents the unnamed (default) slot.
@attr or @attributeDocuments an attribute on your component.
@prop or @propertyDocuments a property on your component.
@cssprop or @csspropertyDocuments a css custom property on your component.
@csspartDocuments a css shadow part on your component.

-----------------------------------------------------

➤ How does this tool analyze my components?

This tool extract information about your components by looking at your code directly and by looking at your JSDoc comments.

Code: Web Component Analyzer supports multiple libraries. Click here for an overview of how each library is analyzed.

JSDoc: Read next section to learn more about how JSDoc is analyzed.

➤ API

Web Component Analyzer analyzes Typescript source files, so you will have to include the Typescript parser. Here are some examples of how to use the API.

Analyze Typescript source file

import { analyzeSourceFile } from "@jackolope/web-component-analyzer";

const result = analyzeSourceFile(sourceFile, { checker });

Analyze text

import { analyzeText } from "@jackolope/web-component-analyzer";

const code = `class MyElement extends HTMLElement {

}

customElements.define("my-element", MyElement);
`;


const { results, program } = analyzeText(code);
// or
const { results, program } = analyzeText([
  { fileName: "file1.js", text: code },
  { fileName: "file2.js", text: "..." }, // these files can depend on each other
  { fileName: "file3.js", text: "...", analyze: false }
]);
// each result in "results" is the result of analyzing the corresponding text where "analyze" is not false

Transform the result

import { transformAnalyzerResult } from "@jackolope/web-component-analyzer";

const result = // the result of analyzing the component using one of the above functions

const format = "markdown"; // or "json"

const output = transformAnalyzerResult(format, result, program);

// "output" is now a string containing the result of the "markdown" transformer

-----------------------------------------------------

➤ Contributors

Rune Mehlsen

-----------------------------------------------------

➤ License

Licensed under MIT.

4.0.2

6 months ago

4.0.1

7 months ago

4.0.0

7 months ago

3.0.0

9 months ago