@blumintinc/eslint-plugin-blumint 0.1.13 → 0.1.14
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/lib/index.js +2 -2
- package/lib/rules/no-conditional-literals.js +59 -0
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -16,7 +16,7 @@ const prefer_type_over_interface_1 = require("./rules/prefer-type-over-interface
|
|
|
16
16
|
module.exports = {
|
|
17
17
|
meta: {
|
|
18
18
|
name: '@blumintinc/eslint-plugin-blumint',
|
|
19
|
-
version: '0.1.
|
|
19
|
+
version: '0.1.14',
|
|
20
20
|
},
|
|
21
21
|
parseOptions: {
|
|
22
22
|
ecmaVersion: 2020,
|
|
@@ -31,7 +31,7 @@ module.exports = {
|
|
|
31
31
|
'@blumintinc/blumint/generic-starts-with-t': 'warn',
|
|
32
32
|
'@blumintinc/blumint/no-async-array-filter': 'error',
|
|
33
33
|
'@blumintinc/blumint/no-async-foreach': 'error',
|
|
34
|
-
'@
|
|
34
|
+
'@blumintinc/blumint/no-conditional-literals-in-jsx': 'error',
|
|
35
35
|
'@blumintinc/blumint/no-filter-without-return': 'error',
|
|
36
36
|
'@blumintinc/blumint/no-misused-switch-case': 'error',
|
|
37
37
|
'@blumintinc/blumint/no-unpinned-dependencies': 'error',
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.noConditionalLiterals = void 0;
|
|
4
|
+
/* eslint-disable @blumintinc/blumint/extract-global-constants */
|
|
5
|
+
// import { ASTHelpers } from '../utils/ASTHelpers';
|
|
6
|
+
const utils_1 = require("@typescript-eslint/utils");
|
|
7
|
+
exports.noConditionalLiterals = {
|
|
8
|
+
create(context) {
|
|
9
|
+
return {
|
|
10
|
+
// Imagine evaluating <div>text {conditional && 'string'}</div>
|
|
11
|
+
JSXExpressionContainer(node) {
|
|
12
|
+
// We start at the expression {conditional && 'string'}
|
|
13
|
+
if (node.expression.type !== 'LogicalExpression') {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const parentChildren = node.parent && 'children' in node.parent ? node.parent.children : [];
|
|
17
|
+
// "text" is one of the siblingTextNodes.
|
|
18
|
+
const siblingTextNodes = parentChildren.filter((n) => {
|
|
19
|
+
if (n.type === utils_1.TSESTree.AST_NODE_TYPES.Literal ||
|
|
20
|
+
n.type === utils_1.TSESTree.AST_NODE_TYPES.JSXText) {
|
|
21
|
+
return !!('value' in n && !!n.value ? `${n.value}`.trim() : false);
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
});
|
|
25
|
+
// If we were evaluating
|
|
26
|
+
// <div>{property} {conditional && 'string'}</div>
|
|
27
|
+
// Then {property} would be one of the siblingExpressionNodes
|
|
28
|
+
const siblingExpressionNodes = parentChildren.filter((n) => n.type === 'JSXExpressionContainer' &&
|
|
29
|
+
'expression' in n &&
|
|
30
|
+
(n.expression.type === 'Identifier' ||
|
|
31
|
+
n.expression.type === 'MemberExpression'));
|
|
32
|
+
// Operands of {conditional && 'string'} -- the conditional and the
|
|
33
|
+
// literal. We want to make sure we have a text literal, otherwise we'd
|
|
34
|
+
// trigger this rule on the (safe) {conditional && <div>string</div>}.
|
|
35
|
+
const expressionOperandTypes = [
|
|
36
|
+
node.expression.left.type,
|
|
37
|
+
node.expression.right.type,
|
|
38
|
+
];
|
|
39
|
+
if (siblingTextNodes.concat(siblingExpressionNodes).length > 0 &&
|
|
40
|
+
expressionOperandTypes.includes(utils_1.TSESTree.AST_NODE_TYPES.Literal)) {
|
|
41
|
+
context.report({ node, messageId: 'unexpected' });
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
meta: {
|
|
47
|
+
type: 'problem',
|
|
48
|
+
docs: {
|
|
49
|
+
description: 'Disallow use of conditional literals in JSX code',
|
|
50
|
+
recommended: 'error',
|
|
51
|
+
},
|
|
52
|
+
messages: {
|
|
53
|
+
unexpected: 'Conditional expression is a sibling of raw text and must be wrapped in <div> or <span>',
|
|
54
|
+
},
|
|
55
|
+
schema: [],
|
|
56
|
+
},
|
|
57
|
+
defaultOptions: [],
|
|
58
|
+
};
|
|
59
|
+
//# sourceMappingURL=no-conditional-literals.js.map
|