Skip to content

Commit 0809ad3

Browse files
Kamil Sobolsdstolworthy
andauthored
build: custom no catch block rule (#915)
* build: custom no catch block rule * comment * fix that * fix that * pr feedback * Update packages/eslint-rules/src/rules/no_empty_catch.ts Co-authored-by: Spencer Stolworthy <[email protected]> --------- Co-authored-by: Spencer Stolworthy <[email protected]>
1 parent cff26fe commit 0809ad3

File tree

11 files changed

+449
-102
lines changed

11 files changed

+449
-102
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/platform-core': patch
3+
---
4+
5+
fix empty catch block

.eslintrc.cjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = {
77
extends: [
88
'eslint:recommended',
99
'plugin:@typescript-eslint/recommended',
10+
'plugin:amplify-backend-rules/recommended',
1011
'plugin:jsdoc/recommended-typescript-error',
1112
'plugin:promise/recommended',
1213
'prettier',
@@ -29,6 +30,7 @@ module.exports = {
2930
},
3031
plugins: [
3132
'@typescript-eslint',
33+
'amplify-backend-rules',
3234
'unicorn',
3335
'jsdoc',
3436
'import',

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"eslint": "^8.38.0",
7070
"eslint-config-prettier": "^8.8.0",
7171
"eslint-config-xo-typescript": "^0.57.0",
72+
"eslint-plugin-amplify-backend-rules": "^0.0.1",
7273
"eslint-plugin-check-file": "^2.6.2",
7374
"eslint-plugin-import": "^2.27.5",
7475
"eslint-plugin-jsdoc": "^43.0.6",

packages/eslint-rules/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# ES lint rules
2+
3+
This package contains custom linter rules used to validate amplify backend codebase.

packages/eslint-rules/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "eslint-plugin-amplify-backend-rules",
3+
"private": true,
4+
"version": "0.0.1",
5+
"type": "commonjs",
6+
"main": "lib/index.js",
7+
"dependencies": {
8+
"@typescript-eslint/utils": "^6.18.1"
9+
},
10+
"devDependencies": {
11+
"@typescript-eslint/rule-tester": "^6.18.1"
12+
},
13+
"license": "Apache-2.0"
14+
}

packages/eslint-rules/src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { rule } from './rules/no_empty_catch.js';
2+
3+
export const rules: Record<string, unknown> = {
4+
'no-empty-catch': rule,
5+
};
6+
7+
export const configs = {
8+
recommended: {
9+
plugins: ['amplify-backend-rules'],
10+
rules: {
11+
'amplify-backend-rules/no-empty-catch': 'error',
12+
},
13+
},
14+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import * as nodeTest from 'node:test';
2+
import { RuleTester } from '@typescript-eslint/rule-tester';
3+
import { rule } from './no_empty_catch.js';
4+
5+
RuleTester.afterAll = nodeTest.after;
6+
// See https://typescript-eslint.io/packages/rule-tester/#with-specific-frameworks
7+
// Node test runner methods return promises which are not relevant in the context of testing.
8+
// We do ignore them in other places with void keyword.
9+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
10+
RuleTester.it = nodeTest.it;
11+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
12+
RuleTester.describe = nodeTest.describe;
13+
14+
const ruleTester = new RuleTester();
15+
16+
ruleTester.run('no-empty-catch', rule, {
17+
valid: [
18+
'try {} catch (e) { console.log(e); }',
19+
'try {} catch (e) { \n /* some comment*/ \n console.log(e); }',
20+
'try {} catch (e) { \n // some comment \n console.log(e); }',
21+
],
22+
invalid: [
23+
{
24+
code: 'try {} catch (e) { /* some comment */ }',
25+
errors: [
26+
{
27+
messageId: 'noEmptyCatch',
28+
},
29+
],
30+
},
31+
{
32+
code: 'try {} catch (e) { \n // some comment \n }',
33+
errors: [
34+
{
35+
messageId: 'noEmptyCatch',
36+
},
37+
],
38+
},
39+
{
40+
code: 'try {} catch (e) { }',
41+
errors: [
42+
{
43+
messageId: 'noEmptyCatch',
44+
},
45+
],
46+
},
47+
],
48+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { ESLintUtils } from '@typescript-eslint/utils';
2+
3+
/**
4+
* This rule flags empty catch blocks. Even if they contain comments.
5+
*
6+
* This rule differs from built in https://github.com/eslint/eslint/blob/main/lib/rules/no-empty.js
7+
* in such a way that it uses typescript-eslint and typescript AST
8+
* which does not include comments as statements in catch clause body block.
9+
*/
10+
export const rule = ESLintUtils.RuleCreator.withoutDocs({
11+
create(context) {
12+
return {
13+
// This naming comes from @typescript-eslint/utils types.
14+
// eslint-disable-next-line @typescript-eslint/naming-convention
15+
CatchClause(node) {
16+
if (node.body.body.length === 0) {
17+
context.report({
18+
messageId: 'noEmptyCatch',
19+
node,
20+
});
21+
}
22+
},
23+
};
24+
},
25+
meta: {
26+
docs: {
27+
description: 'Catch block should contain error-handling logic.',
28+
},
29+
messages: {
30+
noEmptyCatch: 'Catch block must not be empty.',
31+
},
32+
type: 'problem',
33+
schema: [],
34+
},
35+
defaultOptions: [],
36+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": { "rootDir": "src", "outDir": "lib", "allowJs": true },
4+
"references": []
5+
}

0 commit comments

Comments
 (0)