TypeScript/tests/baselines/reference/extractFunction/extractFunction_VariableDeclaration_Multiple2.ts
Andrew Casey 568c8a3298 Allow extraction of variable decls used outside the extracted range
If there are only declarations, use the new function as the initializer
for a destructuring declaration.

If there are declarations and writes, changes all of the `const`
declarations to `let` and add `| undefined` onto any explicit types.
Use destructuring assignment to accomplish both "initialization" and
writes.

I don't believe there is a case where there are both declarations and a
return (since the declarations wouldn't be available after the return).

UNDONE: this could probably be generalized to handle binding patterns
but,
for now, only identifiers are supported.

Fixes #18242
Fixes #18855
2017-10-11 16:38:38 -07:00

16 lines
275 B
TypeScript

// ==ORIGINAL==
/*[#|*/const x = 1, y = "a";
const z = 3;/*|]*/
x; y; z;
// ==SCOPE::Extract to function in global scope==
const { x, y, z } = /*RENAME*/newFunction();
x; y; z;
function newFunction() {
const x = 1, y = "a";
const z = 3;
return { x, y, z };
}