dart-lang / linter

Linter for Dart.
https://dart.dev/tools/linter-rules
BSD 3-Clause "New" or "Revised" License
630 stars 170 forks source link

Lint warnings if ErrorSummary, ErrorDescription, or ErrorHint constructors are called with bad values. #1524

Open jacob314 opened 5 years ago

jacob314 commented 5 years ago

These Flutter constructors provide a hook for a kernel transformer to extract out object references in package:flutter. See: https://github.com/flutter/flutter/issues/27673

A lint warning should trigger if the constructor is called with a value other than a string literal as in that case there is no way to robustly extract out the object ids and we will not attempt to be clever and handle any cases where the value passed to the constructor is more than a string literal. The goal is to keep the kernel transformer simple only handling the very basic cases that are needed rather than providing an unfocused and complex templating scheme.

Examples ``dart // Good: ErrorDescription('$someMessage'); ErrorDescription('Maybe you have fallen prey to floating point rounding errors.') ErrorDescription('The $labelA is $valueA, but the $labelB is $valueB.') // Triggering lint errors: ErrorDescription(someMessage) ErrorDescription('Maybe you have fallen prey to' + ' floating point rounding errors.') // foo.bar.baz() is a complex expression and we cannot be confident we will extract out the value reference for it that the person writing the template expected. ErrorDescription('The $labelA is $valueA, but the $labelB is ${foo.bar.baz()}.')


**Additional context**
It is fine to start out very restricted and then allow more cases later. It would be worse if 
we allow a bunch of cases we cannot handle robustly. There is always the fallback of using DiagnosticsProperty directly if you need to express something the templates cannot handle.
pq commented 5 years ago

Looks great. Thanks for the examples too. 👍

The only one I'd like to get more crisp on is this bit:

foo.bar.baz() is a complex expression and we cannot be confident we will extract out the value reference for it that the person writing the template expected.

Can you clarify what constitutes an expression that's too complex for the kernel transformer?