Skip to content

Commit d2b866f

Browse files
committed
Clean up refactoring
1 parent 601c5b3 commit d2b866f

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

src/services/refactors/convertFunctionToEs6Class.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ namespace ts.refactor {
130130

131131
return memberElements;
132132

133+
function shouldConvertDeclaration(_target: PropertyAccessExpression, source: Expression) {
134+
// Right now the only thing we can convert are function expressions - other values shouldn't get
135+
// transformed. We can update this once ES public class properties are available.
136+
return isFunctionLike(source);
137+
}
138+
133139
function createClassElement(symbol: Symbol, modifiers: Modifier[]): ClassElement {
134140
// both properties and methods are bound as property symbols
135141
if (!(symbol.flags & SymbolFlags.Property)) {
@@ -139,6 +145,10 @@ namespace ts.refactor {
139145
const memberDeclaration = symbol.valueDeclaration as PropertyAccessExpression;
140146
const assignmentBinaryExpression = memberDeclaration.parent as BinaryExpression;
141147

148+
if (!shouldConvertDeclaration(memberDeclaration, assignmentBinaryExpression.right)) {
149+
return;
150+
}
151+
142152
// delete the entire statement if this expression is the sole expression to take care of the semicolon at the end
143153
const nodeToDelete = assignmentBinaryExpression.parent && assignmentBinaryExpression.parent.kind === SyntaxKind.ExpressionStatement
144154
? assignmentBinaryExpression.parent : assignmentBinaryExpression;
@@ -171,7 +181,12 @@ namespace ts.refactor {
171181
}
172182
return createMethod(/*decorators*/ undefined, modifiers, /*asteriskToken*/ undefined, memberDeclaration.name, /*questionToken*/ undefined,
173183
/*typeParameters*/ undefined, arrowFunction.parameters, /*type*/ undefined, bodyBlock);
184+
174185
default:
186+
// Don't try to declare members in JavaScript files
187+
if (isSourceFileJavaScript(sourceFile)) {
188+
return;
189+
}
175190
return createProperty(/*decorators*/ undefined, modifiers, memberDeclaration.name, /*questionToken*/ undefined,
176191
/*type*/ undefined, assignmentBinaryExpression.right);
177192
}

tests/cases/fourslash/convertFunctionToEs6Class1.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,16 @@
1111
//// /*7*/foo.staticMethod1 = function() { return "this is static name"; };
1212
//// /*8*/foo.staticMethod2 = () => "this is static name";|]
1313

14-
1514
['1', '2', '3', '4', '5', '6', '7', '8'].forEach(m => verify.applicableRefactorAvailableAtMarker(m));
1615
verify.fileAfterApplyingRefactorAtMarker('1',
1716
`class foo {
1817
constructor() { }
1918
instanceMethod1() { return "this is name"; }
2019
instanceMethod2() { return "this is name"; }
21-
instanceProp1 = "hello";
22-
instanceProp2 = undefined;
23-
static staticProp = "world";
2420
static staticMethod1() { return "this is static name"; }
2521
static staticMethod2() { return "this is static name"; }
2622
}
23+
foo.prototype.instanceProp1 = "hello";
24+
foo.prototype.instanceProp2 = undefined;
25+
foo.staticProp = "world";
2726
`, 'Convert to ES2015 class');

tests/cases/fourslash/convertFunctionToEs6Class2.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
//// [|var /*1*/foo = function() { };
66
//// /*2*/foo.prototype.instanceMethod1 = function() { return "this is name"; };
77
//// /*3*/foo.prototype.instanceMethod2 = () => { return "this is name"; };
8-
//// /*4*/foo.prototype.instanceProp1 = "hello";
9-
//// /*5*/foo.prototype.instanceProp2 = undefined;
8+
//// /*4*/foo.instanceProp1 = "hello";
9+
//// /*5*/foo.instanceProp2 = undefined;
1010
//// /*6*/foo.staticProp = "world";
1111
//// /*7*/foo.staticMethod1 = function() { return "this is static name"; };
1212
//// /*8*/foo.staticMethod2 = () => "this is static name";|]
@@ -18,10 +18,10 @@ verify.fileAfterApplyingRefactorAtMarker('4',
1818
constructor() { }
1919
instanceMethod1() { return "this is name"; }
2020
instanceMethod2() { return "this is name"; }
21-
instanceProp1 = "hello";
22-
instanceProp2 = undefined;
23-
static staticProp = "world";
2421
static staticMethod1() { return "this is static name"; }
2522
static staticMethod2() { return "this is static name"; }
2623
}
24+
foo.instanceProp1 = "hello";
25+
foo.instanceProp2 = undefined;
26+
foo.staticProp = "world";
2727
`, 'Convert to ES2015 class');

tests/cases/fourslash/convertFunctionToEs6Class3.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ class foo {
1919
constructor() { }
2020
instanceMethod1() { return "this is name"; }
2121
instanceMethod2() { return "this is name"; }
22-
instanceProp1 = "hello";
23-
instanceProp2 = undefined;
24-
static staticProp = "world";
2522
static staticMethod1() { return "this is static name"; }
2623
static staticMethod2() { return "this is static name"; }
2724
}
25+
foo.prototype.instanceProp1 = "hello";
26+
foo.prototype.instanceProp2 = undefined;
27+
foo.staticProp = "world";
2828
`, 'Convert to ES2015 class');
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
// @allowNonTsExtensions: true
22
// @Filename: test123.js
33

4+
/// <reference path="../fourslash.ts" />
5+
6+
//// // Comment
47
//// function fn() {
58
//// this.baz = 10;
69
//// }
710
//// /*1*/fn.prototype.bar = function () {
11+
//// console.log('hello world');
812
//// }
913

10-
debugger;
1114
verify.applicableRefactorAvailableAtMarker('1');
1215
verify.fileAfterApplyingRefactorAtMarker('1',
13-
`class fn {
16+
`// Comment
17+
class fn {
1418
constructor() {
1519
this.baz = 10;
1620
}
17-
baz;
1821
bar() {
22+
console.log('hello world');
1923
}
2024
}
21-
`, 'Convert to ES2015 class');
25+
`, 'Convert to ES2015 class');

0 commit comments

Comments
 (0)