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:
Alex Lewis 2019-12-04 06:56:25 -05:00 committed by Nicolò Ribaudo
parent 3d0c5d2afc
commit bb6cc61979
6 changed files with 15 additions and 19 deletions

View File

@ -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)"
} }

View File

@ -1,8 +1,5 @@
{ {
"sourceType": "module", "sourceType": "module",
"plugins": [ "plugins": ["jsx", "flow"],
"jsx",
"flow"
],
"throws": "Unexpected token (2:23)" "throws": "Unexpected token (2:23)"
} }

View File

@ -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,
), ),
); );

View File

@ -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();

View File

@ -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;

View File

@ -1,5 +1,3 @@
{ {
"plugins": [ "plugins": ["transform-modules-amd"]
"transform-modules-amd"
]
} }