Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow filtering of unused rules, so only matching rules are checked. #33

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" }],
},
],
});