Writing custom lint rules
For example, we recently created a rule enforcing the use of React. Look out for Part 2, where we will dive into the best practices for implementing ESLint fix functions. Ideally, we want the following:. An issue can have a default severity of fatal, error, warning, or ignore. On top of that we have the EsLint plugin for VsCode, which looks like this in the editor: A Detector is responsible for scanning through code and finding Issue instances and reporting them.
A lot more is possible. The last bit of meat is in the CallChecker class, a subclass of ForwardingAstVisitor which will search the source code for places where any method annotated with CarefulNow is called.
The generator sets you up with a nice folder structure, including tests, a proper description and some documentation. There are 4 main parts for every lint check.
Tor Norbye, Apr 28, , 7: One of the tool it supports is Lint. Register Custom Jar File Now we have our customrule. The visitElement method passes a context object. With the plugin installed, you can now run EsLint and it will report on one letter variable names.
This will install the package from the local folder my-eslint-plugin. The last thing we need to do is register the issue registry, such that it can be found by lint. Implementation binds the issue to the detector class. Here's a sample layout file: For example, the manifest detector looks for separate issues like incorrect registration order, missing minSdkVersion declarations, etc. I managed to fix the issue, but due to the nature of the code the possibility of the infinite loop arising again in the future remained.
Start by installing it and the generator: However lint is only a command line tool, it is further integrated with the IntelliJ inspections via the Android plugin for IntelliJ. You can find the relevant code for that here: Ideally, we want the following: We can finish off our rule by adding an automatic fix, and an option to only run this rule on files that match specific extensions no point running the rule on a non-spec file!
The one we want is detekt-sample-ruleset-[version]. The code traversal is accomplished with the lombok. TSLint comes with a huge set of lint rules out of the box, but sometimes you may want to write a custom lint rule that is specific to your codebase.
For example, if you are the author of a library project, and your library project has certain usage requirements, you can write additional lint rules to check that your library is used correctly, and then you can distribute those extra lint rules for users of the library. The above visualization can also be presented as a pure data structure, here in the form of JSON:.
This was not useful for my purposes as the standard annotation processing mechanisms from the API are more geared towards code generation, being used in conjunction with a code generation library like JavaPoet.
Hopefully that was helpful. EsLint is quite a nice alternative for JsHint and is very flexible. In your Android app project, create a new module and select Java Library option. Creating an annotation in and of itself is simple.
TSLint already has a fairly straightforward API for adding custom lint rules, but writing the actual logic of the rules can be quite tricky! You can do that with the following NPM command:. Missing required attribute 'exampleString' [MyId]. This is the static field we defined at the top of the class above: This is a longer explanation of the issue, which should explain to the lint user what the problem is.