@blumintinc/eslint-plugin-blumint 0.1.17 → 0.1.19
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.
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Dynamic elements of errors should be in the 'details' field (`@blumintinc/blumint/dynamic-https-errors`)
|
|
2
|
+
|
|
3
|
+
⚠️ This rule _warns_ in the ✅ `recommended` config.
|
|
4
|
+
|
|
5
|
+
<!-- end auto-generated rule header -->
|
|
6
|
+
|
|
7
|
+
This rule warns against the use of template literals in the `message` field of the `HttpsError` constructor, and suggests their use in the `details` field instead.
|
|
8
|
+
|
|
9
|
+
## Rule Details
|
|
10
|
+
|
|
11
|
+
Examples of **incorrect** code for this rule:
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
throw new https.HttpsError('foo', `Error: ${bar}`, 'baz');
|
|
15
|
+
throw new HttpsError('foo', `Error: ${bar}`, 'baz');
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Examples of **correct** code for this rule:
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
throw new https.HttpsError('foo', 'bar', 'baz');
|
|
22
|
+
throw new https.HttpsError('foo', 'bar', `Details: ${baz}`);
|
|
23
|
+
```
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const array_methods_this_context_1 = require("./rules/array-methods-this-context");
|
|
4
|
+
const dynamic_https_errors_1 = require("./rules/dynamic-https-errors");
|
|
4
5
|
const export_if_in_doubt_1 = require("./rules/export-if-in-doubt");
|
|
5
6
|
const extract_global_constants_1 = require("./rules/extract-global-constants");
|
|
6
7
|
const generic_starts_with_t_1 = require("./rules/generic-starts-with-t");
|
|
@@ -17,7 +18,7 @@ const require_memo_1 = require("./rules/require-memo");
|
|
|
17
18
|
module.exports = {
|
|
18
19
|
meta: {
|
|
19
20
|
name: '@blumintinc/eslint-plugin-blumint',
|
|
20
|
-
version: '0.1.
|
|
21
|
+
version: '0.1.19',
|
|
21
22
|
},
|
|
22
23
|
parseOptions: {
|
|
23
24
|
ecmaVersion: 2020,
|
|
@@ -27,6 +28,7 @@ module.exports = {
|
|
|
27
28
|
plugins: ['@blumintinc/blumint'],
|
|
28
29
|
rules: {
|
|
29
30
|
'@blumintinc/blumint/array-methods-this-context': 'warn',
|
|
31
|
+
'@blumintinc/blumint/dynamic-https-errors': 'warn',
|
|
30
32
|
// '@blumintinc/blumint/export-if-in-doubt': 'warn',
|
|
31
33
|
// '@blumintinc/blumint/extract-global-constants': 'warn',
|
|
32
34
|
'@blumintinc/blumint/generic-starts-with-t': 'warn',
|
|
@@ -45,6 +47,7 @@ module.exports = {
|
|
|
45
47
|
},
|
|
46
48
|
rules: {
|
|
47
49
|
'array-methods-this-context': array_methods_this_context_1.arrayMethodsThisContext,
|
|
50
|
+
'dynamic-https-errors': dynamic_https_errors_1.dynamicHttpsErrors,
|
|
48
51
|
'export-if-in-doubt': export_if_in_doubt_1.exportIfInDoubt,
|
|
49
52
|
'extract-global-constants': extract_global_constants_1.extractGlobalConstants,
|
|
50
53
|
'generic-starts-with-t': generic_starts_with_t_1.genericStartsWithT,
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.dynamicHttpsErrors = void 0;
|
|
4
|
+
/* eslint-disable @blumintinc/blumint/extract-global-constants */
|
|
5
|
+
const createRule_1 = require("../utils/createRule");
|
|
6
|
+
const isHttpsErrorCall = (callee) => {
|
|
7
|
+
if (callee.type === 'MemberExpression') {
|
|
8
|
+
return (callee.object.type === 'Identifier' &&
|
|
9
|
+
callee.object.name === 'https' &&
|
|
10
|
+
callee.property.type === 'Identifier' &&
|
|
11
|
+
callee.property.name === 'HttpsError');
|
|
12
|
+
}
|
|
13
|
+
else if (callee.type === 'Identifier') {
|
|
14
|
+
return callee.name === 'HttpsError';
|
|
15
|
+
}
|
|
16
|
+
return false;
|
|
17
|
+
};
|
|
18
|
+
exports.dynamicHttpsErrors = (0, createRule_1.createRule)({
|
|
19
|
+
name: 'dynamic-https-errors',
|
|
20
|
+
meta: {
|
|
21
|
+
type: 'suggestion',
|
|
22
|
+
docs: {
|
|
23
|
+
description: 'Dynamic error details should only be in the third argument of the HttpsError constructor. The second argument is hashed to produce a unique id.',
|
|
24
|
+
recommended: 'warn',
|
|
25
|
+
},
|
|
26
|
+
schema: [],
|
|
27
|
+
messages: {
|
|
28
|
+
dynamicHttpsErrors: 'Found dynamic error details in the "message" field. Move any dynamic details third argument.',
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
defaultOptions: [],
|
|
32
|
+
create(context) {
|
|
33
|
+
const checkForHttpsError = (node) => {
|
|
34
|
+
const callee = node.callee;
|
|
35
|
+
if (isHttpsErrorCall(callee)) {
|
|
36
|
+
const secondArg = node.arguments[1];
|
|
37
|
+
if (secondArg && secondArg.type === 'TemplateLiteral') {
|
|
38
|
+
if (secondArg.expressions.length > 0) {
|
|
39
|
+
context.report({
|
|
40
|
+
node: secondArg,
|
|
41
|
+
messageId: 'dynamicHttpsErrors',
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
NewExpression(node) {
|
|
49
|
+
return checkForHttpsError(node);
|
|
50
|
+
},
|
|
51
|
+
CallExpression(node) {
|
|
52
|
+
return checkForHttpsError(node);
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
//# sourceMappingURL=dynamic-https-errors.js.map
|
|
@@ -1,31 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.requireMemo = void 0;
|
|
4
|
+
const utils_1 = require("@typescript-eslint/utils");
|
|
4
5
|
const ASTHelpers_1 = require("../utils/ASTHelpers");
|
|
5
6
|
const isComponentExplicitlyUnmemoized = (componentName) => componentName.toLowerCase().includes('unmemoized');
|
|
6
|
-
function
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
if (node.callee.type === 'MemberExpression') {
|
|
10
|
-
const { callee: { object, property }, } = node;
|
|
11
|
-
if (object.type === 'Identifier' &&
|
|
12
|
-
property.type === 'Identifier' &&
|
|
13
|
-
object.name === 'React' &&
|
|
14
|
-
property.name === 'memo') {
|
|
15
|
-
return true;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
else if (node.callee.type === 'Identifier' && node.callee.name === 'memo') {
|
|
19
|
-
return true;
|
|
20
|
-
}
|
|
21
|
-
return false;
|
|
7
|
+
function isFunction(node) {
|
|
8
|
+
return (node.type === utils_1.AST_NODE_TYPES.ArrowFunctionExpression ||
|
|
9
|
+
node.type === utils_1.AST_NODE_TYPES.FunctionExpression);
|
|
22
10
|
}
|
|
23
11
|
function isHigherOrderFunctionReturningJSX(node) {
|
|
24
|
-
if (node
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
12
|
+
if (isFunction(node)) {
|
|
13
|
+
// Check if function takes another function as an argument
|
|
14
|
+
const hasFunctionParam = 'params' in node && node.params.some(isFunction);
|
|
15
|
+
if (node.body && node.body.type === 'BlockStatement') {
|
|
16
|
+
for (const statement of node.body.body) {
|
|
17
|
+
if (statement.type === 'ReturnStatement' && statement.argument) {
|
|
18
|
+
const returnsJSX = ASTHelpers_1.ASTHelpers.returnsJSX(statement.argument);
|
|
19
|
+
const returnsFunction = isFunction(statement.argument);
|
|
20
|
+
return (hasFunctionParam || returnsFunction) && returnsJSX;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
29
23
|
}
|
|
30
24
|
}
|
|
31
25
|
return false;
|
|
@@ -36,15 +30,19 @@ function checkFunction(context, node) {
|
|
|
36
30
|
return;
|
|
37
31
|
}
|
|
38
32
|
if (isHigherOrderFunctionReturningJSX(node)) {
|
|
33
|
+
console.log('Found HOF');
|
|
39
34
|
return;
|
|
40
35
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
currentNode = currentNode.parent;
|
|
36
|
+
const currentNode = node.parent;
|
|
37
|
+
if (node.parent.type === 'CallExpression') {
|
|
38
|
+
return;
|
|
47
39
|
}
|
|
40
|
+
// while (currentNode.type === 'CallExpression') {
|
|
41
|
+
// if (isMemoCallExpression(currentNode) || true) {
|
|
42
|
+
// return;
|
|
43
|
+
// }
|
|
44
|
+
// currentNode = currentNode.parent;
|
|
45
|
+
// }
|
|
48
46
|
if (ASTHelpers_1.ASTHelpers.returnsJSX(node.body) && ASTHelpers_1.ASTHelpers.hasParameters(node)) {
|
|
49
47
|
if (currentNode.type === 'VariableDeclarator' &&
|
|
50
48
|
currentNode.id.type === 'Identifier' &&
|
package/lib/utils/ASTHelpers.js
CHANGED
|
@@ -153,8 +153,20 @@ class ASTHelpers {
|
|
|
153
153
|
statement.argument?.type === 'JSXFragment')) {
|
|
154
154
|
return true;
|
|
155
155
|
}
|
|
156
|
+
// Handle conditional returns
|
|
157
|
+
if (statement.type === 'ReturnStatement' &&
|
|
158
|
+
statement.argument?.type === 'ConditionalExpression') {
|
|
159
|
+
const conditionalExpr = statement.argument;
|
|
160
|
+
if (ASTHelpers.returnsJSX(conditionalExpr.consequent) ||
|
|
161
|
+
ASTHelpers.returnsJSX(conditionalExpr.alternate)) {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
156
165
|
}
|
|
157
166
|
}
|
|
167
|
+
if (node.type === 'ConditionalExpression') {
|
|
168
|
+
return (this.returnsJSX(node.consequent) || this.returnsJSX(node.alternate));
|
|
169
|
+
}
|
|
158
170
|
return false;
|
|
159
171
|
}
|
|
160
172
|
static hasParameters(node) {
|