Reuse getSourceFileImportLocation

This commit is contained in:
Andrew Casey
2017-09-29 17:37:39 -07:00
parent 9097a07758
commit c08308a0f1
11 changed files with 31 additions and 32 deletions

View File

@@ -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)) {

View File

@@ -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 });

View File

@@ -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;
}
}

View File

@@ -9,8 +9,8 @@ const x = 2 + 1;
/*! Copyright */
/* About x */
const newLocal = 2 + 1;
/* About x */
const x = /*RENAME*/newLocal;

View File

@@ -9,8 +9,8 @@ const x = 2 + 1;
/*! Copyright */
/* About x */
const newLocal = 2 + 1;
/* About x */
const x = /*RENAME*/newLocal;

View File

@@ -17,7 +17,6 @@ function F() {
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = 2 + 1;
function F() {

View File

@@ -17,7 +17,6 @@ function F() {
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = 2 + 1;
function F() {

View File

@@ -30,7 +30,6 @@ function F0() {
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = 2 + 1;
function F0() {

View File

@@ -30,7 +30,6 @@ function F0() {
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = 2 + 1;
function F0() {

View File

@@ -5,7 +5,6 @@ class C {
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = 2 + 1;
class C {

View File

@@ -13,7 +13,6 @@ class C {
}
// ==SCOPE::Extract to constant in global scope==
const newLocal = 2 + 1;
class C {