freezed icon indicating copy to clipboard operation
freezed copied to clipboard

freezed produces json functions that don't pass pana analysis

Open tjarvstrand opened this issue 2 years ago • 4 comments

Is your feature request related to a problem? Please describe. We are currently in the process of publishing a library that uses freezed to generate it's data model. Naturally we don't want to force our users to run code generation so we will publish the library with the generated code included.

As it happens, when the freezed generated mixin is private, the from/tojson functions that get generated don't pass the pana code analysis which deducts up to 30 points from the score on pub.dev.

For example:

@freezed
class MyClass with _$MyClass {
  factory MyClass() = _MyClass;

  factory MyClass.fromJson(Map<String, dynamic> json) => _$MyClassFromJson(json);
}

will generate:

_$_MyClass _$$_MyClassFromJson(Map<String, dynamic> json) => _$_MyClass();

Map<String, dynamic> _$$_MyClassToJson(_$_MyClass instance) => <String, dynamic>{};

Describe the solution you'd like At the moment we work around with a pre-publishing step that rewrites _$_ to __$ and _$$_ to __$$.

This works for us and might be a potential solution to the problem, but I'm not sure if it would work in every case.

tjarvstrand avatar May 04 '23 10:05 tjarvstrand

If I remember correctly pana runs dart analyze --format machine lib, but does not respect your analysis_options.yaml, but always overrides it with package:lints/core.yaml.

You have to make sure you add // ignore_for_file: <lint_rule_to_exclude> to the generated files in your package your self.

Add the following to your packages build.yaml files and run build_runner again

targets:
  $default:
    builders:
      source_gen|combining_builder:
        options:
          ignore_for_file:
            - non_constant_identifier_names

SunlightBro avatar May 04 '23 11:05 SunlightBro

It's a known issue. But honestly I should fix it at some point.

rrousselGit avatar May 04 '23 11:05 rrousselGit

@SunlightBro Thanks, that seems to do the trick.

Still, It's not entirely obvious and it would be nice with a fix.

tjarvstrand avatar May 08 '23 09:05 tjarvstrand

@rrousselGit any update regarding this?

natintosh avatar May 25 '23 12:05 natintosh

I think this can be closed as completed:

Duplicate of #983. Closed by #985. Fixed in v2.4.3.

lishaduck avatar Aug 11 '24 00:08 lishaduck