@wuchale/svelte 0.16.1 → 0.16.2

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 {
11
11
  commentDirectivesStack: CommentDirectives[];
12
12
  lastVisitIsComment: boolean;
13
13
  currentSnippet: number;
14
- hasModuleScript: boolean;
14
+ moduleExportRanges: [number, number][];
15
15
  mixedVisitor: MixedVisitor<MixedNodesTypes>;
16
16
  constructor(content: string, filename: string, index: IndexTracker, heuristic: HeuristicFunc, patterns: CodePattern[], catalogExpr: CatalogExpr, rtConf: RuntimeConf);
17
17
  visitExpressionTag: (node: AST.ExpressionTag) => Message[];
@@ -37,6 +37,8 @@ export declare class SvelteTransformer extends Transformer {
37
37
  visitSvelteWindow: (node: AST.SvelteWindow) => Message[];
38
38
  visitRoot: (node: AST.Root) => Message[];
39
39
  visitSv: (node: AST.SvelteNode | AnyNode) => Message[];
40
+ /** collects the ranges that will be checked if a snippet identifier is exported using RegExp test to simplify */
41
+ collectModuleExportRanges: (script: AST.Script) => void;
40
42
  transformSv: () => TransformOutput;
41
43
  }
42
44
  export {};
@@ -14,7 +14,7 @@ export class SvelteTransformer extends Transformer {
14
14
  commentDirectivesStack = [];
15
15
  lastVisitIsComment = false;
16
16
  currentSnippet = 0;
17
- hasModuleScript = false; // to choose which runtime var to use for snippets
17
+ moduleExportRanges = []; // to choose which runtime var to use for snippets
18
18
  mixedVisitor;
19
19
  constructor(content, filename, index, heuristic, patterns, catalogExpr, rtConf) {
20
20
  super(content, filename, index, heuristic, patterns, catalogExpr, rtConf, [varNames.rt, rtModuleVar]);
@@ -26,7 +26,7 @@ export class SvelteTransformer extends Transformer {
26
26
  return msgs;
27
27
  }
28
28
  const needsWrapping = msgs.some(msg => {
29
- if (['$derived', '$derived.by'].includes(msg.details.topLevelCall)) {
29
+ if (['$props', '$derived', '$derived.by'].includes(msg.details.topLevelCall)) {
30
30
  return false;
31
31
  }
32
32
  if (msg.details.declaring !== 'variable') {
@@ -169,7 +169,8 @@ export class SvelteTransformer extends Transformer {
169
169
  visitSnippetBlock = (node) => {
170
170
  // use module runtime var because the snippet may be exported from the module
171
171
  const prevRtVar = this.currentRtVar;
172
- if (this.hasModuleScript) {
172
+ const pattern = new RegExp(`\\b${node.expression.name}\\b`);
173
+ if (this.moduleExportRanges.some(([start, end]) => pattern.test(this.content.slice(start, end)))) {
173
174
  this.currentRtVar = rtModuleVar;
174
175
  }
175
176
  const msgs = this.visitFragment(node.body);
@@ -278,6 +279,34 @@ export class SvelteTransformer extends Transformer {
278
279
  this.commentDirectives = commentDirectivesPrev;
279
280
  return msgs;
280
281
  };
282
+ /** collects the ranges that will be checked if a snippet identifier is exported using RegExp test to simplify */
283
+ collectModuleExportRanges = (script) => {
284
+ for (const stmt of script.content.body) {
285
+ if (stmt.type !== 'ExportNamedDeclaration') {
286
+ continue;
287
+ }
288
+ for (const spec of stmt.specifiers) {
289
+ if (spec.local.type === 'Identifier') {
290
+ const local = spec.local;
291
+ this.moduleExportRanges.push([local.start, local.end]);
292
+ }
293
+ }
294
+ const declaration = stmt.declaration;
295
+ if (!declaration) {
296
+ continue;
297
+ }
298
+ if (declaration.type === 'FunctionDeclaration' || declaration.type === 'ClassDeclaration') {
299
+ this.moduleExportRanges.push([declaration.start, declaration.end]);
300
+ continue;
301
+ }
302
+ for (const decl of declaration.declarations) {
303
+ if (!decl.init) {
304
+ continue;
305
+ }
306
+ this.moduleExportRanges.push([decl.init.start, decl.init.end]);
307
+ }
308
+ }
309
+ };
281
310
  transformSv = () => {
282
311
  const isComponent = this.filename.endsWith('.svelte');
283
312
  let ast;
@@ -291,7 +320,9 @@ export class SvelteTransformer extends Transformer {
291
320
  }
292
321
  this.mstr = new MagicString(this.content);
293
322
  this.mixedVisitor = this.initMixedVisitor();
294
- this.hasModuleScript = ast.type === 'Root' && ast.module != null;
323
+ if (ast.type === 'Root' && ast.module) {
324
+ this.collectModuleExportRanges(ast.module);
325
+ }
295
326
  const msgs = this.visitSv(ast);
296
327
  const initRuntime = this.initRuntime(this.filename, null, null, {});
297
328
  if (ast.type === 'Program') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wuchale/svelte",
3
- "version": "0.16.1",
3
+ "version": "0.16.2",
4
4
  "description": "Protobuf-like i18n from plain code: Svelte adapter",
5
5
  "scripts": {
6
6
  "dev": "tsc --watch",
@@ -56,7 +56,7 @@
56
56
  },
57
57
  "devDependencies": {
58
58
  "acorn": "^8.15.0",
59
- "typescript": "^5.8.3"
59
+ "typescript": "^5.9.3"
60
60
  },
61
61
  "type": "module"
62
62
  }