@fuzdev/fuz_util 0.49.2 → 0.49.3

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.
@@ -67,6 +67,31 @@ export declare const evaluate_static_expr: (expr: Expression, bindings?: Readonl
67
67
  * @returns The resolved static string, or `null` if the value is dynamic.
68
68
  */
69
69
  export declare const extract_static_string: (value: AST.Attribute["value"], bindings?: ReadonlyMap<string, string>) => string | null;
70
+ /** Result of extracting a conditional expression with static string branches. */
71
+ export interface ConditionalStaticStrings {
72
+ /** The source text of the test/condition expression. */
73
+ test_source: string;
74
+ /** The static string value of the consequent (truthy) branch. */
75
+ consequent: string;
76
+ /** The static string value of the alternate (falsy) branch. */
77
+ alternate: string;
78
+ }
79
+ /**
80
+ * Extracts a conditional expression where both branches are static strings.
81
+ *
82
+ * Handles `content={test ? 'a' : 'b'}` where both the consequent and alternate
83
+ * branches resolve to static strings via `evaluate_static_expr`. The test expression
84
+ * is preserved as source text (sliced from the original source) since it may be dynamic.
85
+ *
86
+ * Returns `null` if the attribute value is not an `ExpressionTag` containing a
87
+ * `ConditionalExpression`, or if either branch is not statically resolvable.
88
+ *
89
+ * @param value The attribute value from `AST.Attribute['value']`.
90
+ * @param source The full source string (needed to slice the test expression source text).
91
+ * @param bindings Map of variable names to their resolved static string values.
92
+ * @returns The condition source and both branch values, or `null` if not extractable.
93
+ */
94
+ export declare const try_extract_conditional: (value: AST.Attribute["value"], source: string, bindings: ReadonlyMap<string, string>) => ConditionalStaticStrings | null;
70
95
  /**
71
96
  * Builds a map of statically resolvable `const` bindings from a Svelte AST.
72
97
  *
@@ -1 +1 @@
1
- {"version":3,"file":"svelte_preprocess_helpers.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/svelte_preprocess_helpers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAC,UAAU,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,eAAe,EAAC,MAAM,Q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,CAAC,KACjB,OAgBF,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,kBAAkB,GAAI,MAAM,MAAM,KAAG,MAc/C,CAAC"}
@@ -112,6 +112,37 @@ export const extract_static_string = (value, bindings) => {
112
112
  return null;
113
113
  return evaluate_static_expr(expr, bindings);
114
114
  };
115
+ /**
116
+ * Extracts a conditional expression where both branches are static strings.
117
+ *
118
+ * Handles `content={test ? 'a' : 'b'}` where both the consequent and alternate
119
+ * branches resolve to static strings via `evaluate_static_expr`. The test expression
120
+ * is preserved as source text (sliced from the original source) since it may be dynamic.
121
+ *
122
+ * Returns `null` if the attribute value is not an `ExpressionTag` containing a
123
+ * `ConditionalExpression`, or if either branch is not statically resolvable.
124
+ *
125
+ * @param value The attribute value from `AST.Attribute['value']`.
126
+ * @param source The full source string (needed to slice the test expression source text).
127
+ * @param bindings Map of variable names to their resolved static string values.
128
+ * @returns The condition source and both branch values, or `null` if not extractable.
129
+ */
130
+ export const try_extract_conditional = (value, source, bindings) => {
131
+ if (value === true || Array.isArray(value))
132
+ return null;
133
+ const expr = value.expression;
134
+ if (expr.type !== 'ConditionalExpression')
135
+ return null;
136
+ const consequent = evaluate_static_expr(expr.consequent, bindings);
137
+ if (consequent === null)
138
+ return null;
139
+ const alternate = evaluate_static_expr(expr.alternate, bindings);
140
+ if (alternate === null)
141
+ return null;
142
+ const test = expr.test;
143
+ const test_source = source.slice(test.start, test.end);
144
+ return { test_source, consequent, alternate };
145
+ };
115
146
  // TODO cross-import tracing: resolve `import {x} from './constants.js'` by reading
116
147
  // and parsing the imported module, extracting `export const` values. Would need path
117
148
  // resolution ($lib, tsconfig paths), a Program-node variant of this function, and
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fuzdev/fuz_util",
3
- "version": "0.49.2",
3
+ "version": "0.49.3",
4
4
  "description": "utility belt for JS",
5
5
  "glyph": "🦕",
6
6
  "logo": "logo.svg",
@@ -136,6 +136,50 @@ export const extract_static_string = (
136
136
  return evaluate_static_expr(expr, bindings);
137
137
  };
138
138
 
139
+ /** Result of extracting a conditional expression with static string branches. */
140
+ export interface ConditionalStaticStrings {
141
+ /** The source text of the test/condition expression. */
142
+ test_source: string;
143
+ /** The static string value of the consequent (truthy) branch. */
144
+ consequent: string;
145
+ /** The static string value of the alternate (falsy) branch. */
146
+ alternate: string;
147
+ }
148
+
149
+ /**
150
+ * Extracts a conditional expression where both branches are static strings.
151
+ *
152
+ * Handles `content={test ? 'a' : 'b'}` where both the consequent and alternate
153
+ * branches resolve to static strings via `evaluate_static_expr`. The test expression
154
+ * is preserved as source text (sliced from the original source) since it may be dynamic.
155
+ *
156
+ * Returns `null` if the attribute value is not an `ExpressionTag` containing a
157
+ * `ConditionalExpression`, or if either branch is not statically resolvable.
158
+ *
159
+ * @param value The attribute value from `AST.Attribute['value']`.
160
+ * @param source The full source string (needed to slice the test expression source text).
161
+ * @param bindings Map of variable names to their resolved static string values.
162
+ * @returns The condition source and both branch values, or `null` if not extractable.
163
+ */
164
+ export const try_extract_conditional = (
165
+ value: AST.Attribute['value'],
166
+ source: string,
167
+ bindings: ReadonlyMap<string, string>,
168
+ ): ConditionalStaticStrings | null => {
169
+ if (value === true || Array.isArray(value)) return null;
170
+ const expr = value.expression;
171
+ if (expr.type !== 'ConditionalExpression') return null;
172
+
173
+ const consequent = evaluate_static_expr(expr.consequent, bindings);
174
+ if (consequent === null) return null;
175
+ const alternate = evaluate_static_expr(expr.alternate, bindings);
176
+ if (alternate === null) return null;
177
+
178
+ const test = expr.test as any;
179
+ const test_source = source.slice(test.start, test.end);
180
+ return {test_source, consequent, alternate};
181
+ };
182
+
139
183
  // TODO cross-import tracing: resolve `import {x} from './constants.js'` by reading
140
184
  // and parsing the imported module, extracting `export const` values. Would need path
141
185
  // resolution ($lib, tsconfig paths), a Program-node variant of this function, and