Skip to content

Commit

Permalink
Setting for comment style when disabling a line (#1499)
Browse files Browse the repository at this point in the history
* Contributing nits

* Add disable comment style setting

* Remove unnecessary falsy checks

* Add default comment style setting

* Use getDisableRuleEditInsertionIndex with block comments

* Explicit commentTags types

* Use const

* Remove unused type

Co-authored-by: Dirk Bäumer <dirkb@microsoft.com>
  • Loading branch information
MariaSolOs and dbaeumer authored Sep 7, 2022
1 parent 0176ec0 commit 3178430
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 22 deletions.
1 change: 1 addition & 0 deletions $shared/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type CodeActionSettings = {
disableRuleComment: {
enable: boolean;
location: 'separateLine' | 'sameLine';
commentStyle: 'line' | 'block';
};
showDocumentation: {
enable: boolean;
Expand Down
2 changes: 1 addition & 1 deletion client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ export namespace ESLintClient {
workingDirectory: undefined,
workspaceFolder: undefined,
codeAction: {
disableRuleComment: config.get('codeAction.disableRuleComment', { enable: true, location: 'separateLine' as 'separateLine' }),
disableRuleComment: config.get('codeAction.disableRuleComment', { enable: true, location: 'separateLine' as const, commentStyle: 'line' as const }),
showDocumentation: config.get('codeAction.showDocumentation', { enable: true })
}
};
Expand Down
8 changes: 4 additions & 4 deletions contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

[![Build Status](https://travis-ci.org/Microsoft/vscode-eslint.svg?branch=master)](https://travis-ci.org/Microsoft/vscode-eslint)

Extension to integrate [ESLint](http://eslint.org/) into VSCode.
Extension to integrate [ESLint](http://eslint.org/) into VS Code.

## Development setup
- Use git with symbolic link support enabled
- run npm install
- open VS Code
- Run `npm install`
- Open VS Code
- Run the `watch` task to compile the client and server
- To run/debug the extension use the `Launch Extension` launch configuration
- to debug the server use the `Attach to Server` launch configuration
- To debug the server use the `Attach to Server` launch configuration
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@
"type": "object",
"default": {
"enable": true,
"location": "separateLine"
"location": "separateLine",
"commentStyle": "line"
},
"properties": {
"enable": {
Expand All @@ -372,6 +373,15 @@
],
"default": "separateLine",
"description": "Configure the disable rule code action to insert the comment on the same line or a new line."
},
"commentStyle": {
"type": "string",
"enum": [
"line",
"block"
],
"default": "line",
"definition": "The comment style to use when disabling a rule on a specific line."
}
},
"additionalProperties": false,
Expand Down
77 changes: 61 additions & 16 deletions server/src/eslintServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,41 +657,86 @@ messageQueue.registerRequest(CodeActionRequest.type, async (params) => {
return action;
}

function getDisableRuleEditInsertionIndex(line: string): number {
function getDisableRuleEditInsertionIndex(line: string, commentTags: string | [string, string]): number {
let charIndex = line.indexOf('--');

if (charIndex < 0) {
return line.length;
}
while (charIndex > 1 && line[charIndex - 1] === ' ') {
charIndex--;
if (typeof commentTags === 'string') {
return line.length;
} else { // commentTags is an array containing the block comment closing and opening tags
charIndex = line.indexOf(commentTags[1]);
while (charIndex > 0 && line[charIndex - 1] === ' ') {
charIndex--;
}
}
} else {
while (charIndex > 1 && line[charIndex - 1] === ' ') {
charIndex--;
}
}

return charIndex;
}

function createDisableLineTextEdit(textDocument: TextDocument, editInfo: Problem, indentationText: string): TextEdit {
const lineComment = LanguageDefaults.getLineComment(textDocument.languageId);
const blockComment = LanguageDefaults.getBlockComment(textDocument.languageId);

// If the concerned line is not the first line of the file
if (editInfo.line - 1 > 0) {
// Check previous line if there is a eslint-disable-next-line comment already present
// Check previous line if there is a eslint-disable-next-line comment already present.
const prevLine = textDocument.getText(Range.create(Position.create(editInfo.line - 2, 0), Position.create(editInfo.line - 2, uinteger.MAX_VALUE)));
const matched = prevLine && prevLine.match(new RegExp(`${LanguageDefaults.getLineComment(textDocument.languageId)} eslint-disable-next-line`));
if (matched && matched.length) {
const insertionIndex = getDisableRuleEditInsertionIndex(prevLine);

// For consistency, we ignore the settings here and use the comment style from that
// specific line.
const matchedLineDisable = new RegExp(`${lineComment} eslint-disable-next-line`).test(prevLine);
if (matchedLineDisable) {
const insertionIndex = getDisableRuleEditInsertionIndex(prevLine, lineComment);
return TextEdit.insert(Position.create(editInfo.line - 2, insertionIndex), `, ${editInfo.ruleId}`);
}

const matchedBlockDisable = new RegExp(`${blockComment[0]} eslint-disable-next-line`).test(prevLine);
if (matchedBlockDisable) {
const insertionIndex = getDisableRuleEditInsertionIndex(prevLine, blockComment);
return TextEdit.insert(Position.create(editInfo.line - 2, insertionIndex), `, ${editInfo.ruleId}`);
}
}

// We're creating a new disabling comment. Use the comment style given in settings.
const commentStyle = settings.codeAction.disableRuleComment.commentStyle;
let disableRuleContent: string;
if (commentStyle === 'block') {
disableRuleContent = `${indentationText}${blockComment[0]} eslint-disable-next-line ${editInfo.ruleId} ${blockComment[1]}${EOL}`;
} else { // commentStyle === 'line'
disableRuleContent = `${indentationText}${lineComment} eslint-disable-next-line ${editInfo.ruleId}${EOL}`;
}

return TextEdit.insert(Position.create(editInfo.line - 1, 0), `${indentationText}${LanguageDefaults.getLineComment(textDocument.languageId)} eslint-disable-next-line ${editInfo.ruleId}${EOL}`);
return TextEdit.insert(Position.create(editInfo.line - 1, 0), disableRuleContent);
}

function createDisableSameLineTextEdit(textDocument: TextDocument, editInfo: Problem): TextEdit {
const currentLine = textDocument.getText(Range.create(Position.create(editInfo.line - 1, 0), Position.create(editInfo.line -1, uinteger.MAX_VALUE)));
const matched = currentLine && new RegExp(`${LanguageDefaults.getLineComment(textDocument.languageId)} eslint-disable-line`).exec(currentLine);

const disableCommentExists = matched && matched.length;
const disableRuleContent = disableCommentExists ? `, ${editInfo.ruleId}` : ` ${LanguageDefaults.getLineComment(textDocument.languageId)} eslint-disable-line ${editInfo.ruleId}`;
const insertionIndex = disableCommentExists ? getDisableRuleEditInsertionIndex(currentLine) : uinteger.MAX_VALUE;
const lineComment = LanguageDefaults.getLineComment(textDocument.languageId);
const blockComment = LanguageDefaults.getBlockComment(textDocument.languageId);
const currentLine = textDocument.getText(Range.create(Position.create(editInfo.line - 1, 0), Position.create(editInfo.line - 1, uinteger.MAX_VALUE)));
let disableRuleContent: string;
let insertionIndex: number;

// Check if there's already a disabling comment. If so, we ignore the settings here
// and use the comment style from that specific line.
const matchedLineDisable = new RegExp(`${lineComment} eslint-disable-line`).test(currentLine);
const matchedBlockDisable = new RegExp(`${blockComment[0]} eslint-disable-line`).test(currentLine);
if (matchedLineDisable) {
disableRuleContent = `, ${editInfo.ruleId}`;
insertionIndex = getDisableRuleEditInsertionIndex(currentLine, lineComment);
} else if (matchedBlockDisable) {
disableRuleContent = `, ${editInfo.ruleId}`;
insertionIndex = getDisableRuleEditInsertionIndex(currentLine, blockComment);
} else {
// We're creating a new disabling comment.
const commentStyle = settings.codeAction.disableRuleComment.commentStyle;
disableRuleContent = commentStyle === 'line' ? ` ${lineComment} eslint-disable-line ${editInfo.ruleId}` : ` ${blockComment[0]} eslint-disable-line ${editInfo.ruleId} ${blockComment[1]}`;
insertionIndex = uinteger.MAX_VALUE;
}

return TextEdit.insert(Position.create(editInfo.line - 1, insertionIndex), disableRuleContent);
}
Expand Down

0 comments on commit 3178430

Please sign in to comment.