NodeFactory
Builds source text for the edits of a plugin.
Builds source text for the edits of a plugin. Every method returns code to pass to PluginContext.replace, PluginContext.insertBefore or PluginContext.insertAfter; names are validated and strings are quoted, so a value from the module cannot break out of the text that is built. The compiler parses the module again after the edits, so text that is not valid is reported with the plugin's name.
Import#
ts
import type { NodeFactory } from "@shadow-js/compiler";
Signature#
ts
interface NodeFactory {
/**
* An identifier.
* @param name A name that is a valid identifier and not a reserved word.
* @returns The name.
* @throws TypeError When `name` is not a valid identifier.
*/
identifier(name: string): string;
/**
* A string literal.
* @param value Any string.
* @returns The literal with quotes and escapes.
*/
string(value: string): string;
/**
* A call expression.
* @param callee An identifier or a dotted path (`console.log`).
* @param args Code for each argument.
* @returns `callee(arg, arg)`.
* @throws TypeError When `callee` is not an identifier path.
*/
call(callee: string, args?: readonly string[]): string;
/**
* A JSX attribute.
* @param name The attribute name (`data-testid`, `xlink:href`).
* @param value Code for the value, written as `name={value}`. Omit it for a bare attribute.
* @returns The attribute text.
* @throws TypeError When `name` is not a valid attribute name.
*/
jsxAttribute(name: string, value?: string): string;
/**
* JSX text that cannot be read as markup.
* @param value Any string.
* @returns `{"value"}`.
*/
jsxText(value: string): string;
/**
* A JSX element.
* @param tag A tag name (`div`, `Button`, `Foo.Bar`, `my-element`).
* @param attributes Attribute texts, as built by {@link NodeFactory.jsxAttribute}.
* @param children Child texts: elements, or {@link NodeFactory.jsxText}.
* @returns `<tag attributes>children</tag>`, or `<tag attributes />` without children.
* @throws TypeError When `tag` is not a valid tag name.
*/
jsxElement(tag: string, attributes?: readonly string[], children?: readonly string[]): string;
}
Members#
| Name | Type | Description |
|---|---|---|
identifier |
identifier(name: string): string |
An identifier. |
string |
string(value: string): string |
A string literal. |
call |
call(callee: string, args?: readonly string[]): string |
A call expression. |
jsxAttribute |
jsxAttribute(name: string, value?: string): string |
A JSX attribute. |
jsxText |
jsxText(value: string): string |
JSX text that cannot be read as markup. |
jsxElement |
jsxElement(tag: string, attributes?: readonly string[], children?: readonly string[]): string |
A JSX element. |