From a1dee452faa9a6a5bdf1301d09b887e40d19852e Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Wed, 20 Sep 2017 14:48:11 -0700 Subject: [PATCH] JavaScript: handle lack of modifiers on extracted method The emitter expects undefined, rather than empty. This only affects JS, because TS applies `private` to all extracted methods. (cherry picked from commit 9630c46ea7174f78d9a2661cbcc204bdce1a7781) --- src/services/refactors/extractMethod.ts | 2 +- tests/cases/fourslash/extract-method26.ts | 30 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/extract-method26.ts diff --git a/src/services/refactors/extractMethod.ts b/src/services/refactors/extractMethod.ts index c2ffdc1d2f..30d95e8153 100644 --- a/src/services/refactors/extractMethod.ts +++ b/src/services/refactors/extractMethod.ts @@ -664,7 +664,7 @@ namespace ts.refactor.extractMethod { } newFunction = createMethod( /*decorators*/ undefined, - modifiers, + modifiers.length ? modifiers : undefined, range.facts & RangeFacts.IsGenerator ? createToken(SyntaxKind.AsteriskToken) : undefined, functionName, /*questionToken*/ undefined, diff --git a/tests/cases/fourslash/extract-method26.ts b/tests/cases/fourslash/extract-method26.ts new file mode 100644 index 0000000000..68982eda86 --- /dev/null +++ b/tests/cases/fourslash/extract-method26.ts @@ -0,0 +1,30 @@ +/// + +// Handle having zero modifiers on a method. + +// @allowNonTsExtensions: true +// @Filename: file1.js +//// class C { +//// M() { +//// const q = /*a*/1 + 2/*b*/; +//// q.toString(); +//// } +//// } + +goTo.select('a', 'b') +edit.applyRefactor({ + refactorName: "Extract Method", + actionName: "scope_0", + actionDescription: "Extract to method in class 'C'", + newContent: +`class C { + M() { + const q = this./*RENAME*/newFunction(); + q.toString(); + } + + newFunction() { + return 1 + 2; + } +}` +});