Skip to content

Commit

Permalink
Merge pull request #86 from Titozzz/master
Browse files Browse the repository at this point in the history
fix: object key being wrongly transformed
  • Loading branch information
gksander authored Jul 27, 2023
2 parents 4560484 + 6d87df3 commit cd2e681
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-doors-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"babel-plugin-transform-define": patch
---

Addresses #85, avoids replacing object keys.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
test/binding
test/import-identifiers
test/object-keys
11 changes: 8 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,14 @@ const plugin = function ({ types: t }) {
Identifier(nodePath, state) {
const binding = nodePath.scope.getBinding(nodePath.node.name);

// Don't transform import idenifiers. This is meant to mimic webpack's
// DefinePlugin behavior.
if (binding || isImportIdentifier(nodePath)) {
if (
binding
// 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}
|| nodePath.key === "key" && nodePath.parent.computed === false
) {
return;
}

Expand Down
11 changes: 11 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,17 @@ describe("babel-plugin-transform-define", () => {
path.join(__dirname, "./binding/actual.js"),
path.join(__dirname, "./binding/expected.js"), babelOpts);
});

it("should not transform object keys 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);
});
});

describe("unit tests", () => {
Expand Down
23 changes: 23 additions & 0 deletions test/object-keys/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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__
};
22 changes: 22 additions & 0 deletions test/object-keys/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict";

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var obj = {
__DEV__: true
};
var obj1 = {
__DEV__: "test"
};
var obj2 = {
__DEV__: true
};
var obj3 = {
"__DEV__": true
};

var obj4 = _defineProperty({}, "__DEV__", true);

var obj5 = _defineProperty({}, true, true);

var obj6 = _defineProperty({}, "true", "true");

0 comments on commit cd2e681

Please sign in to comment.