Skip to content

Commit

Permalink
Improve macro code formatting.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
scheglov authored and commit-bot@chromium.org committed Aug 17, 2021
1 parent 4b1de45 commit 892b902
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -805,20 +805,19 @@ void f(A a) {
);

var generatedContent = generatedFile.readAsStringSync();
// TODO(scheglov) Improve macro to be more formatted.
expect(generatedContent, r'''
import 'macro_annotations.dart';
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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/dart/analysis/driver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 6 additions & 4 deletions pkg/analyzer/lib/src/macro/builders/data_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ class HashCodeMacro implements ClassDeclarationMacro {
.join(' ^ ');
builder.addToClass(
Declaration('''
@override
int get hashCode => $expression;'''),
@override
int get hashCode => $expression;
'''),
);
}
}
Expand All @@ -73,8 +74,9 @@ class ToStringMacro implements ClassDeclarationMacro {
}).join(', ');
builder.addToClass(
Declaration('''
@override
String toString() => '${classElement.name}($fieldsCode)';'''),
@override
String toString() => '${classElement.name}($fieldsCode)';
'''),
);
}
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/analyzer/lib/src/macro/builders/observable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/macro/impl/macro.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/summary2/informative_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
66 changes: 33 additions & 33 deletions pkg/analyzer/test/src/summary/resynthesize_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21300,7 +21300,7 @@ class A {
final int a;
final int? b;

A({required this.a, this.b});
A({required this.a, this.b});
}
''');
}
Expand Down Expand Up @@ -21335,7 +21335,7 @@ library
returnType: int
synthetic get b @-1
returnType: int
get hashCode @102
get hashCode @106
metadata
Annotation
atSign: @ @0
Expand All @@ -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;
}
''');
}
Expand Down Expand Up @@ -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';
Expand All @@ -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;
}
''');
}
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
}
''');
}
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
}
''');
}
Expand Down Expand Up @@ -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';
Expand All @@ -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)';
}
''');
}
Expand Down

0 comments on commit 892b902

Please sign in to comment.