@blumintinc/eslint-plugin-blumint 0.1.10 → 0.1.12
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/README.md +15 -12
- package/docs/rules/export-if-in-doubt.md +1 -1
- package/docs/rules/extract-global-constants.md +1 -1
- package/docs/rules/no-async-foreach.md +41 -0
- package/docs/rules/no-conditional-literals-in-jsx.md +27 -0
- package/docs/rules/no-misused-switch-case.md +3 -3
- package/docs/rules/no-useless-fragment.md +23 -0
- package/lib/index.js +10 -1
- package/lib/rules/no-async-foreach.js +37 -0
- package/lib/rules/no-conditional-literals-in-jsx.js +61 -0
- package/lib/rules/no-misused-switch-case.js +2 -2
- package/lib/rules/no-useless-fragment.js +30 -0
- package/lib/rules/prefer-fragment-shorthand.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -56,18 +56,21 @@ Or use the recommended config:
|
|
|
56
56
|
✅ Set in the `recommended` configuration.\
|
|
57
57
|
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).
|
|
58
58
|
|
|
59
|
-
| Name
|
|
60
|
-
|
|
|
61
|
-
| [array-methods-this-context](docs/rules/array-methods-this-context.md)
|
|
62
|
-
| [export-if-in-doubt](docs/rules/export-if-in-doubt.md)
|
|
63
|
-
| [extract-global-constants](docs/rules/extract-global-constants.md)
|
|
64
|
-
| [generic-starts-with-t](docs/rules/generic-starts-with-t.md)
|
|
65
|
-
| [no-async-array-filter](docs/rules/no-async-array-filter.md)
|
|
66
|
-
| [no-
|
|
67
|
-
| [no-
|
|
68
|
-
| [no-
|
|
69
|
-
| [
|
|
70
|
-
| [
|
|
59
|
+
| Name | Description | 💼 | ⚠️ | 🔧 |
|
|
60
|
+
| :----------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------- | :- | :- | :- |
|
|
61
|
+
| [array-methods-this-context](docs/rules/array-methods-this-context.md) | Prevent misuse of Array methods in OOP | | ✅ | |
|
|
62
|
+
| [export-if-in-doubt](docs/rules/export-if-in-doubt.md) | All top-level const definitions, type definitions, and functions should be exported | | ✅ | |
|
|
63
|
+
| [extract-global-constants](docs/rules/extract-global-constants.md) | Extract constants/functions to the global scope when possible | | ✅ | |
|
|
64
|
+
| [generic-starts-with-t](docs/rules/generic-starts-with-t.md) | Enforce TypeScript generic types to start with T | | ✅ | |
|
|
65
|
+
| [no-async-array-filter](docs/rules/no-async-array-filter.md) | Disallow async callbacks for Array.filter | ✅ | | |
|
|
66
|
+
| [no-async-foreach](docs/rules/no-async-foreach.md) | Disallow Array.forEach with an async callback function | ✅ | | |
|
|
67
|
+
| [no-conditional-literals-in-jsx](docs/rules/no-conditional-literals-in-jsx.md) | Disallow use of conditional literals in JSX code | ✅ | | |
|
|
68
|
+
| [no-filter-without-return](docs/rules/no-filter-without-return.md) | Disallow Array.filter callbacks without an explicit return (if part of a block statement) | ✅ | | |
|
|
69
|
+
| [no-misused-switch-case](docs/rules/no-misused-switch-case.md) | Prevent misuse of logical OR in switch case statements | ✅ | | |
|
|
70
|
+
| [no-unpinned-dependencies](docs/rules/no-unpinned-dependencies.md) | Enforces pinned dependencies | ✅ | | 🔧 |
|
|
71
|
+
| [no-useless-fragment](docs/rules/no-useless-fragment.md) | Prevent unnecessary use of React fragments | | ✅ | |
|
|
72
|
+
| [prefer-fragment-shorthand](docs/rules/prefer-fragment-shorthand.md) | Prefer <> shorthand for <React.Fragment> | | ✅ | 🔧 |
|
|
73
|
+
| [prefer-type-over-interface](docs/rules/prefer-type-over-interface.md) | Prefer using type alias over interface | | ✅ | |
|
|
71
74
|
|
|
72
75
|
<!-- end auto-generated rules list -->
|
|
73
76
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# All top-level const definitions, type definitions, and functions should be exported (`@blumintinc/blumint/export-if-in-doubt`)
|
|
2
2
|
|
|
3
3
|
⚠️ This rule _warns_ in the ✅ `recommended` config.
|
|
4
4
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Extract
|
|
1
|
+
# Extract constants/functions to the global scope when possible (`@blumintinc/blumint/extract-global-constants`)
|
|
2
2
|
|
|
3
3
|
⚠️ This rule _warns_ in the ✅ `recommended` config.
|
|
4
4
|
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Disallow Array.forEach with an async callback function (`@blumintinc/blumint/no-async-foreach`)
|
|
2
|
+
|
|
3
|
+
💼 This rule is enabled in the ✅ `recommended` config.
|
|
4
|
+
|
|
5
|
+
<!-- end auto-generated rule header -->
|
|
6
|
+
|
|
7
|
+
This rule disallows the use of `Array.forEach` callbacks with the async keyword. These are typically found when sequential promise execution is desired, but in fact these will execute almost concurrently. The use of a standard `for` loop is suggested instead, which will execute sequentially. If concurrent execution is required, `Promise.all` or `Promise.allSettled` should be used.
|
|
8
|
+
|
|
9
|
+
## Rule Details
|
|
10
|
+
|
|
11
|
+
This rule is aimed at ensuring that the async keyword is not used in an `Array.forEach` callback.
|
|
12
|
+
|
|
13
|
+
Examples of **incorrect** code for this rule:
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
['a','b','c'].forEach(async (letter) => {
|
|
17
|
+
await someAsyncFunction(letter)
|
|
18
|
+
console.log(letter)
|
|
19
|
+
})
|
|
20
|
+
['foo','bar'].forEach(async function (letter) {
|
|
21
|
+
await someAsyncFunction(letter)
|
|
22
|
+
console.log(letter)
|
|
23
|
+
})
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Examples of **correct** code for this rule:
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
['a','b','c'].forEach(letter) => {
|
|
30
|
+
someSyncFunction(letter)
|
|
31
|
+
console.log(letter)
|
|
32
|
+
})
|
|
33
|
+
['foo','bar'].forEach(function (letter) {
|
|
34
|
+
someSyncFunction(letter)
|
|
35
|
+
console.log(letter)
|
|
36
|
+
})
|
|
37
|
+
for (const letter of ['a', 'b', 'c']) {
|
|
38
|
+
someSyncFunction(letter);
|
|
39
|
+
console.log(letter);
|
|
40
|
+
}
|
|
41
|
+
```
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Disallow use of conditional literals in JSX code (`@blumintinc/blumint/no-conditional-literals-in-jsx`)
|
|
2
|
+
|
|
3
|
+
💼 This rule is enabled in the ✅ `recommended` config.
|
|
4
|
+
|
|
5
|
+
<!-- end auto-generated rule header -->
|
|
6
|
+
|
|
7
|
+
This rule disallows the use of conditional literals in JSX.
|
|
8
|
+
|
|
9
|
+
## Rule Details
|
|
10
|
+
|
|
11
|
+
Browser auto-translation will break if pieces of text nodes are be rendered conditionally.
|
|
12
|
+
|
|
13
|
+
Examples of **incorrect** code for this rule:
|
|
14
|
+
|
|
15
|
+
```jsx
|
|
16
|
+
<div>This will cause {conditional && 'errors'}</div>
|
|
17
|
+
<div><span>This will {conditional && 'also'} cause errors </span></div>
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Examples of **correct** code for this rule:
|
|
21
|
+
|
|
22
|
+
```jsx
|
|
23
|
+
<div>Foo</div>
|
|
24
|
+
<div>
|
|
25
|
+
Bar {conditional && <span>Baz</span>}
|
|
26
|
+
</div>
|
|
27
|
+
```
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
# Prevent
|
|
1
|
+
# Prevent misuse of logical OR in switch case statements (`@blumintinc/blumint/no-misused-switch-case`)
|
|
2
2
|
|
|
3
3
|
💼 This rule is enabled in the ✅ `recommended` config.
|
|
4
4
|
|
|
5
5
|
<!-- end auto-generated rule header -->
|
|
6
6
|
|
|
7
|
-
This rule prevents the misuse of logical OR
|
|
7
|
+
This rule prevents the misuse of logical OR in switch case statements. Instead, cascading cases should be used. This improves code readability and understanding.
|
|
8
8
|
|
|
9
9
|
## Rule Details
|
|
10
10
|
|
|
11
|
-
This rule specifically targets `SwitchStatement` and issues a warning if a case uses a logical OR
|
|
11
|
+
This rule specifically targets `SwitchStatement` and issues a warning if a case uses a logical OR in its test.
|
|
12
12
|
|
|
13
13
|
### Examples of incorrect code for this rule:
|
|
14
14
|
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Prevent unnecessary use of React fragments (`@blumintinc/blumint/no-useless-fragment`)
|
|
2
|
+
|
|
3
|
+
⚠️ This rule _warns_ in the ✅ `recommended` config.
|
|
4
|
+
|
|
5
|
+
<!-- end auto-generated rule header -->
|
|
6
|
+
|
|
7
|
+
This rule enforces that React fragments (`<>...</>`) are only used when necessary. A fragment is deemed unnecessary if it wraps only a single child.
|
|
8
|
+
|
|
9
|
+
## Rule Details
|
|
10
|
+
|
|
11
|
+
Examples of **incorrect** code for this rule:
|
|
12
|
+
|
|
13
|
+
```jsx
|
|
14
|
+
<><ChildComponent /></>
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Examples of **correct** code for this rule:
|
|
18
|
+
|
|
19
|
+
```jsx
|
|
20
|
+
<><ChildComponent /><AnotherChild /></>
|
|
21
|
+
<><ChildComponent />Some Text<AnotherChild /></>
|
|
22
|
+
```
|
|
23
|
+
|
package/lib/index.js
CHANGED
|
@@ -5,15 +5,18 @@ const export_if_in_doubt_1 = require("./rules/export-if-in-doubt");
|
|
|
5
5
|
const extract_global_constants_1 = require("./rules/extract-global-constants");
|
|
6
6
|
const generic_starts_with_t_1 = require("./rules/generic-starts-with-t");
|
|
7
7
|
const no_async_array_filter_1 = require("./rules/no-async-array-filter");
|
|
8
|
+
const no_async_foreach_1 = require("./rules/no-async-foreach");
|
|
9
|
+
const no_conditional_literals_in_jsx_1 = require("./rules/no-conditional-literals-in-jsx");
|
|
8
10
|
const no_filter_without_return_1 = require("./rules/no-filter-without-return");
|
|
9
11
|
const no_misused_switch_case_1 = require("./rules/no-misused-switch-case");
|
|
10
12
|
const no_unpinned_dependencies_1 = require("./rules/no-unpinned-dependencies");
|
|
13
|
+
const no_useless_fragment_1 = require("./rules/no-useless-fragment");
|
|
11
14
|
const prefer_fragment_shorthand_1 = require("./rules/prefer-fragment-shorthand");
|
|
12
15
|
const prefer_type_over_interface_1 = require("./rules/prefer-type-over-interface");
|
|
13
16
|
module.exports = {
|
|
14
17
|
meta: {
|
|
15
18
|
name: '@blumintinc/eslint-plugin-blumint',
|
|
16
|
-
version: '0.1.
|
|
19
|
+
version: '0.1.12',
|
|
17
20
|
},
|
|
18
21
|
parseOptions: {
|
|
19
22
|
ecmaVersion: 2020,
|
|
@@ -27,9 +30,12 @@ module.exports = {
|
|
|
27
30
|
'@blumintinc/blumint/extract-global-constants': 'warn',
|
|
28
31
|
'@blumintinc/blumint/generic-starts-with-t': 'warn',
|
|
29
32
|
'@blumintinc/blumint/no-async-array-filter': 'error',
|
|
33
|
+
'@blumintinc/blumint/no-async-foreach': 'error',
|
|
34
|
+
'@blumincinc/blumint/no-conditional-literals-in-jsx': 'error',
|
|
30
35
|
'@blumintinc/blumint/no-filter-without-return': 'error',
|
|
31
36
|
'@blumintinc/blumint/no-misused-switch-case': 'error',
|
|
32
37
|
'@blumintinc/blumint/no-unpinned-dependencies': 'error',
|
|
38
|
+
'@blumintinc/blumint/no-useless-fragment': 'warn',
|
|
33
39
|
'@blumintinc/blumint/prefer-fragment-shorthand': 'warn',
|
|
34
40
|
'@blumintinc/blumint/prefer-type-over-interface': 'warn',
|
|
35
41
|
},
|
|
@@ -41,9 +47,12 @@ module.exports = {
|
|
|
41
47
|
'extract-global-constants': extract_global_constants_1.extractGlobalConstants,
|
|
42
48
|
'generic-starts-with-t': generic_starts_with_t_1.genericStartsWithT,
|
|
43
49
|
'no-async-array-filter': no_async_array_filter_1.noAsyncArrayFilter,
|
|
50
|
+
'no-async-foreach': no_async_foreach_1.noAsyncForEach,
|
|
51
|
+
'no-conditional-literals-in-jsx': no_conditional_literals_in_jsx_1.noConditionalLiteralsInJsx,
|
|
44
52
|
'no-filter-without-return': no_filter_without_return_1.noFilterWithoutReturn,
|
|
45
53
|
'no-misused-switch-case': no_misused_switch_case_1.noMisusedSwitchCase,
|
|
46
54
|
'no-unpinned-dependencies': no_unpinned_dependencies_1.noUnpinnedDependencies,
|
|
55
|
+
'no-useless-fragment': no_useless_fragment_1.noUselessFragment,
|
|
47
56
|
'prefer-fragment-shorthand': prefer_fragment_shorthand_1.preferFragmentShorthand,
|
|
48
57
|
'prefer-type-over-interface': prefer_type_over_interface_1.preferTypeOverInterface,
|
|
49
58
|
},
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.noAsyncForEach = void 0;
|
|
4
|
+
exports.noAsyncForEach = {
|
|
5
|
+
create(context) {
|
|
6
|
+
return {
|
|
7
|
+
CallExpression(node) {
|
|
8
|
+
const callee = node.callee;
|
|
9
|
+
if (callee.type === 'MemberExpression' &&
|
|
10
|
+
callee.property.type === 'Identifier' &&
|
|
11
|
+
callee.property.name === 'forEach' &&
|
|
12
|
+
node.arguments[0] &&
|
|
13
|
+
(node.arguments[0].type === 'ArrowFunctionExpression' ||
|
|
14
|
+
node.arguments[0].type === 'FunctionExpression') &&
|
|
15
|
+
node.arguments[0].async) {
|
|
16
|
+
context.report({
|
|
17
|
+
node,
|
|
18
|
+
messageId: 'noAsyncForEach',
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
},
|
|
24
|
+
meta: {
|
|
25
|
+
type: 'problem',
|
|
26
|
+
docs: {
|
|
27
|
+
description: 'Disallow Array.forEach with an async callback function',
|
|
28
|
+
recommended: 'error',
|
|
29
|
+
},
|
|
30
|
+
messages: {
|
|
31
|
+
noAsyncForEach: 'Do not use async function as callback in Array.forEach. Use a standard for loop for sequential execution or Promise.all for concurrent execution.',
|
|
32
|
+
},
|
|
33
|
+
schema: [],
|
|
34
|
+
},
|
|
35
|
+
defaultOptions: [],
|
|
36
|
+
};
|
|
37
|
+
//# sourceMappingURL=no-async-foreach.js.map
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.noConditionalLiteralsInJsx = void 0;
|
|
4
|
+
/* eslint-disable @blumintinc/blumint/extract-global-constants */
|
|
5
|
+
// import { ASTHelpers } from '../utils/ASTHelpers';
|
|
6
|
+
const createRule_1 = require("../utils/createRule");
|
|
7
|
+
const utils_1 = require("@typescript-eslint/utils");
|
|
8
|
+
exports.noConditionalLiteralsInJsx = (0, createRule_1.createRule)({
|
|
9
|
+
name: 'no-conditional-literals-in-jsx',
|
|
10
|
+
meta: {
|
|
11
|
+
type: 'problem',
|
|
12
|
+
docs: {
|
|
13
|
+
description: 'Disallow use of conditional literals in JSX code',
|
|
14
|
+
recommended: 'error',
|
|
15
|
+
},
|
|
16
|
+
schema: [],
|
|
17
|
+
messages: {
|
|
18
|
+
unexpected: 'Conditional expression is a sibling of raw text and must be wrapped in <div> or <span>',
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
defaultOptions: [],
|
|
22
|
+
create(context) {
|
|
23
|
+
return {
|
|
24
|
+
// Imagine evaluating <div>text {conditional && 'string'}</div>
|
|
25
|
+
JSXExpressionContainer(node) {
|
|
26
|
+
// We start at the expression {conditional && 'string'}
|
|
27
|
+
if (node.expression.type !== 'LogicalExpression') {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const parentChildren = node.parent && 'children' in node.parent ? node.parent.children : [];
|
|
31
|
+
// "text" is one of the siblingTextNodes.
|
|
32
|
+
const siblingTextNodes = parentChildren.filter((n) => {
|
|
33
|
+
if (n.type === utils_1.TSESTree.AST_NODE_TYPES.Literal ||
|
|
34
|
+
n.type === utils_1.TSESTree.AST_NODE_TYPES.JSXText) {
|
|
35
|
+
return !!('value' in n && !!n.value ? `${n.value}`.trim() : false);
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
});
|
|
39
|
+
// If we were evaluating
|
|
40
|
+
// <div>{property} {conditional && 'string'}</div>
|
|
41
|
+
// Then {property} would be one of the siblingExpressionNodes
|
|
42
|
+
const siblingExpressionNodes = parentChildren.filter((n) => n.type === 'JSXExpressionContainer' &&
|
|
43
|
+
'expression' in n &&
|
|
44
|
+
(n.expression.type === 'Identifier' ||
|
|
45
|
+
n.expression.type === 'MemberExpression'));
|
|
46
|
+
// Operands of {conditional && 'string'} -- the conditional and the
|
|
47
|
+
// literal. We want to make sure we have a text literal, otherwise we'd
|
|
48
|
+
// trigger this rule on the (safe) {conditional && <div>string</div>}.
|
|
49
|
+
const expressionOperandTypes = [
|
|
50
|
+
node.expression.left.type,
|
|
51
|
+
node.expression.right.type,
|
|
52
|
+
];
|
|
53
|
+
if (siblingTextNodes.concat(siblingExpressionNodes).length > 0 &&
|
|
54
|
+
expressionOperandTypes.includes(utils_1.TSESTree.AST_NODE_TYPES.Literal)) {
|
|
55
|
+
context.report({ node, messageId: 'unexpected' });
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
//# sourceMappingURL=no-conditional-literals-in-jsx.js.map
|
|
@@ -7,12 +7,12 @@ exports.noMisusedSwitchCase = (0, createRule_1.createRule)({
|
|
|
7
7
|
meta: {
|
|
8
8
|
type: 'problem',
|
|
9
9
|
docs: {
|
|
10
|
-
description: 'Prevent misuse of logical OR
|
|
10
|
+
description: 'Prevent misuse of logical OR in switch case statements',
|
|
11
11
|
recommended: 'error',
|
|
12
12
|
},
|
|
13
13
|
schema: [],
|
|
14
14
|
messages: {
|
|
15
|
-
noMisusedSwitchCase: 'Avoid using logical OR
|
|
15
|
+
noMisusedSwitchCase: 'Avoid using logical OR in switch case. Use cascading cases instead.',
|
|
16
16
|
},
|
|
17
17
|
},
|
|
18
18
|
defaultOptions: [],
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.noUselessFragment = void 0;
|
|
4
|
+
exports.noUselessFragment = {
|
|
5
|
+
create(context) {
|
|
6
|
+
return {
|
|
7
|
+
JSXFragment(node) {
|
|
8
|
+
if (node.children.length === 1) {
|
|
9
|
+
context.report({
|
|
10
|
+
node,
|
|
11
|
+
messageId: 'noUselessFragment',
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
},
|
|
17
|
+
meta: {
|
|
18
|
+
type: 'suggestion',
|
|
19
|
+
docs: {
|
|
20
|
+
description: 'Prevent unnecessary use of React fragments',
|
|
21
|
+
recommended: 'warn',
|
|
22
|
+
},
|
|
23
|
+
messages: {
|
|
24
|
+
noUselessFragment: 'React fragment is unnecessary when wrapping a single child',
|
|
25
|
+
},
|
|
26
|
+
schema: [],
|
|
27
|
+
},
|
|
28
|
+
defaultOptions: [],
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=no-useless-fragment.js.map
|
|
@@ -30,7 +30,7 @@ exports.preferFragmentShorthand = {
|
|
|
30
30
|
recommended: 'warn',
|
|
31
31
|
},
|
|
32
32
|
messages: {
|
|
33
|
-
preferShorthand: 'Use <> shorthand for <React.Fragment
|
|
33
|
+
preferShorthand: 'Use <> shorthand for <React.Fragment>, unless a key is required for an iterator',
|
|
34
34
|
},
|
|
35
35
|
schema: [],
|
|
36
36
|
fixable: 'code',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blumintinc/eslint-plugin-blumint",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12",
|
|
4
4
|
"description": "Custom eslint rules for use at BluMint",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"ts-jest": "29.0.5"
|
|
37
37
|
},
|
|
38
38
|
"engines": {
|
|
39
|
-
"node": "16.0.0"
|
|
39
|
+
"node": "^16.0.0"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"eslint": ">=7"
|