@pairdraw/emit 0.16.1 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunks/estree.js +8 -3
- package/dist/chunks/estree.js.map +1 -1
- package/dist/chunks/index.js +13 -5
- package/dist/chunks/index.js.map +1 -1
- package/dist/index.d.ts +6 -3
- package/dist/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/estree.ts +9 -4
- package/src/jsx.ts +13 -5
package/dist/chunks/estree.js
CHANGED
|
@@ -65,8 +65,10 @@ function parseType(source) {
|
|
|
65
65
|
/** A hole value that removes the object property holding it. */
|
|
66
66
|
const OMIT = Symbol("omit");
|
|
67
67
|
/**
|
|
68
|
-
* Replace `$name` placeholders — identifiers, type references, and string
|
|
69
|
-
* nodes. A
|
|
68
|
+
* Replace the template's `$name` placeholders — identifiers, type references, and string
|
|
69
|
+
* literals — with nodes. A hole's node goes in as given: placeholders inside it are not filled,
|
|
70
|
+
* so a name or text from the document that reads like `$name` stays as written. Fill a nested
|
|
71
|
+
* template first. A list item filled with `OMIT` is removed, and so is a property whose value is.
|
|
70
72
|
*/
|
|
71
73
|
function fill(template, holes) {
|
|
72
74
|
const hole = (name) => typeof name === "string" ? holes[name] : void 0;
|
|
@@ -75,7 +77,10 @@ function fill(template, holes) {
|
|
|
75
77
|
if (node.type === "Literal") return hole(node.value);
|
|
76
78
|
if (node.type === "TSTypeReference") return hole(child(node, "typeName")?.name);
|
|
77
79
|
};
|
|
78
|
-
const omitted = (node) =>
|
|
80
|
+
const omitted = (node) => {
|
|
81
|
+
const value = isNode(node) && node.type === "Property" ? child(node, "value") : node;
|
|
82
|
+
return isNode(value) && holeOf(value) === OMIT;
|
|
83
|
+
};
|
|
79
84
|
const replacement = (node) => {
|
|
80
85
|
const value = holeOf(node);
|
|
81
86
|
if (!value || value === OMIT) return void 0;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"estree.js","names":[],"sources":["../../src/estree.ts"],"sourcesContent":["import { tsPlugin } from '@sveltejs/acorn-typescript'\nimport { Parser } from 'acorn'\nimport { print } from 'esrap'\nimport ts from 'esrap/languages/ts'\n\n/** An ESTree or TS-ESTree node, as acorn parses it and esrap prints it. */\nexport interface SyntaxNode {\n type: string\n [key: string]: unknown\n}\n\nconst TSParser = Parser.extend(tsPlugin())\n\n/** Whether a value is a syntax node, as opposed to a primitive field of one. */\nexport function isNode(value: unknown): value is SyntaxNode {\n return typeof value === 'object' && value !== null && 'type' in value\n}\n\n/** The node under `key`, or undefined when it is absent or not a node. */\nexport function child(node: SyntaxNode | undefined, key: string): SyntaxNode | undefined {\n const value = node?.[key]\n return isNode(value) ? value : undefined\n}\n\nexport function children(node: SyntaxNode | undefined, key: string): SyntaxNode[] {\n const value = node?.[key]\n return Array.isArray(value) ? value.filter(isNode) : []\n}\n\nexport function parseModule(source: string): SyntaxNode & { body: SyntaxNode[] } {\n const program: unknown = TSParser.parse(source, { ecmaVersion: 'latest', sourceType: 'module' })\n if (!isNode(program)) throw new Error('Expected a module')\n return { ...program, body: children(program, 'body') }\n}\n\nexport function parseExpression(source: string): SyntaxNode {\n const expression = child(parseModule(`(${source})`).body.at(0), 'expression')\n if (!expression) throw new Error(`Expected an expression: ${source}`)\n return expression\n}\n\nexport function parseType(source: string): SyntaxNode {\n const type = child(parseModule(`type T = ${source}`).body.at(0), 'typeAnnotation')\n if (!type) throw new Error(`Expected a type: ${source}`)\n return type\n}\n\n/** A hole value that removes the object property holding it. */\nexport const OMIT = Symbol('omit')\n\n/**\n * Replace `$name` placeholders — identifiers, type references, and string literals — with\n * nodes. A property whose value is filled with `OMIT` is removed.\n */\nexport function fill<T extends SyntaxNode>(\n template: T,\n holes: Record<string, SyntaxNode | typeof OMIT>\n): T {\n const hole = (name: unknown) => (typeof name === 'string' ? holes[name] : undefined)\n const holeOf = (node: SyntaxNode) => {\n if (node.type === 'Identifier') return hole(node.name)\n if (node.type === 'Literal') return hole(node.value)\n if (node.type === 'TSTypeReference') return hole(child(node, 'typeName')?.name)\n return undefined\n }\n const omitted = (node: unknown) =>\n isNode(node) && node.type === 'Property' && hole(child(node, 'value')?.name) === OMIT\n const replacement = (node: SyntaxNode): SyntaxNode | undefined => {\n const value = holeOf(node)\n if (!value || value === OMIT) return undefined\n // `$name: Story` keeps its annotation when the identifier is filled, with its own holes filled.\n const annotation = child(node, 'typeAnnotation')\n return annotation\n ? { ...structuredClone(value), typeAnnotation: visit(annotation) }\n : structuredClone(value)\n }\n function visit(node: SyntaxNode): SyntaxNode {\n const filled = replacement(node)\n if (filled) return filled\n const copy: SyntaxNode = { ...node }\n for (const [key, value] of Object.entries(node)) {\n if (Array.isArray(value))\n copy[key] = value\n .filter((item) => !omitted(item))\n .map((item) => (isNode(item) ? visit(item) : item))\n else if (isNode(value)) copy[key] = visit(value)\n }\n return copy\n }\n return visit(template) as T\n}\n\nexport function printModule(program: SyntaxNode): string {\n return print(program, ts(), { indent: ' ' }).code\n}\n\n/** One expression, as a template binding or another host language embeds it. */\nexport function printExpression(expression: SyntaxNode): string {\n const program = {\n type: 'Program',\n sourceType: 'module',\n body: [{ type: 'ExpressionStatement', expression }]\n }\n return printModule(program).trim().replace(/;$/, '')\n}\n\nexport const identifier = (name: string): SyntaxNode => ({ type: 'Identifier', name })\n\nexport const string = (value: string): SyntaxNode => ({ type: 'Literal', value })\n\n/** A finite number, with a negative one written as `-` before its magnitude, as source does. */\nexport function number(value: number): SyntaxNode {\n if (!Number.isFinite(value)) throw new Error(`Not a finite number: ${value}`)\n const literal = { type: 'Literal', value: Math.abs(value) }\n return value < 0\n ? { type: 'UnaryExpression', operator: '-', prefix: true, argument: literal }\n : literal\n}\n\n/** Whether `name` can be written as it is, as a JavaScript identifier, rather than quoted. */\nexport const isIdentifierName = (name: string): boolean => /^[A-Za-z_$][\\w$]*$/.test(name)\n\nconst propertyKey = (key: string) => (isIdentifierName(key) ? identifier(key) : string(key))\n\nexport function object(entries: (readonly [string, SyntaxNode])[]): SyntaxNode {\n return {\n type: 'ObjectExpression',\n properties: entries.map(([key, value]) => ({\n type: 'Property',\n kind: 'init',\n key: propertyKey(key),\n value,\n computed: false,\n method: false,\n shorthand: false\n }))\n }\n}\n\nexport const array = (elements: SyntaxNode[]): SyntaxNode => ({ type: 'ArrayExpression', elements })\n\n/** `object.key`, or `object['key']` for a key that is not an identifier. */\nexport function member(object: SyntaxNode, key: string): SyntaxNode {\n const computed = !/^[A-Za-z_$][\\w$]*$/.test(key)\n return {\n type: 'MemberExpression',\n object,\n property: computed ? string(key) : identifier(key),\n computed,\n optional: false\n }\n}\n\n/**\n * A value read from JSON written as source: strings, finite numbers, booleans, null, and lists\n * and objects of them. Anything else, such as a function or an infinite number, is null.\n */\nexport function json(value: unknown): SyntaxNode {\n if (typeof value === 'string') return string(value)\n if (typeof value === 'number') return Number.isFinite(value) ? number(value) : json(null)\n if (typeof value === 'boolean' || value === null) return { type: 'Literal', value }\n if (Array.isArray(value)) return array(value.map(json))\n if (typeof value === 'object')\n return object(Object.entries(value).map(([key, item]) => [key, json(item)]))\n return json(null)\n}\n\n/** A call of a function by name, such as a helper inside an exported prop value. */\nexport const call = (callee: string, args: SyntaxNode[]): SyntaxNode => ({\n type: 'CallExpression',\n callee: identifier(callee),\n arguments: args,\n optional: false\n})\n\nexport function stringUnionType(values: string[]): SyntaxNode {\n const literals = values.map((value) => ({ type: 'TSLiteralType', literal: string(value) }))\n return literals.length === 1 && literals[0]\n ? literals[0]\n : { type: 'TSUnionType', types: literals }\n}\n\n/** An object type; a member whose third element is `true` is optional. */\nexport function objectType(entries: [string, SyntaxNode, boolean?][]): SyntaxNode {\n return {\n type: 'TSTypeLiteral',\n members: entries.map(([key, typeAnnotation, optional = false]) => ({\n type: 'TSPropertySignature',\n key: propertyKey(key),\n computed: false,\n optional,\n typeAnnotation: { type: 'TSTypeAnnotation', typeAnnotation }\n }))\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWA,MAAM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AAO,OAAO,QAAQ,KAAK,CAAC,CAAC,KAAK,CAAC,KAAK,UAAU,CAAC,KAAK,KAAK,IAAI,CAAC,CAAC,CAAC;CAC7E,OAAO,KAAK,IAAI;AAClB;;AAGA,MAAa,QAAQ,QAAgB,UAAoC;CACvE,MAAM;CACN,QAAQ,WAAW,MAAM;CACzB,WAAW;CACX,UAAU;AACZ;AAEA,SAAgB,gBAAgB,QAA8B;CAC5D,MAAM,WAAW,OAAO,KAAK,WAAW;EAAE,MAAM;EAAiB,SAAS,OAAO,KAAK;CAAE,EAAE;CAC1F,OAAO,SAAS,WAAW,KAAK,SAAS,KACrC,SAAS,KACT;EAAE,MAAM;EAAe,OAAO;CAAS;AAC7C;;AAGA,SAAgB,WAAW,SAAuD;CAChF,OAAO;EACL,MAAM;EACN,SAAS,QAAQ,KAAK,CAAC,KAAK,gBAAgB,WAAW,YAAY;GACjE,MAAM;GACN,KAAK,YAAY,GAAG;GACpB,UAAU;GACV;GACA,gBAAgB;IAAE,MAAM;IAAoB;GAAe;EAC7D,EAAE;CACJ;AACF"}
|
|
1
|
+
{"version":3,"file":"estree.js","names":[],"sources":["../../src/estree.ts"],"sourcesContent":["import { tsPlugin } from '@sveltejs/acorn-typescript'\nimport { Parser } from 'acorn'\nimport { print } from 'esrap'\nimport ts from 'esrap/languages/ts'\n\n/** An ESTree or TS-ESTree node, as acorn parses it and esrap prints it. */\nexport interface SyntaxNode {\n type: string\n [key: string]: unknown\n}\n\nconst TSParser = Parser.extend(tsPlugin())\n\n/** Whether a value is a syntax node, as opposed to a primitive field of one. */\nexport function isNode(value: unknown): value is SyntaxNode {\n return typeof value === 'object' && value !== null && 'type' in value\n}\n\n/** The node under `key`, or undefined when it is absent or not a node. */\nexport function child(node: SyntaxNode | undefined, key: string): SyntaxNode | undefined {\n const value = node?.[key]\n return isNode(value) ? value : undefined\n}\n\nexport function children(node: SyntaxNode | undefined, key: string): SyntaxNode[] {\n const value = node?.[key]\n return Array.isArray(value) ? value.filter(isNode) : []\n}\n\nexport function parseModule(source: string): SyntaxNode & { body: SyntaxNode[] } {\n const program: unknown = TSParser.parse(source, { ecmaVersion: 'latest', sourceType: 'module' })\n if (!isNode(program)) throw new Error('Expected a module')\n return { ...program, body: children(program, 'body') }\n}\n\nexport function parseExpression(source: string): SyntaxNode {\n const expression = child(parseModule(`(${source})`).body.at(0), 'expression')\n if (!expression) throw new Error(`Expected an expression: ${source}`)\n return expression\n}\n\nexport function parseType(source: string): SyntaxNode {\n const type = child(parseModule(`type T = ${source}`).body.at(0), 'typeAnnotation')\n if (!type) throw new Error(`Expected a type: ${source}`)\n return type\n}\n\n/** A hole value that removes the object property holding it. */\nexport const OMIT = Symbol('omit')\n\n/**\n * Replace the template's `$name` placeholders — identifiers, type references, and string\n * literals — with nodes. A hole's node goes in as given: placeholders inside it are not filled,\n * so a name or text from the document that reads like `$name` stays as written. Fill a nested\n * template first. A list item filled with `OMIT` is removed, and so is a property whose value is.\n */\nexport function fill<T extends SyntaxNode>(\n template: T,\n holes: Record<string, SyntaxNode | typeof OMIT>\n): T {\n const hole = (name: unknown) => (typeof name === 'string' ? holes[name] : undefined)\n const holeOf = (node: SyntaxNode) => {\n if (node.type === 'Identifier') return hole(node.name)\n if (node.type === 'Literal') return hole(node.value)\n if (node.type === 'TSTypeReference') return hole(child(node, 'typeName')?.name)\n return undefined\n }\n // An `OMIT` hole leaves the list holding it, and takes the property it is the value of along.\n const omitted = (node: unknown) => {\n const value = isNode(node) && node.type === 'Property' ? child(node, 'value') : node\n return isNode(value) && holeOf(value) === OMIT\n }\n const replacement = (node: SyntaxNode): SyntaxNode | undefined => {\n const value = holeOf(node)\n if (!value || value === OMIT) return undefined\n // `$name: Story` keeps its annotation when the identifier is filled, with its own holes filled.\n const annotation = child(node, 'typeAnnotation')\n return annotation\n ? { ...structuredClone(value), typeAnnotation: visit(annotation) }\n : structuredClone(value)\n }\n function visit(node: SyntaxNode): SyntaxNode {\n const filled = replacement(node)\n if (filled) return filled\n const copy: SyntaxNode = { ...node }\n for (const [key, value] of Object.entries(node)) {\n if (Array.isArray(value))\n copy[key] = value\n .filter((item) => !omitted(item))\n .map((item) => (isNode(item) ? visit(item) : item))\n else if (isNode(value)) copy[key] = visit(value)\n }\n return copy\n }\n return visit(template) as T\n}\n\nexport function printModule(program: SyntaxNode): string {\n return print(program, ts(), { indent: ' ' }).code\n}\n\n/** One expression, as a template binding or another host language embeds it. */\nexport function printExpression(expression: SyntaxNode): string {\n const program = {\n type: 'Program',\n sourceType: 'module',\n body: [{ type: 'ExpressionStatement', expression }]\n }\n return printModule(program).trim().replace(/;$/, '')\n}\n\nexport const identifier = (name: string): SyntaxNode => ({ type: 'Identifier', name })\n\nexport const string = (value: string): SyntaxNode => ({ type: 'Literal', value })\n\n/** A finite number, with a negative one written as `-` before its magnitude, as source does. */\nexport function number(value: number): SyntaxNode {\n if (!Number.isFinite(value)) throw new Error(`Not a finite number: ${value}`)\n const literal = { type: 'Literal', value: Math.abs(value) }\n return value < 0\n ? { type: 'UnaryExpression', operator: '-', prefix: true, argument: literal }\n : literal\n}\n\n/** Whether `name` can be written as it is, as a JavaScript identifier, rather than quoted. */\nexport const isIdentifierName = (name: string): boolean => /^[A-Za-z_$][\\w$]*$/.test(name)\n\nconst propertyKey = (key: string) => (isIdentifierName(key) ? identifier(key) : string(key))\n\nexport function object(entries: (readonly [string, SyntaxNode])[]): SyntaxNode {\n return {\n type: 'ObjectExpression',\n properties: entries.map(([key, value]) => ({\n type: 'Property',\n kind: 'init',\n key: propertyKey(key),\n value,\n computed: false,\n method: false,\n shorthand: false\n }))\n }\n}\n\nexport const array = (elements: SyntaxNode[]): SyntaxNode => ({ type: 'ArrayExpression', elements })\n\n/** `object.key`, or `object['key']` for a key that is not an identifier. */\nexport function member(object: SyntaxNode, key: string): SyntaxNode {\n const computed = !/^[A-Za-z_$][\\w$]*$/.test(key)\n return {\n type: 'MemberExpression',\n object,\n property: computed ? string(key) : identifier(key),\n computed,\n optional: false\n }\n}\n\n/**\n * A value read from JSON written as source: strings, finite numbers, booleans, null, and lists\n * and objects of them. Anything else, such as a function or an infinite number, is null.\n */\nexport function json(value: unknown): SyntaxNode {\n if (typeof value === 'string') return string(value)\n if (typeof value === 'number') return Number.isFinite(value) ? number(value) : json(null)\n if (typeof value === 'boolean' || value === null) return { type: 'Literal', value }\n if (Array.isArray(value)) return array(value.map(json))\n if (typeof value === 'object')\n return object(Object.entries(value).map(([key, item]) => [key, json(item)]))\n return json(null)\n}\n\n/** A call of a function by name, such as a helper inside an exported prop value. */\nexport const call = (callee: string, args: SyntaxNode[]): SyntaxNode => ({\n type: 'CallExpression',\n callee: identifier(callee),\n arguments: args,\n optional: false\n})\n\nexport function stringUnionType(values: string[]): SyntaxNode {\n const literals = values.map((value) => ({ type: 'TSLiteralType', literal: string(value) }))\n return literals.length === 1 && literals[0]\n ? literals[0]\n : { type: 'TSUnionType', types: literals }\n}\n\n/** An object type; a member whose third element is `true` is optional. */\nexport function objectType(entries: [string, SyntaxNode, boolean?][]): SyntaxNode {\n return {\n type: 'TSTypeLiteral',\n members: entries.map(([key, typeAnnotation, optional = false]) => ({\n type: 'TSPropertySignature',\n key: propertyKey(key),\n computed: false,\n optional,\n typeAnnotation: { type: 'TSTypeAnnotation', typeAnnotation }\n }))\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWA,MAAM,WAAW,OAAO,OAAO,SAAS,CAAC;;AAGzC,SAAgB,OAAO,OAAqC;CAC1D,OAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,UAAU;AAClE;;AAGA,SAAgB,MAAM,MAA8B,KAAqC;CACvF,MAAM,QAAQ,OAAO;CACrB,OAAO,OAAO,KAAK,IAAI,QAAQ,KAAA;AACjC;AAEA,SAAgB,SAAS,MAA8B,KAA2B;CAChF,MAAM,QAAQ,OAAO;CACrB,OAAO,MAAM,QAAQ,KAAK,IAAI,MAAM,OAAO,MAAM,IAAI,CAAC;AACxD;AAEA,SAAgB,YAAY,QAA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AAO,KAAK;CAAE,EAAE;CAC1F,OAAO,SAAS,WAAW,KAAK,SAAS,KACrC,SAAS,KACT;EAAE,MAAM;EAAe,OAAO;CAAS;AAC7C;;AAGA,SAAgB,WAAW,SAAuD;CAChF,OAAO;EACL,MAAM;EACN,SAAS,QAAQ,KAAK,CAAC,KAAK,gBAAgB,WAAW,YAAY;GACjE,MAAM;GACN,KAAK,YAAY,GAAG;GACpB,UAAU;GACV;GACA,gBAAgB;IAAE,MAAM;IAAoB;GAAe;EAC7D,EAAE;CACJ;AACF"}
|
package/dist/chunks/index.js
CHANGED
|
@@ -62,11 +62,19 @@ const spread = (argument) => ({
|
|
|
62
62
|
argument
|
|
63
63
|
});
|
|
64
64
|
/** An attribute; a null value prints the bare name, as for `true`. */
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
name:
|
|
68
|
-
|
|
69
|
-
|
|
65
|
+
/** An attribute. A name such as `xlink:href` is namespaced, as JSX parses it. */
|
|
66
|
+
function attribute$1(name, value) {
|
|
67
|
+
const colon = name.indexOf(":");
|
|
68
|
+
return {
|
|
69
|
+
type: "JSXAttribute",
|
|
70
|
+
name: colon > 0 ? {
|
|
71
|
+
type: "JSXNamespacedName",
|
|
72
|
+
namespace: identifier(name.slice(0, colon)),
|
|
73
|
+
name: identifier(name.slice(colon + 1))
|
|
74
|
+
} : identifier(name),
|
|
75
|
+
value
|
|
76
|
+
};
|
|
77
|
+
}
|
|
70
78
|
/** Text content: plain JSX text when it survives as written, a string expression otherwise. */
|
|
71
79
|
function text$1(value) {
|
|
72
80
|
return LITERAL_TEXT.test(value) && value.trim() === value && value.length > 0 ? whitespace(value) : container(literal(value));
|
package/dist/chunks/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["attribute","text","element","printModule"],"sources":["../../src/jsx.ts","../../src/vue.ts"],"sourcesContent":["import { print } from 'esrap'\nimport tsx from 'esrap/languages/tsx'\n\nimport { isNode, type SyntaxNode } from './estree'\n\n/**\n * JSX attribute strings end at `\"`, decode `&` entities, and keep backslashes literally,\n * so the printer's escapes for backslashes and line breaks would change the value. JSX\n * text also treats braces and angle brackets as syntax and trims whitespace at line\n * edges. Anything else is written as a string literal, which the printer escapes.\n */\nconst LITERAL_ATTRIBUTE = /^[^\"&\\\\\\r\\n]*$/\nconst LITERAL_TEXT = /^[^{}<>&\\r\\n]*$/\n\nexport const identifier = (name: string): SyntaxNode => ({ type: 'JSXIdentifier', name })\n\n/** A tag name; a dotted one such as `Switch.Root` is a member of a namespace. */\nfunction tagName(tag: string): SyntaxNode {\n const [first = tag, ...rest] = tag.split('.')\n return rest.reduce<SyntaxNode>(\n (object, property) => ({ type: 'JSXMemberExpression', object, property: identifier(property) }),\n identifier(first)\n )\n}\n\nexport const literal = (value: string | number | boolean): SyntaxNode => ({\n type: 'Literal',\n value\n})\n\nexport const container = (expression: SyntaxNode): SyntaxNode => ({\n type: 'JSXExpressionContainer',\n expression\n})\n\n/** Raw JSX text, printed as is. */\nexport const whitespace = (value: string): SyntaxNode => ({ type: 'JSXText', value, raw: value })\n\n/** A string attribute value: quoted when JSX keeps it as written, an expression otherwise. */\nexport const stringValue = (value: string): SyntaxNode =>\n LITERAL_ATTRIBUTE.test(value) ? literal(value) : container(literal(value))\n\n/** `{...expression}`, passing an object's properties on as attributes. */\nexport const spread = (argument: SyntaxNode): SyntaxNode => ({\n type: 'JSXSpreadAttribute',\n argument\n})\n\n/** An attribute; a null value prints the bare name, as for `true`. */\nexport const attribute = (name: string, value: SyntaxNode | null): SyntaxNode => ({\n type: 'JSXAttribute',\n name: identifier(name),\n value\n})\n\n/** Text content: plain JSX text when it survives as written, a string expression otherwise. */\nexport function text(value: string): SyntaxNode {\n const plain = LITERAL_TEXT.test(value) && value.trim() === value && value.length > 0\n return plain ? whitespace(value) : container(literal(value))\n}\n\n/**\n * Text that shares a line with elements, as beside a span. JSX trims whitespace only at line\n * edges, so text without a line break is kept as written, spaces at its ends included.\n */\nexport function inlineText(value: string): SyntaxNode {\n const plain = LITERAL_TEXT.test(value) && value.length > 0\n return plain ? whitespace(value) : container(literal(value))\n}\n\n/** Children on their own lines; JSX drops whitespace-only lines between elements. */\nfunction indented(children: SyntaxNode[], depth: number): SyntaxNode[] {\n const inner = `\\n${' '.repeat(depth + 1)}`\n return [\n ...children.flatMap((child) => [whitespace(inner), child]),\n whitespace(`\\n${' '.repeat(depth)}`)\n ]\n}\n\n/**\n * An element at nesting `depth`. Children go on their own lines unless `inline`, as for a\n * lone text child; without children the element self-closes.\n */\nexport function element(\n tag: string,\n attributes: SyntaxNode[],\n children: SyntaxNode[],\n depth: number,\n inline = false\n): SyntaxNode {\n const name = tagName(tag)\n const content = inline || children.length === 0 ? children : indented(children, depth)\n return {\n type: 'JSXElement',\n openingElement: {\n type: 'JSXOpeningElement',\n name,\n attributes,\n selfClosing: content.length === 0\n },\n closingElement: content.length > 0 ? { type: 'JSXClosingElement', name } : null,\n children: content\n }\n}\n\n/** `<>…</>`: children without an element around them, laid out as an element's are. */\nexport function fragment(children: SyntaxNode[], depth: number): SyntaxNode {\n return {\n type: 'JSXFragment',\n openingFragment: { type: 'JSXOpeningFragment' },\n closingFragment: { type: 'JSXClosingFragment' },\n children: children.length === 0 ? [] : indented(children, depth)\n }\n}\n\nexport function printJSX(node: SyntaxNode): string {\n return print(node, tsx({ quotes: 'double' }), { indent: ' ' }).code\n}\n\n/** A copy without strings' source spelling, so the printer quotes every string the same way. */\nfunction withoutRawStrings(node: SyntaxNode): SyntaxNode {\n const copy: SyntaxNode = { ...node }\n if (node.type === 'Literal' && typeof node.value === 'string') delete copy.raw\n for (const [key, value] of Object.entries(node)) {\n if (Array.isArray(value))\n copy[key] = value.map((item: unknown) => (isNode(item) ? withoutRawStrings(item) : item))\n else if (isNode(value)) copy[key] = withoutRawStrings(value)\n }\n return copy\n}\n\n/**\n * A TSX module, such as a component built with `es` templates and JSX from this module. Strings\n * parsed from a template and strings built here print with the same double quotes.\n */\nexport function printModule(program: SyntaxNode): string {\n return print(withoutRawStrings(program), tsx({ quotes: 'double' }), { indent: ' ' }).code\n}\n","import { escapeAttribute, escapeText } from 'entities'\n\nimport { printExpression, printModule, type SyntaxNode } from './estree'\n\n/**\n * Text and attribute values are escaped as HTML by `entities`. Vue also reads `{{` as an\n * interpolation before it decodes entities, so text writes `{` as one too and a design's text\n * can never become template syntax.\n */\nconst templateText = (value: string) => escapeText(value).replaceAll('{', '{')\n\nexport type VueAttribute =\n | { type: 'static'; name: string; value: string }\n /** `:name=\"expression\"` */\n | { type: 'bound'; name: string; expression: SyntaxNode }\n /** `v-model=\"expression\"`, or `v-model:argument` */\n | { type: 'model'; argument?: string; expression: SyntaxNode }\n /** `v-if=\"expression\"`: the element renders only while the expression holds. */\n | { type: 'if'; expression: SyntaxNode }\n\nexport interface VueElement {\n type: 'element'\n tag: string\n attributes: VueAttribute[]\n children: VueNode[]\n}\n\nexport interface VueText {\n type: 'text'\n value: string\n}\n\n/** `{{ expression }}`, printed from a syntax tree so it holds only what the tree says. */\nexport interface VueInterpolation {\n type: 'interpolation'\n expression: SyntaxNode\n}\n\nexport type VueNode = VueElement | VueText | VueInterpolation\n\nexport const attribute = (name: string, value: string): VueAttribute => ({\n type: 'static',\n name,\n value\n})\n\nexport const bound = (name: string, expression: SyntaxNode): VueAttribute => ({\n type: 'bound',\n name,\n expression\n})\n\nexport const model = (expression: SyntaxNode, argument?: string): VueAttribute => ({\n type: 'model',\n argument,\n expression\n})\n\nexport const renderIf = (expression: SyntaxNode): VueAttribute => ({ type: 'if', expression })\n\nexport const element = (\n tag: string,\n attributes: VueAttribute[] = [],\n children: VueNode[] = []\n): VueElement => ({ type: 'element', tag, attributes, children })\n\nexport const text = (value: string): VueText => ({ type: 'text', value })\n\nexport const interpolation = (expression: SyntaxNode): VueInterpolation => ({\n type: 'interpolation',\n expression\n})\n\nfunction printAttribute(item: VueAttribute): string {\n if (item.type === 'static') return `${item.name}=\"${escapeAttribute(item.value)}\"`\n const value = escapeAttribute(printExpression(item.expression))\n if (item.type === 'bound') return `:${item.name}=\"${value}\"`\n if (item.type === 'if') return `v-if=\"${value}\"`\n return `${item.argument ? `v-model:${item.argument}` : 'v-model'}=\"${value}\"`\n}\n\n/** Markup with nothing added around it, as text content needs. */\nfunction printInline(node: VueNode): string {\n if (node.type === 'text') return templateText(node.value)\n if (node.type === 'interpolation') {\n const expression = printExpression(node.expression)\n // The template ends an interpolation at the first `}}`, wherever it falls.\n if (expression.includes('}}'))\n throw new Error(`An interpolated expression cannot contain \"}}\": ${expression}`)\n return `{{ ${expression} }}`\n }\n const open = [node.tag, ...node.attributes.map(printAttribute)].join(' ')\n if (node.children.length === 0) return `<${open} />`\n return `<${open}>${node.children.map(printInline).join('')}</${node.tag}>`\n}\n\nfunction printNode(node: VueNode, depth: number): string {\n const indent = ' '.repeat(depth)\n // Vue renders whitespace between text and elements as a space, so an element with text or\n // an interpolation in it keeps its children on its own line, exactly as given.\n if (node.type !== 'element' || node.children.some((child) => child.type !== 'element'))\n return `${indent}${printInline(node)}`\n const open = [node.tag, ...node.attributes.map(printAttribute)].join(' ')\n if (node.children.length === 0) return `${indent}<${open} />`\n const children = node.children.map((child) => printNode(child, depth + 1))\n return [`${indent}<${open}>`, ...children, `${indent}</${node.tag}>`].join('\\n')\n}\n\n/** A template's markup, one element per line. */\nexport function printTemplate(root: VueNode): string {\n return printNode(root, 0)\n}\n\nexport interface VueComponent {\n /** The `<script setup lang=\"ts\">` body, a TypeScript program from `es`. */\n script: SyntaxNode\n template: VueNode\n /** Stylesheet text for a `<style scoped>` block. */\n style?: string\n}\n\n/**\n * A raw block's content, which ends at the first closing tag for its element. Strings in the\n * script and values in the stylesheet can come from an imported design, so a closing tag in\n * them is written `<\\\\/`, which JavaScript strings and CSS both read back as `</`.\n */\nconst rawBlock = (tag: string, content: string) =>\n `<${tag}>\\n${content.trim().replace(/<\\/(?=script|style)/gi, '<\\\\/')}\\n</${tag.split(' ')[0]}>`\n\n/** Whether a component's script has anything to write, so it gets a script block. */\nconst hasScript = (script: SyntaxNode) => printModule(script).trim() !== ''\n\nfunction templateElements(node: VueNode): VueElement[] {\n return node.type === 'element' ? [node, ...node.children.flatMap(templateElements)] : []\n}\n\n/**\n * The elements `printComponent` writes, in the order they open: the template's own, between\n * `null`s for the script, template, and style blocks that hold them.\n */\nexport function componentElements({\n script,\n template,\n style\n}: VueComponent): (VueElement | null)[] {\n return [\n ...(hasScript(script) ? [null] : []),\n null,\n ...templateElements(template),\n ...(style ? [null] : [])\n ]\n}\n\n/** A single-file component: script setup, template, and scoped style. */\nexport function printComponent({ script, template, style }: VueComponent): string {\n const code = printModule(script)\n // A component with nothing to import or declare has no script block.\n const blocks = [\n ...(code.trim() ? [rawBlock('script setup lang=\"ts\"', code)] : []),\n `<template>\\n${printNode(template, 1)}\\n</template>`,\n ...(style ? [rawBlock('style scoped', style)] : [])\n ]\n return `${blocks.join('\\n\\n')}\\n`\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAWA,MAAM,oBAAoB;AAC1B,MAAM,eAAe;AAErB,MAAa,cAAc,UAA8B;CAAE,MAAM;CAAiB;AAAK;;AAGvF,SAAS,QAAQ,KAAyB;CACxC,MAAM,CAAC,QAAQ,KAAK,GAAG,QAAQ,IAAI,MAAM,GAAG;CAC5C,OAAO,KAAK,QACT,QAAQ,cAAc;EAAE,MAAM;EAAuB;EAAQ,UAAU,WAAW,QAAQ;CAAE,IAC7F,WAAW,KAAK,CAClB;AACF;AAEA,MAAa,WAAW,WAAkD;CACxE,MAAM;CACN;AACF;AAEA,MAAa,aAAa,gBAAwC;CAChE,MAAM;CACN;AACF;;AAGA,MAAa,cAAc,WAA+B;CAAE,MAAM;CAAW;CAAO,KAAK;AAAM;;AAG/F,MAAa,eAAe,UAC1B,kBAAkB,KAAK,KAAK,IAAI,QAAQ,KAAK,IAAI,UAAU,QAAQ,KAAK,CAAC;;AAG3E,MAAa,UAAU,cAAsC;CAC3D,MAAM;CACN;AACF;;AAGA,MA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AACjF;;AAGA,SAAgB,cAAc,MAAuB;CACnD,OAAO,UAAU,MAAM,CAAC;AAC1B;;;;;;AAeA,MAAM,YAAY,KAAa,YAC7B,IAAI,IAAI,KAAK,QAAQ,KAAK,CAAC,CAAC,QAAQ,yBAAyB,MAAM,EAAE,MAAM,IAAI,MAAM,GAAG,CAAC,CAAC,GAAG;;AAG/F,MAAM,aAAa,WAAuBC,cAAY,MAAM,CAAC,CAAC,KAAK,MAAM;AAEzE,SAAS,iBAAiB,MAA6B;CACrD,OAAO,KAAK,SAAS,YAAY,CAAC,MAAM,GAAG,KAAK,SAAS,QAAQ,gBAAgB,CAAC,IAAI,CAAC;AACzF;;;;;AAMA,SAAgB,kBAAkB,EAChC,QACA,UACA,SACsC;CACtC,OAAO;EACL,GAAI,UAAU,MAAM,IAAI,CAAC,IAAI,IAAI,CAAC;EAClC;EACA,GAAG,iBAAiB,QAAQ;EAC5B,GAAI,QAAQ,CAAC,IAAI,IAAI,CAAC;CACxB;AACF;;AAGA,SAAgB,eAAe,EAAE,QAAQ,UAAU,SAA+B;CAChF,MAAM,OAAOA,cAAY,MAAM;CAO/B,OAAO,GAAG;EAJR,GAAI,KAAK,KAAK,IAAI,CAAC,SAAS,4BAA0B,IAAI,CAAC,IAAI,CAAC;EAChE,eAAe,UAAU,UAAU,CAAC,EAAE;EACtC,GAAI,QAAQ,CAAC,SAAS,gBAAgB,KAAK,CAAC,IAAI,CAAC;CAEpC,CAAC,CAAC,KAAK,MAAM,EAAE;AAChC"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["attribute","text","element","printModule"],"sources":["../../src/jsx.ts","../../src/vue.ts"],"sourcesContent":["import { print } from 'esrap'\nimport tsx from 'esrap/languages/tsx'\n\nimport { isNode, type SyntaxNode } from './estree'\n\n/**\n * JSX attribute strings end at `\"`, decode `&` entities, and keep backslashes literally,\n * so the printer's escapes for backslashes and line breaks would change the value. JSX\n * text also treats braces and angle brackets as syntax and trims whitespace at line\n * edges. Anything else is written as a string literal, which the printer escapes.\n */\nconst LITERAL_ATTRIBUTE = /^[^\"&\\\\\\r\\n]*$/\nconst LITERAL_TEXT = /^[^{}<>&\\r\\n]*$/\n\nexport const identifier = (name: string): SyntaxNode => ({ type: 'JSXIdentifier', name })\n\n/** A tag name; a dotted one such as `Switch.Root` is a member of a namespace. */\nfunction tagName(tag: string): SyntaxNode {\n const [first = tag, ...rest] = tag.split('.')\n return rest.reduce<SyntaxNode>(\n (object, property) => ({ type: 'JSXMemberExpression', object, property: identifier(property) }),\n identifier(first)\n )\n}\n\nexport const literal = (value: string | number | boolean): SyntaxNode => ({\n type: 'Literal',\n value\n})\n\nexport const container = (expression: SyntaxNode): SyntaxNode => ({\n type: 'JSXExpressionContainer',\n expression\n})\n\n/** Raw JSX text, printed as is. */\nexport const whitespace = (value: string): SyntaxNode => ({ type: 'JSXText', value, raw: value })\n\n/** A string attribute value: quoted when JSX keeps it as written, an expression otherwise. */\nexport const stringValue = (value: string): SyntaxNode =>\n LITERAL_ATTRIBUTE.test(value) ? literal(value) : container(literal(value))\n\n/** `{...expression}`, passing an object's properties on as attributes. */\nexport const spread = (argument: SyntaxNode): SyntaxNode => ({\n type: 'JSXSpreadAttribute',\n argument\n})\n\n/** An attribute; a null value prints the bare name, as for `true`. */\n/** An attribute. A name such as `xlink:href` is namespaced, as JSX parses it. */\nexport function attribute(name: string, value: SyntaxNode | null): SyntaxNode {\n const colon = name.indexOf(':')\n const attributeName =\n colon > 0\n ? {\n type: 'JSXNamespacedName',\n namespace: identifier(name.slice(0, colon)),\n name: identifier(name.slice(colon + 1))\n }\n : identifier(name)\n return { type: 'JSXAttribute', name: attributeName, value }\n}\n\n/** Text content: plain JSX text when it survives as written, a string expression otherwise. */\nexport function text(value: string): SyntaxNode {\n const plain = LITERAL_TEXT.test(value) && value.trim() === value && value.length > 0\n return plain ? whitespace(value) : container(literal(value))\n}\n\n/**\n * Text that shares a line with elements, as beside a span. JSX trims whitespace only at line\n * edges, so text without a line break is kept as written, spaces at its ends included.\n */\nexport function inlineText(value: string): SyntaxNode {\n const plain = LITERAL_TEXT.test(value) && value.length > 0\n return plain ? whitespace(value) : container(literal(value))\n}\n\n/** Children on their own lines; JSX drops whitespace-only lines between elements. */\nfunction indented(children: SyntaxNode[], depth: number): SyntaxNode[] {\n const inner = `\\n${' '.repeat(depth + 1)}`\n return [\n ...children.flatMap((child) => [whitespace(inner), child]),\n whitespace(`\\n${' '.repeat(depth)}`)\n ]\n}\n\n/**\n * An element at nesting `depth`. Children go on their own lines unless `inline`, as for a\n * lone text child; without children the element self-closes.\n */\nexport function element(\n tag: string,\n attributes: SyntaxNode[],\n children: SyntaxNode[],\n depth: number,\n inline = false\n): SyntaxNode {\n const name = tagName(tag)\n const content = inline || children.length === 0 ? children : indented(children, depth)\n return {\n type: 'JSXElement',\n openingElement: {\n type: 'JSXOpeningElement',\n name,\n attributes,\n selfClosing: content.length === 0\n },\n closingElement: content.length > 0 ? { type: 'JSXClosingElement', name } : null,\n children: content\n }\n}\n\n/** `<>…</>`: children without an element around them, laid out as an element's are. */\nexport function fragment(children: SyntaxNode[], depth: number): SyntaxNode {\n return {\n type: 'JSXFragment',\n openingFragment: { type: 'JSXOpeningFragment' },\n closingFragment: { type: 'JSXClosingFragment' },\n children: children.length === 0 ? [] : indented(children, depth)\n }\n}\n\nexport function printJSX(node: SyntaxNode): string {\n return print(node, tsx({ quotes: 'double' }), { indent: ' ' }).code\n}\n\n/** A copy without strings' source spelling, so the printer quotes every string the same way. */\nfunction withoutRawStrings(node: SyntaxNode): SyntaxNode {\n const copy: SyntaxNode = { ...node }\n if (node.type === 'Literal' && typeof node.value === 'string') delete copy.raw\n for (const [key, value] of Object.entries(node)) {\n if (Array.isArray(value))\n copy[key] = value.map((item: unknown) => (isNode(item) ? withoutRawStrings(item) : item))\n else if (isNode(value)) copy[key] = withoutRawStrings(value)\n }\n return copy\n}\n\n/**\n * A TSX module, such as a component built with `es` templates and JSX from this module. Strings\n * parsed from a template and strings built here print with the same double quotes.\n */\nexport function printModule(program: SyntaxNode): string {\n return print(withoutRawStrings(program), tsx({ quotes: 'double' }), { indent: ' ' }).code\n}\n","import { escapeAttribute, escapeText } from 'entities'\n\nimport { printExpression, printModule, type SyntaxNode } from './estree'\n\n/**\n * Text and attribute values are escaped as HTML by `entities`. Vue also reads `{{` as an\n * interpolation before it decodes entities, so text writes `{` as one too and a design's text\n * can never become template syntax.\n */\nconst templateText = (value: string) => escapeText(value).replaceAll('{', '{')\n\nexport type VueAttribute =\n | { type: 'static'; name: string; value: string }\n /** `:name=\"expression\"` */\n | { type: 'bound'; name: string; expression: SyntaxNode }\n /** `v-model=\"expression\"`, or `v-model:argument` */\n | { type: 'model'; argument?: string; expression: SyntaxNode }\n /** `v-if=\"expression\"`: the element renders only while the expression holds. */\n | { type: 'if'; expression: SyntaxNode }\n\nexport interface VueElement {\n type: 'element'\n tag: string\n attributes: VueAttribute[]\n children: VueNode[]\n}\n\nexport interface VueText {\n type: 'text'\n value: string\n}\n\n/** `{{ expression }}`, printed from a syntax tree so it holds only what the tree says. */\nexport interface VueInterpolation {\n type: 'interpolation'\n expression: SyntaxNode\n}\n\nexport type VueNode = VueElement | VueText | VueInterpolation\n\nexport const attribute = (name: string, value: string): VueAttribute => ({\n type: 'static',\n name,\n value\n})\n\nexport const bound = (name: string, expression: SyntaxNode): VueAttribute => ({\n type: 'bound',\n name,\n expression\n})\n\nexport const model = (expression: SyntaxNode, argument?: string): VueAttribute => ({\n type: 'model',\n argument,\n expression\n})\n\nexport const renderIf = (expression: SyntaxNode): VueAttribute => ({ type: 'if', expression })\n\nexport const element = (\n tag: string,\n attributes: VueAttribute[] = [],\n children: VueNode[] = []\n): VueElement => ({ type: 'element', tag, attributes, children })\n\nexport const text = (value: string): VueText => ({ type: 'text', value })\n\nexport const interpolation = (expression: SyntaxNode): VueInterpolation => ({\n type: 'interpolation',\n expression\n})\n\nfunction printAttribute(item: VueAttribute): string {\n if (item.type === 'static') return `${item.name}=\"${escapeAttribute(item.value)}\"`\n const value = escapeAttribute(printExpression(item.expression))\n if (item.type === 'bound') return `:${item.name}=\"${value}\"`\n if (item.type === 'if') return `v-if=\"${value}\"`\n return `${item.argument ? `v-model:${item.argument}` : 'v-model'}=\"${value}\"`\n}\n\n/** Markup with nothing added around it, as text content needs. */\nfunction printInline(node: VueNode): string {\n if (node.type === 'text') return templateText(node.value)\n if (node.type === 'interpolation') {\n const expression = printExpression(node.expression)\n // The template ends an interpolation at the first `}}`, wherever it falls.\n if (expression.includes('}}'))\n throw new Error(`An interpolated expression cannot contain \"}}\": ${expression}`)\n return `{{ ${expression} }}`\n }\n const open = [node.tag, ...node.attributes.map(printAttribute)].join(' ')\n if (node.children.length === 0) return `<${open} />`\n return `<${open}>${node.children.map(printInline).join('')}</${node.tag}>`\n}\n\nfunction printNode(node: VueNode, depth: number): string {\n const indent = ' '.repeat(depth)\n // Vue renders whitespace between text and elements as a space, so an element with text or\n // an interpolation in it keeps its children on its own line, exactly as given.\n if (node.type !== 'element' || node.children.some((child) => child.type !== 'element'))\n return `${indent}${printInline(node)}`\n const open = [node.tag, ...node.attributes.map(printAttribute)].join(' ')\n if (node.children.length === 0) return `${indent}<${open} />`\n const children = node.children.map((child) => printNode(child, depth + 1))\n return [`${indent}<${open}>`, ...children, `${indent}</${node.tag}>`].join('\\n')\n}\n\n/** A template's markup, one element per line. */\nexport function printTemplate(root: VueNode): string {\n return printNode(root, 0)\n}\n\nexport interface VueComponent {\n /** The `<script setup lang=\"ts\">` body, a TypeScript program from `es`. */\n script: SyntaxNode\n template: VueNode\n /** Stylesheet text for a `<style scoped>` block. */\n style?: string\n}\n\n/**\n * A raw block's content, which ends at the first closing tag for its element. Strings in the\n * script and values in the stylesheet can come from an imported design, so a closing tag in\n * them is written `<\\\\/`, which JavaScript strings and CSS both read back as `</`.\n */\nconst rawBlock = (tag: string, content: string) =>\n `<${tag}>\\n${content.trim().replace(/<\\/(?=script|style)/gi, '<\\\\/')}\\n</${tag.split(' ')[0]}>`\n\n/** Whether a component's script has anything to write, so it gets a script block. */\nconst hasScript = (script: SyntaxNode) => printModule(script).trim() !== ''\n\nfunction templateElements(node: VueNode): VueElement[] {\n return node.type === 'element' ? [node, ...node.children.flatMap(templateElements)] : []\n}\n\n/**\n * The elements `printComponent` writes, in the order they open: the template's own, between\n * `null`s for the script, template, and style blocks that hold them.\n */\nexport function componentElements({\n script,\n template,\n style\n}: VueComponent): (VueElement | null)[] {\n return [\n ...(hasScript(script) ? [null] : []),\n null,\n ...templateElements(template),\n ...(style ? [null] : [])\n ]\n}\n\n/** A single-file component: script setup, template, and scoped style. */\nexport function printComponent({ script, template, style }: VueComponent): string {\n const code = printModule(script)\n // A component with nothing to import or declare has no script block.\n const blocks = [\n ...(code.trim() ? [rawBlock('script setup lang=\"ts\"', code)] : []),\n `<template>\\n${printNode(template, 1)}\\n</template>`,\n ...(style ? [rawBlock('style scoped', style)] : [])\n ]\n return `${blocks.join('\\n\\n')}\\n`\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAWA,MAAM,oBAAoB;AAC1B,MAAM,eAAe;AAErB,MAAa,cAAc,UAA8B;CAAE,MAAM;CAAiB;AAAK;;AAGvF,SAAS,QAAQ,KAAyB;CACxC,MAAM,CAAC,QAAQ,KAAK,GAAG,QAAQ,IAAI,MAAM,GAAG;CAC5C,OAAO,KAAK,QACT,QAAQ,cAAc;EAAE,MAAM;EAAuB;EAAQ,UAAU,WAAW,QAAQ;CAAE,IAC7F,WAAW,KAAK,CAClB;AACF;AAEA,MAAa,WAAW,WAAkD;CACxE,MAAM;CACN;AACF;AAEA,MAAa,aAAa,gBAAwC;CAChE,MAAM;CACN;AACF;;AAGA,MAAa,cAAc,WAA+B;CAAE,MAAM;CAAW;CAAO,KAAK;AAAM;;AAG/F,MAAa,eAAe,UAC1B,kBAAkB,KAAK,KAAK,IAAI,QAAQ,KAAK,IAAI,UAAU,QAAQ,KAAK,CAAC;;AAG3E,MAAa,UAAU,cAAsC;CAC3D,MA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
|
19
19
|
/** A hole value that removes the object property holding it. */
|
|
20
20
|
declare const OMIT: unique symbol;
|
|
21
21
|
/**
|
|
22
|
-
* Replace `$name` placeholders — identifiers, type references, and string
|
|
23
|
-
* nodes. A
|
|
22
|
+
* Replace the template's `$name` placeholders — identifiers, type references, and string
|
|
23
|
+
* literals — with nodes. A hole's node goes in as given: placeholders inside it are not filled,
|
|
24
|
+
* so a name or text from the document that reads like `$name` stays as written. Fill a nested
|
|
25
|
+
* template first. A list item filled with `OMIT` is removed, and so is a property whose value is.
|
|
24
26
|
*/
|
|
25
27
|
declare function fill<T extends SyntaxNode>(template: T, holes: Record<string, SyntaxNode | typeof OMIT>): T;
|
|
26
28
|
declare function printModule$1(program: SyntaxNode): string;
|
|
@@ -59,7 +61,8 @@ declare const stringValue: (value: string) => SyntaxNode;
|
|
|
59
61
|
/** `{...expression}`, passing an object's properties on as attributes. */
|
|
60
62
|
declare const spread: (argument: SyntaxNode) => SyntaxNode;
|
|
61
63
|
/** An attribute; a null value prints the bare name, as for `true`. */
|
|
62
|
-
|
|
64
|
+
/** An attribute. A name such as `xlink:href` is namespaced, as JSX parses it. */
|
|
65
|
+
declare function attribute$1(name: string, value: SyntaxNode | null): SyntaxNode;
|
|
63
66
|
/** Text content: plain JSX text when it survives as written, a string expression otherwise. */
|
|
64
67
|
declare function text$1(value: string): SyntaxNode;
|
|
65
68
|
/**
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/estree.ts","../src/jsx.ts","../src/vue.ts"],"mappings":";;;;UAMiB;EACf;GACC;;;iBAMa,OAAO,iBAAiB,SAAS;;iBAKjC,MAAM,MAAM,wBAAwB,cAAc;iBAKlD,SAAS,MAAM,wBAAwB,cAAc;iBAKrD,YAAY,iBAAiB;EAAe,MAAM;;iBAMlD,gBAAgB,iBAAiB;iBAMjC,UAAU,iBAAiB;;cAO9B
|
|
1
|
+
{"version":3,"file":"index.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EACf;EACA,YAAY;;KAGF,UAAU,aAAa,UAAU;cAEhC,YAAa,cAAc,kBAAgB;cAM3C,QAAS,cAAc,YAAY,eAAa;cAMhD,QAAS,YAAY,YAAY,sBAAoB;cAMrD,WAAY,YAAY,eAAa;cAErC,UACX,aACA,aAAY,gBACZ,WAAU,cACT;cAEU,OAAQ,kBAAgB;cAExB,gBAAiB,YAAY,eAAa;;iBAyCvC,cAAc,MAAM;UAInB;;EAEf,QAAQ;EACR,UAAU;;EAEV;;;;;;iBAsBc,oBACd,QACA,UACA,SACC,gBAAgB;;iBAUH,iBAAiB,QAAQ,UAAU,SAAS"}
|
package/package.json
CHANGED
package/src/estree.ts
CHANGED
|
@@ -49,8 +49,10 @@ export function parseType(source: string): SyntaxNode {
|
|
|
49
49
|
export const OMIT = Symbol('omit')
|
|
50
50
|
|
|
51
51
|
/**
|
|
52
|
-
* Replace `$name` placeholders — identifiers, type references, and string
|
|
53
|
-
* nodes. A
|
|
52
|
+
* Replace the template's `$name` placeholders — identifiers, type references, and string
|
|
53
|
+
* literals — with nodes. A hole's node goes in as given: placeholders inside it are not filled,
|
|
54
|
+
* so a name or text from the document that reads like `$name` stays as written. Fill a nested
|
|
55
|
+
* template first. A list item filled with `OMIT` is removed, and so is a property whose value is.
|
|
54
56
|
*/
|
|
55
57
|
export function fill<T extends SyntaxNode>(
|
|
56
58
|
template: T,
|
|
@@ -63,8 +65,11 @@ export function fill<T extends SyntaxNode>(
|
|
|
63
65
|
if (node.type === 'TSTypeReference') return hole(child(node, 'typeName')?.name)
|
|
64
66
|
return undefined
|
|
65
67
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
+
// An `OMIT` hole leaves the list holding it, and takes the property it is the value of along.
|
|
69
|
+
const omitted = (node: unknown) => {
|
|
70
|
+
const value = isNode(node) && node.type === 'Property' ? child(node, 'value') : node
|
|
71
|
+
return isNode(value) && holeOf(value) === OMIT
|
|
72
|
+
}
|
|
68
73
|
const replacement = (node: SyntaxNode): SyntaxNode | undefined => {
|
|
69
74
|
const value = holeOf(node)
|
|
70
75
|
if (!value || value === OMIT) return undefined
|
package/src/jsx.ts
CHANGED
|
@@ -47,11 +47,19 @@ export const spread = (argument: SyntaxNode): SyntaxNode => ({
|
|
|
47
47
|
})
|
|
48
48
|
|
|
49
49
|
/** An attribute; a null value prints the bare name, as for `true`. */
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
name:
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
/** An attribute. A name such as `xlink:href` is namespaced, as JSX parses it. */
|
|
51
|
+
export function attribute(name: string, value: SyntaxNode | null): SyntaxNode {
|
|
52
|
+
const colon = name.indexOf(':')
|
|
53
|
+
const attributeName =
|
|
54
|
+
colon > 0
|
|
55
|
+
? {
|
|
56
|
+
type: 'JSXNamespacedName',
|
|
57
|
+
namespace: identifier(name.slice(0, colon)),
|
|
58
|
+
name: identifier(name.slice(colon + 1))
|
|
59
|
+
}
|
|
60
|
+
: identifier(name)
|
|
61
|
+
return { type: 'JSXAttribute', name: attributeName, value }
|
|
62
|
+
}
|
|
55
63
|
|
|
56
64
|
/** Text content: plain JSX text when it survives as written, a string expression otherwise. */
|
|
57
65
|
export function text(value: string): SyntaxNode {
|