@pairdraw/emit 0.0.0-stage → 0.16.1
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/LICENSE +21 -0
- package/README.md +40 -2
- package/dist/chunks/estree.js +217 -0
- package/dist/chunks/estree.js.map +1 -0
- package/dist/chunks/index.js +258 -0
- package/dist/chunks/index.js.map +1 -0
- package/dist/index.d.ts +149 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/rolldown-runtime.js +13 -0
- package/package.json +48 -4
- package/src/estree.ts +195 -0
- package/src/index.ts +4 -0
- package/src/jsx.ts +138 -0
- package/src/vue.ts +164 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
declare namespace estree_d_exports {
|
|
2
|
+
export { OMIT, SyntaxNode, array, call, child, children, fill, identifier$1 as identifier, isIdentifierName, isNode, json, member, number, object, objectType, parseExpression, parseModule, parseType, printExpression, printModule$1 as printModule, string, stringUnionType };
|
|
3
|
+
}
|
|
4
|
+
/** An ESTree or TS-ESTree node, as acorn parses it and esrap prints it. */
|
|
5
|
+
interface SyntaxNode {
|
|
6
|
+
type: string;
|
|
7
|
+
[key: string]: unknown;
|
|
8
|
+
}
|
|
9
|
+
/** Whether a value is a syntax node, as opposed to a primitive field of one. */
|
|
10
|
+
declare function isNode(value: unknown): value is SyntaxNode;
|
|
11
|
+
/** The node under `key`, or undefined when it is absent or not a node. */
|
|
12
|
+
declare function child(node: SyntaxNode | undefined, key: string): SyntaxNode | undefined;
|
|
13
|
+
declare function children(node: SyntaxNode | undefined, key: string): SyntaxNode[];
|
|
14
|
+
declare function parseModule(source: string): SyntaxNode & {
|
|
15
|
+
body: SyntaxNode[];
|
|
16
|
+
};
|
|
17
|
+
declare function parseExpression(source: string): SyntaxNode;
|
|
18
|
+
declare function parseType(source: string): SyntaxNode;
|
|
19
|
+
/** A hole value that removes the object property holding it. */
|
|
20
|
+
declare const OMIT: unique symbol;
|
|
21
|
+
/**
|
|
22
|
+
* Replace `$name` placeholders — identifiers, type references, and string literals — with
|
|
23
|
+
* nodes. A property whose value is filled with `OMIT` is removed.
|
|
24
|
+
*/
|
|
25
|
+
declare function fill<T extends SyntaxNode>(template: T, holes: Record<string, SyntaxNode | typeof OMIT>): T;
|
|
26
|
+
declare function printModule$1(program: SyntaxNode): string;
|
|
27
|
+
/** One expression, as a template binding or another host language embeds it. */
|
|
28
|
+
declare function printExpression(expression: SyntaxNode): string;
|
|
29
|
+
declare const identifier$1: (name: string) => SyntaxNode;
|
|
30
|
+
declare const string: (value: string) => SyntaxNode;
|
|
31
|
+
/** A finite number, with a negative one written as `-` before its magnitude, as source does. */
|
|
32
|
+
declare function number(value: number): SyntaxNode;
|
|
33
|
+
/** Whether `name` can be written as it is, as a JavaScript identifier, rather than quoted. */
|
|
34
|
+
declare const isIdentifierName: (name: string) => boolean;
|
|
35
|
+
declare function object(entries: (readonly [string, SyntaxNode])[]): SyntaxNode;
|
|
36
|
+
declare const array: (elements: SyntaxNode[]) => SyntaxNode;
|
|
37
|
+
/** `object.key`, or `object['key']` for a key that is not an identifier. */
|
|
38
|
+
declare function member(object: SyntaxNode, key: string): SyntaxNode;
|
|
39
|
+
/**
|
|
40
|
+
* A value read from JSON written as source: strings, finite numbers, booleans, null, and lists
|
|
41
|
+
* and objects of them. Anything else, such as a function or an infinite number, is null.
|
|
42
|
+
*/
|
|
43
|
+
declare function json(value: unknown): SyntaxNode;
|
|
44
|
+
/** A call of a function by name, such as a helper inside an exported prop value. */
|
|
45
|
+
declare const call: (callee: string, args: SyntaxNode[]) => SyntaxNode;
|
|
46
|
+
declare function stringUnionType(values: string[]): SyntaxNode;
|
|
47
|
+
/** An object type; a member whose third element is `true` is optional. */
|
|
48
|
+
declare function objectType(entries: [string, SyntaxNode, boolean?][]): SyntaxNode;
|
|
49
|
+
declare namespace jsx_d_exports {
|
|
50
|
+
export { attribute$1 as attribute, container, element$1 as element, fragment, identifier, inlineText, literal, printJSX, printModule, spread, stringValue, text$1 as text, whitespace };
|
|
51
|
+
}
|
|
52
|
+
declare const identifier: (name: string) => SyntaxNode;
|
|
53
|
+
declare const literal: (value: string | number | boolean) => SyntaxNode;
|
|
54
|
+
declare const container: (expression: SyntaxNode) => SyntaxNode;
|
|
55
|
+
/** Raw JSX text, printed as is. */
|
|
56
|
+
declare const whitespace: (value: string) => SyntaxNode;
|
|
57
|
+
/** A string attribute value: quoted when JSX keeps it as written, an expression otherwise. */
|
|
58
|
+
declare const stringValue: (value: string) => SyntaxNode;
|
|
59
|
+
/** `{...expression}`, passing an object's properties on as attributes. */
|
|
60
|
+
declare const spread: (argument: SyntaxNode) => SyntaxNode;
|
|
61
|
+
/** An attribute; a null value prints the bare name, as for `true`. */
|
|
62
|
+
declare const attribute$1: (name: string, value: SyntaxNode | null) => SyntaxNode;
|
|
63
|
+
/** Text content: plain JSX text when it survives as written, a string expression otherwise. */
|
|
64
|
+
declare function text$1(value: string): SyntaxNode;
|
|
65
|
+
/**
|
|
66
|
+
* Text that shares a line with elements, as beside a span. JSX trims whitespace only at line
|
|
67
|
+
* edges, so text without a line break is kept as written, spaces at its ends included.
|
|
68
|
+
*/
|
|
69
|
+
declare function inlineText(value: string): SyntaxNode;
|
|
70
|
+
/**
|
|
71
|
+
* An element at nesting `depth`. Children go on their own lines unless `inline`, as for a
|
|
72
|
+
* lone text child; without children the element self-closes.
|
|
73
|
+
*/
|
|
74
|
+
declare function element$1(tag: string, attributes: SyntaxNode[], children: SyntaxNode[], depth: number, inline?: boolean): SyntaxNode;
|
|
75
|
+
/** `<>…</>`: children without an element around them, laid out as an element's are. */
|
|
76
|
+
declare function fragment(children: SyntaxNode[], depth: number): SyntaxNode;
|
|
77
|
+
declare function printJSX(node: SyntaxNode): string;
|
|
78
|
+
/**
|
|
79
|
+
* A TSX module, such as a component built with `es` templates and JSX from this module. Strings
|
|
80
|
+
* parsed from a template and strings built here print with the same double quotes.
|
|
81
|
+
*/
|
|
82
|
+
declare function printModule(program: SyntaxNode): string;
|
|
83
|
+
declare namespace vue_d_exports {
|
|
84
|
+
export { VueAttribute, VueComponent, VueElement, VueInterpolation, VueNode, VueText, attribute, bound, componentElements, element, interpolation, model, printComponent, printTemplate, renderIf, text };
|
|
85
|
+
}
|
|
86
|
+
type VueAttribute = {
|
|
87
|
+
type: 'static';
|
|
88
|
+
name: string;
|
|
89
|
+
value: string;
|
|
90
|
+
} |
|
|
91
|
+
/** `:name="expression"` */
|
|
92
|
+
{
|
|
93
|
+
type: 'bound';
|
|
94
|
+
name: string;
|
|
95
|
+
expression: SyntaxNode;
|
|
96
|
+
} |
|
|
97
|
+
/** `v-model="expression"`, or `v-model:argument` */
|
|
98
|
+
{
|
|
99
|
+
type: 'model';
|
|
100
|
+
argument?: string;
|
|
101
|
+
expression: SyntaxNode;
|
|
102
|
+
} |
|
|
103
|
+
/** `v-if="expression"`: the element renders only while the expression holds. */
|
|
104
|
+
{
|
|
105
|
+
type: 'if';
|
|
106
|
+
expression: SyntaxNode;
|
|
107
|
+
};
|
|
108
|
+
interface VueElement {
|
|
109
|
+
type: 'element';
|
|
110
|
+
tag: string;
|
|
111
|
+
attributes: VueAttribute[];
|
|
112
|
+
children: VueNode[];
|
|
113
|
+
}
|
|
114
|
+
interface VueText {
|
|
115
|
+
type: 'text';
|
|
116
|
+
value: string;
|
|
117
|
+
}
|
|
118
|
+
/** `{{ expression }}`, printed from a syntax tree so it holds only what the tree says. */
|
|
119
|
+
interface VueInterpolation {
|
|
120
|
+
type: 'interpolation';
|
|
121
|
+
expression: SyntaxNode;
|
|
122
|
+
}
|
|
123
|
+
type VueNode = VueElement | VueText | VueInterpolation;
|
|
124
|
+
declare const attribute: (name: string, value: string) => VueAttribute;
|
|
125
|
+
declare const bound: (name: string, expression: SyntaxNode) => VueAttribute;
|
|
126
|
+
declare const model: (expression: SyntaxNode, argument?: string) => VueAttribute;
|
|
127
|
+
declare const renderIf: (expression: SyntaxNode) => VueAttribute;
|
|
128
|
+
declare const element: (tag: string, attributes?: VueAttribute[], children?: VueNode[]) => VueElement;
|
|
129
|
+
declare const text: (value: string) => VueText;
|
|
130
|
+
declare const interpolation: (expression: SyntaxNode) => VueInterpolation;
|
|
131
|
+
/** A template's markup, one element per line. */
|
|
132
|
+
declare function printTemplate(root: VueNode): string;
|
|
133
|
+
interface VueComponent {
|
|
134
|
+
/** The `<script setup lang="ts">` body, a TypeScript program from `es`. */
|
|
135
|
+
script: SyntaxNode;
|
|
136
|
+
template: VueNode;
|
|
137
|
+
/** Stylesheet text for a `<style scoped>` block. */
|
|
138
|
+
style?: string;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The elements `printComponent` writes, in the order they open: the template's own, between
|
|
142
|
+
* `null`s for the script, template, and style blocks that hold them.
|
|
143
|
+
*/
|
|
144
|
+
declare function componentElements({ script, template, style }: VueComponent): (VueElement | null)[];
|
|
145
|
+
/** A single-file component: script setup, template, and scoped style. */
|
|
146
|
+
declare function printComponent({ script, template, style }: VueComponent): string;
|
|
147
|
+
//#endregion
|
|
148
|
+
export { type SyntaxNode, estree_d_exports as es, jsx_d_exports as jsx, vue_d_exports as vue };
|
|
149
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +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;;;;;iBAMG,KAAK,UAAU,YAC7B,UAAU,GACV,OAAO,eAAe,oBAAoB,QACzC;iBAmCa,cAAY,SAAS;;iBAKrB,gBAAgB,YAAY;cAS/B,eAAc,iBAAe;cAE7B,SAAU,kBAAgB;;iBAGvB,OAAO,gBAAgB;;cAS1B,mBAAoB;iBAIjB,OAAO,4BAA4B,iBAAiB;cAevD,QAAS,UAAU,iBAAe;;iBAG/B,OAAO,QAAQ,YAAY,cAAc;;;;;iBAezC,KAAK,iBAAiB;;cAWzB,OAAQ,gBAAgB,MAAM,iBAAe;iBAO1C,gBAAgB,mBAAmB;;iBAQnC,WAAW,kBAAkB,0BAA0B;;;;cCzK1D,aAAc,iBAAe;cAW7B,UAAW,qCAAmC;cAK9C,YAAa,YAAY,eAAa;;cAMtC,aAAc,kBAAgB;;cAG9B,cAAe,kBAAgB;;cAI/B,SAAU,UAAU,eAAa;;cAMjC,cAAa,cAAc,OAAO,sBAAoB;;iBAOnD,OAAK,gBAAgB;;;;;iBASrB,WAAW,gBAAgB;;;;;iBAkB3B,UACd,aACA,YAAY,cACZ,UAAU,cACV,eACA,mBACC;;iBAiBa,SAAS,UAAU,cAAc,gBAAgB;iBASjD,SAAS,MAAM;;;;;iBAoBf,YAAY,SAAS;;;;KC5HzB;EACN;EAAgB;EAAc;;;;EAE9B;EAAe;EAAc,YAAY;;;;EAEzC;EAAe;EAAmB,YAAY;;;;EAE9C;EAAY,YAAY;;UAEb;EACf;EACA;EACA,YAAY;EACZ,UAAU;;UAGK;EACf;EACA;;;UAIe;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/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __exportAll = (all, no_symbols) => {
|
|
4
|
+
let target = {};
|
|
5
|
+
for (var name in all) __defProp(target, name, {
|
|
6
|
+
get: all[name],
|
|
7
|
+
enumerable: true
|
|
8
|
+
});
|
|
9
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
10
|
+
return target;
|
|
11
|
+
};
|
|
12
|
+
//#endregion
|
|
13
|
+
export { __exportAll };
|
package/package.json
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pairdraw/emit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.16.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist",
|
|
7
|
+
"src",
|
|
8
|
+
"README.md"
|
|
9
|
+
],
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@sveltejs/acorn-typescript": "^1.0.13",
|
|
14
|
+
"acorn": "^8.18.0",
|
|
15
|
+
"entities": "^8.0.0",
|
|
16
|
+
"esrap": "^2.3.11"
|
|
17
|
+
},
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"imports": {
|
|
26
|
+
"#emit/*": {
|
|
27
|
+
"types": "./src/*.ts",
|
|
28
|
+
"default": "./dist/*.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"description": "How Pairdraw exporters emit source: JavaScript, TypeScript, and JSX built as ESTree and printed with esrap.",
|
|
32
|
+
"keywords": [
|
|
33
|
+
"pairdraw",
|
|
34
|
+
"codegen",
|
|
35
|
+
"estree",
|
|
36
|
+
"jsx"
|
|
37
|
+
],
|
|
38
|
+
"author": "Badryansah Bangsawan",
|
|
39
|
+
"homepage": "https://github.com/BadryansahBangsawan/pairdraw/tree/main/packages/emit#readme",
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/BadryansahBangsawan/pairdraw/issues"
|
|
42
|
+
},
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "git+https://github.com/BadryansahBangsawan/pairdraw.git",
|
|
46
|
+
"directory": "packages/emit"
|
|
47
|
+
},
|
|
48
|
+
"type": "module",
|
|
49
|
+
"sideEffects": false
|
|
50
|
+
}
|
package/src/estree.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { tsPlugin } from '@sveltejs/acorn-typescript'
|
|
2
|
+
import { Parser } from 'acorn'
|
|
3
|
+
import { print } from 'esrap'
|
|
4
|
+
import ts from 'esrap/languages/ts'
|
|
5
|
+
|
|
6
|
+
/** An ESTree or TS-ESTree node, as acorn parses it and esrap prints it. */
|
|
7
|
+
export interface SyntaxNode {
|
|
8
|
+
type: string
|
|
9
|
+
[key: string]: unknown
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const TSParser = Parser.extend(tsPlugin())
|
|
13
|
+
|
|
14
|
+
/** Whether a value is a syntax node, as opposed to a primitive field of one. */
|
|
15
|
+
export function isNode(value: unknown): value is SyntaxNode {
|
|
16
|
+
return typeof value === 'object' && value !== null && 'type' in value
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** The node under `key`, or undefined when it is absent or not a node. */
|
|
20
|
+
export function child(node: SyntaxNode | undefined, key: string): SyntaxNode | undefined {
|
|
21
|
+
const value = node?.[key]
|
|
22
|
+
return isNode(value) ? value : undefined
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function children(node: SyntaxNode | undefined, key: string): SyntaxNode[] {
|
|
26
|
+
const value = node?.[key]
|
|
27
|
+
return Array.isArray(value) ? value.filter(isNode) : []
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function parseModule(source: string): SyntaxNode & { body: SyntaxNode[] } {
|
|
31
|
+
const program: unknown = TSParser.parse(source, { ecmaVersion: 'latest', sourceType: 'module' })
|
|
32
|
+
if (!isNode(program)) throw new Error('Expected a module')
|
|
33
|
+
return { ...program, body: children(program, 'body') }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function parseExpression(source: string): SyntaxNode {
|
|
37
|
+
const expression = child(parseModule(`(${source})`).body.at(0), 'expression')
|
|
38
|
+
if (!expression) throw new Error(`Expected an expression: ${source}`)
|
|
39
|
+
return expression
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function parseType(source: string): SyntaxNode {
|
|
43
|
+
const type = child(parseModule(`type T = ${source}`).body.at(0), 'typeAnnotation')
|
|
44
|
+
if (!type) throw new Error(`Expected a type: ${source}`)
|
|
45
|
+
return type
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** A hole value that removes the object property holding it. */
|
|
49
|
+
export const OMIT = Symbol('omit')
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Replace `$name` placeholders — identifiers, type references, and string literals — with
|
|
53
|
+
* nodes. A property whose value is filled with `OMIT` is removed.
|
|
54
|
+
*/
|
|
55
|
+
export function fill<T extends SyntaxNode>(
|
|
56
|
+
template: T,
|
|
57
|
+
holes: Record<string, SyntaxNode | typeof OMIT>
|
|
58
|
+
): T {
|
|
59
|
+
const hole = (name: unknown) => (typeof name === 'string' ? holes[name] : undefined)
|
|
60
|
+
const holeOf = (node: SyntaxNode) => {
|
|
61
|
+
if (node.type === 'Identifier') return hole(node.name)
|
|
62
|
+
if (node.type === 'Literal') return hole(node.value)
|
|
63
|
+
if (node.type === 'TSTypeReference') return hole(child(node, 'typeName')?.name)
|
|
64
|
+
return undefined
|
|
65
|
+
}
|
|
66
|
+
const omitted = (node: unknown) =>
|
|
67
|
+
isNode(node) && node.type === 'Property' && hole(child(node, 'value')?.name) === OMIT
|
|
68
|
+
const replacement = (node: SyntaxNode): SyntaxNode | undefined => {
|
|
69
|
+
const value = holeOf(node)
|
|
70
|
+
if (!value || value === OMIT) return undefined
|
|
71
|
+
// `$name: Story` keeps its annotation when the identifier is filled, with its own holes filled.
|
|
72
|
+
const annotation = child(node, 'typeAnnotation')
|
|
73
|
+
return annotation
|
|
74
|
+
? { ...structuredClone(value), typeAnnotation: visit(annotation) }
|
|
75
|
+
: structuredClone(value)
|
|
76
|
+
}
|
|
77
|
+
function visit(node: SyntaxNode): SyntaxNode {
|
|
78
|
+
const filled = replacement(node)
|
|
79
|
+
if (filled) return filled
|
|
80
|
+
const copy: SyntaxNode = { ...node }
|
|
81
|
+
for (const [key, value] of Object.entries(node)) {
|
|
82
|
+
if (Array.isArray(value))
|
|
83
|
+
copy[key] = value
|
|
84
|
+
.filter((item) => !omitted(item))
|
|
85
|
+
.map((item) => (isNode(item) ? visit(item) : item))
|
|
86
|
+
else if (isNode(value)) copy[key] = visit(value)
|
|
87
|
+
}
|
|
88
|
+
return copy
|
|
89
|
+
}
|
|
90
|
+
return visit(template) as T
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function printModule(program: SyntaxNode): string {
|
|
94
|
+
return print(program, ts(), { indent: ' ' }).code
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** One expression, as a template binding or another host language embeds it. */
|
|
98
|
+
export function printExpression(expression: SyntaxNode): string {
|
|
99
|
+
const program = {
|
|
100
|
+
type: 'Program',
|
|
101
|
+
sourceType: 'module',
|
|
102
|
+
body: [{ type: 'ExpressionStatement', expression }]
|
|
103
|
+
}
|
|
104
|
+
return printModule(program).trim().replace(/;$/, '')
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export const identifier = (name: string): SyntaxNode => ({ type: 'Identifier', name })
|
|
108
|
+
|
|
109
|
+
export const string = (value: string): SyntaxNode => ({ type: 'Literal', value })
|
|
110
|
+
|
|
111
|
+
/** A finite number, with a negative one written as `-` before its magnitude, as source does. */
|
|
112
|
+
export function number(value: number): SyntaxNode {
|
|
113
|
+
if (!Number.isFinite(value)) throw new Error(`Not a finite number: ${value}`)
|
|
114
|
+
const literal = { type: 'Literal', value: Math.abs(value) }
|
|
115
|
+
return value < 0
|
|
116
|
+
? { type: 'UnaryExpression', operator: '-', prefix: true, argument: literal }
|
|
117
|
+
: literal
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Whether `name` can be written as it is, as a JavaScript identifier, rather than quoted. */
|
|
121
|
+
export const isIdentifierName = (name: string): boolean => /^[A-Za-z_$][\w$]*$/.test(name)
|
|
122
|
+
|
|
123
|
+
const propertyKey = (key: string) => (isIdentifierName(key) ? identifier(key) : string(key))
|
|
124
|
+
|
|
125
|
+
export function object(entries: (readonly [string, SyntaxNode])[]): SyntaxNode {
|
|
126
|
+
return {
|
|
127
|
+
type: 'ObjectExpression',
|
|
128
|
+
properties: entries.map(([key, value]) => ({
|
|
129
|
+
type: 'Property',
|
|
130
|
+
kind: 'init',
|
|
131
|
+
key: propertyKey(key),
|
|
132
|
+
value,
|
|
133
|
+
computed: false,
|
|
134
|
+
method: false,
|
|
135
|
+
shorthand: false
|
|
136
|
+
}))
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export const array = (elements: SyntaxNode[]): SyntaxNode => ({ type: 'ArrayExpression', elements })
|
|
141
|
+
|
|
142
|
+
/** `object.key`, or `object['key']` for a key that is not an identifier. */
|
|
143
|
+
export function member(object: SyntaxNode, key: string): SyntaxNode {
|
|
144
|
+
const computed = !/^[A-Za-z_$][\w$]*$/.test(key)
|
|
145
|
+
return {
|
|
146
|
+
type: 'MemberExpression',
|
|
147
|
+
object,
|
|
148
|
+
property: computed ? string(key) : identifier(key),
|
|
149
|
+
computed,
|
|
150
|
+
optional: false
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* A value read from JSON written as source: strings, finite numbers, booleans, null, and lists
|
|
156
|
+
* and objects of them. Anything else, such as a function or an infinite number, is null.
|
|
157
|
+
*/
|
|
158
|
+
export function json(value: unknown): SyntaxNode {
|
|
159
|
+
if (typeof value === 'string') return string(value)
|
|
160
|
+
if (typeof value === 'number') return Number.isFinite(value) ? number(value) : json(null)
|
|
161
|
+
if (typeof value === 'boolean' || value === null) return { type: 'Literal', value }
|
|
162
|
+
if (Array.isArray(value)) return array(value.map(json))
|
|
163
|
+
if (typeof value === 'object')
|
|
164
|
+
return object(Object.entries(value).map(([key, item]) => [key, json(item)]))
|
|
165
|
+
return json(null)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** A call of a function by name, such as a helper inside an exported prop value. */
|
|
169
|
+
export const call = (callee: string, args: SyntaxNode[]): SyntaxNode => ({
|
|
170
|
+
type: 'CallExpression',
|
|
171
|
+
callee: identifier(callee),
|
|
172
|
+
arguments: args,
|
|
173
|
+
optional: false
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
export function stringUnionType(values: string[]): SyntaxNode {
|
|
177
|
+
const literals = values.map((value) => ({ type: 'TSLiteralType', literal: string(value) }))
|
|
178
|
+
return literals.length === 1 && literals[0]
|
|
179
|
+
? literals[0]
|
|
180
|
+
: { type: 'TSUnionType', types: literals }
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** An object type; a member whose third element is `true` is optional. */
|
|
184
|
+
export function objectType(entries: [string, SyntaxNode, boolean?][]): SyntaxNode {
|
|
185
|
+
return {
|
|
186
|
+
type: 'TSTypeLiteral',
|
|
187
|
+
members: entries.map(([key, typeAnnotation, optional = false]) => ({
|
|
188
|
+
type: 'TSPropertySignature',
|
|
189
|
+
key: propertyKey(key),
|
|
190
|
+
computed: false,
|
|
191
|
+
optional,
|
|
192
|
+
typeAnnotation: { type: 'TSTypeAnnotation', typeAnnotation }
|
|
193
|
+
}))
|
|
194
|
+
}
|
|
195
|
+
}
|
package/src/index.ts
ADDED
package/src/jsx.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { print } from 'esrap'
|
|
2
|
+
import tsx from 'esrap/languages/tsx'
|
|
3
|
+
|
|
4
|
+
import { isNode, type SyntaxNode } from './estree'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* JSX attribute strings end at `"`, decode `&` entities, and keep backslashes literally,
|
|
8
|
+
* so the printer's escapes for backslashes and line breaks would change the value. JSX
|
|
9
|
+
* text also treats braces and angle brackets as syntax and trims whitespace at line
|
|
10
|
+
* edges. Anything else is written as a string literal, which the printer escapes.
|
|
11
|
+
*/
|
|
12
|
+
const LITERAL_ATTRIBUTE = /^[^"&\\\r\n]*$/
|
|
13
|
+
const LITERAL_TEXT = /^[^{}<>&\r\n]*$/
|
|
14
|
+
|
|
15
|
+
export const identifier = (name: string): SyntaxNode => ({ type: 'JSXIdentifier', name })
|
|
16
|
+
|
|
17
|
+
/** A tag name; a dotted one such as `Switch.Root` is a member of a namespace. */
|
|
18
|
+
function tagName(tag: string): SyntaxNode {
|
|
19
|
+
const [first = tag, ...rest] = tag.split('.')
|
|
20
|
+
return rest.reduce<SyntaxNode>(
|
|
21
|
+
(object, property) => ({ type: 'JSXMemberExpression', object, property: identifier(property) }),
|
|
22
|
+
identifier(first)
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const literal = (value: string | number | boolean): SyntaxNode => ({
|
|
27
|
+
type: 'Literal',
|
|
28
|
+
value
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
export const container = (expression: SyntaxNode): SyntaxNode => ({
|
|
32
|
+
type: 'JSXExpressionContainer',
|
|
33
|
+
expression
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
/** Raw JSX text, printed as is. */
|
|
37
|
+
export const whitespace = (value: string): SyntaxNode => ({ type: 'JSXText', value, raw: value })
|
|
38
|
+
|
|
39
|
+
/** A string attribute value: quoted when JSX keeps it as written, an expression otherwise. */
|
|
40
|
+
export const stringValue = (value: string): SyntaxNode =>
|
|
41
|
+
LITERAL_ATTRIBUTE.test(value) ? literal(value) : container(literal(value))
|
|
42
|
+
|
|
43
|
+
/** `{...expression}`, passing an object's properties on as attributes. */
|
|
44
|
+
export const spread = (argument: SyntaxNode): SyntaxNode => ({
|
|
45
|
+
type: 'JSXSpreadAttribute',
|
|
46
|
+
argument
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
/** An attribute; a null value prints the bare name, as for `true`. */
|
|
50
|
+
export const attribute = (name: string, value: SyntaxNode | null): SyntaxNode => ({
|
|
51
|
+
type: 'JSXAttribute',
|
|
52
|
+
name: identifier(name),
|
|
53
|
+
value
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
/** Text content: plain JSX text when it survives as written, a string expression otherwise. */
|
|
57
|
+
export function text(value: string): SyntaxNode {
|
|
58
|
+
const plain = LITERAL_TEXT.test(value) && value.trim() === value && value.length > 0
|
|
59
|
+
return plain ? whitespace(value) : container(literal(value))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Text that shares a line with elements, as beside a span. JSX trims whitespace only at line
|
|
64
|
+
* edges, so text without a line break is kept as written, spaces at its ends included.
|
|
65
|
+
*/
|
|
66
|
+
export function inlineText(value: string): SyntaxNode {
|
|
67
|
+
const plain = LITERAL_TEXT.test(value) && value.length > 0
|
|
68
|
+
return plain ? whitespace(value) : container(literal(value))
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Children on their own lines; JSX drops whitespace-only lines between elements. */
|
|
72
|
+
function indented(children: SyntaxNode[], depth: number): SyntaxNode[] {
|
|
73
|
+
const inner = `\n${' '.repeat(depth + 1)}`
|
|
74
|
+
return [
|
|
75
|
+
...children.flatMap((child) => [whitespace(inner), child]),
|
|
76
|
+
whitespace(`\n${' '.repeat(depth)}`)
|
|
77
|
+
]
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* An element at nesting `depth`. Children go on their own lines unless `inline`, as for a
|
|
82
|
+
* lone text child; without children the element self-closes.
|
|
83
|
+
*/
|
|
84
|
+
export function element(
|
|
85
|
+
tag: string,
|
|
86
|
+
attributes: SyntaxNode[],
|
|
87
|
+
children: SyntaxNode[],
|
|
88
|
+
depth: number,
|
|
89
|
+
inline = false
|
|
90
|
+
): SyntaxNode {
|
|
91
|
+
const name = tagName(tag)
|
|
92
|
+
const content = inline || children.length === 0 ? children : indented(children, depth)
|
|
93
|
+
return {
|
|
94
|
+
type: 'JSXElement',
|
|
95
|
+
openingElement: {
|
|
96
|
+
type: 'JSXOpeningElement',
|
|
97
|
+
name,
|
|
98
|
+
attributes,
|
|
99
|
+
selfClosing: content.length === 0
|
|
100
|
+
},
|
|
101
|
+
closingElement: content.length > 0 ? { type: 'JSXClosingElement', name } : null,
|
|
102
|
+
children: content
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** `<>…</>`: children without an element around them, laid out as an element's are. */
|
|
107
|
+
export function fragment(children: SyntaxNode[], depth: number): SyntaxNode {
|
|
108
|
+
return {
|
|
109
|
+
type: 'JSXFragment',
|
|
110
|
+
openingFragment: { type: 'JSXOpeningFragment' },
|
|
111
|
+
closingFragment: { type: 'JSXClosingFragment' },
|
|
112
|
+
children: children.length === 0 ? [] : indented(children, depth)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function printJSX(node: SyntaxNode): string {
|
|
117
|
+
return print(node, tsx({ quotes: 'double' }), { indent: ' ' }).code
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** A copy without strings' source spelling, so the printer quotes every string the same way. */
|
|
121
|
+
function withoutRawStrings(node: SyntaxNode): SyntaxNode {
|
|
122
|
+
const copy: SyntaxNode = { ...node }
|
|
123
|
+
if (node.type === 'Literal' && typeof node.value === 'string') delete copy.raw
|
|
124
|
+
for (const [key, value] of Object.entries(node)) {
|
|
125
|
+
if (Array.isArray(value))
|
|
126
|
+
copy[key] = value.map((item: unknown) => (isNode(item) ? withoutRawStrings(item) : item))
|
|
127
|
+
else if (isNode(value)) copy[key] = withoutRawStrings(value)
|
|
128
|
+
}
|
|
129
|
+
return copy
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* A TSX module, such as a component built with `es` templates and JSX from this module. Strings
|
|
134
|
+
* parsed from a template and strings built here print with the same double quotes.
|
|
135
|
+
*/
|
|
136
|
+
export function printModule(program: SyntaxNode): string {
|
|
137
|
+
return print(withoutRawStrings(program), tsx({ quotes: 'double' }), { indent: ' ' }).code
|
|
138
|
+
}
|