Skip to content

Commit b7e37ed

Browse files
committed
v0.1.0
1 parent a4854e3 commit b7e37ed

File tree

10 files changed

+2776
-1
lines changed

10 files changed

+2776
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,6 @@ dist
102102

103103
# TernJS port file
104104
.tern-port
105+
106+
# build files
107+
lib/

.npmignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
src/
2+
tests/
3+
4+
.gitignore
5+
.swcrc
6+
pnpm-lock.yaml
7+
tsconfig.json

.swcrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"$schema": "http://json.schemastore.org/swcrc",
3+
"module": {
4+
"type": "commonjs"
5+
},
6+
"jsc": {
7+
"parser": {
8+
"syntax": "typescript",
9+
"tsx": false
10+
}
11+
}
12+
}

README.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,71 @@
11
# eslint-plugin-try-catch-failsafe
2-
Make sure to use try-catch to wrap some dangerous actions!
2+
3+
Make sure to use try-catch to wrap up some dangerous actions!
4+
5+
## Installation
6+
7+
Use package manager to install this plugin.
8+
9+
```bash
10+
npm install --save-dev eslint-plugin-try-catch-failsafe
11+
yarn add -D eslint-plugin-try-catch-failsafe
12+
pnpm add -D eslint-plugin-try-catch-failsafe
13+
```
14+
15+
## Usage
16+
17+
Add this plugin to your ESLint configuration.
18+
19+
```json
20+
{
21+
"plugins": [
22+
"try-catch-failsafe"
23+
]
24+
}
25+
```
26+
27+
## Rules
28+
29+
### try-catch-failsafe/json-parse
30+
31+
- **Default**: `error`
32+
- **Fixable**: `false`
33+
34+
Make sure to use try-catch to wrap up `JSON.parse`. Notice that the `JSON.parse` in `catch` or `finally` block may also throw an error in JavaScript, so we should wrap them up too.
35+
36+
```js
37+
// ❌ Error
38+
JSON.parse('{"foo": "bar"}');
39+
40+
// ❌ Error
41+
try {
42+
// some code that may throw an error
43+
} catch (e) {
44+
JSON.parse('{"foo": "bar"}');
45+
}
46+
47+
// ❌ Error
48+
try {
49+
// some code that may throw an error
50+
} finally {
51+
JSON.parse('{"foo": "bar"}');
52+
}
53+
54+
// ✅ OK
55+
try {
56+
JSON.parse('{"foo": "bar"}');
57+
} catch (e) {
58+
console.error(e);
59+
}
60+
61+
// ✅ OK
62+
try {
63+
// some code that may throw an error
64+
} finally {
65+
try {
66+
JSON.parse('{"foo": "bar"}');
67+
} finally {}
68+
}
69+
```
70+
71+
If you are confident that the `JSON.parse` will not throw an error, you can disable this rule.

package.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "eslint-plugin-try-catch-failsafe",
3+
"version": "0.1.0",
4+
"description": "Make sure to use try-catch to wrap some dangerous actions!",
5+
"main": "lib/index.js",
6+
"scripts": {
7+
"build": "rm -rf lib && swc src -d lib",
8+
"prepack": "pnpm build",
9+
"test": "ts-mocha tests/**/*.spec.ts"
10+
},
11+
"dependencies": {
12+
"requireindex": "^1.2.0"
13+
},
14+
"devDependencies": {
15+
"@swc/cli": "^0.1.59",
16+
"@swc/core": "^1.3.25",
17+
"@types/eslint": "^8.4.10",
18+
"@types/estree": "^1.0.0",
19+
"@types/expect": "^24.3.0",
20+
"@types/mocha": "^10.0.1",
21+
"@types/node": "^18.11.18",
22+
"@types/requireindex": "^1.2.0",
23+
"eslint": "^8.31.0",
24+
"mocha": "^10.2.0",
25+
"ts-mocha": "^10.0.0",
26+
"typescript": "^4.9.4"
27+
},
28+
"repository": {
29+
"type": "git",
30+
"url": "https://github.com/RexSkz/eslint-plugin-try-catch-failsafe.git"
31+
},
32+
"author": "Rex Zeng <rex@rexskz.info>",
33+
"keywords": [
34+
"eslint",
35+
"eslintplugin",
36+
"eslint-plugin",
37+
"try-catch",
38+
"try",
39+
"catch",
40+
"failsafe"
41+
],
42+
"license": "MIT"
43+
}

0 commit comments

Comments
 (0)