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:
parent
3d0c5d2afc
commit
bb6cc61979
@ -1,8 +1,5 @@
|
|||||||
{
|
{
|
||||||
"sourceType": "module",
|
"sourceType": "module",
|
||||||
"plugins": [
|
"plugins": ["jsx", "flow"],
|
||||||
"jsx",
|
|
||||||
"flow"
|
|
||||||
],
|
|
||||||
"throws": "Unexpected token, expected \"{\" (2:26)"
|
"throws": "Unexpected token, expected \"{\" (2:26)"
|
||||||
}
|
}
|
||||||
@ -1,8 +1,5 @@
|
|||||||
{
|
{
|
||||||
"sourceType": "module",
|
"sourceType": "module",
|
||||||
"plugins": [
|
"plugins": ["jsx", "flow"],
|
||||||
"jsx",
|
|
||||||
"flow"
|
|
||||||
],
|
|
||||||
"throws": "Unexpected token (2:23)"
|
"throws": "Unexpected token (2:23)"
|
||||||
}
|
}
|
||||||
@ -14,6 +14,7 @@ export default declare((api, options) => {
|
|||||||
visitor: {
|
visitor: {
|
||||||
"OptionalCallExpression|OptionalMemberExpression"(path) {
|
"OptionalCallExpression|OptionalMemberExpression"(path) {
|
||||||
const { parentPath, scope } = path;
|
const { parentPath, scope } = path;
|
||||||
|
let isDeleteOperation = false;
|
||||||
const optionals = [];
|
const optionals = [];
|
||||||
|
|
||||||
let optionalPath = path;
|
let optionalPath = path;
|
||||||
@ -38,6 +39,7 @@ export default declare((api, options) => {
|
|||||||
let replacementPath = path;
|
let replacementPath = path;
|
||||||
if (parentPath.isUnaryExpression({ operator: "delete" })) {
|
if (parentPath.isUnaryExpression({ operator: "delete" })) {
|
||||||
replacementPath = parentPath;
|
replacementPath = parentPath;
|
||||||
|
isDeleteOperation = true;
|
||||||
}
|
}
|
||||||
for (let i = optionals.length - 1; i >= 0; i--) {
|
for (let i = optionals.length - 1; i >= 0; i--) {
|
||||||
const node = optionals[i];
|
const node = optionals[i];
|
||||||
@ -113,7 +115,9 @@ export default declare((api, options) => {
|
|||||||
scope.buildUndefinedNode(),
|
scope.buildUndefinedNode(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
scope.buildUndefinedNode(),
|
isDeleteOperation
|
||||||
|
? t.booleanLiteral(true)
|
||||||
|
: scope.buildUndefinedNode(),
|
||||||
replacementPath.node,
|
replacementPath.node,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
@ -16,7 +16,7 @@ expect(test).toBe(true);
|
|||||||
|
|
||||||
test = delete obj?.b?.b;
|
test = delete obj?.b?.b;
|
||||||
expect(obj.b).toBeUndefined();
|
expect(obj.b).toBeUndefined();
|
||||||
expect(test).toBeUndefined();
|
expect(test).toBe(true);
|
||||||
|
|
||||||
delete obj?.a;
|
delete obj?.a;
|
||||||
expect(obj.a).toBeUndefined();
|
expect(obj.a).toBeUndefined();
|
||||||
|
|||||||
@ -7,7 +7,7 @@ const obj = {
|
|||||||
b: 0
|
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;
|
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 ? void 0 : delete obj.a.b;
|
test = obj === null || obj === void 0 ? true : 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;
|
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 ? void 0 : delete obj.a;
|
obj === null || obj === void 0 ? true : delete obj.a;
|
||||||
|
|||||||
@ -1,5 +1,3 @@
|
|||||||
{
|
{
|
||||||
"plugins": [
|
"plugins": ["transform-modules-amd"]
|
||||||
"transform-modules-amd"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user