Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'eslint-rule-composer' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
* @fileoverview A modified version of the `multiline-comment-style` rule that ignores banner comments.
* @author Teddy Katz
*/
"use strict";
const ruleComposer = require("eslint-rule-composer");
const multilineCommentStyle = require("../../lib/rules/multiline-comment-style");
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
// The `no-invalid-meta` internal rule has a false positive here.
// eslint-disable-next-line internal-rules/no-invalid-meta
module.exports = ruleComposer.filterReports(
multilineCommentStyle,
(problem, metadata) => {
const problemIndex = metadata.sourceCode.getIndexFromLoc(problem.loc.start);
const reportedToken = metadata.sourceCode.getTokenByRangeStart(problemIndex, { includeComments: true });
return !(reportedToken && reportedToken.type === "Line" && /^-{2,}$/u.test(reportedToken.value));
}
);
import ruleComposer from "eslint-rule-composer";
import eslint from "eslint";
const quotesRule = new eslint.Linter().getRules().get("quotes");
export default ruleComposer.filterReports(quotesRule, problem => {
// Workaround for JSX fragment syntax until
// https://github.com/eslint/eslint/issues/9662
if (problem.node.parent.type === "JSXFragment") {
return false;
}
return true;
});
} else {
if (!isSemicolon(lastToken)) {
if (!exceptOneLine || !isOneLinerBlock(context, node)) {
report(context, node);
}
} else {
if (exceptOneLine && isOneLinerBlock(context, node)) {
report(context, node, true);
}
}
}
},
}),
]);
export default ruleComposer.filterReports(
semiRuleWithClassProperty,
problem => {
const node = problem.node;
// Handle async iterator:
// for await (let something of {})
if (
node.type === "VariableDeclaration" &&
node.parent.type === "ForAwaitStatement"
) {
return false;
}
return true;
},
);
import ruleComposer from "eslint-rule-composer";
import eslint from "eslint";
const objectCurlySpacingRule = new eslint.Linter()
.getRules()
.get("object-curly-spacing");
export default ruleComposer.filterReports(
objectCurlySpacingRule,
(problem, metadata) => {
const node = problem.node;
// Allow `exportNamespaceFrom` and `exportDefaultFrom` syntax:
// export * as x from '...';
// export x from '...';
if (
node.type === "ExportNamedDeclaration" &&
node.specifiers.length > 0 &&
metadata.sourceCode.getTokenBefore(node.specifiers[0]).value === "export"
) {
return false;
}
return true;
import ruleComposer from "eslint-rule-composer";
import eslint from "eslint";
const validTypeOf = new eslint.Linter().getRules().get("valid-typeof");
export default ruleComposer.filterReports(validTypeOf, problem => {
return problem.node.value !== "bigint";
});
}
/**
* @param {ASTNode} node - any node
* @returns {boolean} whether the given node is an optional call expression,
* see https://github.com/tc39/proposal-optional-chaining
*/
function isOptionalCallExpression(node) {
return (
!!node &&
node.type === "ExpressionStatement" &&
node.expression.type === "OptionalCallExpression"
);
}
export default ruleComposer.filterReports(
rule,
problem =>
!isInDoStatement(problem.node) && !isOptionalCallExpression(problem.node),
);
import ruleComposer from "eslint-rule-composer";
import eslint from "eslint";
const noInvalidThisRule = new eslint.Linter().getRules().get("no-invalid-this");
export default ruleComposer.filterReports(noInvalidThisRule, problem => {
let inClassProperty = false;
let node = problem.node;
while (node) {
if (node.type === "ClassProperty" || node.type === "ClassPrivateProperty") {
inClassProperty = true;
return;
}
node = node.parent;
}
return !inClassProperty;
});
import ruleComposer from "eslint-rule-composer";
import eslint from "eslint";
const newCapRule = new eslint.Linter().getRules().get("new-cap");
/**
* Returns whether a node is under a decorator or not.
* @param {ASTNode} node CallExpression node
* @returns {Boolean} Returns true if the node is under a decorator.
*/
function isDecorator(node) {
return node.parent.type === "Decorator";
}
export default ruleComposer.filterReports(
newCapRule,
problem => !isDecorator(problem.node),
);
module.exports = (rules, filterNode, filterAll) => filterReports(ruleComposer.joinReports(rules), filterNode, filterAll);
message = "Extra semicolon.";
loc = loc.start;
fix = function(fixer) {
return fixer.remove(lastToken);
};
}
context.report({
node,
loc,
message,
fix,
});
};
const semiRuleWithClassProperty = ruleComposer.joinReports([
semiRule,
context => ({
ClassProperty(node) {
const options = context.options[1];
const exceptOneLine = options && options.omitLastInOneLineBlock === true;
const sourceCode = context.getSourceCode();
const lastToken = sourceCode.getLastToken(node);
if (context.options[0] === "never") {
if (isUnnecessarySemicolon(context, lastToken)) {
report(context, node, true);
}
} else {
if (!isSemicolon(lastToken)) {
if (!exceptOneLine || !isOneLinerBlock(context, node)) {