Skip to content

Commit 92666ac

Browse files
committed
Init
1 parent 87c5b6a commit 92666ac

22 files changed

+291
-6
lines changed

src/services/refactors/extractSymbol.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ namespace ts.refactor.extractSymbol {
215215
export const cannotExtractReadonlyPropertyInitializerOutsideConstructor = createMessage("Cannot move initialization of read-only class property outside of the constructor");
216216
export const cannotExtractAmbientBlock = createMessage("Cannot extract code from ambient contexts");
217217
export const cannotAccessVariablesFromNestedScopes = createMessage("Cannot access variables from nested scopes");
218-
export const cannotExtractToOtherFunctionLike = createMessage("Cannot extract method to a function-like scope that is not a function");
219218
export const cannotExtractToJSClass = createMessage("Cannot extract constant to a class scope in JS");
220219
export const cannotExtractToExpressionArrowFunction = createMessage("Cannot extract constant to an arrow function without a block");
221220
}
@@ -504,7 +503,7 @@ namespace ts.refactor.extractSymbol {
504503
if (isThis(n)) {
505504
rangeFacts |= RangeFacts.UsesThis;
506505
}
507-
else if (isClassLike(n) || (isFunctionLike(n) && !isArrowFunction(n))) {
506+
else if (isClassLike(n) || (isFunctionLike(n))) {
508507
return false;
509508
}
510509
else {
@@ -1624,10 +1623,7 @@ namespace ts.refactor.extractSymbol {
16241623
usagesPerScope.push({ usages: new Map<string, UsageEntry>(), typeParameterUsages: new Map<string, TypeParameter>(), substitutions: new Map<string, Expression>() });
16251624
substitutionsPerScope.push(new Map<string, Expression>());
16261625

1627-
functionErrorsPerScope.push(
1628-
isFunctionLikeDeclaration(scope) && scope.kind !== SyntaxKind.FunctionDeclaration
1629-
? [createDiagnosticForNode(scope, Messages.cannotExtractToOtherFunctionLike)]
1630-
: []);
1626+
functionErrorsPerScope.push([]);
16311627

16321628
const constantErrors = [];
16331629
if (expressionDiagnostic) {

src/testRunner/unittests/services/extract/functions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@ function parsePrimaryExpression(): any {
352352
`function F() {
353353
[#|function G() { }|]
354354
}`);
355+
// Arrow function
356+
testExtractFunction("extractFunction34",
357+
`const F = () => {
358+
[#|function G() { }|]
359+
};`);
355360

356361
testExtractFunction("extractFunction_RepeatedSubstitution",
357362
`namespace X {

tests/baselines/reference/extractFunction/extractFunction10.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ namespace A {
99
}
1010
}
1111
}
12+
// ==SCOPE::Extract to inner function in method 'a'==
13+
namespace A {
14+
export interface I { x: number };
15+
class C {
16+
a() {
17+
let z = 1;
18+
return /*RENAME*/newFunction();
19+
20+
function newFunction() {
21+
let a1: I = { x: 1 };
22+
return a1.x + 10;
23+
}
24+
}
25+
}
26+
}
1227
// ==SCOPE::Extract to method in class 'C'==
1328
namespace A {
1429
export interface I { x: number };

tests/baselines/reference/extractFunction/extractFunction11.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ namespace A {
1111
}
1212
}
1313
}
14+
// ==SCOPE::Extract to inner function in method 'a'==
15+
namespace A {
16+
let y = 1;
17+
class C {
18+
a() {
19+
let z = 1;
20+
return /*RENAME*/newFunction();
21+
22+
function newFunction() {
23+
let a1 = { x: 1 };
24+
y = 10;
25+
z = 42;
26+
return a1.x + 10;
27+
}
28+
}
29+
}
30+
}
1431
// ==SCOPE::Extract to method in class 'C'==
1532
namespace A {
1633
let y = 1;

tests/baselines/reference/extractFunction/extractFunction12.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@ namespace A {
1313
}
1414
}
1515
}
16+
// ==SCOPE::Extract to inner function in method 'a'==
17+
namespace A {
18+
let y = 1;
19+
class C {
20+
b() {}
21+
a() {
22+
let z = 1;
23+
return /*RENAME*/newFunction();
24+
25+
function newFunction() {
26+
let a1 = { x: 1 };
27+
y = 10;
28+
z = 42;
29+
this.b();
30+
return a1.x + 10;
31+
}
32+
}
33+
}
34+
}
1635
// ==SCOPE::Extract to method in class 'C'==
1736
namespace A {
1837
let y = 1;

tests/baselines/reference/extractFunction/extractFunction13.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@
1414
}
1515
}
1616
}
17+
// ==SCOPE::Extract to inner function in arrow function==
18+
<U1a, U1b>(u1a: U1a, u1b: U1b) => {
19+
function F1<T1a, T1b>(t1a: T1a, t1b: T1b) {
20+
<U2a, U2b>(u2a: U2a, u2b: U2b) => {
21+
function F2<T2a, T2b>(t2a: T2a, t2b: T2b) {
22+
<U3a, U3b>(u3a: U3a, u3b: U3b) => {
23+
/*RENAME*/newFunction();
24+
25+
function newFunction() {
26+
t1a.toString();
27+
t2a.toString();
28+
u1a.toString();
29+
u2a.toString();
30+
u3a.toString();
31+
}
32+
}
33+
}
34+
}
35+
}
36+
}
1737
// ==SCOPE::Extract to inner function in function 'F2'==
1838
<U1a, U1b>(u1a: U1a, u1b: U1b) => {
1939
function F1<T1a, T1b>(t1a: T1a, t1b: T1b) {

tests/baselines/reference/extractFunction/extractFunction17.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ class C<T1, T2> {
44
/*[#|*/t1.toString()/*|]*/;
55
}
66
}
7+
// ==SCOPE::Extract to inner function in method 'M'==
8+
class C<T1, T2> {
9+
M(t1: T1, t2: T2) {
10+
/*RENAME*/newFunction();
11+
12+
function newFunction() {
13+
t1.toString();
14+
}
15+
}
16+
}
717
// ==SCOPE::Extract to method in class 'C'==
818
class C<T1, T2> {
919
M(t1: T1, t2: T2) {

tests/baselines/reference/extractFunction/extractFunction18.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ class C {
44
/*[#|*/t1.toString()/*|]*/;
55
}
66
}
7+
// ==SCOPE::Extract to inner function in method 'M'==
8+
class C {
9+
M<T1, T2>(t1: T1, t2: T2) {
10+
/*RENAME*/newFunction();
11+
12+
function newFunction() {
13+
t1.toString();
14+
}
15+
}
16+
}
717
// ==SCOPE::Extract to method in class 'C'==
818
class C {
919
M<T1, T2>(t1: T1, t2: T2) {

tests/baselines/reference/extractFunction/extractFunction20.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ const _ = class {
55
return a1.x + 10;/*|]*/
66
}
77
}
8+
// ==SCOPE::Extract to inner function in method 'a'==
9+
const _ = class {
10+
a() {
11+
return /*RENAME*/newFunction();
12+
13+
function newFunction() {
14+
let a1 = { x: 1 };
15+
return a1.x + 10;
16+
}
17+
}
18+
}
819
// ==SCOPE::Extract to method in anonymous class expression==
920
const _ = class {
1021
a() {

tests/baselines/reference/extractFunction/extractFunction20.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ const _ = class {
55
return a1.x + 10;/*|]*/
66
}
77
}
8+
// ==SCOPE::Extract to inner function in method 'a'==
9+
const _ = class {
10+
a() {
11+
return /*RENAME*/newFunction();
12+
13+
function newFunction() {
14+
let a1 = { x: 1 };
15+
return a1.x + 10;
16+
}
17+
}
18+
}
819
// ==SCOPE::Extract to method in anonymous class expression==
920
const _ = class {
1021
a() {

0 commit comments

Comments
 (0)