Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Freezed Fails To Generate Class Depending on Line Break #1048

Open
gabrielmcreynolds opened this issue Mar 13, 2024 · 4 comments
Open

Freezed Fails To Generate Class Depending on Line Break #1048

gabrielmcreynolds opened this issue Mar 13, 2024 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@gabrielmcreynolds
Copy link

Describe the bug
I have a class which is generated by freezed and contains a Default decorator on a IList property. Whenever the decorator and the type are on the same line the freezed generator breaks, if they are on different lines it works.

BuildRunner Failed Output

freezed on lib/model.dart: An error `FormatterException` occurred while formatting the generated source for `package:freezed_line_length_issue/model.dart` which was output to `lib/model.freezed.dart`. This may indicate an issue in the generator, the input source code, or in the source formatter. Could not format because the source could not be parsed:

line 20, column 27 of .: Expected an identifier.

20 │ IListConst([])) IList get serverTrails => throw _privateConstructorUsedError;
│ ^

line 20, column 29 of .: A function body must be provided.

20 │ IListConst([])) IList get serverTrails => throw _privateConstructorUsedError;
│ ^

line 20, column 29 of .: Expected a class member.

20 │ IListConst([])) IList get serverTrails => throw _privateConstructorUsedError;
│ ^

line 38, column 27 of .: Expected an identifier.

38 │ IListConst([])) IList serverTrails
│ ^

line 38, column 29 of .: Expected to find '}'.

38 │ IListConst([])) IList serverTrails
│ ^

line 57, column 27 of .: Expected to find ','.

57 │ as IListConst([])) IList,
│ ^

line 57, column 33 of .: Expected to find ','.

57 │ as IListConst([])) IList,
│ ^^^^^

line 58, column 7 of .: Expected to find ';'.

58 │ )as $Val);
│ ^^^^

line 58, column 11 of .: Expected an identifier.

58 │ )as $Val);
│ ^

line 58, column 11 of .: Unexpected text ';'.

58 │ )as $Val);
│ ^

(17 more errors...)
[INFO] Running build completed, took 37ms
[INFO] Caching finalized dependency graph completed, took 38ms
[SEVERE] Failed after 79ms

To Reproduce

//model.dart
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:flutter/foundation.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:freezed_line_length_issue/trail.dart';

part 'model.freezed.dart';

@freezed
class Model with _$Model {
  const Model._();

  const factory Model({
    @Default(IListConst<ServerTrail>([])) IList<ServerTrail> serverTrails, // if IList<ServerTrail> serverTrails is on new line it succeeds
  }) = _Model;
}

For completeness here is trail.dart

import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:flutter/foundation.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'trail.freezed.dart';

@freezed
sealed class Trail with _$Trail {
  const Trail._();

  const factory Trail.FromServer({
    required String id,
    required IList<num> points,
  }) = ServerTrail;
}

Expected behavior
Build runner would generate types w/out throwing errors regardless of whether the Default decorator is on its own line.

@rrousselGit
Copy link
Owner

ServerTrail is code-generated.

Freezed classes do not support generated types as inputs. You'll have to wait for metaprogramming for this.

Do you still have the issue when not using ServerTrail?

@rrousselGit rrousselGit added question Further information is requested and removed needs triage labels Mar 13, 2024
@gabrielmcreynolds
Copy link
Author

gabrielmcreynolds commented Mar 13, 2024

Freezed classes do not support generated types as inputs. You'll have to wait for metaprogramming for this.

I'm a bit confused about this statement. If that is true then why would it work perfectly fine if the @Default is its own line, and I've had a parameter in a freezed class that used a generated type before and it has worked fine.

I think the issue is with the IList class. If I just use a normal List then it builds correctly.

@rrousselGit
Copy link
Owner

There's likely a bug here. It's just that your current sample uses patterns that aren't supported

Overall I don't have too much time to investigate this. Feel free to open a PR :)

@rrousselGit rrousselGit removed the question Further information is requested label Mar 14, 2024
@alvynfash
Copy link

alvynfash commented Apr 2, 2024

Faced the same issue and got it working by replacing with typedef. Very strange behavior.

import 'package:freezed_annotation/freezed_annotation.dart';
part 'filter_cache_state.freezed.dart';

typedef AcceptanceReportFilter = Map<AcceptanceReportFilterType,
    ListFilter<dynamic, AcceptanceReportFilterType>>;

@freezed
class FilterCacheState with _$FilterCacheState {
  const factory FilterCacheState({
    required Session? session,
    required Map<IssueFilterType, ListFilter<dynamic, IssueFilterType>>?
        issueFilter,
    required Map<JournalFilterType, ListFilter<dynamic, JournalFilterType>>?
        journalFilter,

    // This doesn't work as reported in bug with linebreak
    required Map<AcceptanceReportFilterType,
            ListFilter<dynamic, AcceptanceReportFilterType>>?
        acceptanceReportFilter,

    // Replacing same with typedef above works
    // required AcceptanceReportFilter? acceptanceReportFilter,
  }) = _FilterCacheState;

  factory FilterCacheState.empty({required Session session}) =>
      FilterCacheState(
        session: session,
        issueFilter: null,
        journalFilter: null,
        acceptanceReportFilter: null,
      );

  factory FilterCacheState.noSession() => const FilterCacheState(
        session: null,
        issueFilter: null,
        journalFilter: null,
        acceptanceReportFilter: null,
      );
}

enum AcceptanceReportFilterType {
  freeText,
  creator,
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants