astralis-cli 0.6.0 → 0.8.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/CHANGELOG.md +30 -0
- package/README.md +0 -32
- package/package.json +2 -2
- package/src/commands/add.mjs +4 -4
- package/src/commands/create.mjs +68 -21
- package/src/commands/init.mjs +168 -102
- package/src/commands/theme.mjs +66 -26
- package/src/index.mjs +1 -2
- package/src/lib/args.mjs +2 -2
- package/src/lib/ci-setup.mjs +65 -23
- package/src/lib/ci.mjs +1 -3
- package/src/lib/commands.mjs +0 -36
- package/src/lib/detect.mjs +1 -1
- package/src/lib/devtools-setup.mjs +120 -52
- package/src/lib/github.mjs +2 -2
- package/src/lib/library.mjs +16 -11
- package/src/lib/manual.mjs +130 -0
- package/src/lib/snapshot.mjs +3 -3
- package/src/lib/starter.mjs +63 -30
- package/src/lib/validate-core.mjs +64 -4
- package/src/lib/wiring.mjs +145 -0
- package/src/commands/report.mjs +0 -257
- package/src/lib/report-tree.mjs +0 -134
package/src/commands/report.mjs
DELETED
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
/* ==========================================================================
|
|
2
|
-
astralis report: an issue snapshot from source files.
|
|
3
|
-
--------------------------------------------------------------------------
|
|
4
|
-
The same astralis-snapshot/1 the DevTools build from a page, built here
|
|
5
|
-
from the JSX of the files that show the problem: the astralis-ui
|
|
6
|
-
components with their literal props, the validator's findings for those
|
|
7
|
-
files, and the versions installed. It prints the exact JSON and opens it
|
|
8
|
-
as a GitHub issue, from the reporter's own account, only when they
|
|
9
|
-
confirm. The first time, they sign in with a code on github.com.
|
|
10
|
-
========================================================================== */
|
|
11
|
-
|
|
12
|
-
import { createHash } from "node:crypto";
|
|
13
|
-
import { readFileSync, statSync, writeFileSync } from "node:fs";
|
|
14
|
-
import { dirname, join, relative, resolve } from "node:path";
|
|
15
|
-
import { bold, cyan, dim, fail, info, ok, warn, ask, isAssumeYes, confirm, select, posix } from "../lib/ui.mjs";
|
|
16
|
-
import { parseCommandArgs } from "../lib/args.mjs";
|
|
17
|
-
import { commandNamed } from "../lib/commands.mjs";
|
|
18
|
-
import { loadSpec } from "../lib/spec-load.mjs";
|
|
19
|
-
import { detectFramework, readPackageJson } from "../lib/detect.mjs";
|
|
20
|
-
import { prepareSpec, validateSource } from "../lib/validate-core.mjs";
|
|
21
|
-
import { treeFromSource } from "../lib/report-tree.mjs";
|
|
22
|
-
import { SNAPSHOT_FORMAT, LIMITS, checkSnapshot } from "../lib/snapshot.mjs";
|
|
23
|
-
import {
|
|
24
|
-
commentFromSnapshot,
|
|
25
|
-
commentOnIssue,
|
|
26
|
-
createIssue,
|
|
27
|
-
findSimilar,
|
|
28
|
-
finishSignIn,
|
|
29
|
-
issueFromSnapshot,
|
|
30
|
-
issuesRepo,
|
|
31
|
-
newIssueUrl,
|
|
32
|
-
startSignIn,
|
|
33
|
-
tokenStore,
|
|
34
|
-
} from "../lib/github.mjs";
|
|
35
|
-
|
|
36
|
-
/** The nearest node_modules/<name>/package.json above `root`: its version, or null. */
|
|
37
|
-
function installedVersion(root, name) {
|
|
38
|
-
for (let dir = resolve(root); ; dir = dirname(dir)) {
|
|
39
|
-
const file = join(dir, "node_modules", name, "package.json");
|
|
40
|
-
if (statSync(file, { throwIfNoEntry: false })) {
|
|
41
|
-
try {
|
|
42
|
-
return JSON.parse(readFileSync(file, "utf8")).version ?? null;
|
|
43
|
-
} catch {
|
|
44
|
-
return null;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
if (dirname(dir) === dir) return null;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function environmentOf(cwd) {
|
|
52
|
-
const framework = detectFramework(readPackageJson(cwd));
|
|
53
|
-
return {
|
|
54
|
-
framework: { name: framework, version: framework ? installedVersion(cwd, framework) : null },
|
|
55
|
-
react: installedVersion(cwd, "react"),
|
|
56
|
-
browser: null,
|
|
57
|
-
viewport: null,
|
|
58
|
-
colorMode: null,
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/** The reporter's words: flags first, then prompts, with --yes taking what the flags gave. */
|
|
63
|
-
function describe(values) {
|
|
64
|
-
const answer = (flag, question) => {
|
|
65
|
-
if (values[flag] !== undefined) return values[flag];
|
|
66
|
-
if (isAssumeYes()) return undefined;
|
|
67
|
-
return ask(question) || undefined;
|
|
68
|
-
};
|
|
69
|
-
const title = answer("title", `${bold("Title")} ${dim("(one line)")}: `);
|
|
70
|
-
if (!title?.trim()) {
|
|
71
|
-
fail(`A report needs a title — pass ${cyan('--title "…"')}${isAssumeYes() ? " with --yes" : ""}.`);
|
|
72
|
-
}
|
|
73
|
-
const description = { title: title.trim().slice(0, LIMITS.title) };
|
|
74
|
-
const happened = answer("happened", `${bold("What happened?")} ${dim("(Enter to skip)")} `);
|
|
75
|
-
const expected = answer("expected", `${bold("What did you expect?")} ${dim("(Enter to skip)")} `);
|
|
76
|
-
if (happened?.trim()) description.happened = happened.trim().slice(0, LIMITS.body);
|
|
77
|
-
if (expected?.trim()) description.expected = expected.trim().slice(0, LIMITS.body);
|
|
78
|
-
return description;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** Sign in with GitHub's device flow, in the terminal, and save the sign-in. */
|
|
82
|
-
async function signIn(store) {
|
|
83
|
-
let flow;
|
|
84
|
-
try {
|
|
85
|
-
flow = await startSignIn();
|
|
86
|
-
} catch (error) {
|
|
87
|
-
fail(`${error.message} Nothing was sent. ${cyan("--out snapshot.json")} saves the snapshot to attach by hand.`);
|
|
88
|
-
}
|
|
89
|
-
console.log("");
|
|
90
|
-
console.log(` Sign in to GitHub: open ${cyan(flow.verificationUri)} and enter ${bold(flow.userCode)}`);
|
|
91
|
-
console.log(dim(` The Astralis DevTools app can only read and write issues on ${issuesRepo()}.`));
|
|
92
|
-
console.log("");
|
|
93
|
-
try {
|
|
94
|
-
const signedIn = await finishSignIn(flow);
|
|
95
|
-
store.write(signedIn);
|
|
96
|
-
ok(`signed in as ${bold(signedIn.login)}`);
|
|
97
|
-
return signedIn;
|
|
98
|
-
} catch (error) {
|
|
99
|
-
fail(`${error.message} Nothing was sent.`);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export async function run(argv) {
|
|
104
|
-
const args = parseCommandArgs(commandNamed("report"), argv);
|
|
105
|
-
if (!args) return;
|
|
106
|
-
const { values, positionals } = args;
|
|
107
|
-
const cwd = process.cwd();
|
|
108
|
-
if (values["sign-out"]) {
|
|
109
|
-
const store = tokenStore();
|
|
110
|
-
const saved = store.read();
|
|
111
|
-
store.clear();
|
|
112
|
-
if (saved) ok(`signed out of GitHub (${saved.login}). The app's access ends when you revoke it at github.com/settings/applications.`);
|
|
113
|
-
else info("not signed in to GitHub.");
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
if (positionals.length === 0) {
|
|
117
|
-
fail(`Name the files that show the problem: ${cyan("astralis report app/page.tsx")}.`);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const { spec, text: specText } = loadSpec(values.spec, cwd);
|
|
121
|
-
const prepared = prepareSpec(spec);
|
|
122
|
-
const tree = [];
|
|
123
|
-
const findings = [];
|
|
124
|
-
for (const given of positionals) {
|
|
125
|
-
const file = resolve(given);
|
|
126
|
-
if (!/\.(tsx|jsx)$/.test(file) || !statSync(file, { throwIfNoEntry: false })?.isFile()) {
|
|
127
|
-
fail(`${given} is not a .tsx or .jsx file.`);
|
|
128
|
-
}
|
|
129
|
-
const rel = posix(relative(cwd, file));
|
|
130
|
-
if (rel.startsWith("..")) fail(`${given} is outside this project; run the command from the project root.`);
|
|
131
|
-
const source = readFileSync(file, "utf8");
|
|
132
|
-
try {
|
|
133
|
-
tree.push(...treeFromSource(source, spec, { includeText: values["include-text"] }));
|
|
134
|
-
} catch (error) {
|
|
135
|
-
fail(`${rel} does not parse: ${error.message}`);
|
|
136
|
-
}
|
|
137
|
-
const { errors, warnings } = validateSource(source, prepared, rel);
|
|
138
|
-
findings.push(...errors, ...warnings);
|
|
139
|
-
}
|
|
140
|
-
if (tree.length === 0) fail("None of those files renders an astralis-ui component, so there is nothing to report.");
|
|
141
|
-
|
|
142
|
-
const snapshot = {
|
|
143
|
-
format: SNAPSHOT_FORMAT,
|
|
144
|
-
source: "cli",
|
|
145
|
-
library: {
|
|
146
|
-
name: "astralis-ui",
|
|
147
|
-
version: spec.version,
|
|
148
|
-
specVersion: spec.specVersion,
|
|
149
|
-
specSha256: createHash("sha256").update(specText).digest("hex"),
|
|
150
|
-
},
|
|
151
|
-
environment: environmentOf(cwd),
|
|
152
|
-
route: null,
|
|
153
|
-
tree,
|
|
154
|
-
...(values["no-findings"] ? {} : { findings: findings.slice(0, LIMITS.findings) }),
|
|
155
|
-
description: describe(values),
|
|
156
|
-
};
|
|
157
|
-
const problems = checkSnapshot(snapshot);
|
|
158
|
-
if (problems.length) fail(`The snapshot is not valid: ${problems.join("; ")}`);
|
|
159
|
-
|
|
160
|
-
// The exact content, before anything leaves the machine.
|
|
161
|
-
const json = JSON.stringify(snapshot, null, 2);
|
|
162
|
-
console.log(json);
|
|
163
|
-
|
|
164
|
-
if (values.out) {
|
|
165
|
-
writeFileSync(resolve(values.out), `${json}\n`);
|
|
166
|
-
ok(`snapshot written to ${values.out} ${dim("(nothing was sent)")}`);
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
169
|
-
if (values["dry-run"]) {
|
|
170
|
-
info("dry run: nothing was sent.");
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
if (values.link) {
|
|
174
|
-
// No sign-in: GitHub's own form, pre-filled, submitted in the browser.
|
|
175
|
-
ok("open this link to check the report on GitHub and submit it yourself:");
|
|
176
|
-
console.log(` ${cyan(newIssueUrl(snapshot))}`);
|
|
177
|
-
console.log(dim(" Nothing is sent until you press Create on GitHub."));
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
const send = await confirm(`Open this as an issue on github.com/${issuesRepo()}?`, {
|
|
181
|
-
hint: `Pass --yes to send, or ${cyan("--out snapshot.json")} to save it instead.`,
|
|
182
|
-
});
|
|
183
|
-
if (!send) {
|
|
184
|
-
info("nothing was sent.");
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
const store = tokenStore();
|
|
188
|
-
let signedIn = store.read() ?? (await signIn(store));
|
|
189
|
-
const keep = `Nothing was sent. ${cyan("--out snapshot.json")} saves the snapshot to attach by hand.`;
|
|
190
|
-
/** Run `work` signed in; a sign-in GitHub revoked is replaced, once. */
|
|
191
|
-
const asUser = async (work) => {
|
|
192
|
-
try {
|
|
193
|
-
return await work(signedIn.token);
|
|
194
|
-
} catch (error) {
|
|
195
|
-
if (error.code !== "signed-out") throw error;
|
|
196
|
-
store.clear();
|
|
197
|
-
signedIn = await signIn(store);
|
|
198
|
-
return work(signedIn.token);
|
|
199
|
-
}
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
const target = await chooseTarget(values, () => asUser((token) => findSimilar(token, snapshot)));
|
|
203
|
-
try {
|
|
204
|
-
if (target) {
|
|
205
|
-
const added = await asUser((token) => commentOnIssue(token, target, commentFromSnapshot(snapshot)));
|
|
206
|
-
ok(`report added to issue #${added.number} as ${signedIn.login}: ${cyan(added.url)}`);
|
|
207
|
-
} else {
|
|
208
|
-
const created = await asUser((token) => createIssue(token, issueFromSnapshot(snapshot)));
|
|
209
|
-
ok(`issue #${created.number} opened as ${signedIn.login}: ${cyan(created.url)}`);
|
|
210
|
-
}
|
|
211
|
-
} catch (error) {
|
|
212
|
-
fail(`${error.message} ${keep}`);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
const line = (issue) => `#${issue.number} ${issue.title} ${dim(`(${issue.state})`)}`;
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Where the report goes: an existing issue's number, or null for a new issue.
|
|
220
|
-
* --add-to and --new decide without searching. Otherwise the issues that may
|
|
221
|
-
* already report it are shown, and the reporter chooses; with --yes, a new
|
|
222
|
-
* issue is opened and the possible duplicates are listed for them to check.
|
|
223
|
-
*/
|
|
224
|
-
async function chooseTarget(values, searchSimilar) {
|
|
225
|
-
if (values["add-to"] !== undefined) {
|
|
226
|
-
const number = Number(String(values["add-to"]).replace(/^#/, ""));
|
|
227
|
-
if (!Number.isInteger(number) || number < 1) fail(`--add-to takes an issue number, like ${cyan("--add-to 12")}.`);
|
|
228
|
-
return number;
|
|
229
|
-
}
|
|
230
|
-
if (values.new) return null;
|
|
231
|
-
let similar;
|
|
232
|
-
try {
|
|
233
|
-
similar = await searchSimilar();
|
|
234
|
-
} catch (error) {
|
|
235
|
-
warn(`could not check for an existing issue (${error.message}); opening a new one.`);
|
|
236
|
-
return null;
|
|
237
|
-
}
|
|
238
|
-
const { duplicates, related } = similar;
|
|
239
|
-
if (related.length) {
|
|
240
|
-
info(`open issues about the same component:`);
|
|
241
|
-
for (const issue of related) console.log(` ${line(issue)} ${dim(issue.url)}`);
|
|
242
|
-
}
|
|
243
|
-
if (!duplicates.length) return null;
|
|
244
|
-
console.log("");
|
|
245
|
-
console.log(bold("This may already be reported:"));
|
|
246
|
-
for (const issue of duplicates) console.log(` ${line(issue)} ${dim(issue.url)}`);
|
|
247
|
-
if (isAssumeYes()) {
|
|
248
|
-
info(`opening a new issue; pass ${cyan(`--add-to ${duplicates[0].number}`)} to add your report to an existing one instead.`);
|
|
249
|
-
return null;
|
|
250
|
-
}
|
|
251
|
-
const choice = await select("Add your report to one of them, or open a new issue?", [
|
|
252
|
-
...duplicates.slice(0, 3).map((issue) => ({ value: issue.number, label: `Add it to #${issue.number} ${issue.title}` })),
|
|
253
|
-
{ value: 0, label: "Open a new issue" },
|
|
254
|
-
]);
|
|
255
|
-
if (choice === null) fail("Choose one of the listed numbers. Nothing was sent.");
|
|
256
|
-
return choice || null;
|
|
257
|
-
}
|
package/src/lib/report-tree.mjs
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import { parse } from "@babel/parser";
|
|
2
|
-
import { clientTier, keptText, narrowProps, redactedText } from "./snapshot.mjs";
|
|
3
|
-
|
|
4
|
-
/* The component tree `astralis report` records from source files: the
|
|
5
|
-
astralis-ui elements in their JSX, with literal props only. The DevTools
|
|
6
|
-
build the same tree from the rendered page (astralis-devtools). */
|
|
7
|
-
|
|
8
|
-
const PARSE_OPTIONS = { sourceType: "module", plugins: ["typescript", "jsx"] };
|
|
9
|
-
|
|
10
|
-
/** local name -> astralis-ui export, for value imports from astralis-ui. */
|
|
11
|
-
function importsOf(ast) {
|
|
12
|
-
const imported = new Map();
|
|
13
|
-
for (const node of ast.program.body) {
|
|
14
|
-
if (node.type !== "ImportDeclaration" || node.source.value !== "astralis-ui" || node.importKind === "type") continue;
|
|
15
|
-
for (const s of node.specifiers) {
|
|
16
|
-
if (s.type !== "ImportSpecifier" || s.importKind === "type") continue;
|
|
17
|
-
imported.set(s.local.name, s.imported.type === "Identifier" ? s.imported.name : s.imported.value);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
return imported;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/** The public name of a JSX element if it is an astralis-ui component, else null. */
|
|
24
|
-
function publicName(nameNode, imported, spec) {
|
|
25
|
-
if (nameNode.type === "JSXIdentifier") {
|
|
26
|
-
const exported = imported.get(nameNode.name);
|
|
27
|
-
if (!exported || !spec.components?.[exported]) return null;
|
|
28
|
-
// A flat part export (CardBody) is written the way the page names it: Card.Body.
|
|
29
|
-
const partOf = spec.components[exported].partOf;
|
|
30
|
-
return partOf ? `${partOf}.${exported.slice(partOf.length)}` : exported;
|
|
31
|
-
}
|
|
32
|
-
if (nameNode.type === "JSXMemberExpression" && nameNode.object.type === "JSXIdentifier") {
|
|
33
|
-
const owner = imported.get(nameNode.object.name);
|
|
34
|
-
if (!owner || !spec.components?.[owner]) return null;
|
|
35
|
-
const name = `${owner}.${nameNode.property.name}`;
|
|
36
|
-
return spec.components[owner].parts?.includes(nameNode.property.name) ? name : null;
|
|
37
|
-
}
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** A literal attribute value, or undefined when it is computed at run time. */
|
|
42
|
-
function literal(node) {
|
|
43
|
-
if (!node) return true; // <Button disabled />
|
|
44
|
-
if (node.type === "JSXExpressionContainer") return literal(node.expression);
|
|
45
|
-
if (node.type === "StringLiteral") return node.value;
|
|
46
|
-
if (node.type === "NumericLiteral" || node.type === "BooleanLiteral") return node.value;
|
|
47
|
-
if (node.type === "TemplateLiteral" && node.expressions.length === 0) return node.quasis[0].value.cooked;
|
|
48
|
-
if (node.type === "UnaryExpression" && node.operator === "-" && node.argument.type === "NumericLiteral") return -node.argument.value;
|
|
49
|
-
if (node.type === "ObjectExpression") {
|
|
50
|
-
const out = {};
|
|
51
|
-
for (const p of node.properties) {
|
|
52
|
-
if (p.type !== "ObjectProperty" || p.computed) return undefined;
|
|
53
|
-
const key = p.key.type === "Identifier" ? p.key.name : p.key.value;
|
|
54
|
-
const value = literal(p.value);
|
|
55
|
-
if (value === undefined) return undefined;
|
|
56
|
-
out[key] = value;
|
|
57
|
-
}
|
|
58
|
-
return out;
|
|
59
|
-
}
|
|
60
|
-
return undefined;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function propsOf(opening) {
|
|
64
|
-
const props = {};
|
|
65
|
-
for (const attr of opening.attributes) {
|
|
66
|
-
if (attr.type !== "JSXAttribute" || attr.name.type !== "JSXIdentifier") continue;
|
|
67
|
-
const value = literal(attr.value);
|
|
68
|
-
if (value !== undefined) props[attr.name.name] = value;
|
|
69
|
-
}
|
|
70
|
-
return props;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function childrenOf(nodes, ctx) {
|
|
74
|
-
const out = [];
|
|
75
|
-
for (const child of nodes) {
|
|
76
|
-
if (child.type === "JSXText") {
|
|
77
|
-
const text = child.value.replace(/\s+/g, " ").trim();
|
|
78
|
-
if (text) out.push(ctx.includeText ? keptText(text) : redactedText(text));
|
|
79
|
-
} else if (child.type === "JSXExpressionContainer") {
|
|
80
|
-
const value = literal(child.expression);
|
|
81
|
-
if (typeof value === "string" && value.trim()) out.push(ctx.includeText ? keptText(value.trim()) : redactedText(value.trim()));
|
|
82
|
-
} else if (child.type === "JSXElement" || child.type === "JSXFragment") {
|
|
83
|
-
out.push(...elementNodes(child, ctx));
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return out;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/** An element as tree nodes: itself when it is an astralis-ui component, else what it contains. */
|
|
90
|
-
function elementNodes(node, ctx) {
|
|
91
|
-
if (node.type === "JSXFragment") return childrenOf(node.children, ctx);
|
|
92
|
-
const name = publicName(node.openingElement.name, ctx.imported, ctx.spec);
|
|
93
|
-
if (!name) return childrenOf(node.children, ctx);
|
|
94
|
-
return [{
|
|
95
|
-
component: name,
|
|
96
|
-
props: narrowProps(ctx.spec, name, propsOf(node.openingElement)),
|
|
97
|
-
client: clientTier(ctx.spec, name),
|
|
98
|
-
children: childrenOf(node.children, ctx),
|
|
99
|
-
}];
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/** Every outermost JSX element or fragment in the file, in source order. */
|
|
103
|
-
function outermostJsx(ast) {
|
|
104
|
-
const found = [];
|
|
105
|
-
const walk = (node) => {
|
|
106
|
-
if (!node || typeof node.type !== "string") return;
|
|
107
|
-
if (node.type === "JSXElement" || node.type === "JSXFragment") {
|
|
108
|
-
found.push(node);
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
for (const key of Object.keys(node)) {
|
|
112
|
-
if (key === "loc") continue;
|
|
113
|
-
const value = node[key];
|
|
114
|
-
if (Array.isArray(value)) value.forEach(walk);
|
|
115
|
-
else if (value && typeof value === "object") walk(value);
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
walk(ast.program);
|
|
119
|
-
return found;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* The astralis-ui components in one source file, outermost first. Throws a
|
|
124
|
-
* SyntaxError when the file does not parse. Text becomes placeholders unless
|
|
125
|
-
* `includeText` is set.
|
|
126
|
-
*/
|
|
127
|
-
export function treeFromSource(source, spec, { includeText = false } = {}) {
|
|
128
|
-
const ast = parse(source, PARSE_OPTIONS);
|
|
129
|
-
const ctx = { spec, imported: importsOf(ast), includeText };
|
|
130
|
-
// Text outside every component belongs to no component; the tree starts at components.
|
|
131
|
-
return outermostJsx(ast)
|
|
132
|
-
.flatMap((node) => elementNodes(node, ctx))
|
|
133
|
-
.filter((node) => "component" in node);
|
|
134
|
-
}
|