/spicy-lint

Linting rules for the Spicy language

Primary LanguageShellMIT LicenseMIT

Linting rules for Spicy

This repository contains a Spicy integration for ast-grep(sg) and a collection of linting rules.

Quickstart

Building this package requires Node. To build ast-grep support for Spicy run

npm install

With that you can run the bundled linting rules with

# Check all files in directory.
./spicy-lint .

# Check given file(s).
./spicy-lint foo.spicy
module foo;

type X = unit {
    : b"HELLO";
    x: uint8 { print self.x; }
};
$ spicy-lint foo.spicy
note[print-in-prod]: print in non-debug code found
  ┌─ foo.spicy:5:16

5 │     x: uint8 { print self.x; }
  │     -----------^^^^^----------

  = Usually 'print' statements are used for debugging. Adding them in production
    code can introduce overhead, so it is best to either remove them, or move
    them to hooks marked '%debug'.

foo.spicy
note[use-skip]: use 'skip' for anonymous field
@@ -0,6 +0,6 @@
1 1│ module foo;
2 2│
3 3│ type X = unit {
4  │-    : b"HELLO";
  4│+    : skip  b"HELLO";
5 5│     x: uint8 { print self.x; }
6 6│ };
Note:
If a field is anonymous and not accessed via '$$' it can be declared 'skip'
so it is not stored.

foo.spicy
note[use-dd]: use $$ instead of field access
@@ -1,5 +1,5 @@
2 2│
3 3│ type X = unit {
4 4│     : b"HELLO";
5  │-    x: uint8 { print self.x; }
  5│+    x: uint8 { print $$; }
6 6│ };
Note:
Use '$$' to refer to a field in its hooks. This is more compact and requires
less changes if the field gets renamed.

Advanced usage

The linter script spicy-lint is a thin convenience wrapper around ast-grep(sg) which sets up an enviroment to run the linting rules in this repo, but with the Spicy integration in sgconfig.yml one has access to all ast-grep(sg) features, e.g., to run queries on code with sg run:

# To use the sg binary bundled in this package.
$ npm exec -- sg run -p 'foo' foo.spicy
foo.spicy:1:module foo;

# To use a `sg` binary from your system.
$ sg run -p 'foo' foo.spicy
foo.spicy:1:module foo;

See the ast-grep(sg) tooling documentation for an overview.

Contributing

If you developed rules which could be useful in general please consider opening a PR.