cryptosense / ocamllint

Detect common errors in OCaml code
BSD 2-Clause "Simplified" License
68 stars 3 forks source link

Diff output? #11

Open trevorsummerssmith opened 8 years ago

trevorsummerssmith commented 8 years ago

It would be great if ocamlint could be ran in a way that produced a unified diff, instead of a warning. Linting, in my opinion, is so useful because it is, by definition, things that don't require much thought eg trailing whitespace, an the things ocamllint is currently doing. If ocamlint could return a diff, then it can be used interactively which tools such as arcanist to either autocorrect problems, or interactively ask the user if they want the corrections.

I didn't look into the implementation, so I do not know how feasible this is. I did notice that you state the rules work on the abstract syntax tree. Is the original syntax still around?

Curious to hear your thoughts on the usefulness of this idea, as well as the difficulty in implementing.

Thanks!

emillon commented 8 years ago

Hey,

Glad you like it!

I agree that such a feature would be great. Hlint got something similar quite recently and I think it's improved the tool a lot.

I'm afraid that ppx modules are a bit too far in the chain, acting on the AST. Most of the elements have a location attached, which makes it possible to know some information, but not everything. For example you can't see tokens like begin/end at all (and they are parsed in the same way as (/).

It is possible to pretty-print an AST using functions like Pprintast.string_of_expression but it will just display the code without honoring the locations. ocp_indent does this job to fix the indentation, but I think it has its own parser. I think that merlin would be able to do this too, so we'd have to see if some interop is possible.

One first step in this direction (that would be required anyway to have a diff mode) is to start by adding a "suggestion" part to each rule, as it is not done at the moment.