Skip to content

Commit

Permalink
feat: ignore disabling comment
Browse files Browse the repository at this point in the history
  • Loading branch information
lvjiaxuan committed Jan 11, 2024
1 parent efc293e commit da9572e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path'
import type { ExtensionContext } from 'vscode'
import { Position, Range, Selection, SnippetString, TextEditorRevealType, commands, languages, window } from 'vscode'
import { existFileSync, getTextBylines } from './utils'
import { existFileSync, getTextBylines, isDisablingComment } from './utils'
import { blockCommentRegex, getBlockComment, getLineComment, languageId } from './languageDefaults'
import log, { channel } from './log'

Expand All @@ -23,10 +23,12 @@ const disableSelection = commands.registerCommand('eslint-disable.disable', () =

const insertRules = new Set(selectionDiagnostics.map(item => typeof item.code === 'object' ? item.code.value : item.code))

const lineComment = getLineComment(activeTextEditor.document.languageId)
const blockComment = getBlockComment(activeTextEditor.document.languageId)
const languageId = activeTextEditor.document.languageId

const lineComment = getLineComment(languageId)
const blockComment = getBlockComment(languageId)

if (selection.isSingleLine) {
if (selection.isSingleLine && !isDisablingComment(text, languageId)) {
// Insert at previous line.
void activeTextEditor.insertSnippet(
new SnippetString(`${lineComment} eslint-disable-next-line \${1:${[...insertRules].join(', ')}}\n`),
Expand Down
17 changes: 17 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import fs from 'node:fs'
import vscode from 'vscode'
import { getBlockComment, getLineComment } from './languageDefaults'
import log from './log'

export function getTextBylines(startLine: number, endLine?: number) {
return vscode.window.activeTextEditor?.document.getText(
Expand All @@ -20,3 +22,18 @@ export function existFileSync(path: string) {
catch {}
return false
}

/**
* use to single line.
*/
export function isDisablingComment(text: string, languageId: string) {
const blockLike = `${getBlockComment(languageId)[0]} eslint-disable`
const lineLike = `${getLineComment(languageId)} eslint-disable`

if (text.startsWith(blockLike) || text.startsWith(lineLike)) {
log('Your selected line is a disabling comment.')
return false
}

return true
}

0 comments on commit da9572e

Please sign in to comment.