From 249c2cbaf754164c3f6d0e7a03c53002575b4060 Mon Sep 17 00:00:00 2001 From: Charles Pierce Date: Tue, 10 Oct 2017 15:39:59 -0700 Subject: [PATCH] Maintain Export Modifier when Refactoring to ES6 Class #18435 (#19070) --- .../refactors/convertFunctionToEs6Class.ts | 10 ++++++++-- ...nvertFunctionToEs6Class_exportModifier1.ts | 19 +++++++++++++++++++ ...nvertFunctionToEs6Class_exportModifier2.ts | 19 +++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/convertFunctionToEs6Class_exportModifier1.ts create mode 100644 tests/cases/fourslash/convertFunctionToEs6Class_exportModifier2.ts diff --git a/src/services/refactors/convertFunctionToEs6Class.ts b/src/services/refactors/convertFunctionToEs6Class.ts index e4cd1a42083..110f64d1220 100644 --- a/src/services/refactors/convertFunctionToEs6Class.ts +++ b/src/services/refactors/convertFunctionToEs6Class.ts @@ -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); + } } } \ No newline at end of file diff --git a/tests/cases/fourslash/convertFunctionToEs6Class_exportModifier1.ts b/tests/cases/fourslash/convertFunctionToEs6Class_exportModifier1.ts new file mode 100644 index 00000000000..940a68a05b6 --- /dev/null +++ b/tests/cases/fourslash/convertFunctionToEs6Class_exportModifier1.ts @@ -0,0 +1,19 @@ +/// + +// @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'); diff --git a/tests/cases/fourslash/convertFunctionToEs6Class_exportModifier2.ts b/tests/cases/fourslash/convertFunctionToEs6Class_exportModifier2.ts new file mode 100644 index 00000000000..fb1276d4f03 --- /dev/null +++ b/tests/cases/fourslash/convertFunctionToEs6Class_exportModifier2.ts @@ -0,0 +1,19 @@ +/// + +// @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');