@danilqa/eslint-plugin-ts-pattern 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +73 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/rules/index.d.ts +6 -0
- package/dist/rules/index.d.ts.map +1 -0
- package/dist/rules/index.js +5 -0
- package/dist/rules/index.js.map +1 -0
- package/dist/rules/prefer-match-on-union.d.ts +5 -0
- package/dist/rules/prefer-match-on-union.d.ts.map +1 -0
- package/dist/rules/prefer-match-on-union.js +63 -0
- package/dist/rules/prefer-match-on-union.js.map +1 -0
- package/dist/utils/create-rule.d.ts +5 -0
- package/dist/utils/create-rule.d.ts.map +1 -0
- package/dist/utils/create-rule.js +3 -0
- package/dist/utils/create-rule.js.map +1 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Daniil Sitdikov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# @danilqa/eslint-plugin-ts-pattern
|
|
2
|
+
|
|
3
|
+
An ESLint plugin that warns when you branch on a string-literal union type with `if`/`else`, and points you at [`ts-pattern`](https://github.com/gvergnaud/ts-pattern)'s exhaustive `match` instead.
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
type State = 'failed' | 'success' | 'pending'
|
|
9
|
+
|
|
10
|
+
interface Payment {
|
|
11
|
+
state: State
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function describe(payment: Payment) {
|
|
15
|
+
if (payment.state === 'failed') return 'a'
|
|
16
|
+
// …
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
When `State` later grows a `'refunded'` variant, the compiler does not flag this `if`. The branch silently misses the new case — and so does every other `if` block scattered across the codebase.
|
|
21
|
+
|
|
22
|
+
## Solution
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { match } from 'ts-pattern'
|
|
26
|
+
|
|
27
|
+
function describe(payment: Payment) {
|
|
28
|
+
return match(payment.state)
|
|
29
|
+
.with('failed', () => 'a')
|
|
30
|
+
.with('success', () => 'b')
|
|
31
|
+
.with('pending', () => 'c')
|
|
32
|
+
.exhaustive()
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`.exhaustive()` makes a missing case a **compile-time error**. Adding `'refunded'` to `State` immediately fails the build everywhere it isn't handled.
|
|
37
|
+
|
|
38
|
+
## Install
|
|
39
|
+
|
|
40
|
+
```sh
|
|
41
|
+
npm i --dev @danilqa/eslint-plugin-ts-pattern
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
yarn add -D @danilqa/eslint-plugin-ts-pattern
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
pnpm add -D @danilqa/eslint-plugin-ts-pattern
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Usage (flat config)
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
import tsPattern from '@danilqa/eslint-plugin-ts-pattern'
|
|
56
|
+
|
|
57
|
+
export default [
|
|
58
|
+
{
|
|
59
|
+
// ...
|
|
60
|
+
plugins: { 'ts-pattern': tsPattern },
|
|
61
|
+
rules: {
|
|
62
|
+
'ts-pattern/prefer-match-on-union': 'warn',
|
|
63
|
+
},
|
|
64
|
+
// ...
|
|
65
|
+
},
|
|
66
|
+
]
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Rules
|
|
70
|
+
|
|
71
|
+
| Rule | Description |
|
|
72
|
+
| ----------------------- | ------------------------------------------------------------------------ |
|
|
73
|
+
| `prefer-match-on-union` | Warn on `if (x === 'literal')` when `x` has a string-literal union type. |
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
declare const plugin: {
|
|
2
|
+
meta: {
|
|
3
|
+
name: string;
|
|
4
|
+
version: string;
|
|
5
|
+
};
|
|
6
|
+
rules: {
|
|
7
|
+
'prefer-match-on-union': import("@typescript-eslint/utils/ts-eslint").RuleModule<"preferMatch", [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener> & {
|
|
8
|
+
name: string;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
export default plugin;
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,QAAA,MAAM,MAAM;;;;;;;;;;CAMX,CAAA;AAED,eAAe,MAAM,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAE/B,MAAM,MAAM,GAAG;IACb,IAAI,EAAE;QACJ,IAAI,EAAE,mCAAmC;QACzC,OAAO,EAAE,OAAO;KACjB;IACD,KAAK;CACN,CAAA;AAED,eAAe,MAAM,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/rules/index.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,KAAK;;;;CAEjB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/rules/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAE5D,MAAM,CAAC,MAAM,KAAK,GAAG;IACnB,uBAAuB,EAAE,kBAAkB;CAC5C,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prefer-match-on-union.d.ts","sourceRoot":"","sources":["../../src/rules/prefer-match-on-union.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAA;AA4BtD,eAAO,MAAM,kBAAkB;;CA2C7B,CAAA"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
|
+
import { createRule } from '../utils/create-rule.js';
|
|
3
|
+
function isStringLiteralUnion(type) {
|
|
4
|
+
if (!type.isUnion())
|
|
5
|
+
return false;
|
|
6
|
+
const constituents = type.types;
|
|
7
|
+
if (constituents.length < 2)
|
|
8
|
+
return false;
|
|
9
|
+
return constituents.every((t) => t.isStringLiteral());
|
|
10
|
+
}
|
|
11
|
+
function getNonLiteralOperand(node) {
|
|
12
|
+
const { left, right } = node;
|
|
13
|
+
if (left.type === 'PrivateIdentifier')
|
|
14
|
+
return null;
|
|
15
|
+
const leftIsStringLit = left.type === 'Literal' && typeof left.value === 'string';
|
|
16
|
+
const rightIsStringLit = right.type === 'Literal' && typeof right.value === 'string';
|
|
17
|
+
if (leftIsStringLit && !rightIsStringLit)
|
|
18
|
+
return right;
|
|
19
|
+
if (rightIsStringLit && !leftIsStringLit)
|
|
20
|
+
return left;
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
export const preferMatchOnUnion = createRule({
|
|
24
|
+
name: 'prefer-match-on-union',
|
|
25
|
+
meta: {
|
|
26
|
+
type: 'suggestion',
|
|
27
|
+
docs: {
|
|
28
|
+
description: "Warn when `if` or a ternary branches on a string-literal union type via `===`/`!==`. Prefer ts-pattern's `match(...).exhaustive()`.",
|
|
29
|
+
},
|
|
30
|
+
schema: [],
|
|
31
|
+
messages: {
|
|
32
|
+
preferMatch: 'Avoid `===`/`!==` checks on string-literal union types. Use `match(value).with(...).exhaustive()` from ts-pattern so missing cases are caught at compile time.',
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
defaultOptions: [],
|
|
36
|
+
create(context) {
|
|
37
|
+
const services = ESLintUtils.getParserServices(context);
|
|
38
|
+
const checker = services.program.getTypeChecker();
|
|
39
|
+
function check(test) {
|
|
40
|
+
if (test.type !== 'BinaryExpression')
|
|
41
|
+
return;
|
|
42
|
+
if (test.operator !== '===' && test.operator !== '!==')
|
|
43
|
+
return;
|
|
44
|
+
const target = getNonLiteralOperand(test);
|
|
45
|
+
if (!target)
|
|
46
|
+
return;
|
|
47
|
+
const tsNode = services.esTreeNodeToTSNodeMap.get(target);
|
|
48
|
+
const type = checker.getTypeAtLocation(tsNode);
|
|
49
|
+
if (!isStringLiteralUnion(type))
|
|
50
|
+
return;
|
|
51
|
+
context.report({ node: test, messageId: 'preferMatch' });
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
IfStatement(node) {
|
|
55
|
+
check(node.test);
|
|
56
|
+
},
|
|
57
|
+
ConditionalExpression(node) {
|
|
58
|
+
check(node.test);
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
//# sourceMappingURL=prefer-match-on-union.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prefer-match-on-union.js","sourceRoot":"","sources":["../../src/rules/prefer-match-on-union.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAA;AAGtD,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AAIjD,SAAS,oBAAoB,CAAC,IAAa;IACzC,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE;QAAE,OAAO,KAAK,CAAA;IACjC,MAAM,YAAY,GAAG,IAAI,CAAC,KAAK,CAAA;IAC/B,IAAI,YAAY,CAAC,MAAM,GAAG,CAAC;QAAE,OAAO,KAAK,CAAA;IACzC,OAAO,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,eAAe,EAAE,CAAC,CAAA;AACvD,CAAC;AAED,SAAS,oBAAoB,CAC3B,IAA+B;IAE/B,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,IAAI,CAAA;IAC5B,IAAI,IAAI,CAAC,IAAI,KAAK,mBAAmB;QAAE,OAAO,IAAI,CAAA;IAClD,MAAM,eAAe,GACnB,IAAI,CAAC,IAAI,KAAK,SAAS,IAAI,OAAO,IAAI,CAAC,KAAK,KAAK,QAAQ,CAAA;IAC3D,MAAM,gBAAgB,GACpB,KAAK,CAAC,IAAI,KAAK,SAAS,IAAI,OAAO,KAAK,CAAC,KAAK,KAAK,QAAQ,CAAA;IAC7D,IAAI,eAAe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,CAAC,IAAI,CAAC,CAAA;YAClB,CAAC;YACD,qBAAqB,CAAC,IAAI;gBACxB,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;YAClB,CAAC;SACF,CAAA;IACH,CAAC;CACF,CAAC,CAAA"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
2
|
+
export declare const createRule: <Options extends readonly unknown[], MessageIds extends string>({ meta, name, ...rule }: Readonly<ESLintUtils.RuleWithMetaAndName<Options, MessageIds, unknown>>) => ESLintUtils.RuleModule<MessageIds, Options, unknown, ESLintUtils.RuleListener> & {
|
|
3
|
+
name: string;
|
|
4
|
+
};
|
|
5
|
+
//# sourceMappingURL=create-rule.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-rule.d.ts","sourceRoot":"","sources":["../../src/utils/create-rule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAA;AAEtD,eAAO,MAAM,UAAU;;CAGtB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-rule.js","sourceRoot":"","sources":["../../src/utils/create-rule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAA;AAEtD,MAAM,CAAC,MAAM,UAAU,GAAG,WAAW,CAAC,WAAW,CAC/C,CAAC,IAAI,EAAE,EAAE,CACP,4EAA4E,IAAI,KAAK,CACxF,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@danilqa/eslint-plugin-ts-pattern",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "ESLint plugin: warn when `if` is used on string-literal union types instead of ts-pattern's exhaustive `match`",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"README.md"
|
|
21
|
+
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=20"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json --resolve-full-paths",
|
|
27
|
+
"typecheck": "tsc --noEmit",
|
|
28
|
+
"test": "node --import tsx --test 'tests/**/*.test.ts'",
|
|
29
|
+
"test:watch": "node --import tsx --test --watch 'tests/**/*.test.ts'",
|
|
30
|
+
"lint": "eslint .",
|
|
31
|
+
"lint:fix": "eslint . --fix",
|
|
32
|
+
"format": "prettier --write .",
|
|
33
|
+
"format:check": "prettier --check .",
|
|
34
|
+
"check": "pnpm format:check && pnpm lint && pnpm typecheck && pnpm test",
|
|
35
|
+
"prepare": "husky",
|
|
36
|
+
"prepublishOnly": "pnpm check && pnpm build"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"eslint": ">=9",
|
|
40
|
+
"typescript": ">=5"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/node": "25.6.2",
|
|
44
|
+
"@typescript-eslint/parser": "8.59.2",
|
|
45
|
+
"@typescript-eslint/rule-tester": "8.59.2",
|
|
46
|
+
"@typescript-eslint/utils": "8.59.2",
|
|
47
|
+
"eslint": "10.3.0",
|
|
48
|
+
"eslint-config-prettier": "10.1.8",
|
|
49
|
+
"husky": "9.1.7",
|
|
50
|
+
"prettier": "3.8.3",
|
|
51
|
+
"tsc-alias": "1.8.17",
|
|
52
|
+
"tsx": "4.21.0",
|
|
53
|
+
"typescript": "6.0.3",
|
|
54
|
+
"typescript-eslint": "8.59.2"
|
|
55
|
+
},
|
|
56
|
+
"packageManager": "pnpm@11.0.9"
|
|
57
|
+
}
|