@fuzdev/fuz_code 0.43.0 → 0.44.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.
@@ -1 +1 @@
1
- {"version":3,"file":"svelte_preprocess_fuz_code.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/svelte_preprocess_fuz_code.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,iBAAiB,EAAW,MAAM,iBAAiB,CAAC;AAcxE,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,oBAAoB,CAAC;AAErD,MAAM,WAAW,wBAAwB;IACxC,gCAAgC;IAChC,OAAO,CAAC,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC;IAEjC,0DAA0D;IAC1D,aAAa,CAAC,EAAE,YAAY,CAAC;IAE7B,8CAA8C;IAC9C,KAAK,CAAC,EAAE,OAAO,CAAC;IAEhB;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAElC;;;OAGG;IACH,QAAQ,CAAC,EAAE,KAAK,GAAG,OAAO,CAAC;CAC3B;AAED,eAAO,MAAM,0BAA0B,GACtC,UAAS,wBAA6B,KACpC,iBA0DF,CAAC"}
1
+ {"version":3,"file":"svelte_preprocess_fuz_code.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/svelte_preprocess_fuz_code.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,iBAAiB,EAAW,MAAM,iBAAiB,CAAC;AAexE,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,oBAAoB,CAAC;AAErD,MAAM,WAAW,wBAAwB;IACxC,gCAAgC;IAChC,OAAO,CAAC,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC;IAEjC,0DAA0D;IAC1D,aAAa,CAAC,EAAE,YAAY,CAAC;IAE7B,8CAA8C;IAC9C,KAAK,CAAC,EAAE,OAAO,CAAC;IAEhB;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAElC;;;OAGG;IACH,QAAQ,CAAC,EAAE,KAAK,GAAG,OAAO,CAAC;CAC3B;AAED,eAAO,MAAM,0BAA0B,GACtC,UAAS,wBAA6B,KACpC,iBA6DF,CAAC"}
@@ -3,10 +3,10 @@ import MagicString from 'magic-string';
3
3
  import { walk } from 'zimmerframe';
4
4
  import { should_exclude_path } from '@fuzdev/fuz_util/path.js';
5
5
  import { escape_js_string } from '@fuzdev/fuz_util/string.js';
6
- import { find_attribute, evaluate_static_expr, extract_static_string, resolve_component_names, } from '@fuzdev/fuz_util/svelte_preprocess_helpers.js';
6
+ import { find_attribute, evaluate_static_expr, extract_static_string, build_static_bindings, resolve_component_names, } from '@fuzdev/fuz_util/svelte_preprocess_helpers.js';
7
7
  import { syntax_styler_global } from './syntax_styler_global.js';
8
8
  export const svelte_preprocess_fuz_code = (options = {}) => {
9
- const { exclude = [], syntax_styler = syntax_styler_global, cache = true, component_imports = ['@fuzdev/fuz_code/Code.svelte'], on_error = process.env.CI ? 'throw' : 'log', } = options;
9
+ const { exclude = [], syntax_styler = syntax_styler_global, cache = true, component_imports = ['@fuzdev/fuz_code/Code.svelte'], on_error = process.env.CI === 'true' ? 'throw' : 'log', } = options;
10
10
  // In-memory cache: content+lang hash → highlighted HTML
11
11
  const highlight_cache = new Map();
12
12
  return {
@@ -27,12 +27,14 @@ export const svelte_preprocess_fuz_code = (options = {}) => {
27
27
  if (code_names.size === 0) {
28
28
  return { code: content };
29
29
  }
30
+ const bindings = build_static_bindings(ast);
30
31
  // Find Code component usages with static content
31
32
  const transformations = find_code_usages(ast, syntax_styler, code_names, {
32
33
  cache: cache ? highlight_cache : null,
33
34
  on_error,
34
35
  filename,
35
36
  source: content,
37
+ bindings,
36
38
  });
37
39
  if (transformations.length === 0) {
38
40
  return { code: content };
@@ -95,13 +97,15 @@ const find_code_usages = (ast, syntax_styler, code_names, options) => {
95
97
  }
96
98
  // Resolve language - must be static and supported
97
99
  const lang_attr = find_attribute(node, 'lang');
98
- const lang_value = lang_attr ? extract_static_string(lang_attr.value) : 'svelte';
100
+ const lang_value = lang_attr
101
+ ? extract_static_string(lang_attr.value, options.bindings)
102
+ : 'svelte';
99
103
  if (lang_value === null)
100
104
  return;
101
105
  if (!syntax_styler.langs[lang_value])
102
106
  return;
103
107
  // Try simple static string
104
- const content_value = extract_static_string(content_attr.value);
108
+ const content_value = extract_static_string(content_attr.value, options.bindings);
105
109
  if (content_value !== null) {
106
110
  const html = try_highlight(content_value, lang_value, syntax_styler, options);
107
111
  if (html === null || html === content_value)
@@ -114,7 +118,7 @@ const find_code_usages = (ast, syntax_styler, code_names, options) => {
114
118
  return;
115
119
  }
116
120
  // Try conditional expression with static string branches
117
- const conditional = try_extract_conditional(content_attr.value, options.source);
121
+ const conditional = try_extract_conditional(content_attr.value, options.source, options.bindings);
118
122
  if (conditional) {
119
123
  const html_a = try_highlight(conditional.consequent, lang_value, syntax_styler, options);
120
124
  const html_b = try_highlight(conditional.alternate, lang_value, syntax_styler, options);
@@ -136,16 +140,16 @@ const find_code_usages = (ast, syntax_styler, code_names, options) => {
136
140
  * Try to extract a conditional expression where both branches are static strings.
137
141
  * Returns the condition source text and both branch values, or `null` if not applicable.
138
142
  */
139
- const try_extract_conditional = (value, source) => {
143
+ const try_extract_conditional = (value, source, bindings) => {
140
144
  if (value === true || Array.isArray(value))
141
145
  return null;
142
146
  const expr = value.expression;
143
147
  if (expr.type !== 'ConditionalExpression')
144
148
  return null;
145
- const consequent = evaluate_static_expr(expr.consequent);
149
+ const consequent = evaluate_static_expr(expr.consequent, bindings);
146
150
  if (consequent === null)
147
151
  return null;
148
- const alternate = evaluate_static_expr(expr.alternate);
152
+ const alternate = evaluate_static_expr(expr.alternate, bindings);
149
153
  if (alternate === null)
150
154
  return null;
151
155
  const test = expr.test;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fuzdev/fuz_code",
3
- "version": "0.43.0",
3
+ "version": "0.44.0",
4
4
  "description": "syntax styling utilities and components for TypeScript, Svelte, and Markdown",
5
5
  "glyph": "🎨",
6
6
  "logo": "logo.svg",
@@ -32,8 +32,8 @@
32
32
  "node": ">=22.15"
33
33
  },
34
34
  "peerDependencies": {
35
- "@fuzdev/fuz_css": ">=0.44.1",
36
- "@fuzdev/fuz_util": ">=0.49.0",
35
+ "@fuzdev/fuz_css": ">=0.47.0",
36
+ "@fuzdev/fuz_util": ">=0.49.2",
37
37
  "esm-env": "^1",
38
38
  "magic-string": "^0.30",
39
39
  "svelte": "^5",
@@ -60,9 +60,9 @@
60
60
  "@changesets/changelog-git": "^0.2.1",
61
61
  "@fuzdev/fuz_css": "^0.47.0",
62
62
  "@fuzdev/fuz_ui": "^0.181.1",
63
- "@fuzdev/fuz_util": "^0.49.0",
63
+ "@fuzdev/fuz_util": "^0.49.2",
64
64
  "@ryanatkn/eslint-config": "^0.9.0",
65
- "@ryanatkn/gro": "^0.190.0",
65
+ "@ryanatkn/gro": "^0.191.0",
66
66
  "@sveltejs/adapter-static": "^3.0.10",
67
67
  "@sveltejs/kit": "^2.50.1",
68
68
  "@sveltejs/package": "^2.5.7",
@@ -7,6 +7,7 @@ import {
7
7
  find_attribute,
8
8
  evaluate_static_expr,
9
9
  extract_static_string,
10
+ build_static_bindings,
10
11
  resolve_component_names,
11
12
  type ResolvedComponentImport,
12
13
  } from '@fuzdev/fuz_util/svelte_preprocess_helpers.js';
@@ -47,7 +48,7 @@ export const svelte_preprocess_fuz_code = (
47
48
  syntax_styler = syntax_styler_global,
48
49
  cache = true,
49
50
  component_imports = ['@fuzdev/fuz_code/Code.svelte'],
50
- on_error = process.env.CI ? 'throw' : 'log',
51
+ on_error = process.env.CI === 'true' ? 'throw' : 'log',
51
52
  } = options;
52
53
 
53
54
  // In-memory cache: content+lang hash → highlighted HTML
@@ -76,12 +77,15 @@ export const svelte_preprocess_fuz_code = (
76
77
  return {code: content};
77
78
  }
78
79
 
80
+ const bindings = build_static_bindings(ast);
81
+
79
82
  // Find Code component usages with static content
80
83
  const transformations = find_code_usages(ast, syntax_styler, code_names, {
81
84
  cache: cache ? highlight_cache : null,
82
85
  on_error,
83
86
  filename,
84
87
  source: content,
88
+ bindings,
85
89
  });
86
90
 
87
91
  if (transformations.length === 0) {
@@ -112,6 +116,7 @@ interface FindCodeUsagesOptions {
112
116
  on_error: 'log' | 'throw';
113
117
  filename: string | undefined;
114
118
  source: string;
119
+ bindings: ReadonlyMap<string, string>;
115
120
  }
116
121
 
117
122
  /**
@@ -176,12 +181,14 @@ const find_code_usages = (
176
181
 
177
182
  // Resolve language - must be static and supported
178
183
  const lang_attr = find_attribute(node, 'lang');
179
- const lang_value = lang_attr ? extract_static_string(lang_attr.value) : 'svelte';
184
+ const lang_value = lang_attr
185
+ ? extract_static_string(lang_attr.value, options.bindings)
186
+ : 'svelte';
180
187
  if (lang_value === null) return;
181
188
  if (!syntax_styler.langs[lang_value]) return;
182
189
 
183
190
  // Try simple static string
184
- const content_value = extract_static_string(content_attr.value);
191
+ const content_value = extract_static_string(content_attr.value, options.bindings);
185
192
  if (content_value !== null) {
186
193
  const html = try_highlight(content_value, lang_value, syntax_styler, options);
187
194
  if (html === null || html === content_value) return;
@@ -194,7 +201,11 @@ const find_code_usages = (
194
201
  }
195
202
 
196
203
  // Try conditional expression with static string branches
197
- const conditional = try_extract_conditional(content_attr.value, options.source);
204
+ const conditional = try_extract_conditional(
205
+ content_attr.value,
206
+ options.source,
207
+ options.bindings,
208
+ );
198
209
  if (conditional) {
199
210
  const html_a = try_highlight(conditional.consequent, lang_value, syntax_styler, options);
200
211
  const html_b = try_highlight(conditional.alternate, lang_value, syntax_styler, options);
@@ -212,7 +223,7 @@ const find_code_usages = (
212
223
  return transformations;
213
224
  };
214
225
 
215
- type Attribute_Value = AST.Attribute['value'];
226
+ type AttributeValue = AST.Attribute['value'];
216
227
 
217
228
  interface ConditionalStaticStrings {
218
229
  test_source: string;
@@ -225,16 +236,17 @@ interface ConditionalStaticStrings {
225
236
  * Returns the condition source text and both branch values, or `null` if not applicable.
226
237
  */
227
238
  const try_extract_conditional = (
228
- value: Attribute_Value,
239
+ value: AttributeValue,
229
240
  source: string,
241
+ bindings: ReadonlyMap<string, string>,
230
242
  ): ConditionalStaticStrings | null => {
231
243
  if (value === true || Array.isArray(value)) return null;
232
244
  const expr = value.expression;
233
245
  if (expr.type !== 'ConditionalExpression') return null;
234
246
 
235
- const consequent = evaluate_static_expr(expr.consequent);
247
+ const consequent = evaluate_static_expr(expr.consequent, bindings);
236
248
  if (consequent === null) return null;
237
- const alternate = evaluate_static_expr(expr.alternate);
249
+ const alternate = evaluate_static_expr(expr.alternate, bindings);
238
250
  if (alternate === null) return null;
239
251
 
240
252
  const test = expr.test as any;