diff --git a/src/harness/client.ts b/src/harness/client.ts index 5440b85ceae..ba2e920ac59 100644 --- a/src/harness/client.ts +++ b/src/harness/client.ts @@ -832,4 +832,4 @@ namespace ts.server { throw new Error("dispose is not available through the server layer."); } } -} \ No newline at end of file +} diff --git a/src/harness/fourslashImpl.ts b/src/harness/fourslashImpl.ts index 62a48882135..b2007786720 100644 --- a/src/harness/fourslashImpl.ts +++ b/src/harness/fourslashImpl.ts @@ -3659,8 +3659,8 @@ namespace FourSlash { } public toggleLineComment(newFileContent: string): void { - let changes: ts.TextChange[] = []; - for (let range of this.getRanges()) { + const changes: ts.TextChange[] = []; + for (const range of this.getRanges()) { changes.push.apply(changes, this.languageService.toggleLineComment(this.activeFile.fileName, range)); } @@ -3670,8 +3670,8 @@ namespace FourSlash { } public toggleMultilineComment(newFileContent: string): void { - let changes: ts.TextChange[] = []; - for (let range of this.getRanges()) { + const changes: ts.TextChange[] = []; + for (const range of this.getRanges()) { changes.push.apply(changes, this.languageService.toggleMultilineComment(this.activeFile.fileName, range)); } @@ -3681,8 +3681,8 @@ namespace FourSlash { } public commentSelection(newFileContent: string): void { - let changes: ts.TextChange[] = []; - for (let range of this.getRanges()) { + const changes: ts.TextChange[] = []; + for (const range of this.getRanges()) { changes.push.apply(changes, this.languageService.commentSelection(this.activeFile.fileName, range)); } @@ -3692,8 +3692,8 @@ namespace FourSlash { } public uncommentSelection(newFileContent: string): void { - let changes: ts.TextChange[] = []; - for (let range of this.getRanges()) { + const changes: ts.TextChange[] = []; + for (const range of this.getRanges()) { changes.push.apply(changes, this.languageService.uncommentSelection(this.activeFile.fileName, range)); } diff --git a/src/server/session.ts b/src/server/session.ts index 5ede17856d0..96a4b676907 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -2711,28 +2711,28 @@ namespace ts.server { return this.requiredResponse(this.provideCallHierarchyOutgoingCalls(request.arguments)); }, [CommandNames.ToggleLineComment]: (request: protocol.ToggleLineCommentRequest) => { - return this.requiredResponse(this.toggleLineComment(request.arguments, /*simplifiedResult*/true)); + return this.requiredResponse(this.toggleLineComment(request.arguments, /*simplifiedResult*/ true)); }, [CommandNames.ToggleLineCommentFull]: (request: protocol.ToggleLineCommentRequest) => { - return this.requiredResponse(this.toggleLineComment(request.arguments, /*simplifiedResult*/false)); + return this.requiredResponse(this.toggleLineComment(request.arguments, /*simplifiedResult*/ false)); }, [CommandNames.ToggleMultilineComment]: (request: protocol.ToggleMultilineCommentRequest) => { - return this.requiredResponse(this.toggleMultilineComment(request.arguments, /*simplifiedResult*/true)); + return this.requiredResponse(this.toggleMultilineComment(request.arguments, /*simplifiedResult*/ true)); }, [CommandNames.ToggleMultilineCommentFull]: (request: protocol.ToggleMultilineCommentRequest) => { - return this.requiredResponse(this.toggleMultilineComment(request.arguments, /*simplifiedResult*/false)); + return this.requiredResponse(this.toggleMultilineComment(request.arguments, /*simplifiedResult*/ false)); }, [CommandNames.CommentSelection]: (request: protocol.CommentSelectionRequest) => { - return this.requiredResponse(this.commentSelection(request.arguments, /*simplifiedResult*/true)); + return this.requiredResponse(this.commentSelection(request.arguments, /*simplifiedResult*/ true)); }, [CommandNames.CommentSelectionFull]: (request: protocol.CommentSelectionRequest) => { - return this.requiredResponse(this.commentSelection(request.arguments, /*simplifiedResult*/false)); + return this.requiredResponse(this.commentSelection(request.arguments, /*simplifiedResult*/ false)); }, [CommandNames.UncommentSelection]: (request: protocol.UncommentSelectionRequest) => { - return this.requiredResponse(this.uncommentSelection(request.arguments, /*simplifiedResult*/true)); + return this.requiredResponse(this.uncommentSelection(request.arguments, /*simplifiedResult*/ true)); }, [CommandNames.UncommentSelectionFull]: (request: protocol.UncommentSelectionRequest) => { - return this.requiredResponse(this.uncommentSelection(request.arguments, /*simplifiedResult*/false)); + return this.requiredResponse(this.uncommentSelection(request.arguments, /*simplifiedResult*/ false)); }, }); diff --git a/src/services/services.ts b/src/services/services.ts index fe63930a155..b6003b4826e 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1982,7 +1982,7 @@ namespace ts { lineStarts: sourceFile.getLineStarts(), firstLine: sourceFile.getLineAndCharacterOfPosition(textRange.pos).line, lastLine: sourceFile.getLineAndCharacterOfPosition(textRange.end).line - } + }; } function toggleLineComment(fileName: string, textRange: TextRange, insertComment?: boolean): TextChange[] { @@ -1992,9 +1992,9 @@ namespace ts { let isCommenting = insertComment || false; let leftMostPosition = Number.MAX_VALUE; - let lineTextStarts = new Map(); + const lineTextStarts = new Map(); const whiteSpaceRegex = new RegExp(/\S/); - const isJsx = isInsideJsxElement(sourceFile, lineStarts[firstLine]) + const isJsx = isInsideJsxElement(sourceFile, lineStarts[firstLine]); const openComment = isJsx ? "{/*" : "//"; // Check each line before any text changes. @@ -2021,7 +2021,8 @@ namespace ts { if (lineTextStart !== undefined) { if (isJsx) { textChanges.push.apply(textChanges, toggleMultilineComment(fileName, { pos: lineStarts[i] + leftMostPosition, end: sourceFile.getLineEndOfPosition(lineStarts[i]) }, isCommenting, isJsx)); - } else if (isCommenting) { + } + else if (isCommenting) { textChanges.push({ newText: openComment, span: { @@ -2029,7 +2030,8 @@ namespace ts { start: lineStarts[i] + leftMostPosition } }); - } else if (sourceFile.text.substr(lineStarts[i] + lineTextStart, openComment.length) === openComment) { + } + else if (sourceFile.text.substr(lineStarts[i] + lineTextStart, openComment.length) === openComment) { textChanges.push({ newText: "", span: { @@ -2080,8 +2082,9 @@ namespace ts { } pos = commentRange.end + 1; - } else { // If it's not in a comment range, then we need to comment the uncommented portions. - let newPos = text.substring(pos, textRange.end).search(`(${openMultilineRegex})|(${closeMultilineRegex})`); + } + else { // If it's not in a comment range, then we need to comment the uncommented portions. + const newPos = text.substring(pos, textRange.end).search(`(${openMultilineRegex})|(${closeMultilineRegex})`); isCommenting = insertComment !== undefined ? insertComment @@ -2141,16 +2144,17 @@ namespace ts { } }); } - } else { + } + else { // If is not commenting then remove all comments found. - for (let i = 0; i < positions.length; i++) { - const from = positions[i] - closeMultiline.length > 0 ? positions[i] - closeMultiline.length : 0; + for (const pos of positions) { + const from = pos - closeMultiline.length > 0 ? pos - closeMultiline.length : 0; const offset = text.substr(from, closeMultiline.length) === closeMultiline ? closeMultiline.length : 0; textChanges.push({ newText: "", span: { length: openMultiline.length, - start: positions[i] - offset + start: pos - offset } }); } @@ -2160,21 +2164,22 @@ namespace ts { } function commentSelection(fileName: string, textRange: TextRange): TextChange[] { - return toggleLineComment(fileName, textRange, true); + return toggleLineComment(fileName, textRange, /*insertComment*/ true); } + function uncommentSelection(fileName: string, textRange: TextRange): TextChange[] { const sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); const textChanges: TextChange[] = []; for (let i = textRange.pos; i <= textRange.end; i++) { - let commentRange = isInComment(sourceFile, i); + const commentRange = isInComment(sourceFile, i); if (commentRange) { switch (commentRange.kind) { case SyntaxKind.SingleLineCommentTrivia: - textChanges.push.apply(textChanges, toggleLineComment(fileName, { end: commentRange.end, pos: commentRange.pos + 1 }, false)); + textChanges.push.apply(textChanges, toggleLineComment(fileName, { end: commentRange.end, pos: commentRange.pos + 1 }, /*insertComment*/ false)); break; case SyntaxKind.MultiLineCommentTrivia: - textChanges.push.apply(textChanges, toggleMultilineComment(fileName, { end: commentRange.end, pos: commentRange.pos + 1 }, false)); + textChanges.push.apply(textChanges, toggleMultilineComment(fileName, { end: commentRange.end, pos: commentRange.pos + 1 }, /*insertComment*/ false)); } i = commentRange.end + 1; diff --git a/src/services/shims.ts b/src/services/shims.ts index 00ace356119..15b0c1cb488 100644 --- a/src/services/shims.ts +++ b/src/services/shims.ts @@ -278,10 +278,10 @@ namespace ts { getEmitOutput(fileName: string): string; getEmitOutputObject(fileName: string): EmitOutput; - toggleLineComment(fileName: string, textChange: ts.TextRange): string; - toggleMultilineComment(fileName: string, textChange: ts.TextRange): string; - commentSelection(fileName: string, textChange: ts.TextRange): string; - uncommentSelection(fileName: string, textChange: ts.TextRange): string; + toggleLineComment(fileName: string, textChange: TextRange): string; + toggleMultilineComment(fileName: string, textChange:TextRange): string; + commentSelection(fileName: string, textChange: TextRange): string; + uncommentSelection(fileName: string, textChange: TextRange): string; } export interface ClassifierShim extends Shim { @@ -1072,28 +1072,28 @@ namespace ts { this.logPerformance) as EmitOutput; } - public toggleLineComment(fileName: string, textRange: ts.TextRange): string { + public toggleLineComment(fileName: string, textRange: TextRange): string { return this.forwardJSONCall( `toggleLineComment('${fileName}', '${JSON.stringify(textRange)}')`, () => this.languageService.toggleLineComment(fileName, textRange) ); } - public toggleMultilineComment(fileName: string, textRange: ts.TextRange): string { + public toggleMultilineComment(fileName: string, textRange: TextRange): string { return this.forwardJSONCall( `toggleMultilineComment('${fileName}', '${JSON.stringify(textRange)}')`, () => this.languageService.toggleMultilineComment(fileName, textRange) ); } - public commentSelection(fileName: string, textRange: ts.TextRange): string { + public commentSelection(fileName: string, textRange: TextRange): string { return this.forwardJSONCall( `commentSelection('${fileName}', '${JSON.stringify(textRange)}')`, () => this.languageService.commentSelection(fileName, textRange) ); } - public uncommentSelection(fileName: string, textRange: ts.TextRange): string { + public uncommentSelection(fileName: string, textRange: TextRange): string { return this.forwardJSONCall( `uncommentSelection('${fileName}', '${JSON.stringify(textRange)}')`, () => this.languageService.uncommentSelection(fileName, textRange) diff --git a/src/services/utilities.ts b/src/services/utilities.ts index e7642cb80f2..5f936a68e09 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1332,9 +1332,11 @@ namespace ts { || node.kind === SyntaxKind.OpenBraceToken || node.kind === SyntaxKind.SlashToken) { node = node.parent; - } else if (node.kind === SyntaxKind.JsxElement) { + } + else if (node.kind === SyntaxKind.JsxElement) { return position > node.getStart(sourceFile) || isInsideJsxElementRecursion(node.parent); - } else { + } + else { return false; } }