From 8b670bb3cc7963cad82cc02a18b36240db6f0b0a Mon Sep 17 00:00:00 2001 From: Thibault Malbranche Date: Mon, 14 Aug 2023 11:53:41 +0200 Subject: [PATCH 1/2] fix-object-properties --- .eslintignore | 2 +- lib/index.js | 3 +- test/index.js | 6 ++-- test/object-keys-properties/actual.js | 28 +++++++++++++++++++ .../expected.js | 6 +++- test/object-keys/actual.js | 23 --------------- 6 files changed, 39 insertions(+), 29 deletions(-) create mode 100644 test/object-keys-properties/actual.js rename test/{object-keys => object-keys-properties}/expected.js (77%) delete mode 100644 test/object-keys/actual.js diff --git a/.eslintignore b/.eslintignore index c64046a..631c603 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,4 +1,4 @@ node_modules test/binding test/import-identifiers -test/object-keys +test/object-keys-properties diff --git a/lib/index.js b/lib/index.js index 6ea8f10..13c83c5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -97,8 +97,9 @@ const plugin = function ({ types: t }) { // Don't transform import identifiers. This is meant to mimic webpack's // DefinePlugin behavior. || isImportIdentifier(nodePath) - // Do not transform Object keys unless they are computed like {[key]: value} + // Do not transform Object keys / properties unless they are computed like {[key]: value} || nodePath.key === "key" && nodePath.parent.computed === false + || nodePath.key === "property" && nodePath.parent.computed === false ) { return; } diff --git a/test/index.js b/test/index.js index 4bdbfd3..1ce1330 100644 --- a/test/index.js +++ b/test/index.js @@ -202,15 +202,15 @@ describe("babel-plugin-transform-define", () => { path.join(__dirname, "./binding/expected.js"), babelOpts); }); - it("should not transform object keys unless they are computed", () => { + it("should not transform object keys / properties unless they are computed", () => { const babelOpts = getBabelOps({ __DEV__: true, __DEV2__: "true" }); return assertTransform( - path.join(__dirname, "./object-keys/actual.js"), - path.join(__dirname, "./object-keys/expected.js"), babelOpts); + path.join(__dirname, "./object-keys-properties/actual.js"), + path.join(__dirname, "./object-keys-properties/expected.js"), babelOpts); }); }); diff --git a/test/object-keys-properties/actual.js b/test/object-keys-properties/actual.js new file mode 100644 index 0000000..e57feaf --- /dev/null +++ b/test/object-keys-properties/actual.js @@ -0,0 +1,28 @@ +const obj = { + __DEV__ + }; +const obj1 = { + __DEV__: "test" +}; +const obj2 = { + __DEV__: __DEV__ +}; +const obj3 = { + "__DEV__": __DEV__ +}; +const obj4 = { + ["__DEV__"]: __DEV__ +}; + +const obj5 = { + [__DEV__]: __DEV__ +}; + +const obj6 = { + [__DEV2__]: __DEV2__ +}; + + +const access = obj.__DEV__; +const access1 = obj[__DEV2__]; +const access2 = obj["__DEV__"]; \ No newline at end of file diff --git a/test/object-keys/expected.js b/test/object-keys-properties/expected.js similarity index 77% rename from test/object-keys/expected.js rename to test/object-keys-properties/expected.js index 423f640..5b85e90 100644 --- a/test/object-keys/expected.js +++ b/test/object-keys-properties/expected.js @@ -19,4 +19,8 @@ var obj4 = _defineProperty({}, "__DEV__", true); var obj5 = _defineProperty({}, true, true); -var obj6 = _defineProperty({}, "true", "true"); \ No newline at end of file +var obj6 = _defineProperty({}, "true", "true"); + +var access = obj.__DEV__; +var access1 = obj["true"]; +var access2 = obj["__DEV__"]; \ No newline at end of file diff --git a/test/object-keys/actual.js b/test/object-keys/actual.js deleted file mode 100644 index 027d88b..0000000 --- a/test/object-keys/actual.js +++ /dev/null @@ -1,23 +0,0 @@ -const obj = { - __DEV__ - }; - const obj1 = { - __DEV__: "test" - }; - const obj2 = { - __DEV__: __DEV__ - }; - const obj3 = { - "__DEV__": __DEV__ - }; - const obj4 = { - ["__DEV__"]: __DEV__ - }; - - const obj5 = { - [__DEV__]: __DEV__ - }; - - const obj6 = { - [__DEV2__]: __DEV2__ - }; \ No newline at end of file From a34fed7e95cd0b3c32d0c1102ea6f7ec6d94b724 Mon Sep 17 00:00:00 2001 From: Thibault Malbranche Date: Tue, 19 Sep 2023 12:04:50 +0200 Subject: [PATCH 2/2] Create angry-pandas-jam.md --- .changeset/angry-pandas-jam.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/angry-pandas-jam.md diff --git a/.changeset/angry-pandas-jam.md b/.changeset/angry-pandas-jam.md new file mode 100644 index 0000000..c155ef3 --- /dev/null +++ b/.changeset/angry-pandas-jam.md @@ -0,0 +1,5 @@ +--- +"babel-plugin-transform-define": patch +--- + +follow-up to last release, also prevent object properties from being replaced