From d528779ca52a5c19a6646e1cc36949d16da69b86 Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Mon, 8 Nov 2021 00:58:35 +0300 Subject: [PATCH] Oops - do not overwrite keys with values in objects --- __tests__/__snapshots__/index-test.js.snap | 4 ++-- __tests__/index-test.js | 5 +++-- index.js | 2 +- package.json | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/__tests__/__snapshots__/index-test.js.snap b/__tests__/__snapshots__/index-test.js.snap index c15ce11..ceac82a 100644 --- a/__tests__/__snapshots__/index-test.js.snap +++ b/__tests__/__snapshots__/index-test.js.snap @@ -10,8 +10,8 @@ foo = /*#__PURE__*/React.createElement(\\"span\\", null, L(\\"\\\\u041F\\\\u0440 foo = L(\\"У меня много {1}: \\", L(\\"братьев\\")); var age = { [L(\\"\\\\u0411\\\\u0440\\\\u0430\\\\u0442\\\\u044C\\\\u044F\\")]: { - [L(\\"\\\\u0418\\\\u0432\\\\u0430\\\\u043D\\")]: 32, - [L(\\"\\\\u041C\\\\u0438\\\\u0445\\\\u0430\\\\u0438\\\\u043B\\")]: 36 + [L(\\"\\\\u0418\\\\u0432\\\\u0430\\\\u043D\\")]: L(\\"\\\\u0413\\\\u043E\\\\u0440\\\\u044C\\\\u043A\\\\u0438\\\\u0439\\"), + \\"Mikhail\\": L(\\"\\\\u0417\\\\u043E\\\\u043B\\\\u043E\\\\u0442\\\\u0443\\\\u0445\\\\u0438\\\\u043D\\") }, [L(\\"\\\\u0421\\\\u0451\\\\u0441\\\\u0442\\\\u0440\\\\u044B{1}\\", getnum())]: {}, Родители diff --git a/__tests__/index-test.js b/__tests__/index-test.js index 0e1c598..7ac4d9f 100644 --- a/__tests__/index-test.js +++ b/__tests__/index-test.js @@ -10,7 +10,7 @@ foo = "Меня зовут "+name+"! ";\ foo = ` Меня зовут ${name}!`;\ foo = Привет!;\ foo = L("У меня много {1}: ", L("братьев"));\ -var age = { "Братья": { "Иван": 32, "Михаил": 36 }, ["Сёстры"+getnum()]: {}, Родители };\ +var age = { "Братья": { "Иван": "Горький", "Mikhail": "Золотухин" }, ["Сёстры"+getnum()]: {}, Родители };\ '; it('works', () => @@ -26,7 +26,8 @@ it('works', () => "братьев", "Братья", "Иван", - "Михаил", + "Горький", + "Золотухин", "Сёстры{1}" ] }); diff --git a/index.js b/index.js index abae1e1..8df0e51 100644 --- a/index.js +++ b/index.js @@ -165,7 +165,7 @@ module.exports = function(babel) addImport(path); let repl = t.callExpression(t.identifier('L'), [ t.stringLiteral(text) ]); repl = withWhite(lwhite, repl, rwhite); - if (parent.isObjectProperty() && !parent.node.computed) + if (parent.isObjectProperty() && path.node == parent.node.key && !parent.node.computed) parent.replaceWith(t.objectProperty(repl, parent.node.value, true, parent.node.shorthard, parent.node.decorators)); else if (isJSX) path.replaceWith(t.jsxExpressionContainer(repl)); diff --git a/package.json b/package.json index 36a99f2..b915b22 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "babel-plugin-react-translate", - "version": "1.0.5", + "version": "1.0.6", "description": "Babel-based JS/JSX auto-translator for Russian language", "main": "index.js", "scripts": {