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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@
If `--stdin-name` and `--language-version` are both omitted, then parses
stdin using the latest supported language version.

* **Apply class modifiers to API classes.** The dart_style package exposes only
a few classes in its public API: `DartFormatter`, `SourceCode`,
`FormatterException`, and `UnexpectedOutputException`. None were ever
intended to be extended or implemented. They are now all marked `final` to
make that intention explicit.

## 2.3.7

* Allow passing a language version to `DartFomatter()`. Formatted code will be
Expand Down
2 changes: 1 addition & 1 deletion lib/src/back_end/code.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ enum _Marker { start, end }

/// Traverses a [Code] tree and produces the final string of output code and
/// the selection markers, if any.
class _StringBuilder {
final class _StringBuilder {
/// Pre-calculated whitespace strings for various common levels of
/// indentation.
///
Expand Down
4 changes: 2 additions & 2 deletions lib/src/back_end/code_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import 'solution_cache.dart';
/// an instance of this class. It has methods that the piece can call to add
/// output text to the resulting code, recursively format child pieces, insert
/// whitespace, etc.
class CodeWriter {
final class CodeWriter {
final int _pageWidth;

/// Previously cached formatted subtrees.
Expand Down Expand Up @@ -415,7 +415,7 @@ enum Whitespace {
}

/// A level of indentation in the indentation stack.
class _Indent {
final class _Indent {
/// The total number of spaces of indentation.
final int indent;

Expand Down
2 changes: 1 addition & 1 deletion lib/src/back_end/solution.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import 'solution_cache.dart';
/// of the pieces in the tree so they can format themselves. That in turn
/// yields a total number of overflow characters, cost, and formatted output,
/// which are all stored here.
class Solution implements Comparable<Solution> {
final class Solution implements Comparable<Solution> {
/// The states that pieces have been bound to.
///
/// Note that order that keys are inserted into this map is significant. When
Expand Down
2 changes: 1 addition & 1 deletion lib/src/back_end/solution_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import 'solver.dart';
/// the same child Piece and wanting to format it separately with the same
/// indentation. When that happens, sharing this cache allows us to reuse that
/// cached subtree Solution.
class SolutionCache {
final class SolutionCache {
final _cache = <_Key, Solution>{};

/// Returns a previously cached solution for formatting [root] with leading
Expand Down
2 changes: 1 addition & 1 deletion lib/src/back_end/solver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const _maxAttempts = 10000;
/// use it across different Solutions. This enables us to both divide and
/// conquer the Piece tree and solve portions separately, while also
/// reusing work across different solutions.
class Solver {
final class Solver {
final SolutionCache _cache;

final int _pageWidth;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/cli/format_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'output.dart';
import 'show.dart';
import 'summary.dart';

class FormatCommand extends Command<int> {
final class FormatCommand extends Command<int> {
@override
String get name => 'format';

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 @@ -15,7 +15,7 @@ import 'summary.dart';
const dartStyleVersion = '2.3.7';

/// Global options that affect how the formatter produces and uses its outputs.
class FormatterOptions {
final class FormatterOptions {
/// The language version formatted code should be parsed at or `null` if not
/// specified.
final Version? languageVersion;
Expand Down
6 changes: 3 additions & 3 deletions lib/src/cli/summary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import '../source_code.dart';
import 'formatter_options.dart';

/// The kind of summary shown after all formatting is complete.
class Summary {
final class Summary {
static const Summary none = Summary._();

/// Creates a Summary that tracks how many files were formatted and the total
Expand Down Expand Up @@ -41,7 +41,7 @@ class Summary {
}

/// Tracks how many files were formatted and the total time.
class _LineSummary extends Summary {
final class _LineSummary extends Summary {
final DateTime _start = DateTime.now();

/// The number of processed files.
Expand Down Expand Up @@ -81,7 +81,7 @@ class _LineSummary extends Summary {
}

/// Reports how long it took for format each file.
class _ProfileSummary implements Summary {
final class _ProfileSummary implements Summary {
/// The files that have been started but have not completed yet.
///
/// Maps a file label to the time that it started being formatted.
Expand Down
4 changes: 2 additions & 2 deletions lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const tallStyleExperimentFlag = 'tall-style';

/// Constants for the cost heuristics used to determine which set of splits is
/// most desirable.
class Cost {
final class Cost {
/// The cost of splitting after the `=>` in a lambda or arrow-bodied member.
///
/// We make this zero because there is already a span around the entire body
Expand Down Expand Up @@ -64,7 +64,7 @@ class Cost {
}

/// Constants for the number of spaces for various kinds of indentation.
class Indent {
final class Indent {
/// Reset back to no indentation.
static const none = 0;

Expand Down
2 changes: 1 addition & 1 deletion lib/src/dart_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import 'source_code.dart';
import 'string_compare.dart' as string_compare;

/// Dart source code formatter.
class DartFormatter {
final class DartFormatter {
/// The latest Dart language version that can be parsed and formatted by this
/// version of the formatter.
static final latestLanguageVersion = Version(3, 3, 0);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/debug.dart
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ String pieceTree(Piece piece) {
}

/// A stringified representation of a tree of pieces for debug output.
class _PieceDebugTree {
final class _PieceDebugTree {
final String label;
final List<_PieceDebugTree> children = [];

Expand Down
4 changes: 2 additions & 2 deletions lib/src/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:source_span/source_span.dart';

/// Thrown when one or more errors occurs while parsing the code to be
/// formatted.
class FormatterException implements Exception {
final class FormatterException implements Exception {
/// The [AnalysisError]s that occurred.
final List<AnalysisError> errors;

Expand Down Expand Up @@ -53,7 +53,7 @@ class FormatterException implements Exception {

/// Exception thrown when the internal sanity check that only whitespace
/// changes are made fails.
class UnexpectedOutputException implements Exception {
final class UnexpectedOutputException implements Exception {
/// The source being formatted.
final String _input;

Expand Down
2 changes: 1 addition & 1 deletion lib/src/front_end/ast_node_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import 'sequence_builder.dart';
/// To avoid this class becoming a monolith, functionality is divided into a
/// couple of mixins, one for each area of functionality. This class then
/// contains only shared state and the visitor methods for the AST.
class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
@override
final PieceWriter pieces;

Expand Down
2 changes: 1 addition & 1 deletion lib/src/front_end/chain_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import 'piece_factory.dart';
///
/// This lets us create a single [ChainPiece] for the entire series of dotted
/// operations, so that we can control splitting them or not as a unit.
class ChainBuilder {
final class ChainBuilder {
final PieceFactory _visitor;

/// The outermost expression being converted to a chain.
Expand Down
6 changes: 3 additions & 3 deletions lib/src/front_end/comment_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import '../comment_type.dart';
/// construct. These get directly embedded in the [TextPiece] of the code being
/// written. When that [TextPiece] is output later, it will include the comments
/// as well.
class CommentWriter {
final class CommentWriter {
final LineInfo _lineInfo;

/// The tokens whose preceding comments have already been taken by calls to
Expand Down Expand Up @@ -147,7 +147,7 @@ class CommentWriter {

/// A comment in the source, with a bit of information about the surrounding
/// whitespace.
class SourceComment {
final class SourceComment {
/// The text of the comment, including `//`, `/*`, and `*/`.
final String text;

Expand Down Expand Up @@ -210,7 +210,7 @@ class SourceComment {
/// * 2 newlines between `/* c2 */` and `/* c3 */`
/// * Comment `/* c3 */`
/// * 3 newlines between `/* c3 */` and `b`
class CommentSequence extends ListBase<SourceComment> {
final class CommentSequence extends ListBase<SourceComment> {
static const CommentSequence empty = CommentSequence._([0], []);

/// The number of newlines between a pair of comments or the preceding or
Expand Down
2 changes: 1 addition & 1 deletion lib/src/front_end/delimited_list_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import 'piece_factory.dart';
/// delimiter token. Then call [add()] for each [AstNode] that is inside the
/// delimiters. The [rightBracket()] with the closing delimiter and finally
/// [build()] to get the resulting [ListPiece].
class DelimitedListBuilder {
final class DelimitedListBuilder {
final PieceFactory _visitor;

/// The opening bracket before the elements, if any.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/front_end/piece_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import 'sequence_builder.dart';
///
/// Handles updating selection markers and attaching comments to the tokens
/// before and after the comments.
class PieceWriter {
final class PieceWriter {
final DartFormatter _formatter;

final SourceCode _source;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/front_end/sequence_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import 'piece_factory.dart';
/// separate statements inside the sequence. This lets us gracefully handle
/// indenting them and supporting blank lines around them the same way we handle
/// other statements or members in a sequence.
class SequenceBuilder {
final class SequenceBuilder {
final PieceFactory _visitor;

/// The opening bracket before the elements, if any.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/language_version_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import 'profile.dart';
/// looking for package configs for each file as it did formatting if we don't
/// cache. Caching makes it ~10x faster to find the language version for each
/// file.)
class LanguageVersionCache {
final class LanguageVersionCache {
/// The previously cached default language version for all files immediately
/// within a given directory.
///
Expand Down
2 changes: 1 addition & 1 deletion lib/src/piece/adjacent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import '../back_end/code_writer.dart';
import 'piece.dart';

/// A simple piece that just writes its child pieces one after the other.
class AdjacentPiece extends Piece {
final class AdjacentPiece extends Piece {
final List<Piece> pieces;

AdjacentPiece(this.pieces);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/piece/assign.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import 'piece.dart';
/// var [unsplitBlock] =
/// longOperand +
/// anotherOperand;
class AssignPiece extends Piece {
final class AssignPiece extends Piece {
/// Force the block left-hand side to split and allow the right-hand side to
/// split.
static const State _blockSplitLeft = State(1);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/piece/case.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import '../constants.dart';
import 'piece.dart';

/// Piece for a case pattern, guard, and body in a switch expression.
class CaseExpressionPiece extends Piece {
final class CaseExpressionPiece extends Piece {
/// Split after the `=>` before the body.
static const State _beforeBody = State(1);

Expand Down
4 changes: 2 additions & 2 deletions lib/src/piece/chain.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import 'piece.dart';
/// argument,
/// argument,
/// );
class ChainPiece extends Piece {
final class ChainPiece extends Piece {
/// Allow newlines in the last (or next-to-last) call but nowhere else.
static const State _blockFormatTrailingCall = State(1, cost: 0);

Expand Down Expand Up @@ -248,7 +248,7 @@ class ChainPiece extends Piece {

/// A method or getter call in a call chain, along with any postfix operations
/// applies to it.
class ChainCall {
final class ChainCall {
/// Piece for the call.
Piece _call;

Expand Down
2 changes: 1 addition & 1 deletion lib/src/piece/clause.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import 'piece.dart';
///
/// This ensures that when any wrapping occurs, the keywords are always at the
/// beginning of the line.
class ClausePiece extends Piece {
final class ClausePiece extends Piece {
/// State where we split between the clauses but not before the first one.
static const State _betweenClauses = State(1);

Expand Down
2 changes: 1 addition & 1 deletion lib/src/piece/constructor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import 'piece.dart';
/// ]) : firstInitializer = 1,
/// second = 2;
/// // ^ Five spaces of indentation.
class ConstructorPiece extends Piece {
final class ConstructorPiece extends Piece {
static const _splitBeforeInitializers = State(1, cost: 1);

static const _splitBetweenInitializers = State(2, cost: 2);
Expand Down
4 changes: 2 additions & 2 deletions lib/src/piece/control_flow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'piece.dart';

/// A piece for an if statement or element, while statement, or for statement
/// without a block body.
class ControlFlowPiece extends Piece {
final class ControlFlowPiece extends Piece {
/// Whether this is an if statement versus if collection element.
///
/// It's not meaningful for while and for statements/elements.
Expand Down Expand Up @@ -97,7 +97,7 @@ class ControlFlowPiece extends Piece {
/// is the `else if (condition)` and the statement is the subsequent then
/// branch. For the final `else` branch, if there is one, the [header] is just
/// `else` and the statement is the else branch.
class _Section {
final class _Section {
final Piece header;
final Piece statement;

Expand Down
4 changes: 2 additions & 2 deletions lib/src/piece/for.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import '../constants.dart';
import 'piece.dart';

/// A piece for the `for (...)` part of a for statement or element.
class ForPiece extends Piece {
final class ForPiece extends Piece {
/// The `for` keyword.
final Piece _forKeyword;

Expand Down Expand Up @@ -73,7 +73,7 @@ class ForPiece extends Piece {
/// anotherOperand) {
/// ...
/// }
class ForInPiece extends Piece {
final class ForInPiece extends Piece {
/// The variable or pattern initialized with each loop iteration.
final Piece _variable;

Expand Down
2 changes: 1 addition & 1 deletion lib/src/piece/if_case.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import 'piece.dart';
/// if (obj
/// case pattern
/// when cond) ...
class IfCasePiece extends Piece {
final class IfCasePiece extends Piece {
/// Split before the `when` guard clause.
static const State _beforeWhen = State(1);

Expand Down
2 changes: 1 addition & 1 deletion lib/src/piece/infix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'piece.dart';
/// A piece for a series of binary expressions at the same precedence, like:
///
/// a + b + c
class InfixPiece extends Piece {
final class InfixPiece extends Piece {
/// The series of operands.
///
/// Since we don't split on both sides of the operator, the operators will be
Expand Down
2 changes: 1 addition & 1 deletion lib/src/piece/leading_comment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import 'piece.dart';
/// // comment
/// a +
/// b;
class LeadingCommentPiece extends Piece {
final class LeadingCommentPiece extends Piece {
final List<Piece> _comments;
final Piece _piece;

Expand Down
4 changes: 2 additions & 2 deletions lib/src/piece/list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import 'piece.dart';
///
/// ListPieces are usually constructed using [createList()] or
/// [DelimitedListBuilder].
class ListPiece extends Piece {
final class ListPiece extends Piece {
/// The opening bracket before the elements, if any.
final Piece? _before;

Expand Down Expand Up @@ -435,7 +435,7 @@ enum BlockFormat {
/// they vary in whether or not a trailing comma is allowed, whether there
/// should be spaces inside the delimiters when the elements aren't split, etc.
/// This class captures those options.
class ListStyle {
final class ListStyle {
/// How commas should be handled by the list.
///
/// Most lists use [Commas.trailing]. Type parameters and type arguments use
Expand Down
Loading