tsslint icon indicating copy to clipboard operation
tsslint copied to clipboard

[feat] support disable comment

Open tjx666 opened this issue 1 year ago • 3 comments

like eslint, we can disable by:

// tssint-disable-next-line
queryClient.invalidateQueries(['useXXX']);

tjx666 avatar Jun 15 '24 14:06 tjx666

You can do this through the plugin API.

import { Plugin, defineConfig } from '@tsslint/config';

export default defineConfig({
	plugins: [
		createIngorePlugin(/\/\/ tsslint-disable-next-line\n/g),
		createIngorePlugin(/\/\/ eslint-disable-next-line\n/g),
	]
});

function createIngorePlugin(pattern: RegExp): Plugin {
	return ({ languageService }) => ({
		resolveDiagnostics(fileName, results) {
			const sourceFile = languageService.getProgram()?.getSourceFile(fileName);
			if (!sourceFile) {
				return results;
			}
			const comments = [...sourceFile.text.matchAll(pattern)];
			const lines = new Set(comments.map(comment => sourceFile.getLineAndCharacterOfPosition(comment.index).line));
			return results.filter(error => error.source !== 'tsslint' || !lines.has(sourceFile.getLineAndCharacterOfPosition(error.start).line - 1));
		},
	});
}

johnsoncodehk avatar Jun 15 '24 15:06 johnsoncodehk

@johnsoncodehk works fine, but I think this should be builtin feature.

tjx666 avatar Jun 15 '24 15:06 tjx666

This is not yet a built-in feature, as projects migrating from ESLint may still need to support eslint-disable-next-line instead of tsslint-disable-next-line/@tsslint-ignore, so this syntax needs to be available configuration.

I will keep this issue open.

johnsoncodehk avatar Jun 16 '24 04:06 johnsoncodehk

@johnsoncodehk

It would be fine to also support use tslint-disable-next-line, because some useful eslint rules which used to check unused eslint disable comments will report error to it:

image

tjx666 avatar Dec 03 '24 08:12 tjx666

for anyone don't know how to config the plugins, you can use like following:

image

tjx666 avatar Dec 03 '24 08:12 tjx666

@johnsoncodehk the unused-disable-next-line check is not a good idea for eslint & tslint mixed project.

image

tjx666 avatar Dec 03 '24 13:12 tjx666

@tjx666 it can be disabled by createDisableNextLinePlugin(false).

johnsoncodehk avatar Dec 03 '24 15:12 johnsoncodehk

@johnsoncodehk I see your latest implementation add 2 plugin arguments, so, I use following now(I just copy you plugin code because you had not publish the latest code yet):

plugins: [
        createDisableNextLinePlugin(
            true,
            new RegExp(/\/\/\s*tslint-disable-next-line\b[ \t]*(?<ruleId>\S*)\b/g),
        ),
        createShowDocsActionPlugin(),
],

tjx666 avatar Dec 03 '24 15:12 tjx666

plugins: [
        createDisableNextLinePlugin(
            true,
            new RegExp(/\/\/\s*tslint-disable-next-line\b[ \t]*(?<ruleId>\S*)\b/g),
        ),
        createShowDocsActionPlugin(),
],

This is now available in 1.3.0.

johnsoncodehk avatar Dec 05 '24 08:12 johnsoncodehk