How to fix a specific lint warning for the entire project? - flutter

There are many lint warnings in different files of my project like:
Prefer const with constant constructors.
Use key in widget constructors.
...
Unnecessary string interpolation.
Is there a way to only fix a particular warning, something like
dart fix prefer_const_constructors
PS: I don't want to fix all the warnings, for that I can run dart fix --apply.

Yes, It is possible by changing lint rules. For time being, you have to add only rules which you want to fix and ignore all others.
Follow these steps
In the project, you have to create analysis_options.ymal file. The content of the file will look like this.
linter:
rules:
prefer_const_constructors: true
More details here
After that try to run dart fix, since only one lint rule is enabled it only gives you suggestions for that only.

To ignore a single line, you can add a comment above the line:
// ignore: non_constant_identifier_names
final NEW = 'NEW';
To ignore for the whole file, you can add a comment at the top of the file:
// ignore_for_file: non_constant_identifier_names
To ignore for the whole project, you can set the rule to false in your analysis_options.yaml file:
include: package:lints/recommended.yaml
linter:
rules:
non_constant_identifier_names: false
Refer this for more
Customizing static analysis
Setting up Lint Rules in Dart-Flutter

Related

Flutter linter rules and structure in analysis_options.yaml file

I have been looking off and on for a few days now and I'm trying to customize the rules for linting my flutter project. I'd like to turn off the obvious warnings like unused local variables. Yet when I enter it into the rules section it doesn't work amongst other rules. Yet some do work.
I'm also confused as to the formatting. I see some answers on the internet that use a hyphen then the rule where others are stating the rule with a colon and a true or false. I tried the hyphen and I get a block mapping type of error thrown at me.
Here is my file right now...
include: package:flutter_lints/flutter.yaml
# include: package:lint/analysis_options.yaml
linter:
rules:
always_declare_return_types: false
use_key_in_widget_constructors: false
unused_element: false
unused_local_variable: true
unused_import: false
avoid_print: false
prefer_const_constructors: false
require_trailing_commas: true
always_use_package_imports: false
The trailing commas one worked but the more annoying and obvious warnings I cant seem to suppress.
Can someone please clarify the structure and maybe why the some rules are working and some arent?
If I do the quick fixes inside the files to ignore them it works but I want them project wide.
EDIT
well the unused_local_variable is the warning that started me on this quest for information and understanding of how the analysis_options file works.

Flutter - Objectbox generation additional options for non CamelCase

When I run the generator in flutter, it will generate my objectbox.g.dart file as expected and works fine but I get 100's of lint warnings
Name non-constant identifiers using lowerCamelCase.
I don't have my properties set with camel case, So I have properties like CustomerID, Billing_Address1 etc.
In my model files I have
// ignore_for_file: non_constant_identifier_names
which stops the linting warnings. I prefer this style for models.
I want the generated file to also have this added when rebuilt. It already adds
// ignore_for_file: camel_case_types
So my question is how can I default this also?
Thanks
Just add analysis_options.yaml to your project, e.g. with the following contents:
linter:
rules:
non_constant_identifier_names: false
or
analyzer:
exclude:
- lib/objectbox.g.dart

VSCode codeActionsOnSave ignore specific file

I'm using organizeImports on save, but there are some instances where the order matters and this causes an issue. I can't find anywhere if there's a way to simply ignore a page, either through comments within the page (ideally) or within the config settings.
Perhaps there's an extension that provides this functionality if not baked in. In any case, really appreciate any help tracking down a solution for this.
Couldn't find how to do it properly but here is a solution that might solve your end needs if you use prettier.
The way I handled organizeImports selectively for files was as follow.
1 - Make sure your default formatter is prettier (as explained below)
https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode
2 - In your settings set formatOnSave to true but organizeImports to false such as below.
{
"editor.codeActionsOnSave": {
"source.organizeImports": false
},
"editor.formatOnSave": true
}
3 - Install https://github.com/simonhaenisch/prettier-plugin-organize-imports
This is a prettier plugin that allows organizing import as part of the prettier formatting and has an option to disable organize import for files (i.e. // organize-imports-ignore )
If you're using ESLint (which I would highly recommend), you can use sort-imports or import/order (via eslint-plugin-import) to sort your import statements across the entire project, then ignore the rule in specific files/regions with special comments, like this:
/* eslint-disable import/order */
import * from "abcdefg";
import "cool-module";
// etc...
VSCode has a great ESLint plugin, which in combination with
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true,
},
will auto format and fix your code whenever the file is saved.

Ignore or bypass errors phpcs

How to bypass or ignore specific errors/warnings in vscode?, I am using phpcs.
What you are looking for is to ignore the warning and/or errors, that are notified by the phpcs in the console of the vscode.
For Warnings
Use the following config in your settings.json
"phpcs.showWarnings": false,
this will remove all the warnings displayed in the output console.
For Errors
You should go trough the DOCS for complete details but to remove the errors related to the Doc block and the formatting standards you can set the
"phpcs.errorSeverity": 6,
Although it is mostly used for testing or code reviews to check for total warnings and errors by setting different values for both, but for development i dont do that and keep it to the default value that is 5 but you can get rid of those errors above in your image.
The vscode-phpcs refers to the GitHub project squizlabs/PHP_CodeSniffer, which integrates PHP_CodeSniffer into VSCode.
Its readme mentions the setting phpcs.ignorePatterns:
An array of glob patterns to skip files and folders that match when linting your documents.
{
"phpcs.ignorePatterns": [
"*/ignored-file.php",
"*/ignored-dir/*"
]
}
That refers to PHP CodeSniffer --ignore option.
That is not what you want exactly, since it ignores all errors on a given set of file.
But you could use PHP CodeSniffer syntax to ignore errors:
Ignoring Parts of a File
Some parts of your code may be unable to conform to your coding standard. For example, you might have to break your standard to integrate with an external library or web service.
To stop PHP_CodeSniffer generating errors for this code, you can wrap it in special comments. PHP_CodeSniffer will then hide all errors and warnings that are generated for these lines of code.
$xmlPackage = new XMLPackage;
// phpcs:disable
$xmlPackage['error_code'] = get_default_error_code_value();
$xmlPackage->send();
// phpcs:enable
Again, not exactly what you want, since you have to specify that on a file-by-file basis
You can disable multiple error message codes, sniff, categories, or standards by using a comma separated list.
You can also selectively re-enable just the ones you want.
The following example disables the entire PEAR coding standard, and all the Squiz array sniffs, before selectively re-enabling a specific sniff. It then re-enables all checking rules at the end.
// phpcs:disable PEAR,Squiz.Arrays
$foo = [1,2,3];
bar($foo,true);
// phpcs:enable PEAR.Functions.FunctionCallSignature
bar($foo,false);
// phpcs:enable

Set .history folder as ignored by intellisense

I need to ignore .history folder from intellisense.
Now it looks like this when I start typing Focus:
As you can see intellisense will offer me every Identical class found in .history folder and this is very very confusing (and find correct one).
I try find something in vscode settings, and on editor site but no success.
.history folder is ignored from git, display in explorer, and tslint:
"files.exclude": {
"**/.history": true ...
},
"files.watcherExclude": {
"**/.history/**": true ...
},
"tslint.exclude": ["**/.history/**"] ...
How to achieve ignore .history from intellisense?
Next part is based on the answer from Matt
An important assumption:
Visual Studio Code itself does not contain automatic import and you need an extension for it.
Solution:
I am using extension Auto Import (steoates.autoimport) which contains the setting autoimport.filesToScan. I changed default value from "**/*.{ts,tsx}" to "{src,e2e,node_modules}/**/*.{ts,tsx}" and now everything work as I expected.
Those suggestions are coming whatever extension you have installed that is providing auto-import functionality. Please check to see if that extension has its own exclude setting that you also need to configure
In your settings.json, you could add this:
"autoimport.filesToScan": "./index.{ts,tsx} ./App.{ts,tsx} ./src/**.*.{ts,tsx}"
Or, if you prefer:
"autoimport.filesToScan": "./{index,App,src/**}.{ts,tsx}"