@wuchale/jsx 0.9.7 → 0.10.1

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.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { HeuristicFunc, Adapter, AdapterArgs, LoaderChoice, CreateHeuristicOpts } from 'wuchale';
2
- import { type JSXLib } from "./transformer.js";
1
+ import type { Adapter, AdapterArgs, CreateHeuristicOpts, HeuristicFunc, LoaderChoice } from 'wuchale';
2
+ import { type JSXLib } from './transformer.js';
3
3
  export declare function createJsxHeuristic(opts: CreateHeuristicOpts): HeuristicFunc;
4
4
  export declare const jsxDefaultHeuristic: HeuristicFunc;
5
5
  type LoadersAvailable = 'default' | 'react' | 'solidjs';
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
- import { defaultGenerateLoadID, deepMergeObjects, createHeuristic, defaultHeuristicOpts } from 'wuchale';
2
- import { pluralPattern, getDefaultLoaderPath as getDefaultLoaderPathVanilla } from 'wuchale/adapter-vanilla';
3
- import { JSXTransformer } from "./transformer.js";
1
+ import { createHeuristic, deepMergeObjects, defaultGenerateLoadID, defaultHeuristicOpts } from 'wuchale';
4
2
  import { loaderPathResolver } from 'wuchale/adapter-utils';
3
+ import { getDefaultLoaderPath as getDefaultLoaderPathVanilla, pluralPattern } from 'wuchale/adapter-vanilla';
4
+ import { JSXTransformer } from './transformer.js';
5
5
  export function createJsxHeuristic(opts) {
6
6
  const defaultHeuristic = createHeuristic(opts);
7
- return msg => {
7
+ return (msg) => {
8
8
  const defRes = defaultHeuristic(msg);
9
9
  if (!defRes) {
10
10
  return false;
@@ -20,36 +20,31 @@ export function createJsxHeuristic(opts) {
20
20
  }
21
21
  export const jsxDefaultHeuristic = createJsxHeuristic(defaultHeuristicOpts);
22
22
  const defaultRuntime = {
23
- useReactive: ({ funcName, nested }) => {
23
+ initReactive: ({ funcName, nested }) => {
24
24
  const inTopLevel = funcName == null;
25
25
  const insideReactive = !inTopLevel && !nested && ((funcName.startsWith('use') && funcName.length > 3) || /[A-Z]/.test(funcName[0]));
26
- return {
27
- init: inTopLevel ? null : insideReactive,
28
- use: insideReactive
29
- };
26
+ return inTopLevel ? null : insideReactive;
30
27
  },
28
+ useReactive: ({ funcName, nested }) => funcName != null &&
29
+ !nested &&
30
+ ((funcName.startsWith('use') && funcName.length > 3) || /[A-Z]/.test(funcName[0])),
31
31
  reactive: {
32
- wrapInit: expr => expr,
33
- wrapUse: expr => expr,
32
+ wrapInit: (expr) => expr,
33
+ wrapUse: (expr) => expr,
34
34
  },
35
35
  plain: {
36
- wrapInit: expr => expr,
37
- wrapUse: expr => expr,
36
+ wrapInit: (expr) => expr,
37
+ wrapUse: (expr) => expr,
38
38
  },
39
39
  };
40
40
  const defaultRuntimeSolid = {
41
41
  ...defaultRuntime,
42
- useReactive: ({ funcName }) => {
43
- const inTopLevel = funcName == null;
44
- return {
45
- init: inTopLevel ? true : null, // init only in top level
46
- use: true, // always use reactive
47
- };
48
- },
42
+ initReactive: ({ funcName }) => (funcName == null ? true : null), // init only in top level
43
+ useReactive: true, // always reactive, because solidjs doesn't have a problem with it
49
44
  reactive: {
50
- wrapInit: expr => `() => ${expr}`,
51
- wrapUse: expr => `${expr}()`
52
- }
45
+ wrapInit: (expr) => `() => ${expr}`,
46
+ wrapUse: (expr) => `${expr}()`,
47
+ },
53
48
  };
54
49
  const defaultArgs = {
55
50
  files: { include: 'src/**/*.{js,ts,jsx,tsx}', ignore: '**/*.d.ts' },
@@ -89,7 +84,7 @@ export const adapter = (args = defaultArgs) => {
89
84
  defaultLoaderPath: getDefaultLoaderPath(loader, rest.bundleLoad),
90
85
  runtime,
91
86
  getRuntimeVars: {
92
- reactive: 'useW_load_rx_'
87
+ reactive: 'useW_load_rx_',
93
88
  },
94
89
  ...rest,
95
90
  };
package/dist/runtime.d.ts CHANGED
@@ -2,9 +2,9 @@ import type { Composite, Mixed } from 'wuchale';
2
2
  export type WuchaleComponentProps = {
3
3
  n?: boolean;
4
4
  x: Composite;
5
- t: Function;
5
+ t: Function[];
6
6
  a: any[];
7
7
  };
8
- export declare function selectFragment({ n, x, t, a }: WuchaleComponentProps, i: number): string | Mixed | Composite;
9
- declare const _default: (props: WuchaleComponentProps) => (string | Composite | Mixed)[];
8
+ export declare function selectFragment({ n, x, t, a }: WuchaleComponentProps, i: number): string | Mixed | Composite | undefined;
9
+ declare const _default: (props: WuchaleComponentProps) => (string | Composite | Mixed | undefined)[];
10
10
  export default _default;
package/dist/runtime.jsx CHANGED
@@ -6,7 +6,7 @@ export function selectFragment({ n, x, t, a }, i) {
6
6
  if (!n || i > 0) {
7
7
  return a[x];
8
8
  }
9
- return `i18n-400:${x}`;
9
+ return;
10
10
  }
11
11
  const tag = t[x[0]];
12
12
  if (tag == null) {
@@ -1,7 +1,7 @@
1
- import { selectFragment } from './runtime.jsx';
2
1
  import { For } from 'solid-js';
2
+ import { selectFragment } from './runtime.jsx';
3
3
  export default (props) => {
4
- return <For each={props.x}>
5
- {(fragment, i) => <>{selectFragment({ ...props, x: fragment }, i())}</>}
6
- </For>;
4
+ return (<For each={props.x}>
5
+ {(fragment, i) => <>{selectFragment({ ...props, x: fragment }, i())}</>}
6
+ </For>);
7
7
  };
@@ -1,16 +1,14 @@
1
- import { Message } from 'wuchale';
2
- import type * as JX from 'estree-jsx';
3
1
  import type * as Estree from 'acorn';
2
+ import type * as JX from 'estree-jsx';
3
+ import type { CatalogExpr, CodePattern, HeuristicFunc, IndexTracker, Message, RuntimeConf, TransformOutput, UrlMatcher } from 'wuchale';
4
+ import { MixedVisitor } from 'wuchale/adapter-utils';
4
5
  import { Transformer } from 'wuchale/adapter-vanilla';
5
- import type { IndexTracker, HeuristicFunc, TransformOutput, RuntimeConf, CatalogExpr, CodePattern, UrlMatcher } from 'wuchale';
6
- import { MixedVisitor, type CommentDirectives } from "wuchale/adapter-utils";
7
6
  export declare function parseScriptJSX(content: string): [Estree.Program, Estree.Comment[][]];
8
7
  type MixedNodesTypes = JX.JSXElement | JX.JSXFragment | JX.JSXText | JX.JSXExpressionContainer | JX.JSXSpreadChild;
9
8
  export type JSXLib = 'default' | 'solidjs';
10
9
  export declare class JSXTransformer extends Transformer {
11
10
  currentElement?: string;
12
11
  inCompoundText: boolean;
13
- commentDirectivesStack: CommentDirectives[];
14
12
  lastVisitIsComment: boolean;
15
13
  currentJsxKey?: number;
16
14
  mixedVisitor: MixedVisitor<MixedNodesTypes>;
@@ -28,7 +26,6 @@ export declare class JSXTransformer extends Transformer {
28
26
  visitJSXExpressionContainer: (node: JX.JSXExpressionContainer) => Message[];
29
27
  visitJSXAttribute: (node: JX.JSXAttribute) => Message[];
30
28
  visitJSXSpreadAttribute: (node: JX.JSXSpreadAttribute) => Message[];
31
- visitJSXEmptyExpression: (node: JX.JSXEmptyExpression) => Message[];
32
29
  visitJx: (node: JX.Node | JX.JSXSpreadChild | Estree.Program) => Message[];
33
30
  transformJx: (lib: JSXLib) => TransformOutput;
34
31
  }
@@ -1,9 +1,8 @@
1
- import MagicString from 'magic-string';
2
- import { Parser } from 'acorn';
3
- import { Message } from 'wuchale';
4
1
  import { tsPlugin } from '@sveltejs/acorn-typescript';
5
- import { Transformer, scriptParseOptionsWithComments, parseScript } from 'wuchale/adapter-vanilla';
6
- import { nonWhitespaceText, MixedVisitor, processCommentDirectives } from "wuchale/adapter-utils";
2
+ import { Parser } from 'acorn';
3
+ import MagicString from 'magic-string';
4
+ import { MixedVisitor, nonWhitespaceText } from 'wuchale/adapter-utils';
5
+ import { parseScript, scriptParseOptionsWithComments, Transformer } from 'wuchale/adapter-vanilla';
7
6
  const JsxParser = Parser.extend(tsPlugin({ jsx: true }));
8
7
  export function parseScriptJSX(content) {
9
8
  const [opts, comments] = scriptParseOptionsWithComments();
@@ -15,7 +14,6 @@ export class JSXTransformer extends Transformer {
15
14
  // state
16
15
  currentElement;
17
16
  inCompoundText = false;
18
- commentDirectivesStack = [];
19
17
  lastVisitIsComment = false;
20
18
  currentJsxKey;
21
19
  mixedVisitor;
@@ -25,19 +23,19 @@ export class JSXTransformer extends Transformer {
25
23
  initMixedVisitor = () => new MixedVisitor({
26
24
  mstr: this.mstr,
27
25
  vars: this.vars,
28
- getRange: node => ({
26
+ getRange: (node) => ({
29
27
  start: node.start,
30
- end: node.end
28
+ end: node.end,
31
29
  }),
32
- isComment: node => node.type === 'JSXExpressionContainer'
33
- && node.expression.type === 'JSXEmptyExpression'
34
- && node.expression.end > node.expression.start,
35
- isText: node => node.type === 'JSXText',
30
+ isComment: (node) => node.type === 'JSXExpressionContainer' &&
31
+ node.expression.type === 'JSXEmptyExpression' &&
32
+ node.expression.end > node.expression.start,
33
+ isText: (node) => node.type === 'JSXText',
36
34
  leaveInPlace: () => false,
37
- isExpression: node => node.type === 'JSXExpressionContainer',
35
+ isExpression: (node) => node.type === 'JSXExpressionContainer',
38
36
  getTextContent: (node) => node.value,
39
37
  getCommentData: (node) => this.getMarkupCommentBody(node.expression),
40
- canHaveChildren: node => nodesWithChildren.includes(node.type),
38
+ canHaveChildren: (node) => nodesWithChildren.includes(node.type),
41
39
  visitFunc: (child, inCompoundText) => {
42
40
  const inCompoundTextPrev = this.inCompoundText;
43
41
  this.inCompoundText = inCompoundText;
@@ -81,13 +79,19 @@ export class JSXTransformer extends Transformer {
81
79
  this.mstr.appendRight(lastChildEnd, end);
82
80
  },
83
81
  });
84
- visitChildrenJ = (node) => this.mixedVisitor.visit({
85
- children: node.children,
86
- commentDirectives: this.commentDirectives,
87
- inCompoundText: this.inCompoundText,
88
- scope: 'markup',
89
- element: this.currentElement,
90
- });
82
+ visitChildrenJ = (node) => {
83
+ const prevInsideProg = this.heuristciDetails.insideProgram;
84
+ this.heuristciDetails.insideProgram = false;
85
+ const msg = this.mixedVisitor.visit({
86
+ children: node.children,
87
+ commentDirectives: this.commentDirectives,
88
+ inCompoundText: this.inCompoundText,
89
+ scope: 'markup',
90
+ element: this.currentElement,
91
+ });
92
+ this.heuristciDetails.insideProgram = prevInsideProg; // restore
93
+ return msg;
94
+ };
91
95
  visitNameJSXNamespacedName = (node) => {
92
96
  return `${this.visitName(node.namespace)}:${this.visitName(node.name)}`;
93
97
  };
@@ -106,7 +110,7 @@ export class JSXTransformer extends Transformer {
106
110
  msgs.push(...this.visitJx(attr));
107
111
  }
108
112
  if (this.inCompoundText && this.currentJsxKey != null) {
109
- const key = node.openingElement.attributes.find(attr => attr.type === 'JSXAttribute' && attr.name.name === 'key');
113
+ const key = node.openingElement.attributes.find((attr) => attr.type === 'JSXAttribute' && attr.name.name === 'key');
110
114
  if (!key) {
111
115
  this.mstr.appendLeft(node.openingElement.name.end, ` key="_${this.currentJsxKey}"`);
112
116
  this.currentJsxKey++;
@@ -135,9 +139,7 @@ export class JSXTransformer extends Transformer {
135
139
  }
136
140
  return comment.slice(2, -2).trim();
137
141
  };
138
- visitJSXExpressionContainer = (node) => {
139
- return this.visit(node.expression);
140
- };
142
+ visitJSXExpressionContainer = (node) => this.visit(node.expression);
141
143
  visitJSXAttribute = (node) => {
142
144
  if (node.value == null) {
143
145
  return [];
@@ -179,46 +181,10 @@ export class JSXTransformer extends Transformer {
179
181
  return [msgInfo];
180
182
  };
181
183
  visitJSXSpreadAttribute = (node) => this.visit(node.argument);
182
- visitJSXEmptyExpression = (node) => {
183
- const commentContents = this.getMarkupCommentBody(node);
184
- if (!commentContents) {
185
- return [];
186
- }
187
- this.commentDirectives = processCommentDirectives(commentContents, this.commentDirectives);
188
- if (this.lastVisitIsComment) {
189
- this.commentDirectivesStack[this.commentDirectivesStack.length - 1] = this.commentDirectives;
190
- }
191
- else {
192
- this.commentDirectivesStack.push(this.commentDirectives);
193
- }
194
- this.lastVisitIsComment = true;
195
- return [];
196
- };
197
- visitJx = (node) => {
198
- if (node.type === 'JSXText' && !node.value.trim()) {
199
- return [];
200
- }
201
- if (node.type === 'JSXExpressionContainer' && node.expression.type === 'JSXEmptyExpression') { // markup comment
202
- return this.visitJSXEmptyExpression(node.expression);
203
- }
204
- let msgs = [];
205
- const commentDirectivesPrev = this.commentDirectives;
206
- if (this.lastVisitIsComment) {
207
- this.commentDirectives = this.commentDirectivesStack.pop();
208
- this.lastVisitIsComment = false;
209
- }
210
- if (this.commentDirectives.ignoreFile) {
211
- return [];
212
- }
213
- if (this.commentDirectives.forceType !== false) {
214
- msgs = this.visit(node);
215
- }
216
- this.commentDirectives = commentDirectivesPrev;
217
- return msgs;
218
- };
184
+ visitJx = (node) => this.visit(node);
219
185
  transformJx = (lib) => {
220
186
  // jsx vs type casting is not ambiguous in all files except .ts files
221
- const [ast, comments] = (this.filename.endsWith('.ts') ? parseScript : parseScriptJSX)(this.content);
187
+ const [ast, comments] = (this.heuristciDetails.file.endsWith('.ts') ? parseScript : parseScriptJSX)(this.content);
222
188
  this.comments = comments;
223
189
  this.mstr = new MagicString(this.content);
224
190
  this.mixedVisitor = this.initMixedVisitor();
@@ -228,7 +194,7 @@ export class JSXTransformer extends Transformer {
228
194
  const msgs = this.visitJx(ast);
229
195
  const header = [
230
196
  `import ${rtComponent} from "@wuchale/jsx/runtime${lib === 'solidjs' ? '.solid' : ''}.jsx"`,
231
- this.initRuntime(this.filename),
197
+ this.initRuntime(),
232
198
  ].join('\n');
233
199
  const bodyStart = this.getRealBodyStart(ast.body);
234
200
  return this.finalize(msgs, bodyStart, header);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wuchale/jsx",
3
- "version": "0.9.7",
3
+ "version": "0.10.1",
4
4
  "description": "Protobuf-like i18n from plain code: JSX adapter",
5
5
  "scripts": {
6
6
  "dev": "tsc --watch",
@@ -61,7 +61,7 @@
61
61
  "@sveltejs/acorn-typescript": "^1.0.8",
62
62
  "acorn": "^8.15.0",
63
63
  "magic-string": "^0.30.21",
64
- "wuchale": "^0.18.8"
64
+ "wuchale": "^0.19.1"
65
65
  },
66
66
  "devDependencies": {
67
67
  "@types/estree-jsx": "^1.0.5",
@@ -1,10 +1,14 @@
1
- import { useState, useEffect, useMemo } from 'react'
1
+ import { useEffect, useMemo, useState } from 'react'
2
2
  import toRuntime from 'wuchale/runtime'
3
3
  import { locales } from '${DATA}'
4
4
 
5
5
  let locale = locales[0]
6
6
 
7
- const callbacks = new Set([(/** @type {string} */ loc) => {locale = loc}])
7
+ const callbacks = new Set([
8
+ (/** @type {string} */ loc) => {
9
+ locale = loc
10
+ },
11
+ ])
8
12
 
9
13
  /**
10
14
  * @param {string} locale
@@ -26,4 +30,5 @@ export const getRuntimeRx = (/** @type {{[locale: string]: import('wuchale/runti
26
30
  }
27
31
 
28
32
  // non-reactive
29
- export const getRuntime = (/** @type {{[locale: string]: import('wuchale/runtime').CatalogModule }} */ catalogs) => toRuntime(catalogs[locale], locale)
33
+ export const getRuntime = (/** @type {{[locale: string]: import('wuchale/runtime').CatalogModule }} */ catalogs) =>
34
+ toRuntime(catalogs[locale], locale)
@@ -1,6 +1,6 @@
1
- import { loadCatalog, loadIDs } from '${PROXY}'
1
+ import { useEffect, useState } from 'react'
2
2
  import { registerLoaders } from 'wuchale/load-utils'
3
- import { useState, useEffect } from 'react'
3
+ import { loadCatalog, loadIDs } from '${PROXY}'
4
4
 
5
5
  export const key = '${KEY}'
6
6
  /** @type {{[loadID: string]: Set<Function>}} */
@@ -14,16 +14,19 @@ const collection = {
14
14
  get: getRuntime,
15
15
  set: (/** @type {string} */ loadID, /** @type {import('wuchale/runtime').Runtime} */ runtime) => {
16
16
  store[loadID] = runtime // for when useEffect hasn't run yet
17
- callbacks[loadID]?.forEach(cb => cb(runtime))
18
- }
17
+ callbacks[loadID]?.forEach((cb) => {
18
+ cb(runtime)
19
+ })
20
+ },
19
21
  }
20
22
 
21
23
  registerLoaders(key, loadCatalog, loadIDs, collection)
22
24
 
23
25
  export const getRuntimeRx = (/** @type {string} */ loadID) => {
24
- const [runtime, setRuntime] = useState(getRuntime(loadID))
26
+ // function to useState because runtime is a function too
27
+ const [runtime, setRuntime] = useState(() => getRuntime(loadID))
25
28
  useEffect(() => {
26
- const cb = (/** @type {import('wuchale/runtime').Runtime} */ runtime) => setRuntime(runtime)
29
+ const cb = (/** @type {import('wuchale/runtime').Runtime} */ runtime) => setRuntime(() => runtime)
27
30
  callbacks[loadID] ??= new Set()
28
31
  callbacks[loadID].add(cb)
29
32
  return () => callbacks[loadID].delete(cb)
@@ -1,5 +1,5 @@
1
- import { createSignal } from "solid-js"
2
- import toRuntime from "wuchale/runtime"
1
+ import { createSignal } from 'solid-js'
2
+ import toRuntime from 'wuchale/runtime'
3
3
 
4
4
  const [locale, setLocale] = createSignal('en')
5
5
 
@@ -8,6 +8,6 @@ export { setLocale }
8
8
  /**
9
9
  * @param {{ [locale: string]: import('wuchale/runtime').CatalogModule }} catalogs
10
10
  */
11
- export const getRuntimeRx = catalogs => toRuntime(catalogs[locale()], locale())
11
+ export const getRuntimeRx = (catalogs) => toRuntime(catalogs[locale()], locale())
12
12
  // same function, because solid-js can use them anywhere
13
13
  export const getRuntime = getRuntimeRx
@@ -1,6 +1,6 @@
1
- import { loadCatalog, loadIDs } from '${PROXY}'
2
- import { registerLoaders } from 'wuchale/load-utils'
3
1
  import { createStore } from 'solid-js/store'
2
+ import { registerLoaders } from 'wuchale/load-utils'
3
+ import { loadCatalog, loadIDs } from '${PROXY}'
4
4
 
5
5
  const key = '${KEY}'
6
6
 
@@ -8,7 +8,7 @@ const [store, setStore] = createStore({})
8
8
 
9
9
  // two exports. can be the same because solid-js can use them anywhere unlike react
10
10
  export const getRuntimeRx = registerLoaders(key, loadCatalog, loadIDs, {
11
- get: loadID => store[loadID],
12
- set: setStore,
11
+ get: (loadID) => store[loadID],
12
+ set: (loadID, runtime) => setStore(loadID, () => runtime),
13
13
  })
14
14
  export const getRuntime = getRuntimeRx