From c08308a0f13bb4e3e0462865bb7d17611540477c Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Fri, 29 Sep 2017 17:37:39 -0700 Subject: [PATCH] Reuse getSourceFileImportLocation --- src/services/codefixes/importFixes.ts | 22 ------------------- src/services/refactors/extractSymbol.ts | 9 ++++++-- src/services/utilities.ts | 22 +++++++++++++++++++ ...ractConstant_PinnedCommentAndDocComment.js | 2 +- ...ractConstant_PinnedCommentAndDocComment.ts | 2 +- ...actConstant_StatementInsertionPosition4.js | 1 - ...actConstant_StatementInsertionPosition4.ts | 1 - ...actConstant_StatementInsertionPosition5.js | 1 - ...actConstant_StatementInsertionPosition5.ts | 1 - ...actConstant_StatementInsertionPosition6.js | 1 - ...actConstant_StatementInsertionPosition6.ts | 1 - 11 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/services/codefixes/importFixes.ts b/src/services/codefixes/importFixes.ts index 96f4b7ad4b5..f516bc15de3 100644 --- a/src/services/codefixes/importFixes.ts +++ b/src/services/codefixes/importFixes.ts @@ -407,28 +407,6 @@ namespace ts.codefix { moduleSpecifierWithoutQuotes ); - function getSourceFileImportLocation(node: SourceFile) { - // For a source file, it is possible there are detached comments we should not skip - const text = node.text; - let ranges = getLeadingCommentRanges(text, 0); - if (!ranges) return 0; - let position = 0; - // However we should still skip a pinned comment at the top - if (ranges.length && ranges[0].kind === SyntaxKind.MultiLineCommentTrivia && isPinnedComment(text, ranges[0])) { - position = ranges[0].end + 1; - ranges = ranges.slice(1); - } - // As well as any triple slash references - for (const range of ranges) { - if (range.kind === SyntaxKind.SingleLineCommentTrivia && isRecognizedTripleSlashComment(node.text, range.pos, range.end)) { - position = range.end + 1; - continue; - } - break; - } - return position; - } - function getSingleQuoteStyleFromExistingImports() { const firstModuleSpecifier = forEach(sourceFile.statements, node => { if (isImportDeclaration(node) || isExportDeclaration(node)) { diff --git a/src/services/refactors/extractSymbol.ts b/src/services/refactors/extractSymbol.ts index ad4da2c61fc..549bf653e9d 100644 --- a/src/services/refactors/extractSymbol.ts +++ b/src/services/refactors/extractSymbol.ts @@ -930,8 +930,13 @@ namespace ts.refactor.extractSymbol { const nodeToInsertBefore = getNodeToInsertConstantBefore(node, scope); if (nodeToInsertBefore.pos === 0) { // If we're at the beginning of the file, we need to take care not to insert before header comments - // (e.g. copyright, triple-slash references). - changeTracker.insertNodeAt(context.file, nodeToInsertBefore.getStart(), newVariableStatement, { suffix: context.newLineCharacter + context.newLineCharacter }); + // (e.g. copyright, triple-slash references). Fortunately, this problem has already been solved + // for imports. + const insertionPos = getSourceFileImportLocation(file); + changeTracker.insertNodeAt(context.file, insertionPos, newVariableStatement, { + prefix: insertionPos === 0 ? undefined : context.newLineCharacter, + suffix: isLineBreak(file.text.charCodeAt(insertionPos)) ? context.newLineCharacter : context.newLineCharacter + context.newLineCharacter + }); } else { changeTracker.insertNodeBefore(context.file, nodeToInsertBefore, newVariableStatement, { suffix: context.newLineCharacter + context.newLineCharacter }); diff --git a/src/services/utilities.ts b/src/services/utilities.ts index f367c48ac6f..ee74a8680eb 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1332,4 +1332,26 @@ namespace ts { export function getOpenBraceOfClassLike(declaration: ClassLikeDeclaration, sourceFile: SourceFile) { return getTokenAtPosition(sourceFile, declaration.members.pos - 1, /*includeJsDocComment*/ false); } + + export function getSourceFileImportLocation(node: SourceFile) { + // For a source file, it is possible there are detached comments we should not skip + const text = node.text; + let ranges = getLeadingCommentRanges(text, 0); + if (!ranges) return 0; + let position = 0; + // However we should still skip a pinned comment at the top + if (ranges.length && ranges[0].kind === SyntaxKind.MultiLineCommentTrivia && isPinnedComment(text, ranges[0])) { + position = ranges[0].end + 1; + ranges = ranges.slice(1); + } + // As well as any triple slash references + for (const range of ranges) { + if (range.kind === SyntaxKind.SingleLineCommentTrivia && isRecognizedTripleSlashComment(node.text, range.pos, range.end)) { + position = range.end + 1; + continue; + } + break; + } + return position; + } } diff --git a/tests/baselines/reference/extractConstant/extractConstant_PinnedCommentAndDocComment.js b/tests/baselines/reference/extractConstant/extractConstant_PinnedCommentAndDocComment.js index 763473d49cd..24397bc9743 100644 --- a/tests/baselines/reference/extractConstant/extractConstant_PinnedCommentAndDocComment.js +++ b/tests/baselines/reference/extractConstant/extractConstant_PinnedCommentAndDocComment.js @@ -9,8 +9,8 @@ const x = 2 + 1; /*! Copyright */ -/* About x */ const newLocal = 2 + 1; +/* About x */ const x = /*RENAME*/newLocal; \ No newline at end of file diff --git a/tests/baselines/reference/extractConstant/extractConstant_PinnedCommentAndDocComment.ts b/tests/baselines/reference/extractConstant/extractConstant_PinnedCommentAndDocComment.ts index 763473d49cd..24397bc9743 100644 --- a/tests/baselines/reference/extractConstant/extractConstant_PinnedCommentAndDocComment.ts +++ b/tests/baselines/reference/extractConstant/extractConstant_PinnedCommentAndDocComment.ts @@ -9,8 +9,8 @@ const x = 2 + 1; /*! Copyright */ -/* About x */ const newLocal = 2 + 1; +/* About x */ const x = /*RENAME*/newLocal; \ No newline at end of file diff --git a/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition4.js b/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition4.js index 19fd25382e8..c019c085634 100644 --- a/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition4.js +++ b/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition4.js @@ -17,7 +17,6 @@ function F() { } // ==SCOPE::Extract to constant in global scope== - const newLocal = 2 + 1; function F() { diff --git a/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition4.ts b/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition4.ts index 19fd25382e8..c019c085634 100644 --- a/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition4.ts +++ b/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition4.ts @@ -17,7 +17,6 @@ function F() { } // ==SCOPE::Extract to constant in global scope== - const newLocal = 2 + 1; function F() { diff --git a/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition5.js b/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition5.js index a19bd6afa28..c6019846fbe 100644 --- a/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition5.js +++ b/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition5.js @@ -30,7 +30,6 @@ function F0() { } // ==SCOPE::Extract to constant in global scope== - const newLocal = 2 + 1; function F0() { diff --git a/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition5.ts b/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition5.ts index a19bd6afa28..c6019846fbe 100644 --- a/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition5.ts +++ b/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition5.ts @@ -30,7 +30,6 @@ function F0() { } // ==SCOPE::Extract to constant in global scope== - const newLocal = 2 + 1; function F0() { diff --git a/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition6.js b/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition6.js index 06a665140d9..0c81cb756a1 100644 --- a/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition6.js +++ b/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition6.js @@ -5,7 +5,6 @@ class C { } // ==SCOPE::Extract to constant in global scope== - const newLocal = 2 + 1; class C { diff --git a/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition6.ts b/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition6.ts index a34b0a9ecc3..44a3456fc23 100644 --- a/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition6.ts +++ b/tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition6.ts @@ -13,7 +13,6 @@ class C { } // ==SCOPE::Extract to constant in global scope== - const newLocal = 2 + 1; class C {