Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom_lint with visual studio code is not working #308

Closed
lberaldodev opened this issue Jan 27, 2025 · 5 comments
Closed

custom_lint with visual studio code is not working #308

lberaldodev opened this issue Jan 27, 2025 · 5 comments
Assignees
Labels
bug Something isn't working question Further information is requested

Comments

@lberaldodev
Copy link

Describe the bug
I made a new linter for my project, creating a new package, I did run get and closed/open the vsc, but the errors are not showing as expected on visual studio code.

If I run dart run custom_lint, it show every error as expected, but out of command line, it's not working. In the first time that I created the package, installed everything, I was able to see the errors, but idk why it stopped. I did every step again but it's not working.

To Reproduce

Flutter 3.16.9
analyzer: 6.4.1
custom_lint_builder: 0.6.4

Expected behavior
Example of simple lint:

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/error/listener.dart';
import 'package:benefits_lints/models/benefits_lint_rule.dart';
import 'package:benefits_lints/models/rule_config.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';

/// Rule which warns about usages of bang operator ("!")
/// as it may result in unexpected runtime exceptions.
///
/// "Bang" operator with Maps is allowed, as [Dart docs](https://dart.dev/null-safety/understanding-null-safety#the-map-index-operator-is-nullable)
/// recommend using it for accessing Map values that are known to be present.
///
/// ### Example
/// #### BAD:
///
/// ```dart
/// Object? object;
/// int? number;
///
/// final int computed = 1 + number!; // LINT
/// object!.method(); // LINT
/// ```
///
/// #### GOOD:
/// ```dart
/// Object? object;
/// int? number;
///
/// if (number != null) {
///   final int computed = 1 + number;
/// }
/// object?.method();
///
/// // No lint on maps
/// final map = {'key': 'value'};
/// map['key']!;
/// ```
class AvoidNonNullAssertionRule extends BenefitsLintRule {
  /// The [LintCode] of this lint rule that represents
  /// the error whether we use bang operator.
  static const lintName = 'avoid_non_null_assertion';

  AvoidNonNullAssertionRule._(super.config);

  /// Creates a new instance of [AvoidNonNullAssertionRule]
  /// based on the lint configuration.
  factory AvoidNonNullAssertionRule.createRule(CustomLintConfigs configs) {
    final rule = RuleConfig(
      configs: configs,
      name: lintName,
      problemMessage: (_) => 'Avoid using the bang operator. '
          'It may result in runtime exceptions.',
    );

    return AvoidNonNullAssertionRule._(rule);
  }

  @override
  void run(
    CustomLintResolver resolver,
    ErrorReporter reporter,
    CustomLintContext context,
  ) {
    context.registry.addPostfixExpression((node) {
      if (node.operator.type != TokenType.BANG) {
        return;
      }

      // DCM's and Flutter's documentation treats "bang" as a valid way of
      // accessing a Map. For compatibility it's excluded from this rule.
      // See more:
      // * https://dcm.dev/docs/rules/common/avoid-non-null-assertion
      // * https://dart.dev/null-safety/understanding-null-safety#the-map-index-operator-is-nullable
      final operand = node.operand;
      if (operand is IndexExpression) {
        final type = operand.target?.staticType;
        final isInterface = type is InterfaceType;
        final isMap = isInterface &&
            (type.isDartCoreMap ||
                type.allSupertypes.any((v) => v.isDartCoreMap));

        if (isMap) {
          return;
        }
      }

      reporter.reportErrorForNode(code, node);
    });
  }
}

`analyzer:
plugins:
- custom_lint
errors:
deprecated_member_use: info
deprecated_member_use_from_same_package: info
lines_longer_than_80_chars: warning
prefer_const_constructors: warning
unnecessary_type_check: warning
dead_code: warning
unnecessary_import: warning
override_on_non_overriding_member: warning
always_use_package_imports: warning

custom_lint:
rules:
- avoid_non_null_assertion: true

linter:
rules:
- directives_ordering
- constant_identifier_names
- prefer_single_quotes
- lines_longer_than_80_chars
- unnecessary_string_interpolations
- always_declare_return_types
- file_names
- avoid_unused_constructor_parameters
- provide_deprecation_message
- always_use_package_imports
- avoid_relative_lib_imports
- avoid_returning_null_for_void
- avoid_void_async
- unawaited_futures
- unnecessary_await_in_return
- unnecessary_brace_in_string_interps
- unnecessary_lambdas
- avoid_print
- prefer_const_constructors
- prefer_const_constructors_in_immutables
- empty_constructor_bodies
- null_check_on_nullable_type_parameter
- use_build_context_synchronously
`

and set the point of analysis file to my custom package.
include: package:my_custom_lints/analysis_options.yaml

When I enable the debug, I can see that it's reading the file, and my condition is accepted (it calls to reporter.reportError).

I tried to run individual check for each package, it report as well on terminal, but is not working for vsc 👎

Image Image
@lberaldodev lberaldodev added the bug Something isn't working label Jan 27, 2025
@rrousselGit
Copy link
Collaborator

You're using out of date versions. Please try with the latest

@rrousselGit rrousselGit added the question Further information is requested label Jan 27, 2025
@lberaldodev
Copy link
Author

You're using out of date versions. Please try with the latest
Hi men! thanks for you reply :)

I can do a test, but I don't found any related issue about it in the out date / latest versions, so I just want to confirm that everyting is right before, because to I update I think that I will need to update the flutter version (3.19.6) together because it have cross dependencies (meta etc). However I see errors with [android/intellij in versions after 0.6.6]

(#307) :( So i'm confuse if I will fix vsc but in the others will keep failing. What do you think? :)

@rrousselGit
Copy link
Collaborator

Android Studio is known to be broken, but VScode isn't

@MartinAKovachev
Copy link

@rrousselGit Any updates on Android Studio and IntelliJ?
The custom_lint plugin is amazing but it affects the development process.

@rrousselGit
Copy link
Collaborator

Closing as there's no issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants