atmn 2.0.11 → 2.0.13
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/bin.js +725 -186
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/actions/init/runInit.ts +2 -0
- package/src/actions/pull/applyMappings.ts +100 -0
- package/src/actions/pull/locateFixture.ts +19 -4
- package/src/actions/pull.ts +22 -0
- package/src/actions/skills/ignoreBundledSkills.ts +32 -0
- package/src/cli.ts +5 -0
- package/src/generated/apiRoutes.ts +24 -24
- package/src/generated/emit.ts +42 -0
- package/src/generated/emitRuntime.ts +3 -5
- package/src/generated/mappingAssignments.ts +204 -0
- package/src/generated/skills.ts +10 -10
- package/src/surgery/appendPropertyEdit.ts +3 -1
- package/src/surgery/findFixture.ts +24 -12
- package/src/surgery/patchFixturePaths.ts +204 -0
- package/src/surgery/readFixtureValue.ts +99 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from "node:util";
|
|
2
|
+
import { type Edit, Lang, parse, type SgNode } from "@ast-grep/napi";
|
|
3
|
+
import { appendPropertyEdit } from "./appendPropertyEdit";
|
|
4
|
+
import {
|
|
5
|
+
type FixtureConstraint,
|
|
6
|
+
type FixtureShape,
|
|
7
|
+
findFixtures,
|
|
8
|
+
fixtureObjectOf,
|
|
9
|
+
} from "./findFixture";
|
|
10
|
+
import {
|
|
11
|
+
DYNAMIC_VALUE,
|
|
12
|
+
fixtureArrayElements,
|
|
13
|
+
fixtureKeyOf,
|
|
14
|
+
readFixtureValue,
|
|
15
|
+
} from "./readFixtureValue";
|
|
16
|
+
|
|
17
|
+
type Assignment = { path: (string | number)[]; text: string | null };
|
|
18
|
+
type PatchResult = {
|
|
19
|
+
source: string;
|
|
20
|
+
skipped: { path: (string | number)[]; reason: string }[];
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const isDynamic = (value: unknown): boolean => {
|
|
24
|
+
if (value === DYNAMIC_VALUE) return true;
|
|
25
|
+
if (value === null || typeof value !== "object") return false;
|
|
26
|
+
return Object.values(value).some(isDynamic);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const matchesLiteral = ({
|
|
30
|
+
value,
|
|
31
|
+
text,
|
|
32
|
+
}: {
|
|
33
|
+
value: unknown;
|
|
34
|
+
text: string;
|
|
35
|
+
}): boolean => {
|
|
36
|
+
const root = parse(Lang.TypeScript, `const value = (${text});`).root();
|
|
37
|
+
if (root.find({ rule: { kind: "ERROR" } }) !== null) return false;
|
|
38
|
+
const expression = root
|
|
39
|
+
.find({ rule: { kind: "variable_declarator" } })
|
|
40
|
+
?.field("value");
|
|
41
|
+
if (expression?.kind() !== "parenthesized_expression") return false;
|
|
42
|
+
const literal = expression
|
|
43
|
+
.namedChildren()
|
|
44
|
+
.filter((node) => node.kind() !== "comment");
|
|
45
|
+
if (literal.length !== 1 || literal[0] === undefined) return false;
|
|
46
|
+
const proposed = readFixtureValue({ node: literal[0] });
|
|
47
|
+
return !isDynamic(proposed) && isDeepStrictEqual(value, proposed);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const removeMemberEdits = ({ member }: { member: SgNode }): Edit[] => {
|
|
51
|
+
const siblings = member.parent()?.children() ?? [];
|
|
52
|
+
const index = siblings.findIndex(
|
|
53
|
+
(sibling) => sibling.range().start.index === member.range().start.index,
|
|
54
|
+
);
|
|
55
|
+
const next = siblings
|
|
56
|
+
.slice(index + 1)
|
|
57
|
+
.find((sibling) => sibling.kind() !== "comment");
|
|
58
|
+
const previous = siblings
|
|
59
|
+
.slice(0, index)
|
|
60
|
+
.reverse()
|
|
61
|
+
.find((sibling) => sibling.kind() !== "comment");
|
|
62
|
+
const comma =
|
|
63
|
+
next?.kind() === "," ? next : previous?.kind() === "," ? previous : null;
|
|
64
|
+
return [member, ...(comma === null ? [] : [comma])].map((node) => ({
|
|
65
|
+
startPos: node.range().start.index,
|
|
66
|
+
endPos: node.range().end.index,
|
|
67
|
+
insertedText: "",
|
|
68
|
+
}));
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const pathEdits = ({
|
|
72
|
+
source,
|
|
73
|
+
object,
|
|
74
|
+
path,
|
|
75
|
+
text,
|
|
76
|
+
builders,
|
|
77
|
+
}: {
|
|
78
|
+
source: string;
|
|
79
|
+
object: SgNode;
|
|
80
|
+
builders: readonly string[];
|
|
81
|
+
} & Assignment): Edit[] | string => {
|
|
82
|
+
let current = object;
|
|
83
|
+
for (let index = 0; index < path.length; index += 1) {
|
|
84
|
+
if (
|
|
85
|
+
current.kind() === "call_expression" &&
|
|
86
|
+
builders.includes(current.field("function")?.text() ?? "")
|
|
87
|
+
) {
|
|
88
|
+
const args = current
|
|
89
|
+
.field("arguments")
|
|
90
|
+
?.namedChildren()
|
|
91
|
+
.filter((child) => child.kind() !== "comment");
|
|
92
|
+
if (args?.length !== 1 || args[0]?.kind() !== "object")
|
|
93
|
+
return "dynamic-value";
|
|
94
|
+
current = args[0];
|
|
95
|
+
}
|
|
96
|
+
const segment = path[index];
|
|
97
|
+
const last = index === path.length - 1;
|
|
98
|
+
let member: SgNode | undefined;
|
|
99
|
+
let value: SgNode | null | undefined;
|
|
100
|
+
if (typeof segment === "number") {
|
|
101
|
+
if (
|
|
102
|
+
current.kind() !== "array" ||
|
|
103
|
+
!Number.isSafeInteger(segment) ||
|
|
104
|
+
segment < 0
|
|
105
|
+
)
|
|
106
|
+
return "invalid-array-path";
|
|
107
|
+
const elements = fixtureArrayElements({ node: current });
|
|
108
|
+
if (elements === null) return "dynamic-array";
|
|
109
|
+
value = elements[segment];
|
|
110
|
+
if (value === undefined) return "missing-array-element";
|
|
111
|
+
if (last && text === null) return "array-element-removal";
|
|
112
|
+
} else {
|
|
113
|
+
if (current.kind() !== "object") return "non-object-container";
|
|
114
|
+
if (readFixtureValue({ node: current }) === DYNAMIC_VALUE)
|
|
115
|
+
return "ambiguous-object";
|
|
116
|
+
member = current.namedChildren().find((child) => {
|
|
117
|
+
const key = child.kind() === "pair" ? child.field("key") : child;
|
|
118
|
+
return key !== null && fixtureKeyOf({ node: key }) === segment;
|
|
119
|
+
});
|
|
120
|
+
if (member === undefined) {
|
|
121
|
+
if (text === null) return [];
|
|
122
|
+
const remaining = path.slice(index + 1);
|
|
123
|
+
if (remaining.some((key) => typeof key === "number"))
|
|
124
|
+
return "missing-array-element";
|
|
125
|
+
let inserted = text;
|
|
126
|
+
for (const key of remaining.reverse())
|
|
127
|
+
inserted = `{ ${JSON.stringify(key)}: ${inserted} }`;
|
|
128
|
+
const pair = `${JSON.stringify(segment)}: ${inserted}`;
|
|
129
|
+
const members = current
|
|
130
|
+
.namedChildren()
|
|
131
|
+
.filter((child) => child.kind() !== "comment");
|
|
132
|
+
if (members.length === 0) {
|
|
133
|
+
const closing = current.range().end.index - 1;
|
|
134
|
+
return [
|
|
135
|
+
{ startPos: closing, endPos: closing, insertedText: ` ${pair} ` },
|
|
136
|
+
];
|
|
137
|
+
}
|
|
138
|
+
return [appendPropertyEdit({ source, object: current, pair })];
|
|
139
|
+
}
|
|
140
|
+
value = member.field("value");
|
|
141
|
+
}
|
|
142
|
+
if (value == null) return "dynamic-value";
|
|
143
|
+
if (!last) {
|
|
144
|
+
current = value;
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
const currentValue = readFixtureValue({ node: value });
|
|
148
|
+
if (isDynamic(currentValue)) return "dynamic-value";
|
|
149
|
+
if (text === null)
|
|
150
|
+
return member === undefined
|
|
151
|
+
? "array-element-removal"
|
|
152
|
+
: removeMemberEdits({ member });
|
|
153
|
+
if (matchesLiteral({ value: currentValue, text })) return [];
|
|
154
|
+
return [
|
|
155
|
+
{
|
|
156
|
+
startPos: value.range().start.index,
|
|
157
|
+
endPos: value.range().end.index,
|
|
158
|
+
insertedText: text,
|
|
159
|
+
},
|
|
160
|
+
];
|
|
161
|
+
}
|
|
162
|
+
return "empty-path";
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
export const patchFixturePaths = ({
|
|
166
|
+
source,
|
|
167
|
+
builder,
|
|
168
|
+
idField,
|
|
169
|
+
id,
|
|
170
|
+
where,
|
|
171
|
+
assignments,
|
|
172
|
+
builders = [],
|
|
173
|
+
}: {
|
|
174
|
+
source: string;
|
|
175
|
+
builder: FixtureShape;
|
|
176
|
+
idField: string;
|
|
177
|
+
id: string;
|
|
178
|
+
where?: FixtureConstraint[];
|
|
179
|
+
assignments: Assignment[];
|
|
180
|
+
builders?: readonly string[];
|
|
181
|
+
}): PatchResult | null => {
|
|
182
|
+
const selector = { builder, idField, id, where, allowDynamic: true };
|
|
183
|
+
const initial = findFixtures({ source, ...selector });
|
|
184
|
+
if (initial.length === 0) return null;
|
|
185
|
+
const skipped: PatchResult["skipped"] = [];
|
|
186
|
+
for (const assignment of assignments) {
|
|
187
|
+
const matches = findFixtures({ source, ...selector });
|
|
188
|
+
const match = matches[0];
|
|
189
|
+
const object =
|
|
190
|
+
matches.length === 1 && match !== undefined
|
|
191
|
+
? fixtureObjectOf(match)
|
|
192
|
+
: null;
|
|
193
|
+
const edits =
|
|
194
|
+
object === null
|
|
195
|
+
? "ambiguous-fixture"
|
|
196
|
+
: pathEdits({ source, object, builders, ...assignment });
|
|
197
|
+
if (typeof edits === "string") {
|
|
198
|
+
skipped.push({ path: assignment.path, reason: edits });
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
source = parse(Lang.TypeScript, source).root().commitEdits(edits);
|
|
202
|
+
}
|
|
203
|
+
return { source, skipped };
|
|
204
|
+
};
|
|
@@ -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
|
+
};
|