@masknet/eslint-plugin
Version:
eslint plugin for masknet
60 lines • 1.96 kB
JavaScript
import { createRule } from "../../rule.js";
const DEFAULT_LIMIT = 2;
export default createRule({
name: 'jsx/no-logical',
meta: {
type: 'problem',
docs: {
description: 'Limit the complexity of JSX logic expression',
},
schema: [
{
oneOf: [
{ type: 'integer', minimum: 0 },
{
type: 'object',
properties: {
attribute: { type: 'integer', minimum: 0 },
element: { type: 'integer', minimum: 0 },
},
additionalProperties: false,
},
],
},
],
messages: {
invalid: 'Limit the complexity of JSX logic expression',
},
},
defaultOptions: [DEFAULT_LIMIT],
create(context, [options]) {
function report(node, limit) {
const disallow = node.expression.type === 'ConditionalExpression' || getLogicalCount(node.expression) > limit;
if (!disallow)
return;
context.report({ node, messageId: 'invalid' });
}
return {
JSXExpressionContainer(node) {
const { parent } = node;
if (parent?.type === 'JSXAttribute') {
report(node, typeof options === 'number' ? options : options.attribute);
}
else if (parent?.type === 'JSXElement') {
report(node, typeof options === 'number' ? options : options.element);
}
},
};
},
});
function getLogicalCount(node) {
if (node.type !== 'LogicalExpression')
return 0;
let count = 1;
while (node.type === 'LogicalExpression') {
count += 1;
node = node.left;
}
return count;
}
//# sourceMappingURL=no-logical.js.map