Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Dart's analyzer and linter?

Tags:

dart

You must have seen inside a analysis_options.yaml:

analyzer:
  exclude: [build]
  strong-mode:
    implicit-casts: false

linter:
  rules:
    - camel_case_types

Are both used at compile time? What are the differences between the two?

like image 704
iDecode Avatar asked May 21 '20 01:05

iDecode


People also ask

What is DART Linter?

The Dart Linter package defines lint rules that identify and report on "lints" found in Dart code. Linting is performed by the Dart analysis server and the dart analyze command in the Dart command-line tool.


1 Answers

The short answer is that they have focus on different things.

The goal of the analyzer is to check that your program is valid. It checks for syntax errors and type errors. Historically, in Dart 1, it was the only way to get type checking because compilers ignored types, but in Dart 2 that's no longer the case.

The analyzer ended up added more checking than what the language required. It can detect dead code or definitely wrong assignments, even when the language allows them, because it has a better static analysis than the language specification requires. In general, the analyzer warns about invalid programs or likely problems. Some warnings are enabled by default, and other you need to enable, because they could lead to false warnings. The gravity of each problem problem can be configured as error, warning, hint (or ignore). Being invalid Dart is always an error.

The linter is developed as a separate project. It only works on valid Dart programs, and it is intended to enforce a coding style. The language doesn't care whether your classes are Capitalized and your variables are lowerCase, but the style guide says they should be, and the linter can enforce that style by reporting a lint error if it's not satisfied. That's what the linter does: It reports style violations. Since style is subjective, all lints need to be enabled, there are no lints enabled by default.

The lints can also be very specific. There are lints which only apply to code using specific libraries, in order to enforce a specific style for that code. A project like Flutter might enable some lints by default in packages that it creates.

The analyzer existed before the linter, and some warnings added to the analyzer would perhaps have been made lints if they were added today. Both depends on annotations in package:meta for adding metadata to drive warnings/lints. The analyzer now includes the linter and provides errors/warnings/hints/lints from both, so a programmer will rarely need to make a distinction. The main difference is that lints are documented in the linter repository and discussions about new lints happens there, independently of changes in the analyzer.

The Dart package pedantic defines a set of lints which are used for all internal Google code. It's very strict and opinionated, with the goal of preventing both potentially dangerous code and any unnecessary style discussion. Other packages provide other sets of lints. There is no official set of lints recommended by the Dart team (yet), as long as you follow the style guide.

Edit: There now are official lints recommended by the Dart team: package:lints.

like image 177
lrn Avatar answered Sep 21 '22 03:09

lrn