@wuchale/jsx 0.10.1 → 0.11.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.
package/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@ type LoadersAvailable = 'default' | 'react' | 'solidjs';
6
6
  export type JSXArgs = AdapterArgs<LoadersAvailable> & {
7
7
  variant: JSXLib;
8
8
  };
9
+ export declare const defaultArgs: JSXArgs;
9
10
  export declare function getDefaultLoaderPath(loader: LoaderChoice<LoadersAvailable>, bundle: boolean): string | {
10
11
  client: string;
11
12
  server: string;
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
- import { createHeuristic, deepMergeObjects, defaultGenerateLoadID, defaultHeuristicOpts } from 'wuchale';
1
+ import { createHeuristic, deepMergeObjects, defaultGenerateLoadID, defaultHeuristicOpts, pofile } from 'wuchale';
2
2
  import { loaderPathResolver } from 'wuchale/adapter-utils';
3
3
  import { getDefaultLoaderPath as getDefaultLoaderPathVanilla, pluralPattern } from 'wuchale/adapter-vanilla';
4
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;
@@ -29,12 +29,12 @@ const defaultRuntime = {
29
29
  !nested &&
30
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 = {
@@ -42,13 +42,13 @@ const defaultRuntimeSolid = {
42
42
  initReactive: ({ funcName }) => (funcName == null ? true : null), // init only in top level
43
43
  useReactive: true, // always reactive, because solidjs doesn't have a problem with it
44
44
  reactive: {
45
- wrapInit: (expr) => `() => ${expr}`,
46
- wrapUse: (expr) => `${expr}()`,
45
+ wrapInit: expr => `() => ${expr}`,
46
+ wrapUse: expr => `${expr}()`,
47
47
  },
48
48
  };
49
- const defaultArgs = {
49
+ export const defaultArgs = {
50
50
  files: { include: 'src/**/*.{js,ts,jsx,tsx}', ignore: '**/*.d.ts' },
51
- localesDir: './src/locales',
51
+ storage: pofile(),
52
52
  patterns: [pluralPattern],
53
53
  heuristic: jsxDefaultHeuristic,
54
54
  granularLoad: false,
@@ -1,6 +1,7 @@
1
1
  import { tsPlugin } from '@sveltejs/acorn-typescript';
2
2
  import { Parser } from 'acorn';
3
3
  import MagicString from 'magic-string';
4
+ import { getKey } from 'wuchale';
4
5
  import { MixedVisitor, nonWhitespaceText } from 'wuchale/adapter-utils';
5
6
  import { parseScript, scriptParseOptionsWithComments, Transformer } from 'wuchale/adapter-vanilla';
6
7
  const JsxParser = Parser.extend(tsPlugin({ jsx: true }));
@@ -23,19 +24,19 @@ export class JSXTransformer extends Transformer {
23
24
  initMixedVisitor = () => new MixedVisitor({
24
25
  mstr: this.mstr,
25
26
  vars: this.vars,
26
- getRange: (node) => ({
27
+ getRange: node => ({
27
28
  start: node.start,
28
29
  end: node.end,
29
30
  }),
30
- isComment: (node) => node.type === 'JSXExpressionContainer' &&
31
+ isComment: node => node.type === 'JSXExpressionContainer' &&
31
32
  node.expression.type === 'JSXEmptyExpression' &&
32
33
  node.expression.end > node.expression.start,
33
- isText: (node) => node.type === 'JSXText',
34
+ isText: node => node.type === 'JSXText',
34
35
  leaveInPlace: () => false,
35
- isExpression: (node) => node.type === 'JSXExpressionContainer',
36
+ isExpression: node => node.type === 'JSXExpressionContainer',
36
37
  getTextContent: (node) => node.value,
37
38
  getCommentData: (node) => this.getMarkupCommentBody(node.expression),
38
- canHaveChildren: (node) => nodesWithChildren.includes(node.type),
39
+ canHaveChildren: node => nodesWithChildren.includes(node.type),
39
40
  visitFunc: (child, inCompoundText) => {
40
41
  const inCompoundTextPrev = this.inCompoundText;
41
42
  this.inCompoundText = inCompoundText;
@@ -67,7 +68,7 @@ export class JSXTransformer extends Transformer {
67
68
  begin += `{${this.vars().nestCtx}} n`;
68
69
  }
69
70
  else {
70
- const index = this.index.get(msgInfo.toKey());
71
+ const index = this.index.get(getKey(msgInfo.msgStr, msgInfo.context));
71
72
  begin += `{${this.vars().rtCtx}(${index})}`;
72
73
  }
73
74
  let end = ' />';
@@ -110,7 +111,7 @@ export class JSXTransformer extends Transformer {
110
111
  msgs.push(...this.visitJx(attr));
111
112
  }
112
113
  if (this.inCompoundText && this.currentJsxKey != null) {
113
- const key = node.openingElement.attributes.find((attr) => attr.type === 'JSXAttribute' && attr.name.name === 'key');
114
+ const key = node.openingElement.attributes.find(attr => attr.type === 'JSXAttribute' && attr.name.name === 'key');
114
115
  if (!key) {
115
116
  this.mstr.appendLeft(node.openingElement.name.end, ` key="_${this.currentJsxKey}"`);
116
117
  this.currentJsxKey++;
@@ -128,7 +129,7 @@ export class JSXTransformer extends Transformer {
128
129
  if (!pass) {
129
130
  return [];
130
131
  }
131
- this.mstr.update(node.start + startWh, node.end - endWh, `{${this.vars().rtTrans}(${this.index.get(msgInfo.toKey())})}`);
132
+ this.mstr.update(node.start + startWh, node.end - endWh, `{${this.vars().rtTrans}(${this.index.get(getKey(msgInfo.msgStr, msgInfo.context))})}`);
132
133
  return [msgInfo];
133
134
  };
134
135
  visitJSXFragment = (node) => this.visitChildrenJ(node);
@@ -177,7 +178,7 @@ export class JSXTransformer extends Transformer {
177
178
  if (!pass) {
178
179
  return [];
179
180
  }
180
- this.mstr.update(value.start, value.end, `{${this.vars().rtTrans}(${this.index.get(msgInfo.toKey())})}`);
181
+ this.mstr.update(value.start, value.end, `{${this.literalRepl(msgInfo)}}`);
181
182
  return [msgInfo];
182
183
  };
183
184
  visitJSXSpreadAttribute = (node) => this.visit(node.argument);
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@wuchale/jsx",
3
- "version": "0.10.1",
3
+ "version": "0.11.0",
4
4
  "description": "Protobuf-like i18n from plain code: JSX adapter",
5
5
  "scripts": {
6
6
  "dev": "tsc --watch",
7
7
  "build": "tsc",
8
- "test": "node tests/index.ts"
8
+ "test": "node --import ../wuchale/testing/resolve.ts --test"
9
9
  },
10
10
  "keywords": [
11
11
  "i18n",
@@ -58,10 +58,10 @@
58
58
  "author": "K1DV5",
59
59
  "license": "MIT",
60
60
  "dependencies": {
61
- "@sveltejs/acorn-typescript": "^1.0.8",
62
- "acorn": "^8.15.0",
61
+ "@sveltejs/acorn-typescript": "^1.0.9",
62
+ "acorn": "^8.16.0",
63
63
  "magic-string": "^0.30.21",
64
- "wuchale": "^0.19.1"
64
+ "wuchale": "^0.21.0"
65
65
  },
66
66
  "devDependencies": {
67
67
  "@types/estree-jsx": "^1.0.5",
@@ -5,13 +5,13 @@ import { locales } from '${DATA}'
5
5
  let locale = locales[0]
6
6
 
7
7
  const callbacks = new Set([
8
- (/** @type {string} */ loc) => {
8
+ (/** @type {import('${DATA}').Locale} */ loc) => {
9
9
  locale = loc
10
10
  },
11
11
  ])
12
12
 
13
13
  /**
14
- * @param {string} locale
14
+ * @param {import('${DATA}').Locale} locale
15
15
  */
16
16
  export function setLocale(locale) {
17
17
  for (const callback of callbacks) {
@@ -14,7 +14,7 @@ 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) => {
17
+ callbacks[loadID]?.forEach(cb => {
18
18
  cb(runtime)
19
19
  })
20
20
  },
@@ -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
@@ -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],
11
+ get: loadID => store[loadID],
12
12
  set: (loadID, runtime) => setStore(loadID, () => runtime),
13
13
  })
14
14
  export const getRuntime = getRuntimeRx