From 936a91d27118c4b836f7549668976bbfe7050be8 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Thu, 9 Mar 2017 16:12:58 -0800 Subject: [PATCH] Add comment --- src/services/codefixes/disableJsDiagnostics.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/services/codefixes/disableJsDiagnostics.ts b/src/services/codefixes/disableJsDiagnostics.ts index 6e4a5f38586..c18c0153a03 100644 --- a/src/services/codefixes/disableJsDiagnostics.ts +++ b/src/services/codefixes/disableJsDiagnostics.ts @@ -20,9 +20,14 @@ namespace ts.codefix { let { line } = getLineAndCharacterOfPosition(sourceFile, position); const lineStartPosition = getStartPositionOfLine(line, sourceFile); const startPosition = getFirstNonSpaceCharacterPosition(sourceFile.text, lineStartPosition); + + // First try to see if we can put the '// @ts-suppress' on the previous line. + // We need to make sure that we are not in the middle of a string literal or a comment. + // We also want to check if the previous line holds a comment for a node on the next line + // if so, we do not want to separate the node from its comment if we can. if (!isInComment(sourceFile, startPosition) && !isInString(sourceFile, startPosition) && !isInTemplateString(sourceFile, startPosition)) { const token = getTouchingToken(sourceFile, startPosition); - const tokenLeadingCommnets = getLeadingCommentRangesOfNode(token, sourceFile) + const tokenLeadingCommnets = getLeadingCommentRangesOfNode(token, sourceFile); if (!tokenLeadingCommnets || !tokenLeadingCommnets.length || tokenLeadingCommnets[0].pos >= startPosition) { return { span: { start: startPosition, length: 0 }, @@ -30,6 +35,8 @@ namespace ts.codefix { }; } } + + // If all fails, add an extra new line immediatlly before the error span. return { span: { start: position, length: 0 }, newText: `${position === startPosition ? "" : newLineCharacter}// @ts-suppress${newLineCharacter}`