@smartive/graphql-magic 23.2.0 → 23.3.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 +2 -2
- package/dist/bin/gqm.cjs +36 -1
- package/package.json +1 -1
- package/src/bin/gqm/static-eval.ts +48 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
## 23.
|
|
1
|
+
## 23.3.0 (2026-01-30)
|
|
2
2
|
|
|
3
|
-
* feat:
|
|
3
|
+
* feat: enhance staticEval to support destructuring and rest parameters in function arguments (#406) ([adf91c6](https://github.com/smartive/graphql-magic/commit/adf91c6)), closes [#406](https://github.com/smartive/graphql-magic/issues/406)
|
package/dist/bin/gqm.cjs
CHANGED
|
@@ -2325,6 +2325,7 @@ var KNOWN_IDENTIFIERS = {
|
|
|
2325
2325
|
var staticEval = (node, context) => visit(node, context, VISITOR);
|
|
2326
2326
|
var VISITOR = {
|
|
2327
2327
|
undefined: () => void 0,
|
|
2328
|
+
[import_ts_morph2.SyntaxKind.BindingElement]: (node, context) => context[node.getName()],
|
|
2328
2329
|
[import_ts_morph2.SyntaxKind.VariableDeclaration]: (node, context) => staticEval(node.getInitializer(), context),
|
|
2329
2330
|
[import_ts_morph2.SyntaxKind.ArrayLiteralExpression]: (node, context) => {
|
|
2330
2331
|
const values = [];
|
|
@@ -2420,7 +2421,41 @@ var VISITOR = {
|
|
|
2420
2421
|
const parameters = {};
|
|
2421
2422
|
let i = 0;
|
|
2422
2423
|
for (const parameter of node.getParameters()) {
|
|
2423
|
-
|
|
2424
|
+
const argument = args2[i];
|
|
2425
|
+
if (parameter.isRestParameter()) {
|
|
2426
|
+
parameters[parameter.getName()] = args2.slice(i);
|
|
2427
|
+
} else {
|
|
2428
|
+
const nameNode = parameter.getNameNode();
|
|
2429
|
+
if (import_ts_morph2.Node.isObjectBindingPattern(nameNode)) {
|
|
2430
|
+
const value2 = argument ?? {};
|
|
2431
|
+
const usedKeys = /* @__PURE__ */ new Set();
|
|
2432
|
+
for (const element of nameNode.getElements()) {
|
|
2433
|
+
if (element.getDotDotDotToken()) {
|
|
2434
|
+
const restName = element.getName();
|
|
2435
|
+
const restValue = {};
|
|
2436
|
+
for (const key in value2) {
|
|
2437
|
+
if (!usedKeys.has(key)) {
|
|
2438
|
+
restValue[key] = value2[key];
|
|
2439
|
+
}
|
|
2440
|
+
}
|
|
2441
|
+
parameters[restName] = restValue;
|
|
2442
|
+
} else {
|
|
2443
|
+
const propertyNameNode = element.getPropertyNameNode();
|
|
2444
|
+
const elementDetails = element.getNameNode();
|
|
2445
|
+
const key = propertyNameNode ? propertyNameNode.getText() : elementDetails.getText();
|
|
2446
|
+
const variableName = elementDetails.getText();
|
|
2447
|
+
usedKeys.add(key);
|
|
2448
|
+
let propertyValue = value2[key];
|
|
2449
|
+
if (propertyValue === void 0 && element.hasInitializer()) {
|
|
2450
|
+
propertyValue = staticEval(element.getInitializer(), { ...context, ...parameters });
|
|
2451
|
+
}
|
|
2452
|
+
parameters[variableName] = propertyValue;
|
|
2453
|
+
}
|
|
2454
|
+
}
|
|
2455
|
+
} else {
|
|
2456
|
+
parameters[parameter.getName()] = argument;
|
|
2457
|
+
}
|
|
2458
|
+
}
|
|
2424
2459
|
i++;
|
|
2425
2460
|
}
|
|
2426
2461
|
return staticEval(node.getBody(), { ...context, ...parameters });
|
package/package.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { camelCase, Dictionary, kebabCase, lowerFirst, snakeCase, startCase, upperFirst } from 'lodash';
|
|
2
2
|
import {
|
|
3
|
+
BindingElement,
|
|
3
4
|
CaseClause,
|
|
4
5
|
ElementAccessExpression,
|
|
5
6
|
Identifier,
|
|
@@ -47,6 +48,7 @@ export const staticEval = (node: Node | undefined, context: Dictionary<unknown>)
|
|
|
47
48
|
|
|
48
49
|
const VISITOR: Visitor<unknown, Dictionary<unknown>> = {
|
|
49
50
|
undefined: () => undefined,
|
|
51
|
+
[SyntaxKind.BindingElement]: (node: BindingElement, context) => context[node.getName()],
|
|
50
52
|
[SyntaxKind.VariableDeclaration]: (node, context) => staticEval(node.getInitializer(), context),
|
|
51
53
|
[SyntaxKind.ArrayLiteralExpression]: (node, context) => {
|
|
52
54
|
const values: unknown[] = [];
|
|
@@ -146,7 +148,52 @@ const VISITOR: Visitor<unknown, Dictionary<unknown>> = {
|
|
|
146
148
|
const parameters: Dictionary<unknown> = {};
|
|
147
149
|
let i = 0;
|
|
148
150
|
for (const parameter of node.getParameters()) {
|
|
149
|
-
|
|
151
|
+
const argument = args[i];
|
|
152
|
+
|
|
153
|
+
if (parameter.isRestParameter()) {
|
|
154
|
+
parameters[parameter.getName()] = args.slice(i);
|
|
155
|
+
} else {
|
|
156
|
+
const nameNode = parameter.getNameNode();
|
|
157
|
+
if (Node.isObjectBindingPattern(nameNode)) {
|
|
158
|
+
const value = (argument ?? {}) as Dictionary<unknown>;
|
|
159
|
+
const usedKeys = new Set<string>();
|
|
160
|
+
|
|
161
|
+
for (const element of nameNode.getElements()) {
|
|
162
|
+
if (element.getDotDotDotToken()) {
|
|
163
|
+
// Handle rest element (...args)
|
|
164
|
+
const restName = element.getName();
|
|
165
|
+
const restValue: Dictionary<unknown> = {};
|
|
166
|
+
for (const key in value) {
|
|
167
|
+
if (!usedKeys.has(key)) {
|
|
168
|
+
restValue[key] = value[key];
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
parameters[restName] = restValue;
|
|
172
|
+
} else {
|
|
173
|
+
// Handle property destructuring (prop: alias = defaultValue)
|
|
174
|
+
const propertyNameNode = element.getPropertyNameNode();
|
|
175
|
+
const elementDetails = element.getNameNode();
|
|
176
|
+
|
|
177
|
+
// If "prop: alias", key is "prop". If just "alias", key is "alias".
|
|
178
|
+
const key = propertyNameNode ? propertyNameNode.getText() : elementDetails.getText();
|
|
179
|
+
const variableName = elementDetails.getText();
|
|
180
|
+
|
|
181
|
+
usedKeys.add(key);
|
|
182
|
+
|
|
183
|
+
let propertyValue = value[key];
|
|
184
|
+
|
|
185
|
+
// Handle default values (= defaultValue)
|
|
186
|
+
if (propertyValue === undefined && element.hasInitializer()) {
|
|
187
|
+
propertyValue = staticEval(element.getInitializer(), { ...context, ...parameters });
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
parameters[variableName] = propertyValue;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
} else {
|
|
194
|
+
parameters[parameter.getName()] = argument;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
150
197
|
i++;
|
|
151
198
|
}
|
|
152
199
|
return staticEval(node.getBody(), { ...context, ...parameters });
|