rrousselGit/freezed

It's impossible to use single-quote in an assertion message

オープン

#867 opened on 2023/03/02

 (3 件のコメント) (1 件のリアクション) (1 人の担当者)Dart (300 件のフォーク)github user discovery
buggood first issue

Repository metrics

Stars
 (2,177 個のスター)
PR merge metrics
 (平均マージ 25d 18h) (30d で 1 merged PR)

説明

Versions: freezed: 2.3.2 freezed_annotation: 2.2.0 build_runner: 2.3.3

Describe the bug When using Assert annotation and the message argument has a single-quote mark (apostrophe, '), the build fails.

To Reproduce

Try building the following freezed class:

@freezed
class WithDoubleQuotes with _$WithDoubleQuotes {
  @Assert('false', "This assertion isn't really useful!")
  const factory WithDoubleQuotes() = _WithDoubleQuotes;
}

I also tested with escaped string, multi-line string and using a constant string instead of a string literal. Nothing worked. It's, thus, basically impossible to use single-quote mark in an assertion's message.

Expected behavior The generated constructor should be:

const _$_WithDoubleQuotes(): assert(false, "This assertion isn't really useful!");

or

const _$_WithDoubleQuotes(): assert(false, 'This assertion isn\'t really useful!');

However, the generator tries to generate the following:

const _$_WithDoubleQuotes(): assert(false, 'This assertion isn't really useful!');

The tentative has parsing errors, which breaks the remaining building process.

The full output of trying to generate the given class is:

pintome@BTGDEV548NMD6M ~/P/freezed_double_quote> flutter pub get && flutter pub run build_runner build -d
Running "flutter pub get" in freezed_double_quote...             2,402ms
[INFO] Generating build script...
[INFO] Generating build script completed, took 461ms

[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 50ms

[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 497ms

[INFO] Running build...
[INFO] 1.7s elapsed, 0/1 actions completed.
[SEVERE] freezed on bin/freezed_double_quote.dart:
An error `FormatterException` occurred while formatting the generated source for
  `asset:freezed_double_quote/bin/freezed_double_quote.dart`
which was output to
  `bin/freezed_double_quote.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 76, column 84 of .: Unterminated string literal.
   ╷
76 │   const _$_WithDoubleQuotes(): assert(false, 'This assertion isn't really useful!');
   │                                                                                    ^
   ╵
line 105, column 1 of .: Expected to find ')'.
    ╷
105 │ }
    │ ^
    ╵
line 76, column 66 of .: A function body must be provided.
   ╷
76 │   const _$_WithDoubleQuotes(): assert(false, 'This assertion isn't really useful!');
   │                                                                  ^
   ╵
line 76, column 66 of .: Const constructors can't have a body.
   ╷
76 │   const _$_WithDoubleQuotes(): assert(false, 'This assertion isn't really useful!');
   │                                                                  ^
   ╵
line 76, column 68 of .: Expected to find ';'.
   ╷
76 │   const _$_WithDoubleQuotes(): assert(false, 'This assertion isn't really useful!');
   │                                                                    ^^^^^^
   ╵
line 76, column 75 of .: Variables must be declared using the keywords 'const', 'final', 'var' or a type name.
   ╷
76 │   const _$_WithDoubleQuotes(): assert(false, 'This assertion isn't really useful!');
   │                                                                           ^^^^^^
   ╵
line 76, column 75 of .: Expected to find ';'.
   ╷
76 │   const _$_WithDoubleQuotes(): assert(false, 'This assertion isn't really useful!');
   │                                                                           ^^^^^^
   ╵
line 76, column 81 of .: Operator declarations must be preceded by the keyword 'operator'.
   ╷
76 │   const _$_WithDoubleQuotes(): assert(false, 'This assertion isn't really useful!');
   │                                                                                 ^
   ╵
line 76, column 81 of .: The string '!' isn't a user-definable operator.
   ╷
76 │   const _$_WithDoubleQuotes(): assert(false, 'This assertion isn't really useful!');
   │                                                                                 ^
   ╵
line 76, column 81 of .: Methods must have an explicit list of parameters.
   ╷
76 │   const _$_WithDoubleQuotes(): assert(false, 'This assertion isn't really useful!');
   │                                                                                 ^
   ╵
(2 more errors...)
[INFO] Running build completed, took 2.0s

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 24ms

[SEVERE] Failed after 2.1s
pub finished with exit code 1

コントリビューターガイド