@wuchale/svelte 0.19.1 → 0.19.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.
@@ -11,7 +11,7 @@ export declare class SvelteTransformer extends Transformer<RuntimeCtxSv> {
11
11
  currentElement?: string;
12
12
  inCompoundText: boolean;
13
13
  currentSnippet: number;
14
- moduleExportRanges: [number, number][];
14
+ moduleExportExprs: AnyNode[];
15
15
  mixedVisitor: MixedVisitor<MixedNodesTypes>;
16
16
  constructor(content: string, filename: string, index: IndexTracker, heuristic: HeuristicFunc, patterns: CodePattern[], catalogExpr: CatalogExpr, rtConf: RuntimeConf<RuntimeCtxSv>, matchUrl: UrlMatcher);
17
17
  visitExpressionTag: (node: AST.ExpressionTag) => Message[];
@@ -25,6 +25,9 @@ export declare class SvelteTransformer extends Transformer<RuntimeCtxSv> {
25
25
  visitAttribute: (node: AST.Attribute) => Message[];
26
26
  visitConstTag: (node: AST.ConstTag) => Message[];
27
27
  visitRenderTag: (node: AST.RenderTag) => Message[];
28
+ visitHtmlTag: (node: AST.HtmlTag) => Message[];
29
+ visitOnDirective: (node: AST.OnDirective) => Message[];
30
+ hasIdentifier: (node: AnyNode | AnyNode[], name: string) => boolean;
28
31
  visitSnippetBlock: (node: AST.SnippetBlock) => Message[];
29
32
  visitIfBlock: (node: AST.IfBlock) => Message[];
30
33
  visitEachBlock: (node: AST.EachBlock) => Message[];
@@ -40,7 +43,7 @@ export declare class SvelteTransformer extends Transformer<RuntimeCtxSv> {
40
43
  visitRoot: (node: AST.Root) => Message[];
41
44
  visitSv: (node: AST.SvelteNode | AnyNode) => Message[];
42
45
  /** collects the ranges that will be checked if a snippet identifier is exported using RegExp test to simplify */
43
- collectModuleExportRanges: (script: AST.Script) => void;
46
+ collectModuleExportExprs: (script: AST.Script) => void;
44
47
  transformSv: () => Promise<TransformOutput>;
45
48
  }
46
49
  export {};
@@ -20,7 +20,7 @@ export class SvelteTransformer extends Transformer {
20
20
  currentElement;
21
21
  inCompoundText = false;
22
22
  currentSnippet = 0;
23
- moduleExportRanges = []; // to choose which runtime var to use for snippets
23
+ moduleExportExprs = []; // to choose which runtime var to use for snippets
24
24
  mixedVisitor;
25
25
  constructor(content, filename, index, heuristic, patterns, catalogExpr, rtConf, matchUrl) {
26
26
  super(content, filename, index, heuristic, patterns, catalogExpr, rtConf, matchUrl, [varNames.rt, rtModuleVar]);
@@ -37,6 +37,9 @@ export class SvelteTransformer extends Transformer {
37
37
  return msgs;
38
38
  }
39
39
  const needsWrapping = msgs.some(msg => {
40
+ if (msg.details.leftSide) {
41
+ return false;
42
+ }
40
43
  const topCall = msg.details.topLevelCall ?? '';
41
44
  if (noWrapTopCalls.includes(topCall) || noWrapTopCalls.some(c => topCall.startsWith(`${c}.`))) {
42
45
  return false;
@@ -49,7 +52,7 @@ export class SvelteTransformer extends Transformer {
49
52
  if (!needsWrapping) {
50
53
  return msgs;
51
54
  }
52
- const isExported = this.moduleExportRanges.some(([start, end]) => init.start >= start && init.end <= end);
55
+ const isExported = this.moduleExportExprs.some(node => init.start >= node.start && init.end <= node.end);
53
56
  if (!isExported) {
54
57
  this.mstr.appendLeft(init.start, '$derived(');
55
58
  this.mstr.appendRight(init.end, ')');
@@ -139,7 +142,7 @@ export class SvelteTransformer extends Transformer {
139
142
  if (!pass) {
140
143
  return [];
141
144
  }
142
- this.mstr.update(node.start + startWh, node.end - endWh, `{${this.vars().rtTrans}(${this.index.get(getKey(msgInfo.msgStr, msgInfo.context))})}`);
145
+ this.mstr.update(node.start + startWh, node.end - endWh, `{${this.literalRepl(msgInfo)}}`);
143
146
  return [msgInfo];
144
147
  };
145
148
  visitSpreadAttribute = (node) => this.visit(node.expression);
@@ -197,15 +200,25 @@ export class SvelteTransformer extends Transformer {
197
200
  // @ts-expect-error
198
201
  return this.visitVariableDeclaration(node.declaration);
199
202
  };
200
- visitRenderTag = (node) => {
201
- // @ts-expect-error
202
- return this.visit(node.expression);
203
+ visitRenderTag = (node) => this.visit(node.expression);
204
+ visitHtmlTag = (node) => this.visit(node.expression);
205
+ visitOnDirective = (node) => this.visit(node.expression);
206
+ hasIdentifier = (node, name) => {
207
+ if (!node || typeof node !== 'object') {
208
+ return false;
209
+ }
210
+ if (Array.isArray(node)) {
211
+ return node.some(child => this.hasIdentifier(child, name));
212
+ }
213
+ if (node.type === 'Identifier') {
214
+ return node.name === name;
215
+ }
216
+ return Object.values(node).some(value => this.hasIdentifier(value, name));
203
217
  };
204
218
  visitSnippetBlock = (node) => {
205
219
  // use module runtime var because the snippet may be exported from the module
206
220
  const prevRtVar = this.currentRtVar;
207
- const pattern = new RegExp(`\\b${node.expression.name}\\b`);
208
- if (this.moduleExportRanges.some(([start, end]) => pattern.test(this.content.slice(start, end)))) {
221
+ if (this.hasIdentifier(this.moduleExportExprs, node.expression.name)) {
209
222
  this.currentRtVar = rtModuleVar;
210
223
  }
211
224
  const msgs = this.visitFragment(node.body);
@@ -283,7 +296,7 @@ export class SvelteTransformer extends Transformer {
283
296
  };
284
297
  visitSv = (node) => this.visit(node);
285
298
  /** collects the ranges that will be checked if a snippet identifier is exported using RegExp test to simplify */
286
- collectModuleExportRanges = (script) => {
299
+ collectModuleExportExprs = (script) => {
287
300
  for (const stmt of script.content.body) {
288
301
  if (stmt.type !== 'ExportNamedDeclaration') {
289
302
  continue;
@@ -291,7 +304,7 @@ export class SvelteTransformer extends Transformer {
291
304
  for (const spec of stmt.specifiers) {
292
305
  if (spec.local.type === 'Identifier') {
293
306
  const local = spec.local;
294
- this.moduleExportRanges.push([local.start, local.end]);
307
+ this.moduleExportExprs.push(local);
295
308
  }
296
309
  }
297
310
  const declaration = stmt.declaration;
@@ -299,14 +312,14 @@ export class SvelteTransformer extends Transformer {
299
312
  continue;
300
313
  }
301
314
  if (declaration.type === 'FunctionDeclaration' || declaration.type === 'ClassDeclaration') {
302
- this.moduleExportRanges.push([declaration.start, declaration.end]);
315
+ this.moduleExportExprs.push(declaration);
303
316
  continue;
304
317
  }
305
318
  for (const decl of declaration?.declarations ?? []) {
306
319
  if (!decl.init) {
307
320
  continue;
308
321
  }
309
- this.moduleExportRanges.push([decl.init.start, decl.init.end]);
322
+ this.moduleExportExprs.push(decl.init);
310
323
  }
311
324
  }
312
325
  };
@@ -324,7 +337,7 @@ export class SvelteTransformer extends Transformer {
324
337
  this.mstr = new MagicString(this.content);
325
338
  this.mixedVisitor = this.initMixedVisitor();
326
339
  if (ast.type === 'Root' && ast.module) {
327
- this.collectModuleExportRanges(ast.module);
340
+ this.collectModuleExportExprs(ast.module);
328
341
  }
329
342
  const msgs = this.visitSv(ast);
330
343
  const initRuntime = this.initRuntime();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wuchale/svelte",
3
- "version": "0.19.1",
3
+ "version": "0.19.3",
4
4
  "description": "Protobuf-like i18n from plain code: Svelte adapter",
5
5
  "scripts": {
6
6
  "dev": "tsc --watch",
@@ -53,7 +53,7 @@
53
53
  "dependencies": {
54
54
  "magic-string": "^0.30.21",
55
55
  "svelte": "^5",
56
- "wuchale": "^0.21.2"
56
+ "wuchale": "^0.22.1"
57
57
  },
58
58
  "devDependencies": {
59
59
  "acorn": "^8.16.0",