Skip to content

Commit

Permalink
Merge pull request #30 from hildjj/add-rules
Browse files Browse the repository at this point in the history
Add rules needed for imports
  • Loading branch information
hildjj authored Feb 21, 2024
2 parents 1dd9441 + bcafd62 commit c3277ba
Show file tree
Hide file tree
Showing 77 changed files with 1,739 additions and 2,136 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ coverage/
node_modules/
examples/node_modules/
.vscode/
lib/
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"eslint.validate": [
"javascript",
"peggy",
"typescript"
],
"eslint.experimental.useFlatConfig": true
}
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,27 @@ module.exports = {
};
```

You can also use "plugin:@peggyjs/all" to get ALL of the rules.
Or, if you are using ESLint Flat configs, add this to your `eslint.config.js`
file:

```js
// ESM
import peggylint from "@peggyjs/eslint-plugin/lib/flat/recommended.js"
export default [
peggyLint,
];
```

Or:

```js
// CommonJS
module.exports = [
...require("@peggyjs/eslint-plugin/lib/flat/recommended.js"),
];
```

You can also use "plugin:@peggyjs/all" or to get ALL of the rules.
If you'd like more control:

```js
Expand Down Expand Up @@ -52,6 +72,21 @@ module.exports = {
};
```

## VSCode

If you are using VSCode, you might be interested in these for your `.vscode/settings.json` file:

```json
{
"eslint.validate": [
"javascript",
"peggy",
"typescript"
],
"eslint.experimental.useFlatConfig": true
}
```

## Rules

- ✒️ - Fixable rules.
Expand All @@ -67,10 +102,11 @@ module.exports = {
| [@peggyjs/no-unused-rules](./docs/rules/no-unused-rules.md) | All rules except for the first one must be referenced by another rule. | ⭐️ |
| [@peggyjs/quotes](./docs/rules/quotes.md) | Enforce the consistent use of double or single quotes. | ✒️ ⭐️ |
| [@peggyjs/rule-order](./docs/rules/rule-order.md) | Rule definitions should come after all references to that rule, unless there is a rule loop. | |
| [@peggyjs/semi](./docs/rules/semi.md) | Enforce consistent semicolon usage. | ✒️ ⭐️ |
| [@peggyjs/semantic-predicate-must-return](./docs/rules/semantic-predicate-must-return.md) | Semantic predicates must have a return statement. | ⭐️ |
| [@peggyjs/semi](./docs/rules/semi.md) | Enforce consistent semicolon usage. | ✒️ ⭐️ |
| [@peggyjs/separate-choices](./docs/rules/separate-choices.md) | Ensure that each top-level choice in a rule is on a new line. | ✒️ ⭐️ |
| [@peggyjs/space-ops](./docs/rules/space-ops.md) | Consistent spacing around operators and other punctuation. | ✒️ ⭐️ |
| [@peggyjs/valid-imports](./docs/rules/valid-imports.md) | All imports must point to correct JS files, compiled by Peggy 4.0.0 or later, which export the expected rule name as an allowedStartRule. | ⭐️ |

## Settings

Expand Down
3 changes: 2 additions & 1 deletion docs/rules/camelCase.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# @peggyjs/camelCase

> Rule names should be UpperCamelCase and label names should be lowerCamelCase.
> - ⭐️ This rule is included in `plugin:@peggyjs/recommended` preset.
> - ✒️ This rule will fix many, but not all errors.
Expand Down Expand Up @@ -44,4 +45,4 @@ _ = [ \t]
## 🔎 Implementation

- [Rule source](../../src/rules/camelCase.ts)
- [Test source](../../test/lib/rules/camelCase.js)
- [Test source](../../test/rules/camelCase.test.js)
3 changes: 2 additions & 1 deletion docs/rules/equal-next-line.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# @peggyjs/equal-next-line

> Ensure that the equals sign in a rule is in a consistent location.
> - ⭐️ This rule is included in `plugin:@peggyjs/recommended` preset.
> - ✒️ This rule will fix all errors it finds.
Expand Down Expand Up @@ -87,4 +88,4 @@ See [Settings](../settings.md).
## 🔎 Implementation

- [Rule source](../../src/rules/equal-next-line.ts)
- [Test source](../../test/lib/rules/equal-next-line.js)
- [Test source](../../test/rules/equal-next-line.test.js)
3 changes: 2 additions & 1 deletion docs/rules/no-empty-code-blocks.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# @peggyjs/no-empty-code-blocks

> Code blocks should not be empty.
> - ⭐️ This rule is included in `plugin:@peggyjs/recommended` preset.
> - ✒️ This rule will fix all errors it finds.
Expand Down Expand Up @@ -43,4 +44,4 @@ bar = !{ return false; } "1"
## 🔎 Implementation

- [Rule source](../../src/rules/no-empty-code-blocks.ts)
- [Test source](../../test/lib/rules/no-empty-code-blocks.js)
- [Test source](../../test/rules/no-empty-code-blocks.test.js)
3 changes: 2 additions & 1 deletion docs/rules/no-empty-initializers.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# @peggyjs/no-empty-initializers

> Top-level and per-instance initializers should not be empty.
> - ⭐️ This rule is included in `plugin:@peggyjs/recommended` preset.
> - ✒️ This rule will fix all errors it finds.
Expand Down Expand Up @@ -55,4 +56,4 @@ foo = "1" { return loc({ type: "foo" }); }
## 🔎 Implementation

- [Rule source](../../src/rules/no-empty-initializers.ts)
- [Test source](../../test/lib/rules/no-empty-initializers.js)
- [Test source](../../test/rules/no-empty-initializers.test.js)
3 changes: 2 additions & 1 deletion docs/rules/no-unused-labels.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# @peggyjs/no-unused-rules

> Labels may not be used without either an action or a semantic predicate to
> reference them.
> - ⭐️ This rule is included in `plugin:@peggyjs/recommended` preset.
Expand Down Expand Up @@ -34,4 +35,4 @@ bar = n:"1" &{ return n === "1"; }
## 🔎 Implementation

- [Rule source](../../src/rules/no-unused-labels.ts)
- [Test source](../../test/lib/rules/no-unused-labels.js)
- [Test source](../../test/rules/no-unused-labels.test.js)
9 changes: 5 additions & 4 deletions docs/rules/no-unused-rules.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# @peggyjs/no-unused-rules

> All rules except for the first one must be referenced by another rule.
> - ⭐️ This rule is included in `plugin:@peggyjs/recommended` preset.
Expand All @@ -12,7 +13,7 @@ might be caused by typos, or they may be left over from previous versions.

```peg.js
// eslint @peggyjs/no-unused-rules

import baz from "./baz.js" // Bad. Not referenced.
foo = "1" // Good. Default entry point.
bar = "2" // Bad. Not referenced.
```
Expand All @@ -21,12 +22,12 @@ bar = "2" // Bad. Not referenced.

```peg.js
// eslint @peggyjs/no-unused-rules

foo = bar
import baz from "./baz.js"
foo = bar / baz
bar = "2"
```

## 🔎 Implementation

- [Rule source](../../src/rules/no-unused-rules.ts)
- [Test source](../../test/lib/rules/no-unused-rules.js)
- [Test source](../../test/rules/no-unused-rules.test.js)
7 changes: 4 additions & 3 deletions docs/rules/quotes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# @peggyjs/quotes

> Enforce the consistent use of double or single quotes.
> - ⭐️ This rule is included in `plugin:@peggyjs/recommended` preset.
> - ✒️ This rule will fix all errors it finds.
Expand Down Expand Up @@ -26,8 +27,8 @@ Foo 'Frequently Observed Ocelot' = "bar"

```peg.js
// eslint @peggyjs/quotes: ["error", "double"]

Foo "More Ocelots" = "bar"
import {"boo" as bar} from "bar.js"
Foo "More Ocelots" = "bar" / boo
```

```peg.js
Expand Down Expand Up @@ -67,4 +68,4 @@ there are no exceptions.
## 🔎 Implementation

- [Rule source](../../src/rules/quotes.ts)
- [Test source](../../test/lib/rules/quotes.js)
- [Test source](../../test/rules/quotes.test.js)
3 changes: 2 additions & 1 deletion docs/rules/rule-order.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# @peggyjs/rule-order

> Rule definitions should come after all references to that rule, unless there
> is a rule loop.
Expand Down Expand Up @@ -36,4 +37,4 @@ Boo = Foo // Loop ok
## 🔎 Implementation

- [Rule source](../../src/rules/rule-order.ts)
- [Test source](../../test/lib/rules/rule-order.js)
- [Test source](../../test/rules/rule-order.test.js)
3 changes: 2 additions & 1 deletion docs/rules/semantic-predicate-must-return.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# @peggyjs/semantic-predicate-must-return

> Top-level and per-instance initializers should not be empty.
> - ⭐️ This rule is included in `plugin:@peggyjs/recommended` preset.
Expand Down Expand Up @@ -40,4 +41,4 @@ foo = n:"1" !{ return n === "2"; }
## 🔎 Implementation

- [Rule source](../../src/rules/semantic-predicate-must-return.ts)
- [Test source](../../test/lib/rules/semantic-predicate-must-return.js)
- [Test source](../../test/rules/semantic-predicate-must-return.test.js)
5 changes: 3 additions & 2 deletions docs/rules/semi.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# @peggyjs/semi

> Enforce consistent use of semicolons
> - ⭐️ This rule is included in `plugin:@peggyjs/recommended` preset.
> - ✒️ This rule will fix errors.
Expand Down Expand Up @@ -42,5 +43,5 @@ foo = "bar"

## 🔎 Implementation

- [Rule source](../../src/rules/camelCase.ts)
- [Test source](../../test/lib/rules/camelCase.js)
- [Rule source](../../src/rules/semi.ts)
- [Test source](../../test/rules/semi.test.js)
3 changes: 2 additions & 1 deletion docs/rules/separate-choices.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# @peggyjs/separate-choices

> Ensure that each top-level choice in a rule is on a new line.
> - ⭐️ This rule is included in `plugin:@peggyjs/recommended` preset.
> - ✒️ This rule will fix all errors it finds.
Expand Down Expand Up @@ -33,4 +34,4 @@ See [Settings](../settings.md).
## 🔎 Implementation

- [Rule source](../../src/rules/separate-choices.ts)
- [Test source](../../test/lib/rules/separate-choices.js)
- [Test source](../../test/rules/separate-choices.test.js)
3 changes: 2 additions & 1 deletion docs/rules/space-ops.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# @peggyjs/space-ops

> Consistent spacing around operators and other punctuation.
> - ⭐️ This rule is included in `plugin:@peggyjs/recommended` preset.
> - ✒️ This rule will fix all errors it finds.
Expand Down Expand Up @@ -66,4 +67,4 @@ The value is a number (see above for meaning).
## 🔎 Implementation

- [Rule source](../../src/rules/space-ops.ts)
- [Test source](../../test/lib/rules/space-ops.js)
- [Test source](../../test/rules/space-ops.test.js)
38 changes: 38 additions & 0 deletions docs/rules/valid-imports.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# @peggyjs/valid-imports

> All imports must point to correct JS files, compiled by Peggy 4.0.0 or later,
> which export the expected rule name as an allowedStartRule.
> - ⭐️ This rule is included in `plugin:@peggyjs/recommended` preset.
## 📖 Rule Details

This rule does a spot-check to see if a module that is imported with a set
of library rules will work. It does NOT load the module with the node loader,
but it does read the file and ensure that:

- The file exists
- It looks like it is a Peggy parser
- It was compiled by a recent-enough version of Peggy that it can be used
for library calls
- It was compiled with the correct rules marked as allowedStartRules

:-1: Examples of **incorrect** code for this rule:

```peg.js
// eslint @peggyjs/valid-imports: "error"
import * as bar from "NO_SUCH_FILE.js"
Bar = bar.boo
```

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

```peg.js
// eslint @peggyjs/valid-imports: "error"
import {"foo" as bar} from "./bar.js"
foo = bar
```

## 🔎 Implementation

- [Rule source](../../src/rules/valid-imports.ts)
- [Test source](../../test/rules/valid-imports.test.js)
18 changes: 11 additions & 7 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";
const pp = require("./");

const globals = require("@peggyjs/eslint-config/flat/globals");

module.exports = [
{
Expand All @@ -8,16 +9,19 @@ module.exports = [
"docs/**",
"lib/**",
"node_modules/**",
"test/rules/fixtures/*.[cm]js",
],
},
require("@peggyjs/eslint-config/flat/js"),
require("@peggyjs/eslint-config/flat/mocha"),
require("@peggyjs/eslint-config/flat/ts"),
require("@peggyjs/eslint-config/flat/mjs"),
{
files: ["*.peggy", "*.pegjs"],
plugin: {
"@peggyjs": pp,
...require("@peggyjs/eslint-config/flat/mocha"),
files: ["test/**/*.js"],
languageOptions: {
globals: { ...globals.node, ...globals.mocha },
ecmaVersion: 2020,
},
rules: pp.configs.recommended.overrides[0].rules,
},
require("@peggyjs/eslint-config/flat/ts"),
require("./lib/flat/all.js"),
];
3 changes: 0 additions & 3 deletions lib/configs/all.d.ts

This file was deleted.

12 changes: 0 additions & 12 deletions lib/configs/all.js

This file was deleted.

3 changes: 0 additions & 3 deletions lib/configs/recommended.d.ts

This file was deleted.

24 changes: 0 additions & 24 deletions lib/configs/recommended.js

This file was deleted.

10 changes: 0 additions & 10 deletions lib/index.d.ts

This file was deleted.

Loading

0 comments on commit c3277ba

Please sign in to comment.