Skip to content

Commit

Permalink
fix(optional chaining): Optional delete returns true with nullish base (
Browse files Browse the repository at this point in the history
#10806)

Per issue 10805, the return value when using delete on a nullish base is
currently undefined. The correct return type should be true.
  • Loading branch information
mpaarating authored and nicolo-ribaudo committed Dec 4, 2019
1 parent 3d0c5d2 commit bb6cc61
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 19 deletions.
@@ -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;
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"]
}

0 comments on commit bb6cc61

Please sign in to comment.