mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 12:51:30 -05:00
Reuse getSourceFileImportLocation
This commit is contained in:
@@ -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)) {
|
||||
|
||||
@@ -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 });
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ const x = 2 + 1;
|
||||
|
||||
/*! Copyright */
|
||||
|
||||
/* About x */
|
||||
const newLocal = 2 + 1;
|
||||
|
||||
/* About x */
|
||||
const x = /*RENAME*/newLocal;
|
||||
|
||||
@@ -9,8 +9,8 @@ const x = 2 + 1;
|
||||
|
||||
/*! Copyright */
|
||||
|
||||
/* About x */
|
||||
const newLocal = 2 + 1;
|
||||
|
||||
/* About x */
|
||||
const x = /*RENAME*/newLocal;
|
||||
|
||||
@@ -17,7 +17,6 @@ function F() {
|
||||
}
|
||||
|
||||
// ==SCOPE::Extract to constant in global scope==
|
||||
|
||||
const newLocal = 2 + 1;
|
||||
|
||||
function F() {
|
||||
|
||||
@@ -17,7 +17,6 @@ function F() {
|
||||
}
|
||||
|
||||
// ==SCOPE::Extract to constant in global scope==
|
||||
|
||||
const newLocal = 2 + 1;
|
||||
|
||||
function F() {
|
||||
|
||||
@@ -30,7 +30,6 @@ function F0() {
|
||||
}
|
||||
|
||||
// ==SCOPE::Extract to constant in global scope==
|
||||
|
||||
const newLocal = 2 + 1;
|
||||
|
||||
function F0() {
|
||||
|
||||
@@ -30,7 +30,6 @@ function F0() {
|
||||
}
|
||||
|
||||
// ==SCOPE::Extract to constant in global scope==
|
||||
|
||||
const newLocal = 2 + 1;
|
||||
|
||||
function F0() {
|
||||
|
||||
@@ -5,7 +5,6 @@ class C {
|
||||
}
|
||||
|
||||
// ==SCOPE::Extract to constant in global scope==
|
||||
|
||||
const newLocal = 2 + 1;
|
||||
|
||||
class C {
|
||||
|
||||
@@ -13,7 +13,6 @@ class C {
|
||||
}
|
||||
|
||||
// ==SCOPE::Extract to constant in global scope==
|
||||
|
||||
const newLocal = 2 + 1;
|
||||
|
||||
class C {
|
||||
|
||||
Reference in New Issue
Block a user