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(optional chaining): Optional delete returns true with nullish base #10806

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,8 +1,5 @@
{
"sourceType": "module",
"plugins": [
"jsx",
"flow"
],
"plugins": ["jsx", "flow"],
"throws": "Unexpected token, expected \"{\" (2:26)"
}
}
@@ -1,8 +1,5 @@
{
"sourceType": "module",
"plugins": [
"jsx",
"flow"
],
"plugins": ["jsx", "flow"],
"throws": "Unexpected token (2:23)"
}
}
Expand Up @@ -14,6 +14,7 @@ export default declare((api, options) => {
visitor: {
"OptionalCallExpression|OptionalMemberExpression"(path) {
const { parentPath, scope } = path;
let isDeleteOperation = false;
const optionals = [];

let optionalPath = path;
Expand All @@ -38,6 +39,7 @@ export default declare((api, options) => {
let replacementPath = path;
if (parentPath.isUnaryExpression({ operator: "delete" })) {
replacementPath = parentPath;
isDeleteOperation = true;
}
for (let i = optionals.length - 1; i >= 0; i--) {
const node = optionals[i];
Expand Down Expand Up @@ -113,7 +115,9 @@ export default declare((api, options) => {
scope.buildUndefinedNode(),
),
),
scope.buildUndefinedNode(),
isDeleteOperation
? t.booleanLiteral(true)
: scope.buildUndefinedNode(),
replacementPath.node,
),
);
Expand Down
Expand Up @@ -16,7 +16,7 @@ expect(test).toBe(true);

test = delete obj?.b?.b;
expect(obj.b).toBeUndefined();
expect(test).toBeUndefined();
expect(test).toBe(true);

delete obj?.a;
expect(obj.a).toBeUndefined();
Expand Up @@ -7,7 +7,7 @@ const obj = {
b: 0
}
};
let test = obj === null || obj === void 0 ? void 0 : (_obj$a = obj.a) === null || _obj$a === void 0 ? void 0 : delete _obj$a.b;
test = obj === null || obj === void 0 ? void 0 : delete obj.a.b;
test = obj === null || obj === void 0 ? void 0 : (_obj$b = obj.b) === null || _obj$b === void 0 ? void 0 : delete _obj$b.b;
obj === null || obj === void 0 ? void 0 : delete obj.a;
let test = obj === null || obj === void 0 ? true : (_obj$a = obj.a) === null || _obj$a === void 0 ? true : delete _obj$a.b;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent!

test = obj === null || obj === void 0 ? true : delete obj.a.b;
test = obj === null || obj === void 0 ? true : (_obj$b = obj.b) === null || _obj$b === void 0 ? true : delete _obj$b.b;
obj === null || obj === void 0 ? true : delete obj.a;
@@ -1,5 +1,3 @@
{
"plugins": [
"transform-modules-amd"
]
"plugins": ["transform-modules-amd"]
}