nim-lang / RFCs

A repository for your Nim proposals.
135 stars 26 forks source link

Nim v2: Any plans for Lint ? #502

Closed YaDev closed 1 year ago

YaDev commented 1 year ago

I really like Nim lang and think it will/should be more popular.

However, it is lacking a "basic static code analyzer" and the code can be written in many ways (no right way).

As a JS/TS dev, ESLint is our best tool to make sure the deliver code for the clients matches the required quality. Also, Python has Flake8 and is used in many Python projects.

My proposal/suggestion: Adding a new function to Nim cmd to check if the project root dir has a config json file named ".nimlint" if this file exists, Nim cmd will check the rules, analyze the code, print any warnings and stop code compilation if these rules are enforced.

This proposal should not break any existing projects or force people to write a new code but will give DEVs an opportunity to write code in the same style.

I think code quality is really critical for businesses and nim lang can be the first option for many (fast + easy writing).

Araq commented 1 year ago

The compiler keeps producing new warnings outlining questionable code all the time already. There is nothing to do here.

YaDev commented 1 year ago

@Araq thanks for replying.

I think you should check lint tools.

The code var x = 1 and var x: int = 1 are both valid but one has a type. So, nim cmd will not show any warnings.

Also, the naming "Enforcing naming conventions" is import and is missing Python var my_name = "test" vs JS/TS camel pascal var myName = "test"

Nim cmd has no function to check these.

Araq commented 1 year ago

--styleCheck:error does enforce naming conventions.

YaDev commented 1 year ago

Thanks @Araq

Glad to see such changes.

is there a way to enforce rules like "no-unsed-var" or "no-unsed-function"?

Araq commented 1 year ago

--warningAsError:MsgX:on where MsgX is what the compiler outputs as unused variable x [MsgX]

arnetheduck commented 1 year ago

Related: https://github.com/nim-lang/Nim/issues/20905

it would be nice to make regular the options for "levels", which in turn would make it easier to define "linting configurations" that are more strict, for clean code

ringabout commented 1 year ago

Hello @YaDev You can also import compiler API to implement your own lint. See also https://github.com/nim-dev/nimlint which is outdated though.

YaDev commented 1 year ago

Thanks for sharing this @ringabout

I am working on it. (many regex but wont be hard)

Araq commented 1 year ago

Regexes cannot work as they would report the issues in string literals and comments. Real "linters" use real parsers, or at least real tokenizers.

YaDev commented 1 year ago

Thanks for the tip @Araq and the great work for building this lang agin.

I think I am going to do it the ESLint way since many IDEAs support ESLint and it should make it easier to port existing ESLInt IDEAs plugins to work for nim.