Skip to content

Commit 9639b52

Browse files
Andymhegazy
Andy
authored andcommitted
Fixes to emit / format for codeFix (#18486)
1 parent b62d5f6 commit 9639b52

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

src/compiler/emitter.ts

+6
Original file line numberDiff line numberDiff line change
@@ -2417,6 +2417,12 @@ namespace ts {
24172417

24182418
const isEmpty = isUndefined || children.length === 0 || start >= children.length || count === 0;
24192419
if (isEmpty && format & ListFormat.OptionalIfEmpty) {
2420+
if (onBeforeEmitNodeArray) {
2421+
onBeforeEmitNodeArray(children);
2422+
}
2423+
if (onAfterEmitNodeArray) {
2424+
onAfterEmitNodeArray(children);
2425+
}
24202426
return;
24212427
}
24222428

src/services/formatting/rules.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -757,9 +757,8 @@ namespace ts.formatting {
757757
return true;
758758
case SyntaxKind.Block: {
759759
const blockParent = context.currentTokenParent.parent;
760-
if (blockParent.kind !== SyntaxKind.ArrowFunction &&
761-
blockParent.kind !== SyntaxKind.FunctionExpression
762-
) {
760+
// In a codefix scenario, we can't rely on parents being set. So just always return true.
761+
if (!blockParent || blockParent.kind !== SyntaxKind.ArrowFunction && blockParent.kind !== SyntaxKind.FunctionExpression) {
763762
return true;
764763
}
765764
}

tests/cases/fourslash/convertFunctionToEs6Class3.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
// @allowNonTsExtensions: true
44
// @Filename: test123.js
5-
//// [|var bar = 10, /*1*/foo = function() { };
5+
//// var bar = 10, /*1*/foo = function() { };
66
//// /*2*/foo.prototype.instanceMethod1 = function() { return "this is name"; };
77
//// /*3*/foo.prototype.instanceMethod2 = () => { return "this is name"; };
88
//// /*4*/foo.prototype.instanceProp1 = "hello";
99
//// /*5*/foo.prototype.instanceProp2 = undefined;
1010
//// /*6*/foo.staticProp = "world";
1111
//// /*7*/foo.staticMethod1 = function() { return "this is static name"; };
12-
//// /*8*/foo.staticMethod2 = () => "this is static name";|]
12+
//// /*8*/foo.staticMethod2 = () => "this is static name";
1313

1414

1515
['1', '2', '3', '4', '5', '6', '7', '8'].forEach(m => verify.applicableRefactorAvailableAtMarker(m));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @allowNonTsExtensions: true
4+
// @Filename: /a.js
5+
////function /**/MyClass() {
6+
////}
7+
////MyClass.prototype.f = function(x) {
8+
//// switch (x) {
9+
//// case 0:
10+
//// }
11+
////}
12+
13+
verify.applicableRefactorAvailableAtMarker("");
14+
verify.fileAfterApplyingRefactorAtMarker("",
15+
`class MyClass {
16+
constructor() {
17+
}
18+
f(x) {
19+
switch (x) {
20+
case 0:
21+
}
22+
}
23+
}
24+
`,
25+
'Convert to ES2015 class', 'convert');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @allowNonTsExtensions: true
4+
// @Filename: /a.js
5+
////function /**/MyClass() {
6+
////}
7+
////MyClass.prototype.foo = function() {
8+
//// ({ bar: () => { } })
9+
////}
10+
11+
verify.applicableRefactorAvailableAtMarker("");
12+
verify.fileAfterApplyingRefactorAtMarker("",
13+
`class MyClass {
14+
constructor() {
15+
}
16+
foo() {
17+
({ bar: () => { } });
18+
}
19+
}
20+
`,
21+
'Convert to ES2015 class', 'convert');

0 commit comments

Comments
 (0)