@saykit/transform-jsx 0.6.1 → 0.7.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.
Files changed (2) hide show
  1. package/dist/parser.mjs +37 -6
  2. package/package.json +3 -3
package/dist/parser.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { AUTO_INCREMENT_IDENTIFIER, ArgumentMessage, ChoiceMessage, CompositeMessage, ElementMessage, LiteralMessage, validateBranchIdentifier } from "@saykit/config/features/messages";
1
+ import { AUTO_INCREMENT_IDENTIFIER, ArgumentMessage, ChoiceMessage, CompositeMessage, ElementMessage, LiteralMessage, isArgumentType, validateArgumentStyle, validateBranchIdentifier } from "@saykit/config/features/messages";
2
2
  import { unwrapPlaceholder } from "@saykit/transform-js/parser";
3
3
  import * as t from "@babel/types";
4
4
  //#region src/parser.ts
@@ -42,15 +42,23 @@ function parseJSXOpeningElement(element) {
42
42
  const processed = processJSXOpeningElement(element);
43
43
  if (!processed) return null;
44
44
  const [accessor, kind] = processed;
45
+ const descriptorId = findAttributeValueIfStringLiteralAsString(element.attributes, "id");
46
+ const descriptorContext = findAttributeValueIfStringLiteralAsString(element.attributes, "context");
47
+ const wrap = (children) => new CompositeMessage({
48
+ id: descriptorId,
49
+ context: descriptorContext
50
+ }, [], getReferences(element), children, accessor);
45
51
  if (typeof kind === "string" && [
46
52
  "select",
47
53
  "ordinal",
48
54
  "plural"
49
55
  ].includes(kind)) {
56
+ const offset = kind === "select" ? void 0 : findPluralOffset(element.attributes);
50
57
  const branches = element.attributes.reduce((b, a) => {
51
58
  if (!t.isJSXAttribute(a)) return b;
52
59
  let identifier = getAttributeNameAsString(a);
53
60
  if (identifier === "_" || identifier === "id" || identifier === "context") return b;
61
+ if (offset !== void 0 && identifier === "offset") return b;
54
62
  if (identifier.startsWith("_") && identifier.length > 1 && !Number.isNaN(+identifier.slice(1))) identifier = identifier.slice(1);
55
63
  if (t.isStringLiteral(a.value)) b.push({
56
64
  identifier,
@@ -79,15 +87,38 @@ function parseJSXOpeningElement(element) {
79
87
  const initialiser = findAttributeValueIfExpressionOrStringLiteral(element.attributes, "_");
80
88
  if (!initialiser) return null;
81
89
  const [identifier, selector] = unwrapPlaceholder(initialiser);
82
- const choice = new ChoiceMessage(kind, identifier, branches, selector);
83
- return new CompositeMessage({
84
- id: findAttributeValueIfStringLiteralAsString(element.attributes, "id"),
85
- context: findAttributeValueIfStringLiteralAsString(element.attributes, "context")
86
- }, [], getReferences(element), [choice], accessor);
90
+ return wrap([new ChoiceMessage(kind, identifier, branches, selector, offset)]);
91
+ }
92
+ if (typeof kind === "string" && isArgumentType(kind)) {
93
+ const initialiser = findAttributeValueIfExpressionOrStringLiteral(element.attributes, "_");
94
+ if (!initialiser) return null;
95
+ const style = findAttributeValueIfStringLiteralAsString(element.attributes, "style");
96
+ if (style !== void 0) validateArgumentStyle(kind, style);
97
+ const [identifier, value] = unwrapPlaceholder(initialiser);
98
+ return wrap([new ArgumentMessage(identifier, value, {
99
+ type: kind,
100
+ style
101
+ })]);
87
102
  }
88
103
  return null;
89
104
  }
90
105
  /**
106
+ * The `offset` a plural subtracts from its selector before `#` is formatted.
107
+ * See the JS parser's counterpart — the constraints are the same, only the
108
+ * syntax carrying it differs.
109
+ */
110
+ function findPluralOffset(attributes) {
111
+ for (const attribute of attributes) {
112
+ if (!t.isJSXAttribute(attribute)) continue;
113
+ if (getAttributeNameAsString(attribute) !== "offset") continue;
114
+ if (!t.isJSXExpressionContainer(attribute.value)) continue;
115
+ const { expression } = attribute.value;
116
+ if (!t.isNumericLiteral(expression)) continue;
117
+ if (!Number.isInteger(expression.value) || expression.value < 0) continue;
118
+ return expression.value;
119
+ }
120
+ }
121
+ /**
91
122
  * Punctuation that never takes a space in front of it. A formatter wrapping
92
123
  * long JSX regularly strands these at the start of a line, on their own or
93
124
  * ahead of the rest of a clause.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saykit/transform-jsx",
3
- "version": "0.6.1",
3
+ "version": "0.7.0",
4
4
  "description": "JSX and TSX source transformer for SayKit",
5
5
  "keywords": [
6
6
  "i18n",
@@ -47,8 +47,8 @@
47
47
  "@babel/parser": "^7.29.7",
48
48
  "@babel/traverse": "^7.29.7",
49
49
  "@babel/types": "^7.29.7",
50
- "@saykit/config": "^0.6.1",
51
- "@saykit/transform-js": "^0.6.1"
50
+ "@saykit/config": "^0.7.0",
51
+ "@saykit/transform-js": "^0.7.0"
52
52
  },
53
53
  "devDependencies": {
54
54
  "@babel/core": "^7.29.7",