From c3b2783b49cf236e8752c88fdb98fea4489b555c Mon Sep 17 00:00:00 2001 From: Sergio Moreno Date: Tue, 26 Jan 2021 12:27:04 +0100 Subject: [PATCH] fix: ` + ) + const tokens = macro.tokenizeNode(exp) + expect(tokens).toEqual({ + format: "select", + name: "gender", + options: expect.objectContaining({ + female: "she", + male: "he", + offset: undefined, + other: "they" + }), + type: "arg", + value: expect.objectContaining({ + "end": 31, + "loc": { + "end": { + "column": 23, + "line": 2, + }, + "identifierName": "gender", + "start": { + "column": 17, + "line": 2, + }, + }, + name: "gender", + type: "Identifier", + }) + }) + }) }) }) diff --git a/packages/macro/src/macroJsx.ts b/packages/macro/src/macroJsx.ts index 3a0d86039..bf32ee6f0 100644 --- a/packages/macro/src/macroJsx.ts +++ b/packages/macro/src/macroJsx.ts @@ -157,16 +157,17 @@ export default class MacroJSX { path.replaceWith(newNode) } + attrName = (names, exclude = false) => { + const namesRe = new RegExp("^(" + names.join("|") + ")$") + return (attr) => + exclude ? !namesRe.test(attr.name.name) : namesRe.test(attr.name.name) + } + stripMacroAttributes = (node) => { const { attributes } = node.openingElement - const attrName = (names, exclude = false) => { - const namesRe = new RegExp("^(" + names.join("|") + ")$") - return (attr) => - exclude ? !namesRe.test(attr.name.name) : namesRe.test(attr.name.name) - } - const id = attributes.filter(attrName([ID]))[0] - const message = attributes.filter(attrName([MESSAGE]))[0] - const comment = attributes.filter(attrName([COMMENT]))[0] + const id = attributes.filter(this.attrName([ID]))[0] + const message = attributes.filter(this.attrName([MESSAGE]))[0] + const comment = attributes.filter(this.attrName([COMMENT]))[0] let reserved = [ID, MESSAGE, COMMENT] if (this.isI18nComponent(node)) { @@ -191,7 +192,7 @@ export default class MacroJSX { id: maybeNodeValue(id), message: maybeNodeValue(message), comment: maybeNodeValue(comment), - attributes: attributes.filter(attrName(reserved, true)), + attributes: attributes.filter(this.attrName(reserved, true)), } } @@ -268,7 +269,12 @@ export default class MacroJSX { tokenizeChoiceComponent = (node) => { const element = node.openingElement const format = element.name.name.toLowerCase() - const props = element.attributes + const props = element.attributes.filter(this.attrName([ + ID, + COMMENT, + MESSAGE, + 'render', + ], true)) const token = { type: "arg", @@ -294,14 +300,18 @@ export default class MacroJSX { token.options.offset = this.types.isStringLiteral(attr.value) ? attr.value.value : attr.value.expression.value - } else if (pluralRuleRe.test(name)) { + } else { let value if (this.types.isStringLiteral(attr.value)) { value = attr.value.extra.raw.replace(/(["'])(.*)\1/, "$2") } else { value = this.tokenizeChildren(attr.value) } - token.options[jsx2icuExactChoice(name)] = value + if (pluralRuleRe.test(name)) { + token.options[jsx2icuExactChoice(name)] = value + } else { + token.options[name] = value + } } }