Skip to content

Commit c08308a

Browse files
committed
Reuse getSourceFileImportLocation
1 parent 9097a07 commit c08308a

11 files changed

+31
-32
lines changed

src/services/codefixes/importFixes.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -407,28 +407,6 @@ namespace ts.codefix {
407407
moduleSpecifierWithoutQuotes
408408
);
409409

410-
function getSourceFileImportLocation(node: SourceFile) {
411-
// For a source file, it is possible there are detached comments we should not skip
412-
const text = node.text;
413-
let ranges = getLeadingCommentRanges(text, 0);
414-
if (!ranges) return 0;
415-
let position = 0;
416-
// However we should still skip a pinned comment at the top
417-
if (ranges.length && ranges[0].kind === SyntaxKind.MultiLineCommentTrivia && isPinnedComment(text, ranges[0])) {
418-
position = ranges[0].end + 1;
419-
ranges = ranges.slice(1);
420-
}
421-
// As well as any triple slash references
422-
for (const range of ranges) {
423-
if (range.kind === SyntaxKind.SingleLineCommentTrivia && isRecognizedTripleSlashComment(node.text, range.pos, range.end)) {
424-
position = range.end + 1;
425-
continue;
426-
}
427-
break;
428-
}
429-
return position;
430-
}
431-
432410
function getSingleQuoteStyleFromExistingImports() {
433411
const firstModuleSpecifier = forEach(sourceFile.statements, node => {
434412
if (isImportDeclaration(node) || isExportDeclaration(node)) {

src/services/refactors/extractSymbol.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -930,8 +930,13 @@ namespace ts.refactor.extractSymbol {
930930
const nodeToInsertBefore = getNodeToInsertConstantBefore(node, scope);
931931
if (nodeToInsertBefore.pos === 0) {
932932
// If we're at the beginning of the file, we need to take care not to insert before header comments
933-
// (e.g. copyright, triple-slash references).
934-
changeTracker.insertNodeAt(context.file, nodeToInsertBefore.getStart(), newVariableStatement, { suffix: context.newLineCharacter + context.newLineCharacter });
933+
// (e.g. copyright, triple-slash references). Fortunately, this problem has already been solved
934+
// for imports.
935+
const insertionPos = getSourceFileImportLocation(file);
936+
changeTracker.insertNodeAt(context.file, insertionPos, newVariableStatement, {
937+
prefix: insertionPos === 0 ? undefined : context.newLineCharacter,
938+
suffix: isLineBreak(file.text.charCodeAt(insertionPos)) ? context.newLineCharacter : context.newLineCharacter + context.newLineCharacter
939+
});
935940
}
936941
else {
937942
changeTracker.insertNodeBefore(context.file, nodeToInsertBefore, newVariableStatement, { suffix: context.newLineCharacter + context.newLineCharacter });

src/services/utilities.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,4 +1332,26 @@ namespace ts {
13321332
export function getOpenBraceOfClassLike(declaration: ClassLikeDeclaration, sourceFile: SourceFile) {
13331333
return getTokenAtPosition(sourceFile, declaration.members.pos - 1, /*includeJsDocComment*/ false);
13341334
}
1335+
1336+
export function getSourceFileImportLocation(node: SourceFile) {
1337+
// For a source file, it is possible there are detached comments we should not skip
1338+
const text = node.text;
1339+
let ranges = getLeadingCommentRanges(text, 0);
1340+
if (!ranges) return 0;
1341+
let position = 0;
1342+
// However we should still skip a pinned comment at the top
1343+
if (ranges.length && ranges[0].kind === SyntaxKind.MultiLineCommentTrivia && isPinnedComment(text, ranges[0])) {
1344+
position = ranges[0].end + 1;
1345+
ranges = ranges.slice(1);
1346+
}
1347+
// As well as any triple slash references
1348+
for (const range of ranges) {
1349+
if (range.kind === SyntaxKind.SingleLineCommentTrivia && isRecognizedTripleSlashComment(node.text, range.pos, range.end)) {
1350+
position = range.end + 1;
1351+
continue;
1352+
}
1353+
break;
1354+
}
1355+
return position;
1356+
}
13351357
}

tests/baselines/reference/extractConstant/extractConstant_PinnedCommentAndDocComment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const x = 2 + 1;
99

1010
/*! Copyright */
1111

12-
/* About x */
1312
const newLocal = 2 + 1;
1413

14+
/* About x */
1515
const x = /*RENAME*/newLocal;
1616

tests/baselines/reference/extractConstant/extractConstant_PinnedCommentAndDocComment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const x = 2 + 1;
99

1010
/*! Copyright */
1111

12-
/* About x */
1312
const newLocal = 2 + 1;
1413

14+
/* About x */
1515
const x = /*RENAME*/newLocal;
1616

tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition4.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ function F() {
1717
}
1818

1919
// ==SCOPE::Extract to constant in global scope==
20-
2120
const newLocal = 2 + 1;
2221

2322
function F() {

tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition4.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ function F() {
1717
}
1818

1919
// ==SCOPE::Extract to constant in global scope==
20-
2120
const newLocal = 2 + 1;
2221

2322
function F() {

tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition5.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ function F0() {
3030
}
3131

3232
// ==SCOPE::Extract to constant in global scope==
33-
3433
const newLocal = 2 + 1;
3534

3635
function F0() {

tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition5.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ function F0() {
3030
}
3131

3232
// ==SCOPE::Extract to constant in global scope==
33-
3433
const newLocal = 2 + 1;
3534

3635
function F0() {

tests/baselines/reference/extractConstant/extractConstant_StatementInsertionPosition6.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ class C {
55
}
66

77
// ==SCOPE::Extract to constant in global scope==
8-
98
const newLocal = 2 + 1;
109

1110
class C {

0 commit comments

Comments
 (0)