Skip to content

Commit dd84bc1

Browse files
author
Orta Therox
authored
Handles creating a reasonable AST when destructuring into a parens'd expresssion (microsoft#40115)
* Handles creating a lgical AST when destructuring into a parens * Adds an async example
1 parent 82f3ac9 commit dd84bc1

File tree

6 files changed

+190
-1
lines changed

6 files changed

+190
-1
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7820,9 +7820,13 @@ namespace ts {
78207820
const propName = getDestructuringPropertyName(node);
78217821
if (propName) {
78227822
const literal = setTextRange(parseNodeFactory.createStringLiteral(propName), node);
7823-
const result = setTextRange(parseNodeFactory.createElementAccessExpression(parentAccess, literal), node);
7823+
const lhsExpr = isLeftHandSideExpression(parentAccess) ? parentAccess : parseNodeFactory.createParenthesizedExpression(parentAccess);
7824+
const result = setTextRange(parseNodeFactory.createElementAccessExpression(lhsExpr, literal), node);
78247825
setParent(literal, result);
78257826
setParent(result, node);
7827+
if (lhsExpr !== parentAccess) {
7828+
setParent(lhsExpr, result);
7829+
}
78267830
result.flowNode = parentAccess.flowNode;
78277831
return result;
78287832
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error TS2468: Cannot find global value 'Promise'.
2+
tests/cases/compiler/destructuringControlFlowNoCrash.ts(3,3): error TS2339: Property 'date' does not exist on type '(inspectedElement: any) => number'.
3+
tests/cases/compiler/destructuringControlFlowNoCrash.ts(10,3): error TS2339: Property 'date2' does not exist on type '(inspectedElement: any) => any'.
4+
tests/cases/compiler/destructuringControlFlowNoCrash.ts(11,28): error TS1005: '=>' expected.
5+
tests/cases/compiler/destructuringControlFlowNoCrash.ts(16,25): error TS2705: An async function or method in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your `--lib` option.
6+
7+
8+
!!! error TS2468: Cannot find global value 'Promise'.
9+
==== tests/cases/compiler/destructuringControlFlowNoCrash.ts (4 errors) ====
10+
// legal JS, if nonsensical, which also triggers the issue
11+
const {
12+
date,
13+
~~~~
14+
!!! error TS2339: Property 'date' does not exist on type '(inspectedElement: any) => number'.
15+
} = (inspectedElement: any) => 0;
16+
17+
date.toISOString();
18+
19+
// Working flow code
20+
const {
21+
date2,
22+
~~~~~
23+
!!! error TS2339: Property 'date2' does not exist on type '(inspectedElement: any) => any'.
24+
} = (inspectedElement: any).props;
25+
~
26+
!!! error TS1005: '=>' expected.
27+
28+
date2.toISOString();
29+
30+
// It could also be an async function
31+
const { constructor } = async () => {};
32+
~~~~~~~~~~~~~~
33+
!!! error TS2705: An async function or method in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your `--lib` option.
34+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//// [destructuringControlFlowNoCrash.ts]
2+
// legal JS, if nonsensical, which also triggers the issue
3+
const {
4+
date,
5+
} = (inspectedElement: any) => 0;
6+
7+
date.toISOString();
8+
9+
// Working flow code
10+
const {
11+
date2,
12+
} = (inspectedElement: any).props;
13+
14+
date2.toISOString();
15+
16+
// It could also be an async function
17+
const { constructor } = async () => {};
18+
19+
20+
//// [destructuringControlFlowNoCrash.js]
21+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23+
return new (P || (P = Promise))(function (resolve, reject) {
24+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27+
step((generator = generator.apply(thisArg, _arguments || [])).next());
28+
});
29+
};
30+
var __generator = (this && this.__generator) || function (thisArg, body) {
31+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
32+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
33+
function verb(n) { return function (v) { return step([n, v]); }; }
34+
function step(op) {
35+
if (f) throw new TypeError("Generator is already executing.");
36+
while (_) try {
37+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
38+
if (y = 0, t) op = [op[0] & 2, t.value];
39+
switch (op[0]) {
40+
case 0: case 1: t = op; break;
41+
case 4: _.label++; return { value: op[1], done: false };
42+
case 5: _.label++; y = op[1]; op = [0]; continue;
43+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
44+
default:
45+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
46+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
47+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
48+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
49+
if (t[2]) _.ops.pop();
50+
_.trys.pop(); continue;
51+
}
52+
op = body.call(thisArg, _);
53+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
54+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
55+
}
56+
};
57+
var _this = this;
58+
// legal JS, if nonsensical, which also triggers the issue
59+
var date = function (inspectedElement) { return 0; }.date;
60+
date.toISOString();
61+
// Working flow code
62+
var date2 = function (inspectedElement) { return ; }.date2, props;
63+
date2.toISOString();
64+
// It could also be an async function
65+
var constructor = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
66+
return [2 /*return*/];
67+
}); }); }.constructor;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=== tests/cases/compiler/destructuringControlFlowNoCrash.ts ===
2+
// legal JS, if nonsensical, which also triggers the issue
3+
const {
4+
date,
5+
>date : Symbol(date, Decl(destructuringControlFlowNoCrash.ts, 1, 7))
6+
7+
} = (inspectedElement: any) => 0;
8+
>inspectedElement : Symbol(inspectedElement, Decl(destructuringControlFlowNoCrash.ts, 3, 5))
9+
10+
date.toISOString();
11+
>date : Symbol(date, Decl(destructuringControlFlowNoCrash.ts, 1, 7))
12+
13+
// Working flow code
14+
const {
15+
date2,
16+
>date2 : Symbol(date2, Decl(destructuringControlFlowNoCrash.ts, 8, 7))
17+
18+
} = (inspectedElement: any).props;
19+
>inspectedElement : Symbol(inspectedElement, Decl(destructuringControlFlowNoCrash.ts, 10, 5))
20+
>props : Symbol(props, Decl(destructuringControlFlowNoCrash.ts, 10, 28))
21+
22+
date2.toISOString();
23+
>date2 : Symbol(date2, Decl(destructuringControlFlowNoCrash.ts, 8, 7))
24+
25+
// It could also be an async function
26+
const { constructor } = async () => {};
27+
>constructor : Symbol(constructor, Decl(destructuringControlFlowNoCrash.ts, 15, 7))
28+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
=== tests/cases/compiler/destructuringControlFlowNoCrash.ts ===
2+
// legal JS, if nonsensical, which also triggers the issue
3+
const {
4+
date,
5+
>date : any
6+
7+
} = (inspectedElement: any) => 0;
8+
>(inspectedElement: any) => 0 : (inspectedElement: any) => number
9+
>inspectedElement : any
10+
>0 : 0
11+
12+
date.toISOString();
13+
>date.toISOString() : any
14+
>date.toISOString : any
15+
>date : any
16+
>toISOString : any
17+
18+
// Working flow code
19+
const {
20+
date2,
21+
>date2 : any
22+
23+
} = (inspectedElement: any).props;
24+
>(inspectedElement: any) : (inspectedElement: any) => any
25+
>inspectedElement : any
26+
> : any
27+
>props : any
28+
29+
date2.toISOString();
30+
>date2.toISOString() : any
31+
>date2.toISOString : any
32+
>date2 : any
33+
>toISOString : any
34+
35+
// It could also be an async function
36+
const { constructor } = async () => {};
37+
>constructor : Function
38+
>async () => {} : () => Promise<void>
39+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
// legal JS, if nonsensical, which also triggers the issue
3+
const {
4+
date,
5+
} = (inspectedElement: any) => 0;
6+
7+
date.toISOString();
8+
9+
// Working flow code
10+
const {
11+
date2,
12+
} = (inspectedElement: any).props;
13+
14+
date2.toISOString();
15+
16+
// It could also be an async function
17+
const { constructor } = async () => {};

0 commit comments

Comments
 (0)