Re-add optional chaining delete (#7257)

* Re-add optional chaining delete

* Move exec tests next to output tests

* Forgot to commit these
This commit is contained in:
Justin Ridgewell 2018-01-24 11:26:02 -08:00 committed by GitHub
parent 63d9998aa4
commit ca18ea5e79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 122 additions and 63 deletions

View File

@ -113,6 +113,12 @@ export default function(api, options) {
if (path.key == "callee" && parentPath.isCallExpression()) { if (path.key == "callee" && parentPath.isCallExpression()) {
return false; return false;
} }
if (
path.key == "argument" &&
parentPath.isUnaryExpression({ operator: "delete" })
) {
return false;
}
return true; return true;
}); });

View File

@ -1,63 +0,0 @@
"use strict";
let calls = 0;
const obj = {
a: {
b(val) {
assert.equal(val, 1);
assert.equal(this, obj.a);
return calls++;
},
},
c(val) {
assert.equal(val, 1);
assert.equal(this, obj);
return calls++;
},
};
let ab = obj?.a?.b(1);
assert.equal(ab, 0);
ab = obj?.a.b(1);
assert.equal(ab, 1);
ab = obj?.a?.b?.(1);
assert.equal(ab, 2);
ab = obj?.a.b?.(1);
assert.equal(ab, 3);
ab = obj?.b?.b(1);
assert.equal(ab, undefined);
ab = obj?.b?.b?.(1);
assert.equal(ab, undefined);
let c = obj?.c(1);
assert.equal(c, 4);
c = obj?.c?.(1);
assert.equal(c, 5);
c = obj?.d?.(1);
assert.equal(c, undefined);
obj?.a.b(1);
assert.equal(calls, 7);
obj?.a?.b(1);
assert.equal(calls, 8);
obj?.a?.b?.(1);
assert.equal(calls, 9);
obj?.a.b?.(1);
assert.equal(calls, 10);
obj?.c?.(1);
assert.equal(calls, 11);
obj?.b?.b(1);
obj?.b?.b?.(1);
obj?.d?.(1);

View File

@ -0,0 +1,20 @@
"use strict";
const obj = {
a: {
b: {
c: {
d: 2,
},
},
},
};
const a = obj?.a;
const b = obj?.a?.b;
const bad = obj?.b?.b;
let val;
val = obj?.a?.b;

View File

@ -0,0 +1,18 @@
"use strict";
var _obj$a, _obj$b, _obj$a2;
const obj = {
a: {
b: {
c: {
d: 2
}
}
}
};
const a = obj === null || obj === void 0 ? void 0 : obj.a;
const b = obj === null || obj === void 0 ? void 0 : (_obj$a = obj.a) === null || _obj$a === void 0 ? void 0 : _obj$a.b;
const bad = obj === null || obj === void 0 ? void 0 : (_obj$b = obj.b) === null || _obj$b === void 0 ? void 0 : _obj$b.b;
let val;
val = obj === null || obj === void 0 ? void 0 : (_obj$a2 = obj.a) === null || _obj$a2 === void 0 ? void 0 : _obj$a2.b;

View File

@ -0,0 +1,22 @@
"use strict";
const obj = {
a: {
b: 0,
},
};
let test = delete obj?.a?.b;
assert.equal(obj.a.b, undefined);
assert.equal(test, true);
test = delete obj?.a.b;
assert.equal(obj.a.b, undefined);
assert.equal(test, true);
test = delete obj?.b?.b;
assert.equal(obj.b, undefined);
assert.equal(test, undefined);
delete obj?.a;
assert.equal(obj.a, undefined);

View File

@ -0,0 +1,15 @@
"use strict";
const obj = {
a: {
b: 0,
},
};
let test = delete obj?.a?.b;
test = delete obj?.a.b;
test = delete obj?.b?.b;
delete obj?.a;

View File

@ -0,0 +1,13 @@
"use strict";
var _obj$a, _obj$b;
const obj = {
a: {
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;

View File

@ -0,0 +1,15 @@
"use strict";
const obj = {
a: {
b: 0,
},
};
let test = +obj?.a?.b;
test = +obj?.a.b;
test = +obj?.b?.b;
test = +obj?.b?.b;

View File

@ -0,0 +1,13 @@
"use strict";
var _obj$a, _obj$b, _obj$b2;
const obj = {
a: {
b: 0
}
};
let test = +(obj === null || obj === void 0 ? void 0 : (_obj$a = obj.a) === null || _obj$a === void 0 ? void 0 : _obj$a.b);
test = +(obj === null || obj === void 0 ? void 0 : obj.a.b);
test = +(obj === null || obj === void 0 ? void 0 : (_obj$b = obj.b) === null || _obj$b === void 0 ? void 0 : _obj$b.b);
test = +(obj === null || obj === void 0 ? void 0 : (_obj$b2 = obj.b) === null || _obj$b2 === void 0 ? void 0 : _obj$b2.b);