Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.1.4-wip

* Remove dependencies on analyzer internal implementation.

## 3.1.3

* No longer format imports with configurations and a prefix in the wrong order.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/cli/formatter_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'show.dart';
import 'summary.dart';

// Note: The following line of code is modified by tool/grind.dart.
const dartStyleVersion = '3.1.3';
const dartStyleVersion = '3.1.4';

/// Global options parsed from the command line that affect how the formatter
/// produces and uses its outputs.
Expand Down
74 changes: 65 additions & 9 deletions lib/src/dart_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/diagnostic/diagnostic.dart';
import 'package:analyzer/error/error.dart';
// ignore: implementation_imports
import 'package:analyzer/src/dart/scanner/scanner.dart';
// ignore: implementation_imports
import 'package:analyzer/src/string_source.dart';
import 'package:analyzer/source/source.dart';
import 'package:analyzer/source/timestamped_data.dart';
import 'package:path/path.dart' as p;
import 'package:pub_semver/pub_semver.dart';

import 'exceptions.dart';
Expand Down Expand Up @@ -193,13 +192,16 @@ final class DartFormatter {
// Make sure we consumed all of the source.
var token = node.endToken.next!;
if (token.type != TokenType.CLOSE_CURLY_BRACKET) {
var stringSource = StringSource(text, source.uri);
var error = Diagnostic.tmp(
source: stringSource,
var error = Diagnostic.forValues(
source: _StringSource(text, source.uri ?? ''),
offset: token.offset - inputOffset,
length: math.max(token.length, 1),
diagnosticCode: ParserErrorCode.unexpectedToken,
arguments: [token.lexeme],
diagnosticCode: _FormatterDiagnosticCode(
name: 'UnexpectedToken',
problemMessage: "Unexpected token '${token.lexeme}'.",
uniqueName: 'ParserErrorCode.UNEXPECTED_TOKEN',
),
message: "Unexpected token '${token.lexeme}'.",
);
throw FormatterException([error]);
}
Expand Down Expand Up @@ -273,3 +275,57 @@ enum TrailingCommas {
/// trailing comma is preserved.
preserve,
}

/// A custom [DiagnosticCode] implementation to avoid depending on internal
/// analyzer implementation.
class _FormatterDiagnosticCode extends DiagnosticCode {
_FormatterDiagnosticCode({
required super.name,
required super.problemMessage,
required super.uniqueName,
});

@override
DiagnosticSeverity get severity => DiagnosticSeverity.ERROR;

@override
DiagnosticType get type => DiagnosticType.SYNTACTIC_ERROR;
}

/// An implementation of [Source] for an in-memory Dart string.
///
/// Mostly copied from `package:analyzer/lib/src/string_source.dart`. Copied
/// here to avoid depending on internal implementation.
class _StringSource extends Source {
/// The content of the source.
final String _contents;

@override
final String fullName;

@override
Uri get uri => p.toUri(fullName);

_StringSource(this._contents, this.fullName);

@override
TimestampedData<String> get contents => TimestampedData(0, _contents);

@override
int get hashCode => _contents.hashCode ^ fullName.hashCode;

@override
String get shortName => fullName;

/// Return `true` if the given [object] is a string source that is equal to
/// this source.
@override
bool operator ==(Object object) {
return object is _StringSource &&
object._contents == _contents &&
object.fullName == fullName;
}

@override
bool exists() => true;
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: dart_style
# Note: See tool/grind.dart for how to bump the version.
version: 3.1.3
version: 3.1.4-wip
description: >-
Opinionated, automatic Dart source code formatter.
Provides an API and a CLI tool.
Expand Down