Files
sdk/pkg/linter/tool/util/formatter.dart
Paul Berry afcfbbeba8 Migrate developer experience packages to new constructor decl syntax.
(Part of https://github.com/dart-lang/sdk/issues/63288)

This change migrates the packages owned by the developer experience
team to use the new constructor declaration syntax, described in
https://github.com/dart-lang/language/blob/main/accepted/future-releases/primary-constructors/feature-specification.md#abbreviations-of-in-body-constructor-declarations.

This change was performed in an automated fashion, by (a) bumping the
packages' SDK constraints to `3.13.0-0`, (b) enabling the lints
`unnecessary_type_name_in_constructor` and
`unnecessary_const_in_enum_constructor`, (c) fixing the resulting lint
failures using `dart fix`, and then (d) reformatting the affected
files.

To ease code review, I've reverted unrelated formatting changes.

Since this change requires bumping SDK constaints to `3.13.0-0`, it
was only performed on packages that are *not* published on
pub. (Packages that *are* published on pub should remain on lower
language versions until at least after the stable version of 3.13 is
released, so that we don't block users on the stable channel from
receiving updates to those packages.)

Change-Id: Ibb4daebafd239da58251e838ea6a3f336a6a6964
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/505046
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
SLSA-Policy-Verified: SLSA Policy Verification Service <devtools-gerritcodereview-exitgate@google.com>
2026-05-27 14:52:58 -07:00

115 lines
3.2 KiB
Dart

// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
import 'dart:math';
import 'package:analyzer/diagnostic/diagnostic.dart';
import 'package:analyzer/source/line_info.dart';
import 'package:analyzer/src/utilities/extensions/source.dart';
import 'package:analyzer/src/utilities/extensions/string.dart';
String _getLineContents(int lineNumber, Diagnostic diagnostic) {
var path = diagnostic.source.fullName;
var file = File(path);
String failureDetails;
if (!file.existsSync()) {
failureDetails = 'file at $path does not exist';
} else {
var lines = file.readAsLinesSync();
var lineIndex = lineNumber - 1;
if (lines.length > lineIndex) {
return lines[lineIndex];
}
failureDetails =
'line index ($lineIndex), outside of file line range (${lines.length})';
}
throw StateError('Unable to get contents for line: $failureDetails');
}
class ReportFormatter {
final StringSink out;
final Iterable<Diagnostic> diagnostics;
int diagnosticCount = 0;
new(this.diagnostics, this.out);
/// Override to influence diagnostic sorting.
int compare(Diagnostic diagnostic1, Diagnostic diagnostic2) {
// Severity.
var compare = diagnostic2.diagnosticCode.severity.compareTo(
diagnostic1.diagnosticCode.severity,
);
if (compare != 0) {
return compare;
}
// Path.
compare = Comparable.compare(
diagnostic1.source.fullName.toLowerCase(),
diagnostic2.source.fullName.toLowerCase(),
);
if (compare != 0) {
return compare;
}
// Offset.
return diagnostic1.offset - diagnostic2.offset;
}
void write() {
_writeLints();
_writeSummary();
out.writeln();
}
void writeLint(
Diagnostic diagnostic, {
required int offset,
required int line,
required int column,
}) {
// test/engine_test.dart 452:9 [lint] DO name types using UpperCamelCase.
out
..write('${diagnostic.source.fullName} ')
..write('$line:$column ')
..writeln(
'[${diagnostic.diagnosticCode.type.displayName}] ${diagnostic.message}',
);
var contents = _getLineContents(line, diagnostic);
out.writeln(contents);
var spaces = column - 1;
var arrows = max(1, min(diagnostic.length, contents.length - spaces));
var result = '${" " * spaces}${"^" * arrows}';
out.writeln(result);
}
void _writeLint(Diagnostic diagnostic) {
var offset = diagnostic.offset;
var lineInfo = LineInfo.fromContent(diagnostic.source.stringContents);
var location = lineInfo.getLocation(offset);
var line = location.lineNumber;
var column = location.columnNumber;
writeLint(diagnostic, offset: offset, column: column, line: line);
}
void _writeLints() {
for (var e in (diagnostics.toList()..sort(compare))) {
++diagnosticCount;
_writeLint(e);
}
out.writeln();
}
void _writeSummary() {
var summary =
'files analyzed, '
'$diagnosticCount ${"issue".pluralized(diagnosticCount)} found.';
out.writeln(summary);
}
}