Gathering detailed insights and metrics for eslint-rule-composer
Gathering detailed insights and metrics for eslint-rule-composer
Gathering detailed insights and metrics for eslint-rule-composer
Gathering detailed insights and metrics for eslint-rule-composer
A utility for composing ESLint rules from other ESLint rules
npm install eslint-rule-composer
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
56 Stars
12 Commits
2 Forks
4 Watching
1 Branches
4 Contributors
Updated on 19 Aug 2024
JavaScript (95.53%)
TypeScript (4.47%)
Cumulative downloads
Total Downloads
Last day
-7.1%
623,868
Compared to previous day
Last week
0.5%
3,408,916
Compared to previous week
Last month
8.4%
14,117,471
Compared to previous month
Last year
30.3%
161,484,047
Compared to previous year
This is a utility that allows you to build ESLint rules out of other ESLint rules.
npm install eslint-rule-composer --save
Requires Node 4 or later.
If you're using TypeScript, it's recommended to install @types/eslint
:
npm install -D @types/eslint
The following example creates a modified version of the no-unused-expressions
rule which does not report lines starting with expect
.
1const ruleComposer = require('eslint-rule-composer'); 2const eslint = require('eslint'); 3const noUnusedExpressionsRule = new eslint.Linter().getRules().get('no-unused-expressions'); 4 5module.exports = ruleComposer.filterReports( 6 noUnusedExpressionsRule, 7 (problem, metadata) => metadata.sourceCode.getFirstToken(problem.node).value !== 'expect' 8);
The following example creates a modified version of the semi
rule which reports missing semicolons after experimental class properties:
1const ruleComposer = require('eslint-rule-composer'); 2const eslint = require('eslint'); 3const semiRule = new eslint.Linter().getRules().get('semi'); 4 5module.exports = ruleComposer.joinReports([ 6 semiRule, 7 context => ({ 8 ClassProperty(node) { 9 if (context.getSourceCode().getLastToken(node).value !== ';') { 10 context.report({ node, message: 'Missing semicolon.' }) 11 } 12 } 13 }) 14]);
You can access rule's options and shared settings from the current ESLint configuration. The following example creates a modified version of the no-unused-expressions
rule which accepts a list of exceptions.
1 2/* 3 rule configuration: 4 5 { 6 "custom-no-unused-expressions": ["error", { 7 "whitelist": ["expect", "test"] 8 }] 9 } 10*/ 11 12const ruleComposer = require('eslint-rule-composer'); 13const eslint = require('eslint'); 14const noUnusedExpressionsRule = new eslint.Linter().getRules().get('no-unused-expressions'); 15 16module.exports = ruleComposer.filterReports( 17 noUnusedExpressionsRule, 18 (problem, metadata) => { 19 const firstToken = metadata.sourceCode.getFirstToken(problem.node); 20 const whitelist = metadata.options[0].whitelist; 21 return whitelist.includes(value) === false 22 } 23);
ruleComposer.filterReports(rule, predicate)
and ruleComposer.mapReports(rule, predicate)
Both of these functions accept two arguments: rule
(an ESLint rule object) and predicate
(a function)
filterReports(rule, predicate)
returns a new rule such that whenever the original rule would have reported a problem, the new rule will report a problem only if predicate
returns true for that problem.
mapReports(rule, predicate)
returns a new rule such that whenever the original rule would have reported a problem, the new rule reports the result of calling predicate
on the problem.
In both cases, predicate
is called with two arguments: problem
and metadata
.
problem
is a normalized representation of a problem reported by the original rule. This has the following schema:
{
node: ASTNode | null,
message: string,
messageId: string | null,
data: Object | null,
loc: {
start: { line: number, column: number },
end: { line: number, column: number } | null
},
fix: Function
}
Note that the messageId
and data
properties will only be present if the original rule reported a problem using Message IDs, otherwise they will be null.
When returning a descriptor with mapReports
, the messageId
property on the returned descriptor will be used to generate the new message. To modify a report message directly for a rule that uses message IDs, ensure that the predicate
function returns an object without a messageId
property.
metadata
is an object containing information about the source text that was linted. This has the following properties:
sourceCode
: a SourceCode
instance corresponding to the linted text.settings
: linter instance's shared settingsoptions
: rule's configuration optionsfilename
: corresponding filename for the linted text.ruleComposer.joinReports(rules)
Given an array of ESLint rule objects, joinReports
returns a new rule that will report all of the problems from any of the rules in the array. The options provided to the new rule will also be provided to all of the rules in the array.
To get a reference to an ESLint core rule, you can use ESLint's public API like this:
1// get a reference to the 'semi' rule 2 3const eslint = require('eslint'); 4const semiRule = new eslint.Linter().getRules().get('semi');
To get a reference to a rule from a plugin, you can do this:
1// get a reference to the 'react/boolean-prop-naming' rule 2const booleanPropNamingRule = require('eslint-plugin-react').rules['boolean-prop-naming'];
You can also create your own rules (see the rule documentation):
1const myCustomRule = { 2 create(context) { 3 return { 4 DebuggerStatement(node) { 5 context.report({ node, message: 'Do not use debugger statements.' }); 6 } 7 } 8 } 9};
MIT License
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 3/12 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More