Skip to content

Commit

Permalink
Merge pull request #33 from hildjj/filter-unused-rules
Browse files Browse the repository at this point in the history
Allow filtering of unused rules, so only matching rules are checked.
  • Loading branch information
hildjj authored Feb 29, 2024
2 parents 6a2ded2 + a98f1a5 commit 401b920
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
20 changes: 20 additions & 0 deletions docs/rules/no-unused-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ foo = "1" // Good. Default entry point.
bar = "2" // Bad. Not referenced.
```

```peg.js
// eslint @peggyjs/no-unused-rules: ["error", {filter: "^_"}]
foo = "foo" // Good.
bar = "bar" // Doesn't match the filter
_boo = "boo" // Bad. Not referenced, and matches the filter.
```

:+1: Examples of **correct** code for this rule:

```peg.js
Expand All @@ -27,6 +34,19 @@ foo = bar / baz
bar = "2"
```

```peg.js
// eslint @peggyjs/no-unused-rules: ["error", {filter: "^_"}]
foo = _boo
bar = "bar" // Doesn't match the filter
_boo = "boo"
```

### Options

The first option can be an object with the key "filter". The filter is treated
as a regular expression; only rules that match this expression will cause
errors if they are unused.

## 🔎 Implementation

- [Rule source](../../src/rules/no-unused-rules.ts)
Expand Down
19 changes: 18 additions & 1 deletion src/rules/no-unused-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,26 @@ const rule: Rule.RuleModule = {
unused: "Rule '{{ name }}' is never used.",
unusedImport: "Library import '{{ name }}' is never used.",
},
schema: [],
schema: [
{
type: "object",
properties: {
filter: {
type: "string",
},
},
additionalProperties: false,
},
],
},
create(context: Rule.RuleContext): Rule.RuleListener {
const imports = new Map<string, visitor.AST.Name>();
const rules = new Map<string, visitor.AST.Rule>();
const refs = new Set<string>();
const importRefs = new Set<string>();
const filter = context.options[0]?.filter;
const match = filter ? new RegExp(filter) : undefined;

return makeListener({
import_binding(node: visitor.AST.ImportBinding): void {
imports.set(node.binding.id.value, node.binding.id);
Expand Down Expand Up @@ -52,7 +65,11 @@ const rule: Rule.RuleModule = {
rules.delete(name);
imports.delete(name);
}

for (const [name, r] of rules) {
if (match && !match.test(name)) {
continue;
}
context.report({
node: n(r),
messageId: "unused",
Expand Down
16 changes: 16 additions & 0 deletions test/rules/no-unused-rules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ foo = bar`,
import {"foo" as bar} from 'bar'
foo = bar`,
},
{
options: [{ filter: "^_" }],
code: `
foo = _bar
baz = "boo"
_bar = "bar"`,
},
],
invalid: [
{
Expand Down Expand Up @@ -76,5 +83,14 @@ foo = bar`,
foo = "1"`,
errors: [{ messageId: "unusedImport" }],
},
{
options: [{ filter: "^_" }],
code: `
foo = _bar
baz = "boo"
_bar = "bar"
_boo = "boo"`,
errors: [{ messageId: "unused" }],
},
],
});

0 comments on commit 401b920

Please sign in to comment.