Maintain Export Modifier when Refactoring to ES6 Class #18435 (#19070)

This commit is contained in:
Charles Pierce 2017-10-10 15:39:59 -07:00 committed by Mohamed Hegazy
parent 611e0f7b4a
commit 249c2cbaf7
3 changed files with 46 additions and 2 deletions

View File

@ -243,7 +243,8 @@ namespace ts.refactor.convertFunctionToES6Class {
memberElements.unshift(createConstructor(/*decorators*/ undefined, /*modifiers*/ undefined, initializer.parameters, initializer.body));
}
const cls = createClassDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, node.name,
const modifiers = getExportModifierFromSource(precedingNode);
const cls = createClassDeclaration(/*decorators*/ undefined, modifiers, node.name,
/*typeParameters*/ undefined, /*heritageClauses*/ undefined, memberElements);
// Don't call copyComments here because we'll already leave them in place
return cls;
@ -255,10 +256,15 @@ namespace ts.refactor.convertFunctionToES6Class {
memberElements.unshift(createConstructor(/*decorators*/ undefined, /*modifiers*/ undefined, node.parameters, node.body));
}
const cls = createClassDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, node.name,
const modifiers = getExportModifierFromSource(node);
const cls = createClassDeclaration(/*decorators*/ undefined, modifiers, node.name,
/*typeParameters*/ undefined, /*heritageClauses*/ undefined, memberElements);
// Don't call copyComments here because we'll already leave them in place
return cls;
}
function getExportModifierFromSource(source: Node) {
return filter(source.modifiers, modifier => modifier.kind === SyntaxKind.ExportKeyword);
}
}
}

View File

@ -0,0 +1,19 @@
/// <reference path='fourslash.ts' />
// @allowNonTsExtensions: true
// @Filename: test123.js
////export function /**/MyClass() {
////}
////MyClass.prototype.foo = function() {
////}
verify.applicableRefactorAvailableAtMarker("");
verify.fileAfterApplyingRefactorAtMarker("",
`export class MyClass {
constructor() {
}
foo() {
}
}
`,
'Convert to ES2015 class', 'convert');

View File

@ -0,0 +1,19 @@
/// <reference path='fourslash.ts' />
// @allowNonTsExtensions: true
// @Filename: test123.js
////export const /**/foo = function() {
////};
////foo.prototype.instanceMethod = function() {
////};
verify.applicableRefactorAvailableAtMarker("");
verify.fileAfterApplyingRefactorAtMarker("",
`export class foo {
constructor() {
}
instanceMethod() {
}
}
`,
'Convert to ES2015 class', 'convert');