Skip to content

Commit c102135

Browse files
committed
feat(config): add createCategoryPlugin API (#64)
1 parent 952e862 commit c102135

File tree

7 files changed

+43
-4
lines changed

7 files changed

+43
-4
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export default defineConfig({
133133
},
134134
plugins: [
135135
({ typescript: ts }) => ({
136-
resolveDiagnostics(_fileName, diagnostics) {
136+
resolveDiagnostics(file, diagnostics) {
137137
for (const diagnostic of diagnostics) {
138138
if (diagnostic.code === 'no-console') {
139139
diagnostic.category = ts.DiagnosticCategory.Error;
@@ -170,7 +170,7 @@ You can also lint multiple projects at once:
170170

171171
```sh
172172
npx tsslint --project packages/*/tsconfig.json
173-
npx tsslint --project packages/pkg-a/tsconfig.json packages/pkg-b/tsconfig.json
173+
npx tsslint --project {packages/pkg-a/tsconfig.json,packages/pkg-b/tsconfig.json}
174174
```
175175

176176
This command will run the linter on all TypeScript projects located in the subdirectories of the `packages` directory. Each subdirectory should contain a `tsconfig.json` file defining a TypeScript project. Any linting errors will be output to the console.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"release:base": "lerna publish --exact --force-publish --yes --sync-workspace-lock",
1111
"release:vscode": "cd packages/vscode && npm run publish:all",
1212
"start": "node packages/cli/bin/tsslint.js",
13-
"lint": "node packages/cli/bin/tsslint.js --project packages/*/tsconfig.json",
13+
"lint": "node packages/cli/bin/tsslint.js --project {tsconfig.json,packages/*/tsconfig.json}",
1414
"lint:fix": "npm run lint -- --fix",
1515
"lint:fixtures": "node packages/cli/bin/tsslint.js --project fixtures/*/tsconfig.json --vue-project fixtures/meta-frameworks-support/tsconfig.json --mdx-project fixtures/meta-frameworks-support/tsconfig.json --astro-project fixtures/meta-frameworks-support/tsconfig.json --ts-macro-project fixtures/meta-frameworks-support/tsconfig.json"
1616
},

packages/config/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from '@tsslint/types';
2+
export { create as createCategoryPlugin } from './lib/plugins/category.js';
23
export { create as createDiagnosticsPlugin } from './lib/plugins/diagnostics.js';
34
export { create as createIgnorePlugin } from './lib/plugins/ignore.js';
45

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { Plugin } from '@tsslint/types';
2+
import type * as ts from 'typescript';
3+
4+
import minimatch = require('minimatch');
5+
6+
export function create(config: Record<string, ts.DiagnosticCategory>, source = 'tsslint'): Plugin {
7+
const matchCache = new Map<string | number, ts.DiagnosticCategory | undefined>();
8+
return () => ({
9+
resolveDiagnostics(_file, diagnostics) {
10+
for (const diagnostic of diagnostics) {
11+
if (diagnostic.source !== source) {
12+
continue;
13+
}
14+
const category = match(diagnostic.code.toString());
15+
if (category !== undefined) {
16+
diagnostic.category = category;
17+
}
18+
}
19+
return diagnostics;
20+
}
21+
});
22+
function match(code: string) {
23+
if (matchCache.has(code)) {
24+
return matchCache.get(code);
25+
}
26+
for (const pattern in config) {
27+
const category = config[pattern];
28+
if (minimatch.minimatch(code, pattern, { dot: true })) {
29+
matchCache.set(code, category);
30+
return category;
31+
}
32+
}
33+
}
34+
}

packages/config/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
"dependencies": {
1515
"@tsslint/types": "2.0.2",
16+
"minimatch": "^10.0.1",
1617
"ts-api-utils": "^2.0.0"
1718
}
1819
}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"compilerOptions": {
44
"noEmit": true,
55
},
6-
"include": [ ],
6+
"include": [ "tsslint.config.ts" ],
77
"references": [
88
{ "path": "./packages/cli/tsconfig.json" },
99
{ "path": "./packages/tslint/tsconfig.json" },

0 commit comments

Comments
 (0)