Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix-object-properties #88

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules
test/binding
test/import-identifiers
test/object-keys
test/object-keys-properties
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down
28 changes: 28 additions & 0 deletions test/object-keys-properties/actual.js
Original file line number Diff line number Diff line change
@@ -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__"];
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ var obj4 = _defineProperty({}, "__DEV__", true);

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

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

var access = obj.__DEV__;
var access1 = obj["true"];
var access2 = obj["__DEV__"];
23 changes: 0 additions & 23 deletions test/object-keys/actual.js

This file was deleted.