@wuchale/svelte 0.20.0 → 0.20.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.
@@ -4,17 +4,13 @@ import type { CodePattern, HeuristicFunc, Message, RuntimeConf, TransformCtx, Tr
4
4
  import { MixedVisitor } from 'wuchale/adapter-utils';
5
5
  import { Transformer } from 'wuchale/adapter-vanilla';
6
6
  type MixedNodesTypes = AST.Text | AST.Tag | AST.ElementLike | AST.SvelteElement | AST.Block | AST.Comment;
7
- type AddCtx = {
8
- currentSnippet: number;
9
- };
10
- type MixedVisitorSvelte = MixedVisitor<MixedNodesTypes, AddCtx, AST.Text, AST.Comment, AST.ExpressionTag>;
7
+ type MixedVisitorSvelte = MixedVisitor<MixedNodesTypes, AST.Text, AST.Comment, AST.ExpressionTag>;
11
8
  export type RuntimeCtxSv = {
12
9
  module: boolean;
13
10
  };
14
11
  export declare class SvelteTransformer extends Transformer {
15
12
  currentElement?: string | undefined;
16
- inCompoundText: boolean;
17
- addCtx: AddCtx;
13
+ currentSnippet: number;
18
14
  moduleExportExprs: AnyNode[];
19
15
  mixedVisitor: MixedVisitorSvelte;
20
16
  constructor(ctx: TransformCtx, heuristic: HeuristicFunc, patterns: CodePattern[], rtConf: RuntimeConf);
@@ -24,7 +20,6 @@ export declare class SvelteTransformer extends Transformer {
24
20
  visitFragment(node: AST.Fragment): Message[];
25
21
  visitRegularElement(node: AST.ElementLike): Message[];
26
22
  visitComponent(node: AST.Component): Message[];
27
- visitText(node: AST.Text): Message[];
28
23
  visitSpreadAttribute(node: AST.SpreadAttribute): Message[];
29
24
  visitAttribute(node: AST.Attribute): Message[];
30
25
  visitConstTag(node: AST.ConstTag): Message[];
@@ -1,6 +1,6 @@
1
1
  import { parse, preprocess } from 'svelte/compiler';
2
2
  import { getKey } from 'wuchale';
3
- import { MixedVisitor, nonWhitespaceText, varNames } from 'wuchale/adapter-utils';
3
+ import { MixedVisitor, varNames } from 'wuchale/adapter-utils';
4
4
  import { parseScript, Transformer } from 'wuchale/adapter-vanilla';
5
5
  const nodesWithChildren = ['RegularElement', 'Component', 'SvelteElement'];
6
6
  const noWrapTopCalls = ['$props', '$state', '$derived', '$effect'];
@@ -17,8 +17,7 @@ const removeCSS = ({ content }) => ({
17
17
  export class SvelteTransformer extends Transformer {
18
18
  // state
19
19
  currentElement;
20
- inCompoundText = false;
21
- addCtx = { currentSnippet: 0 };
20
+ currentSnippet = 0;
22
21
  moduleExportExprs = []; // to choose which runtime var to use for snippets
23
22
  mixedVisitor;
24
23
  constructor(ctx, heuristic, patterns, rtConf) {
@@ -64,27 +63,30 @@ export class SvelteTransformer extends Transformer {
64
63
  }
65
64
  initMixedVisitor() {
66
65
  return new MixedVisitor({
67
- vars: this.vars.bind(this),
66
+ mstr: this.mstr,
67
+ index: this.index,
68
68
  content: this.content,
69
+ vars: this.vars.bind(this),
69
70
  getRange: node => ({ start: node.start, end: node.end }),
70
71
  isText: node => node.type === 'Text',
71
72
  isComment: node => node.type === 'Comment',
72
73
  leaveInPlace: node => ['ConstTag', 'SnippetBlock'].includes(node.type),
74
+ canHaveChildren: node => nodesWithChildren.includes(node.type),
73
75
  isExpression: node => node.type === 'ExpressionTag',
74
76
  getTextContent: node => node.data,
75
77
  getCommentData: node => node.data.trim(),
76
- canHaveChildren: node => nodesWithChildren.includes(node.type),
77
- visitFunc: MixedVisitor.withCtxRestore(this, this.visitSv.bind(this)),
78
+ visitFunc: this.visitSv.bind(this),
78
79
  fullHeuristicDetails: this.fullHeuristicDetails.bind(this),
79
80
  checkHeuristic: this.getHeuristicMessageType.bind(this),
80
- wrapNested: (msgInfo, hasExprs, nestedRanges, lastChildEnd) => {
81
+ wrapNested: (inNested, msgInfo, hasExprs, nestedRanges, lastChildEnd) => {
81
82
  const snippets = [];
83
+ const vars = this.vars();
82
84
  // create and reference snippets
83
85
  for (const [childStart, childEnd, haveCtx] of nestedRanges) {
84
- const snippetName = `${snipPrefix}${this.addCtx.currentSnippet}`;
86
+ const snippetName = `${snipPrefix}${this.currentSnippet}`;
85
87
  snippets.push(snippetName);
86
- this.addCtx.currentSnippet++;
87
- const snippetBegin = `\n{#snippet ${snippetName}(${haveCtx ? this.vars().nestCtx : ''})}\n`;
88
+ this.currentSnippet++;
89
+ const snippetBegin = `\n{#snippet ${snippetName}(${haveCtx ? vars.nestCtx : ''})}\n`;
88
90
  this.mstr.appendRight(childStart, snippetBegin);
89
91
  this.mstr.prependLeft(childEnd, '\n{/snippet}\n');
90
92
  }
@@ -93,12 +95,12 @@ export class SvelteTransformer extends Transformer {
93
95
  begin += ` t={[${snippets.join(', ')}]}`;
94
96
  }
95
97
  begin += ' x=';
96
- if (this.inCompoundText) {
97
- begin += `{${this.vars().nestCtx}} n`;
98
+ if (inNested) {
99
+ begin += `{${vars.nestCtx}} n`;
98
100
  }
99
101
  else {
100
102
  const index = this.index.get(getKey(msgInfo.msgStr, msgInfo.context));
101
- begin += `{${this.vars().rtCtx}(${index})}`;
103
+ begin += `{${vars.rtCtx}(${index})}`;
102
104
  }
103
105
  let end = ' />\n';
104
106
  if (hasExprs) {
@@ -112,12 +114,8 @@ export class SvelteTransformer extends Transformer {
112
114
  }
113
115
  visitFragment(node) {
114
116
  return this.mixedVisitor.visit({
115
- mstr: this.mstr,
116
- index: this.index,
117
- addCtx: this.addCtx,
118
117
  children: node.nodes,
119
118
  commentDirectives: this.commentDirectives,
120
- inCompoundText: this.inCompoundText,
121
119
  scope: 'markup',
122
120
  element: this.currentElement,
123
121
  useComponent: this.currentElement !== 'title',
@@ -137,18 +135,6 @@ export class SvelteTransformer extends Transformer {
137
135
  visitComponent(node) {
138
136
  return this.visitRegularElement(node);
139
137
  }
140
- visitText(node) {
141
- const [startWh, trimmed, endWh] = nonWhitespaceText(node.data);
142
- const [pass, msgInfo] = this.checkHeuristic(trimmed, {
143
- scope: 'markup',
144
- element: this.currentElement,
145
- });
146
- if (!pass) {
147
- return [];
148
- }
149
- this.mstr.update(node.start + startWh, node.end - endWh, `{${this.literalRepl(msgInfo)}}`);
150
- return [msgInfo];
151
- }
152
138
  visitSpreadAttribute(node) {
153
139
  return this.visit(node.expression);
154
140
  }
@@ -164,17 +150,15 @@ export class SvelteTransformer extends Transformer {
164
150
  values = [node.value];
165
151
  }
166
152
  if (values.length > 1) {
167
- return this.mixedVisitor.visit({
168
- mstr: this.mstr,
169
- index: this.index,
170
- addCtx: this.addCtx,
153
+ const msgs = this.mixedVisitor.visit({
171
154
  children: values,
172
155
  commentDirectives: this.commentDirectives,
173
- inCompoundText: false,
174
156
  scope: 'attribute',
175
157
  element: this.currentElement,
176
158
  attribute: node.name,
177
159
  });
160
+ msgs.push(...this.mixedVisitor.applyMod('attribute'));
161
+ return msgs;
178
162
  }
179
163
  const value = values[0];
180
164
  const heuDetails = {
@@ -334,7 +318,7 @@ export class SvelteTransformer extends Transformer {
334
318
  this.commentDirectives = {}; // reset
335
319
  msgs.push(...this.visitProgram(node.instance.content));
336
320
  }
337
- msgs.push(...this.visitFragment(node.fragment));
321
+ msgs.push(...this.visitFragment(node.fragment), ...this.mixedVisitor.applyMod());
338
322
  return msgs;
339
323
  }
340
324
  visitSv(node) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wuchale/svelte",
3
- "version": "0.20.0",
3
+ "version": "0.20.2",
4
4
  "description": "Protobuf-like i18n from plain code: Svelte adapter",
5
5
  "scripts": {
6
6
  "dev": "tsc --watch",
@@ -54,12 +54,12 @@
54
54
  "svelte": "^5"
55
55
  },
56
56
  "dependencies": {
57
- "wuchale": "^0.24.0"
57
+ "wuchale": "^0.25.3"
58
58
  },
59
59
  "devDependencies": {
60
- "@types/node": "~24.13.1",
60
+ "@types/node": "~24.13.2",
61
61
  "svelte": "^5.56.3",
62
- "acorn": "^8.16.0",
62
+ "acorn": "^8.17.0",
63
63
  "typescript": "^6.0.3"
64
64
  },
65
65
  "type": "module"