johnsoncodehk / tsslint

🔋⚡️ The fastest and lightest TypeScript semantic linting solution
MIT License
287 stars 2 forks source link

[feat] support disable comment #12

Open tjx666 opened 4 months ago

tjx666 commented 4 months ago

like eslint, we can disable by:

// tssint-disable-next-line
queryClient.invalidateQueries(['useXXX']);
johnsoncodehk commented 4 months ago

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));
        },
    });
}
tjx666 commented 4 months ago

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

johnsoncodehk commented 4 months ago

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.