fix(optional chaining): Optional delete returns true with nullish base (#10806)
Per issue 10805, the return value when using delete on a nullish base is currently undefined. The correct return type should be true.
This commit is contained in:
committed by
Nicolò Ribaudo
parent
3d0c5d2afc
commit
bb6cc61979
@@ -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;
|
||||
@@ -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];
|
||||
@@ -113,7 +115,9 @@ export default declare((api, options) => {
|
||||
scope.buildUndefinedNode(),
|
||||
),
|
||||
),
|
||||
scope.buildUndefinedNode(),
|
||||
isDeleteOperation
|
||||
? t.booleanLiteral(true)
|
||||
: scope.buildUndefinedNode(),
|
||||
replacementPath.node,
|
||||
),
|
||||
);
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user