atmn 2.0.10 → 2.0.12

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.
@@ -0,0 +1,99 @@
1
+ import type { SgNode } from "@ast-grep/napi";
2
+ import { stringLiteralValue } from "./findFixture";
3
+
4
+ export const DYNAMIC_VALUE = Symbol("dynamic-fixture-value");
5
+
6
+ export const fixtureKeyOf = ({ node }: { node: SgNode }): string | null => {
7
+ if (
8
+ node.kind() === "property_identifier" ||
9
+ node.kind() === "shorthand_property_identifier"
10
+ )
11
+ return node.text();
12
+ return stringLiteralValue(node);
13
+ };
14
+
15
+ export const fixtureArrayElements = ({
16
+ node,
17
+ }: {
18
+ node: SgNode;
19
+ }): SgNode[] | null => {
20
+ const elements: SgNode[] = [];
21
+ let expectsElement = true;
22
+ for (const child of node.children()) {
23
+ const kind = child.kind();
24
+ if (kind === "[" || kind === "]" || kind === "comment") continue;
25
+ if (kind === ",") {
26
+ if (expectsElement) return null;
27
+ expectsElement = true;
28
+ continue;
29
+ }
30
+ if (!expectsElement || kind === "spread_element") return null;
31
+ elements.push(child);
32
+ expectsElement = false;
33
+ }
34
+ return elements;
35
+ };
36
+
37
+ export const readFixtureValue = ({
38
+ node,
39
+ builders = [],
40
+ }: {
41
+ node: SgNode;
42
+ builders?: readonly string[];
43
+ }): unknown => {
44
+ const kind = node.kind();
45
+ if (
46
+ kind === "call_expression" &&
47
+ builders.includes(node.field("function")?.text() ?? "")
48
+ ) {
49
+ const args = node
50
+ .field("arguments")
51
+ ?.namedChildren()
52
+ .filter((child) => child.kind() !== "comment");
53
+ return args?.length === 1 && args[0]?.kind() === "object"
54
+ ? readFixtureValue({ node: args[0], builders })
55
+ : DYNAMIC_VALUE;
56
+ }
57
+ if (kind === "string") return stringLiteralValue(node) ?? DYNAMIC_VALUE;
58
+ if (kind === "true") return true;
59
+ if (kind === "false") return false;
60
+ if (kind === "null") return null;
61
+ if (
62
+ kind === "number" ||
63
+ (kind === "unary_expression" && /^[+-]\s*[\d.]/.test(node.text()))
64
+ ) {
65
+ const value = Number(node.text().replaceAll("_", "").replace(/\s/g, ""));
66
+ return Number.isFinite(value) ? value : DYNAMIC_VALUE;
67
+ }
68
+ if (kind === "array") {
69
+ const elements = fixtureArrayElements({ node });
70
+ return elements === null
71
+ ? DYNAMIC_VALUE
72
+ : elements.map((element) =>
73
+ readFixtureValue({ node: element, builders }),
74
+ );
75
+ }
76
+ if (kind !== "object") return DYNAMIC_VALUE;
77
+ const value: Record<string, unknown> = {};
78
+ for (const member of node.namedChildren()) {
79
+ if (member.kind() === "comment") continue;
80
+ const shorthand = member.kind() === "shorthand_property_identifier";
81
+ if (member.kind() !== "pair" && !shorthand) return DYNAMIC_VALUE;
82
+ const keyNode = shorthand ? member : member.field("key");
83
+ const key = keyNode === null ? null : fixtureKeyOf({ node: keyNode });
84
+ if (
85
+ key === null ||
86
+ Object.getOwnPropertyDescriptor(value, key) !== undefined
87
+ )
88
+ return DYNAMIC_VALUE;
89
+ const child = member.field("value");
90
+ Object.defineProperty(value, key, {
91
+ value:
92
+ child === null
93
+ ? DYNAMIC_VALUE
94
+ : readFixtureValue({ node: child, builders }),
95
+ enumerable: true,
96
+ });
97
+ }
98
+ return value;
99
+ };