Skip to content

Commit 2bd66b3

Browse files
author
Andy
authored
textChanges: Add insertCommentBeforeLine method (#22902)
1 parent e9e1d0d commit 2bd66b3

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/services/codefixes/disableJsDiagnostics.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace ts.codefix {
2727
fixId: undefined,
2828
}];
2929

30-
if (isValidLocationToAddComment(sourceFile, span.start)) {
30+
if (textChanges.isValidLocationToAddComment(sourceFile, span.start)) {
3131
fixes.unshift({
3232
description: getLocaleSpecificMessage(Diagnostics.Ignore_this_error_message),
3333
changes: textChanges.ChangeTracker.with(context, t => makeChange(t, sourceFile, span.start)),
@@ -41,17 +41,13 @@ namespace ts.codefix {
4141
getAllCodeActions: context => {
4242
const seenLines = createMap<true>();
4343
return codeFixAll(context, errorCodes, (changes, diag) => {
44-
if (isValidLocationToAddComment(diag.file!, diag.start!)) {
44+
if (textChanges.isValidLocationToAddComment(diag.file!, diag.start!)) {
4545
makeChange(changes, diag.file!, diag.start!, seenLines);
4646
}
4747
});
4848
},
4949
});
5050

51-
export function isValidLocationToAddComment(sourceFile: SourceFile, position: number) {
52-
return !isInComment(sourceFile, position) && !isInString(sourceFile, position) && !isInTemplateString(sourceFile, position);
53-
}
54-
5551
function makeChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, position: number, seenLines?: Map<true>) {
5652
const { line: lineNumber } = getLineAndCharacterOfPosition(sourceFile, position);
5753
// Only need to add `// @ts-ignore` for a line once.

src/services/textChanges.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ namespace ts.textChanges {
349349
// We need to make sure that we are not in the middle of a string literal or a comment.
350350
// If so, we do not want to separate the node from its comment if we can.
351351
// Otherwise, add an extra new line immediately before the error span.
352-
const insertAtLineStart = codefix.isValidLocationToAddComment(sourceFile, startPosition);
352+
const insertAtLineStart = isValidLocationToAddComment(sourceFile, startPosition);
353353
const token = getTouchingToken(sourceFile, insertAtLineStart ? startPosition : position, /*includeJsDocComment*/ false);
354354
const text = `${insertAtLineStart ? "" : this.newLineCharacter}${sourceFile.text.slice(lineStartPosition, startPosition)}//${commentText}${this.newLineCharacter}`;
355355
this.insertText(sourceFile, token.getStart(sourceFile), text);
@@ -879,4 +879,8 @@ namespace ts.textChanges {
879879
}
880880
}
881881
}
882+
883+
export function isValidLocationToAddComment(sourceFile: SourceFile, position: number) {
884+
return !isInComment(sourceFile, position) && !isInString(sourceFile, position) && !isInTemplateString(sourceFile, position);
885+
}
882886
}

0 commit comments

Comments
 (0)