From 2bd66b329206556d36555f7eb06c23c1ce9c58cf Mon Sep 17 00:00:00 2001 From: Andy Date: Tue, 27 Mar 2018 11:50:48 -0700 Subject: [PATCH] textChanges: Add insertCommentBeforeLine method (#22902) --- src/services/codefixes/disableJsDiagnostics.ts | 8 ++------ src/services/textChanges.ts | 6 +++++- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/services/codefixes/disableJsDiagnostics.ts b/src/services/codefixes/disableJsDiagnostics.ts index 8ee89ecbf2..2d82a198ce 100644 --- a/src/services/codefixes/disableJsDiagnostics.ts +++ b/src/services/codefixes/disableJsDiagnostics.ts @@ -27,7 +27,7 @@ namespace ts.codefix { fixId: undefined, }]; - if (isValidLocationToAddComment(sourceFile, span.start)) { + if (textChanges.isValidLocationToAddComment(sourceFile, span.start)) { fixes.unshift({ description: getLocaleSpecificMessage(Diagnostics.Ignore_this_error_message), changes: textChanges.ChangeTracker.with(context, t => makeChange(t, sourceFile, span.start)), @@ -41,17 +41,13 @@ namespace ts.codefix { getAllCodeActions: context => { const seenLines = createMap(); return codeFixAll(context, errorCodes, (changes, diag) => { - if (isValidLocationToAddComment(diag.file!, diag.start!)) { + if (textChanges.isValidLocationToAddComment(diag.file!, diag.start!)) { makeChange(changes, diag.file!, diag.start!, seenLines); } }); }, }); - export function isValidLocationToAddComment(sourceFile: SourceFile, position: number) { - return !isInComment(sourceFile, position) && !isInString(sourceFile, position) && !isInTemplateString(sourceFile, position); - } - function makeChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, position: number, seenLines?: Map) { const { line: lineNumber } = getLineAndCharacterOfPosition(sourceFile, position); // Only need to add `// @ts-ignore` for a line once. diff --git a/src/services/textChanges.ts b/src/services/textChanges.ts index a02f91904d..05c218b224 100644 --- a/src/services/textChanges.ts +++ b/src/services/textChanges.ts @@ -349,7 +349,7 @@ namespace ts.textChanges { // We need to make sure that we are not in the middle of a string literal or a comment. // If so, we do not want to separate the node from its comment if we can. // Otherwise, add an extra new line immediately before the error span. - const insertAtLineStart = codefix.isValidLocationToAddComment(sourceFile, startPosition); + const insertAtLineStart = isValidLocationToAddComment(sourceFile, startPosition); const token = getTouchingToken(sourceFile, insertAtLineStart ? startPosition : position, /*includeJsDocComment*/ false); const text = `${insertAtLineStart ? "" : this.newLineCharacter}${sourceFile.text.slice(lineStartPosition, startPosition)}//${commentText}${this.newLineCharacter}`; this.insertText(sourceFile, token.getStart(sourceFile), text); @@ -879,4 +879,8 @@ namespace ts.textChanges { } } } + + export function isValidLocationToAddComment(sourceFile: SourceFile, position: number) { + return !isInComment(sourceFile, position) && !isInString(sourceFile, position) && !isInTemplateString(sourceFile, position); + } }