Skip to content

Commit

Permalink
Change formatOutput function to accept a language version to parse wi…
Browse files Browse the repository at this point in the history
…th (#718)

Updates to the latest dart_style version and passes in a language version. This is in preparation for the tall style formatter which will require a language version to work properly.

- This is a breaking change because the `formatOutput` callback now needs to accept a language version.
- Also drops the default fix for single cascades, fixes will no longer be supported in dart_style.
  • Loading branch information
jakemac53 authored Sep 11, 2024
1 parent e2ba988 commit 76475e3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 36 deletions.
7 changes: 6 additions & 1 deletion source_gen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## 1.5.1-wip
## 2.0.0-wip

- **Breaking Change**: Change `formatOutput` function to accept a language
version parameter.
- **Formatting Change**: Generated code will no longer apply any fixes by
default (previously it would apply the single cascades statements fix). The
new formatter does not support applying fixes.
- Document deduplication behavior for the output of
`GeneratorForAnnotation.generateForAnnotatedElement`.
- Support all the glob quotes.
Expand Down
41 changes: 19 additions & 22 deletions source_gen/lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:build/build.dart';
import 'package:dart_style/dart_style.dart';
import 'package:pub_semver/pub_semver.dart';

import 'generated_output.dart';
import 'generator.dart';
Expand All @@ -18,7 +19,10 @@ import 'utils.dart';
/// A [Builder] wrapping on one or more [Generator]s.
class _Builder extends Builder {
/// Function that determines how the generated code is formatted.
final String Function(String) formatOutput;
///
/// The `languageVersion` is the version to parse the file with, but it may be
/// overridden using a language version comment in the file.
final String Function(String code, Version languageVersion) formatOutput;

/// The generators run for each targeted library.
final List<Generator> _generators;
Expand All @@ -43,7 +47,7 @@ class _Builder extends Builder {
/// [options] to allow output files to be generated into a different directory
_Builder(
this._generators, {
String Function(String code)? formatOutput,
this.formatOutput = _defaultFormatOutput,
String generatedExtension = '.g.dart',
List<String> additionalOutputExtensions = const [],
String? header,
Expand All @@ -57,7 +61,6 @@ class _Builder extends Builder {
...additionalOutputExtensions,
],
}),
formatOutput = formatOutput ?? _formatter.format,
_header = (header ?? defaultFileHeader).trim() {
if (_generatedExtension.isEmpty || !_generatedExtension.startsWith('.')) {
throw ArgumentError.value(
Expand Down Expand Up @@ -165,7 +168,8 @@ class _Builder extends Builder {
var genPartContent = contentBuffer.toString();

try {
genPartContent = formatOutput(genPartContent);
genPartContent =
formatOutput(genPartContent, library.languageVersion.effective);
} catch (e, stack) {
log.severe(
'''
Expand Down Expand Up @@ -299,7 +303,7 @@ class LibraryBuilder extends _Builder {
/// should be indicated in [additionalOutputExtensions].
///
/// [formatOutput] is called to format the generated code. Defaults to
/// [DartFormatter.format].
/// using the standard [DartFormatter].
///
/// [header] is used to specify the content at the top of each generated file.
/// If `null`, the content of [defaultFileHeader] is used.
Expand All @@ -309,21 +313,13 @@ class LibraryBuilder extends _Builder {
/// libraries.
LibraryBuilder(
Generator generator, {
String Function(String code)? formatOutput,
String generatedExtension = '.g.dart',
List<String> additionalOutputExtensions = const [],
String? header,
bool allowSyntaxErrors = false,
BuilderOptions? options,
}) : super(
[generator],
formatOutput: formatOutput,
generatedExtension: generatedExtension,
additionalOutputExtensions: additionalOutputExtensions,
header: header,
allowSyntaxErrors: allowSyntaxErrors,
options: options,
);
super.formatOutput,
super.generatedExtension,
super.additionalOutputExtensions,
super.header,
super.allowSyntaxErrors,
super.options,
}) : super([generator]);
}

Stream<GeneratedOutput> _generate(
Expand Down Expand Up @@ -381,10 +377,11 @@ Future<bool> _hasAnyTopLevelAnnotations(
return false;
}

final _formatter = DartFormatter(fixes: [StyleFix.singleCascadeStatements]);

const defaultFileHeader = '// GENERATED CODE - DO NOT MODIFY BY HAND';

String _defaultFormatOutput(String code, Version version) =>
DartFormatter(languageVersion: version).format(code);

final _headerLine = '// '.padRight(77, '*');

const partIdRegExpLiteral = r'[A-Za-z_\d-]+';
Expand Down
5 changes: 3 additions & 2 deletions source_gen/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: source_gen
version: 1.5.1-wip
version: 2.0.0-wip
description: >-
Source code generation builders and utilities for the Dart build system
repository: https://github.com/dart-lang/source_gen/tree/master/source_gen
Expand All @@ -12,9 +12,10 @@ dependencies:
analyzer: ^6.4.0
async: ^2.5.0
build: ^2.1.0
dart_style: ^2.0.0
dart_style: ^2.3.7
glob: ^2.0.0
path: ^1.8.0
pub_semver: ^2.1.4
source_span: ^1.8.0
yaml: ^3.0.0

Expand Down
6 changes: 3 additions & 3 deletions source_gen/test/builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ foo generated content
PartBuilder(
[const UnformattedCodeGenerator()],
'.foo.dart',
formatOutput: (s) => s,
formatOutput: (s, _) => s,
),
{'$_pkgName|lib/a.dart': 'library a; part "a.foo.dart";'},
generateFor: {'$_pkgName|lib/a.dart'},
Expand All @@ -842,7 +842,7 @@ foo generated content
PartBuilder(
[const UnformattedCodeGenerator()],
'.foo.dart',
formatOutput: (_) => customOutput,
formatOutput: (_, __) => customOutput,
),
{'$_pkgName|lib/a.dart': 'library a; part "a.foo.dart";'},
generateFor: {'$_pkgName|lib/a.dart'},
Expand Down Expand Up @@ -938,7 +938,7 @@ Map<String, String> _createPackageStub({
PartBuilder _unformattedLiteral([String? content]) => PartBuilder(
[_StubGenerator('Literal', () => content)],
'.foo.dart',
formatOutput: (s) => s,
formatOutput: (s, _) => s,
);

class _StubGenerator implements Generator {
Expand Down
8 changes: 0 additions & 8 deletions source_gen/test/src/unformatted_code_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,9 @@ class UnformattedCodeGenerator extends Generator {

static const formattedCode = '''
void hello() => print('hello');
void x() {
<String>[].add('y');
}
''';

static const unformattedCode = '''
void hello ()=> print('hello');
void x() {
<String>[]..add('y');
}
''';
}

0 comments on commit 76475e3

Please sign in to comment.