Skip to content

Commit

Permalink
Fix-inconsistent-exception (#1063)
Browse files Browse the repository at this point in the history
  • Loading branch information
rrousselGit authored Apr 5, 2024
1 parent 2ea97ed commit 5514d8c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/_internal/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,4 @@ packages:
source: hosted
version: "3.1.2"
sdks:
dart: ">=3.0.0 <4.0.0"
dart: ">=2.19.0 <4.0.0"
8 changes: 8 additions & 0 deletions packages/freezed/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 2.5.0

- Added `format: false` flag in the `build.yaml`, to disable formatting
in generated files.
This can significantly improve performance, but may require updating your
CI to not check that generated files are formatted.
- Fixed an `InconsistentAnalysisException`

## 2.4.8

- Fixed a performance problem
Expand Down
7 changes: 5 additions & 2 deletions packages/freezed/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ The following docs are left unedited and kept for the Dart users who have yet to
migrate to Dart 3.
But in the long term, you should stop relying on `when`/`map`.

____
---

Coming from other languages, you may be used to features like "union types"/"sealed classes"/pattern matching.
These are powerful tools in combination with a type system, but Dart currently does not support them.
Expand Down Expand Up @@ -1175,13 +1175,16 @@ targets:
builders:
freezed:
options:
# Tells Freezed not to format .freezed.dart files.
# This can significantly speed up code-generation.
format: false
# Disable the generation of copyWith/== for the entire project
copy_with: false
equal: false
# `when` and `map` can be enabled/disabled entirely by setting them to `true`/`false`
map: false
# We can also enable/disable specific variants of `when`/`map` by setting them to `true`/`false`:
when:
when:
when: true
maybe_when: true
when_or_null: false
Expand Down
2 changes: 2 additions & 0 deletions packages/freezed/example/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ targets:
builders:
freezed:
options:
# Disable formatting of generated files, to improve generation performance.
format: false
union_key: custom-key
union_value_case: pascal
maybe_map: true
Expand Down
1 change: 1 addition & 0 deletions packages/freezed/lib/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'src/freezed_generator.dart';
Builder freezed(BuilderOptions options) {
return PartBuilder(
[FreezedGenerator(Freezed.fromJson(options.config))],
formatOutput: options.config['format'] == false ? (str) => str : null,
'.freezed.dart',
header: '''
// coverage:ignore-file
Expand Down
22 changes: 7 additions & 15 deletions packages/freezed/lib/src/parse_generator.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:async';

import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/analysis/session.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart';
Expand All @@ -16,26 +15,19 @@ abstract class ParserGenerator<GlobalData, Data, Annotation>
LibraryReader oldLibrary,
BuildStep buildStep,
) async {
late SomeResolvedUnitResult unit;
for (var i = 0; i < 10; i++) {
try {
unit = await oldLibrary.element.session.getResolvedUnit(
oldLibrary.element.source.fullName,
);
break;
} on InconsistentAnalysisException {
// retry
}
}
final firstClass = oldLibrary.classes.firstOrNull;
if (firstClass == null) return '';

if (unit is! ResolvedUnitResult) return '';
final ast = await buildStep.resolver.astNodeFor(firstClass, resolve: true);
final unit = ast?.root as CompilationUnit?;
if (unit == null) return '';

final values = StringBuffer();
final globalData = parseGlobalData(unit.unit);
final globalData = parseGlobalData(unit);

var hasGeneratedGlobalCode = false;

for (var declaration in unit.unit.declarations) {
for (var declaration in unit.declarations) {
final declaredElement = declaration.declaredElement;
if (declaredElement == null) continue;

Expand Down

0 comments on commit 5514d8c

Please sign in to comment.