Improve macro code formatting.

Change-Id: I3c9f1ddaf6b5562a10b767c2f5d7297a5ed655e7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/210421
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2021-08-17 20:23:42 +00:00 committed by commit-bot@chromium.org
parent 4b1de45034
commit 892b902d11
7 changed files with 53 additions and 51 deletions

View file

@ -805,7 +805,6 @@ void f(A a) {
);
var generatedContent = generatedFile.readAsStringSync();
// TODO(scheglov) Improve macro to be more formatted.
expect(generatedContent, r'''
import 'macro_annotations.dart';
@ -813,12 +812,12 @@ class A {
@observable
int _foo = 0;
int get foo => _foo;
int get foo => _foo;
set foo(int val) {
print('Setting foo to ${val}');
_foo = val;
}
set foo(int val) {
print('Setting foo to ${val}');
_foo = val;
}
}
void f(A a) {

View file

@ -80,7 +80,7 @@ import 'package:meta/meta.dart';
/// TODO(scheglov) Clean up the list of implicitly analyzed files.
class AnalysisDriver implements AnalysisDriverGeneric {
/// The version of data format, should be incremented on every format change.
static const int DATA_VERSION = 169;
static const int DATA_VERSION = 170;
/// The number of exception contexts allowed to write. Once this field is
/// zero, we stop writing any new exception contexts in this process.

View file

@ -52,8 +52,9 @@ class HashCodeMacro implements ClassDeclarationMacro {
.join(' ^ ');
builder.addToClass(
Declaration('''
@override
int get hashCode => $expression;'''),
@override
int get hashCode => $expression;
'''),
);
}
}
@ -73,8 +74,9 @@ class ToStringMacro implements ClassDeclarationMacro {
}).join(', ');
builder.addToClass(
Declaration('''
@override
String toString() => '${classElement.name}($fieldsCode)';'''),
@override
String toString() => '${classElement.name}($fieldsCode)';
'''),
);
}
}

View file

@ -33,15 +33,16 @@ class ObservableMacro implements FieldDeclarationMacro {
var publicName = name.substring(1);
var getter = Declaration(
'$typeCode get $publicName => $name;',
' $typeCode get $publicName => $name;',
);
builder.addToClass(getter);
var setter = Declaration('''
set $publicName($typeCode val) {
print('Setting $publicName to \${val}');
$name = val;
}''');
set $publicName($typeCode val) {
print('Setting $publicName to \${val}');
$name = val;
}
''');
builder.addToClass(setter);
}
}

View file

@ -114,7 +114,7 @@ class MacroGeneratedContent {
return a.data.id - b.data.id;
});
const classMemberCodePrefix = '\n';
const classMemberCodePrefix = '\n ';
const classMemberCodeSuffix = '\n';
// TODO(scheglov) make it required?
var generatedContent = linkingUnit.input.sourceContent!;

View file

@ -669,7 +669,7 @@ class InformativeDataApplier {
) {
var macroGenerationDataList = unitElement.macroGenerationDataList;
if (macroGenerationDataList != null) {
const classMemberCodePrefix = '\n';
const classMemberCodePrefix = '\n ';
const classMemberCodeSuffix = '\n';
var generatedContent = unitInfoData.content;
var shift = 0;

View file

@ -21282,11 +21282,11 @@ library
final b @88
type: int?
constructors
@92
@94
parameters
requiredName final this.a @109
requiredName final this.a @111
type: int
optionalNamed final this.b @117
optionalNamed final this.b @119
type: int?
accessors
synthetic get a @-1
@ -21300,7 +21300,7 @@ class A {
final int a;
final int? b;
A({required this.a, this.b});
A({required this.a, this.b});
}
''');
}
@ -21335,7 +21335,7 @@ library
returnType: int
synthetic get b @-1
returnType: int
get hashCode @102
get hashCode @106
metadata
Annotation
atSign: @ @0
@ -21352,8 +21352,8 @@ class A {
final int a;
final int b;
@override
int get hashCode => a.hashCode ^ b.hashCode;
@override
int get hashCode => a.hashCode ^ b.hashCode;
}
''');
}
@ -21410,15 +21410,15 @@ library
accessors
synthetic get b @-1
returnType: int
get hashCode @126
get hashCode @130
metadata
Annotation
atSign: @ @108
atSign: @ @110
element: dart:core::@getter::override
name: SimpleIdentifier
staticElement: dart:core::@getter::override
staticType: null
token: override @109
token: override @111
returnType: int
macroGeneratedContent
import 'macro_annotations.dart';
@ -21431,8 +21431,8 @@ class A {
class B extends A {
final int b;
@override
int get hashCode => b.hashCode ^ a.hashCode;
@override
int get hashCode => b.hashCode ^ a.hashCode;
}
''');
}
@ -21479,11 +21479,11 @@ library
requiredPositional __f @-1
type: int
returnType: void
get f @80
get f @82
returnType: int
set f @94
set f @98
parameters
requiredPositional val @100
requiredPositional val @104
type: int
returnType: void
macroGeneratedContent
@ -21492,12 +21492,12 @@ class A {
@observable
int _f = 0;
int get f => _f;
int get f => _f;
set f(int val) {
print('Setting f to ${val}');
_f = val;
}
set f(int val) {
print('Setting f to ${val}');
_f = val;
}
}
''');
}
@ -21547,11 +21547,11 @@ library
requiredPositional __f @-1
type: T
returnType: void
get f @75
get f @77
returnType: T
set f @89
set f @93
parameters
requiredPositional val @93
requiredPositional val @97
type: T
returnType: void
macroGeneratedContent
@ -21560,12 +21560,12 @@ class A<T> {
@observable
T _f;
T get f => _f;
T get f => _f;
set f(T val) {
print('Setting f to ${val}');
_f = val;
}
set f(T val) {
print('Setting f to ${val}');
_f = val;
}
}
''');
}
@ -21611,15 +21611,15 @@ library
synthetic get b @-1
returnType: int
methods
toString @101
toString @105
metadata
Annotation
atSign: @ @84
atSign: @ @86
element: dart:core::@getter::override
name: SimpleIdentifier
staticElement: dart:core::@getter::override
staticType: null
token: override @85
token: override @87
returnType: String
macroGeneratedContent
import 'macro_annotations.dart';
@ -21628,8 +21628,8 @@ class A {
final int a;
final int b;
@override
String toString() => 'A(a: $a, b: $b)';
@override
String toString() => 'A(a: $a, b: $b)';
}
''');
}