Remove old optional chain features (#6345)

This commit is contained in:
Justin Ridgewell
2018-01-22 14:03:43 -08:00
committed by GitHub
parent b3969d35fa
commit 180eda3211
17 changed files with 25 additions and 193 deletions

View File

@@ -10,11 +10,7 @@ export default function(api, options) {
const nil = scope.buildUndefinedNode();
let objectPath = path;
while (
objectPath.isMemberExpression() ||
objectPath.isCallExpression() ||
objectPath.isNewExpression()
) {
while (objectPath.isMemberExpression() || objectPath.isCallExpression()) {
const { node } = objectPath;
if (node.optional) {
optionals.push(node);
@@ -32,8 +28,7 @@ export default function(api, options) {
node.optional = false;
const isCall = t.isCallExpression(node);
const replaceKey =
isCall || t.isNewExpression(node) ? "callee" : "object";
const replaceKey = isCall ? "callee" : "object";
const chain = node[replaceKey];
let ref;
@@ -102,24 +97,20 @@ export default function(api, options) {
const { parentPath } = path;
if (path.key == "left" && parentPath.isAssignmentExpression()) {
return false;
throw path.buildCodeFrameError(
"Illegal optional chain in assignment expression",
);
}
if (path.key == "argument" && parentPath.isUpdateExpression()) {
throw path.buildCodeFrameError(
"Illegal optional chain in update expression",
);
}
if (path.key == "object" && parentPath.isMemberExpression()) {
return false;
}
if (
path.key == "callee" &&
(parentPath.isCallExpression() || parentPath.isNewExpression())
) {
return false;
}
if (path.key == "argument" && parentPath.isUpdateExpression()) {
return false;
}
if (
path.key == "argument" &&
parentPath.isUnaryExpression({ operator: "delete" })
) {
if (path.key == "callee" && parentPath.isCallExpression()) {
return false;
}
@@ -131,7 +122,7 @@ export default function(api, options) {
inherits: syntaxOptionalChaining,
visitor: {
"MemberExpression|NewExpression|CallExpression"(path) {
"MemberExpression|CallExpression"(path) {
if (!path.node.optional) {
return;
}

View File

@@ -1,16 +0,0 @@
"use strict";
const obj = {
a: {
b: 0,
},
};
obj?.a.b = 1;
assert.equal(obj.a.b, 1);
obj?.a?.b = 2;
assert.equal(obj.a.b, 2);
obj?.b?.b = 3;
assert.equal(obj.b, undefined);

View File

@@ -1,22 +0,0 @@
"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

@@ -1,67 +0,0 @@
"use strict";
let calls = 0;
const obj = {
a: {
b: class {
constructor(val) {
assert.equal(val, 1);
assert(this instanceof obj.a.b);
calls++;
}
},
},
c: class {
constructor(val) {
assert.equal(val, 1);
assert(this instanceof obj.c);
calls++;
}
},
};
let ab = new obj?.a?.b(1);
assert(ab instanceof obj.a.b);
ab = new obj?.a.b(1);
assert(ab instanceof obj.a.b);
ab = new obj?.a?.b?.(1);
assert(ab instanceof obj.a.b);
ab = new obj?.a.b?.(1);
assert(ab instanceof obj.a.b);
ab = new obj?.b?.b(1);
assert.equal(ab, undefined);
ab = new obj?.b?.b?.(1);
assert.equal(ab, undefined);
let c = new obj?.c(1);
assert(c instanceof obj.c);
c = new obj?.c?.(1);
assert(c instanceof obj.c);
c = new obj?.d?.(1);
assert.equal(c, undefined);
new obj?.a.b(1);
assert.equal(calls, 7);
new obj?.a?.b(1);
assert.equal(calls, 8);
new obj?.a?.b?.(1);
assert.equal(calls, 9);
new obj?.a.b?.(1);
assert.equal(calls, 10);
new obj?.c?.(1);
assert.equal(calls, 11);
new obj?.b?.b(1);
new obj?.b?.b?.(1);
new obj?.d?.(1);

View File

@@ -1,16 +0,0 @@
"use strict";
const obj = {
a: {
b: 0,
},
};
obj?.a.b++;
assert.equal(obj.a.b, 1);
obj?.a?.b++;
assert.equal(obj.a.b, 2);
obj?.b?.b++;
assert.equal(obj.b, undefined);

View File

@@ -1,7 +0,0 @@
a?.b = 42
a?.b?.c?.d = 42
a?.b?.c?.d++
a?.b?.c?.d += 1

View File

@@ -1,6 +0,0 @@
var _a, _a2, _a2$b, _a2$b$c, _a3, _a3$b, _a3$b$c, _a4, _a4$b, _a4$b$c;
(_a = a) === null || _a === void 0 ? void 0 : _a.b = 42;
(_a2 = a) === null || _a2 === void 0 ? void 0 : (_a2$b = _a2.b) === null || _a2$b === void 0 ? void 0 : (_a2$b$c = _a2$b.c) === null || _a2$b$c === void 0 ? void 0 : _a2$b$c.d = 42;
(_a3 = a) === null || _a3 === void 0 ? void 0 : (_a3$b = _a3.b) === null || _a3$b === void 0 ? void 0 : (_a3$b$c = _a3$b.c) === null || _a3$b$c === void 0 ? void 0 : _a3$b$c.d++;
(_a4 = a) === null || _a4 === void 0 ? void 0 : (_a4$b = _a4.b) === null || _a4$b === void 0 ? void 0 : (_a4$b$c = _a4$b.c) === null || _a4$b$c === void 0 ? void 0 : _a4$b$c.d += 1;

View File

@@ -1,3 +0,0 @@
delete a?.b
delete a?.b?.c?.d

View File

@@ -1,4 +0,0 @@
var _a, _a2, _a2$b, _a2$b$c;
(_a = a) === null || _a === void 0 ? void 0 : delete _a.b;
(_a2 = a) === null || _a2 === void 0 ? void 0 : (_a2$b = _a2.b) === null || _a2$b === void 0 ? void 0 : (_a2$b$c = _a2$b.c) === null || _a2$b$c === void 0 ? void 0 : delete _a2$b$c.d;

View File

@@ -0,0 +1,3 @@
{
"throws": "Illegal optional chain in assignment expression"
}

View File

@@ -0,0 +1,3 @@
{
"throws": "Illegal optional chain in assignment expression"
}

View File

@@ -0,0 +1,3 @@
{
"throws": "Illegal optional chain in update expression"
}

View File

@@ -1,16 +0,0 @@
new a?.b
new a?.b?.c?.d
new a?.b()
new a?.b?.c?.d()
new b?.(b)
new a?.b?.(a.b, true)
new b?.().c
new b?.()?.c
new a.b?.().c
new a.b?.()?.c
new a?.b?.().c
new a?.b?.()?.c

View File

@@ -1,14 +0,0 @@
var _a, _a2, _a2$b, _a2$b$c, _a3, _a4, _a4$b, _a4$b$c, _b, _a5, _a5$b, _b2, _b3, _ref, _a$b, _a$b2, _ref2, _a6, _a6$b, _a7, _a7$b, _ref3;
(_a = a) === null || _a === void 0 ? void 0 : new _a.b();
(_a2 = a) === null || _a2 === void 0 ? void 0 : (_a2$b = _a2.b) === null || _a2$b === void 0 ? void 0 : (_a2$b$c = _a2$b.c) === null || _a2$b$c === void 0 ? void 0 : new _a2$b$c.d();
(_a3 = a) === null || _a3 === void 0 ? void 0 : new _a3.b();
(_a4 = a) === null || _a4 === void 0 ? void 0 : (_a4$b = _a4.b) === null || _a4$b === void 0 ? void 0 : (_a4$b$c = _a4$b.c) === null || _a4$b$c === void 0 ? void 0 : new _a4$b$c.d();
(_b = b) === null || _b === void 0 ? void 0 : new _b(b);
(_a5 = a) === null || _a5 === void 0 ? void 0 : (_a5$b = _a5.b) === null || _a5$b === void 0 ? void 0 : new _a5$b(a.b, true);
(_b2 = b) === null || _b2 === void 0 ? void 0 : new _b2().c;
(_b3 = b) === null || _b3 === void 0 ? void 0 : (_ref = new _b3()) === null || _ref === void 0 ? void 0 : _ref.c;
(_a$b = a.b) === null || _a$b === void 0 ? void 0 : new _a$b().c;
(_a$b2 = a.b) === null || _a$b2 === void 0 ? void 0 : (_ref2 = new _a$b2()) === null || _ref2 === void 0 ? void 0 : _ref2.c;
(_a6 = a) === null || _a6 === void 0 ? void 0 : (_a6$b = _a6.b) === null || _a6$b === void 0 ? void 0 : new _a6$b().c;
(_a7 = a) === null || _a7 === void 0 ? void 0 : (_a7$b = _a7.b) === null || _a7$b === void 0 ? void 0 : (_ref3 = new _a7$b()) === null || _ref3 === void 0 ? void 0 : _ref3.c;