Skip to content

Commit

Permalink
add a dataClass macro that combines several other macros
Browse files Browse the repository at this point in the history
Change-Id: I1d20f286805fa85e4b7853b5f0fa8fd7a77beb47
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/210426
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Auto-Submit: Jake Macdonald <jakemac@google.com>
  • Loading branch information
scheglov authored and commit-bot@chromium.org committed Aug 17, 2021
1 parent 892b902 commit 099c5a5
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/analyzer/lib/src/macro/builders/data_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ class AutoConstructorMacro implements ClassDeclarationMacro {
}
}

class DataClassMacro implements ClassDeclarationMacro {
const DataClassMacro();

@override
void visitClassDeclaration(
ast.ClassDeclaration declaration,
ClassDeclarationBuilder builder,
) {
const AutoConstructorMacro().visitClassDeclaration(declaration, builder);
const HashCodeMacro().visitClassDeclaration(declaration, builder);
const ToStringMacro().visitClassDeclaration(declaration, builder);
}
}

class HashCodeMacro implements ClassDeclarationMacro {
const HashCodeMacro();

Expand Down
6 changes: 6 additions & 0 deletions pkg/analyzer/lib/src/summary2/library_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ class LibraryBuilder {
classBuilder,
);
}
if (hasMacroAnnotation(declaration, 'dataClass')) {
macro.DataClassMacro().visitClassDeclaration(
declaration,
classBuilder,
);
}
if (hasMacroAnnotation(declaration, 'hashCode')) {
macro.HashCodeMacro().visitClassDeclaration(
declaration,
Expand Down
86 changes: 86 additions & 0 deletions pkg/analyzer/test/src/summary/resynthesize_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21305,6 +21305,92 @@ class A {
''');
}

test_macro_dataClass() async {
addLibrarySource('/macro_annotations.dart', r'''
library analyzer.macro.annotations;
const dataClass = 0;
''');
var library = await checkLibrary(r'''
import 'macro_annotations.dart';
@dataClass
class A {
final int a;
final int b;
}
''');
checkElementText(library, r'''
library
imports
macro_annotations.dart
definingUnit
classes
class A @50
metadata
Annotation
atSign: @ @33
element: macro_annotations.dart::@getter::dataClass
name: SimpleIdentifier
staticElement: macro_annotations.dart::@getter::dataClass
staticType: null
token: dataClass @34
fields
final a @66
type: int
final b @81
type: int
synthetic hashCode @-1
type: int
constructors
@87
parameters
requiredName final this.a @104
type: int
requiredName final this.b @121
type: int
accessors
synthetic get a @-1
returnType: int
synthetic get b @-1
returnType: int
get hashCode @149
metadata
Annotation
atSign: @ @129
element: dart:core::@getter::override
name: SimpleIdentifier
staticElement: dart:core::@getter::override
staticType: null
token: override @130
returnType: int
methods
toString @208
metadata
Annotation
atSign: @ @189
element: dart:core::@getter::override
name: SimpleIdentifier
staticElement: dart:core::@getter::override
staticType: null
token: override @190
returnType: String
macroGeneratedContent
import 'macro_annotations.dart';
@dataClass
class A {
final int a;
final int b;

A({required this.a, required this.b});

@override
int get hashCode => a.hashCode ^ b.hashCode;

@override
String toString() => 'A(a: $a, b: $b)';
}
''');
}

test_macro_hashCode() async {
addLibrarySource('/macro_annotations.dart', r'''
library
Expand Down

0 comments on commit 099c5a5

Please sign in to comment.