run method

  1. @override
void run(
  1. CustomLintResolver resolver,
  2. ChangeReporter reporter,
  3. CustomLintContext context,
  4. AnalysisError analysisError,
  5. List<AnalysisError> others,
)

Emits lints for a given file.

run will only be invoked with files respecting filesToAnalyze Emits source changes for a given error.

Optionally others can be specified with a list of similar errors within the same file. This can be used to provide an option for fixing multiple errors at once.

Implementation

@override
void run(
  CustomLintResolver resolver,
  ChangeReporter reporter,
  CustomLintContext context,
  AnalysisError analysisError,
  List<AnalysisError> others,
) async {
  final resolved = await resolver.getResolvedUnitResult();

  context.registry.addInstanceCreationExpression((node) {
    if (!analysisError.sourceRange.intersects(node.sourceRange)) return;

    final builder = reporter.createChangeBuilder(
      message: 'Use path.join(...)',
      // TODO: Is there a standard priority for lint fixes
      priority: 999,
    );

    final path = analysisError.data as String;
    final segmentsString = createSegmentsString(path);

    builder.addDartFileEdit((builder) {
      final pathImport = resolved.importFromUri('package:path/path.dart');
      if (pathImport == null) {
        builder.addSimpleInsertion(
          resolved.lastImportEnd,
          resolved.createImportText('package:path/path.dart', as: 'path'),
        );
      }

      final pathAlias = pathImport?.prefix ?? 'path';
      builder.addSimpleReplacement(
        analysisError.sourceRange,
        '$pathAlias.join($segmentsString)',
      );
    });
  });
}