From 5341e747ff7a35ee56d46dd7d803afccb5197977 Mon Sep 17 00:00:00 2001 From: Alex Lewis Date: Wed, 4 Dec 2019 01:51:15 -0500 Subject: [PATCH] fix(optional chaining): Optional delete returns true with nullish base Per issue 10805, the return value when using delete on a nullish base is currently undefined. The correct return type should be true. --- .../typeof-reserved-invalid-1/options.json | 7 ++----- .../typeof-reserved-invalid-5/options.json | 7 ++----- .../babel-plugin-proposal-optional-chaining/src/index.js | 6 +++++- .../test/fixtures/general/delete/exec.js | 2 +- .../test/fixtures/general/delete/output.js | 8 ++++---- .../test/fixtures/regression/9346/options.json | 4 +--- 6 files changed, 15 insertions(+), 19 deletions(-) diff --git a/packages/babel-parser/test/fixtures/flow/type-annotations/typeof-reserved-invalid-1/options.json b/packages/babel-parser/test/fixtures/flow/type-annotations/typeof-reserved-invalid-1/options.json index 151f6c5a3cd5..077518333db1 100644 --- a/packages/babel-parser/test/fixtures/flow/type-annotations/typeof-reserved-invalid-1/options.json +++ b/packages/babel-parser/test/fixtures/flow/type-annotations/typeof-reserved-invalid-1/options.json @@ -1,8 +1,5 @@ { "sourceType": "module", - "plugins": [ - "jsx", - "flow" - ], + "plugins": ["jsx", "flow"], "throws": "Unexpected token, expected \"{\" (2:26)" -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/flow/type-annotations/typeof-reserved-invalid-5/options.json b/packages/babel-parser/test/fixtures/flow/type-annotations/typeof-reserved-invalid-5/options.json index 749dcb796d32..a06d10b640b8 100644 --- a/packages/babel-parser/test/fixtures/flow/type-annotations/typeof-reserved-invalid-5/options.json +++ b/packages/babel-parser/test/fixtures/flow/type-annotations/typeof-reserved-invalid-5/options.json @@ -1,8 +1,5 @@ { "sourceType": "module", - "plugins": [ - "jsx", - "flow" - ], + "plugins": ["jsx", "flow"], "throws": "Unexpected token (2:23)" -} \ No newline at end of file +} diff --git a/packages/babel-plugin-proposal-optional-chaining/src/index.js b/packages/babel-plugin-proposal-optional-chaining/src/index.js index 918a59ade508..6a30dc74b909 100644 --- a/packages/babel-plugin-proposal-optional-chaining/src/index.js +++ b/packages/babel-plugin-proposal-optional-chaining/src/index.js @@ -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, ), ); diff --git a/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/delete/exec.js b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/delete/exec.js index b2ecbb0a69c0..51021b6cbdb9 100644 --- a/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/delete/exec.js +++ b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/delete/exec.js @@ -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(); diff --git a/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/delete/output.js b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/delete/output.js index 5a9498166caf..e5260f8ca9a0 100644 --- a/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/delete/output.js +++ b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/delete/output.js @@ -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; diff --git a/packages/babel-plugin-transform-modules-amd/test/fixtures/regression/9346/options.json b/packages/babel-plugin-transform-modules-amd/test/fixtures/regression/9346/options.json index 9e5a4ccdb0df..8cbc099aab70 100644 --- a/packages/babel-plugin-transform-modules-amd/test/fixtures/regression/9346/options.json +++ b/packages/babel-plugin-transform-modules-amd/test/fixtures/regression/9346/options.json @@ -1,5 +1,3 @@ { - "plugins": [ - "transform-modules-amd" - ] + "plugins": ["transform-modules-amd"] }