X Tutup
Skip to content

Commit d9f362a

Browse files
committed
fix(transformers): Fix @Input/@output annotations with setters/getters
Fix @input annotations to work with setter methods in dart, and fix @output annotations to work with getter methods in Dart when using transformers. Closes #5251 Closes #5259
1 parent 7f6289c commit d9f362a

File tree

3 files changed

+45
-29
lines changed

3 files changed

+45
-29
lines changed

modules_dart/transform/lib/src/transform/common/directive_metadata_reader.dart

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -232,21 +232,11 @@ class _DirectiveMetadataVisitor extends Object
232232
for (var variable in node.fields.variables) {
233233
for (var meta in node.metadata) {
234234
if (_isAnnotation(meta, 'Output')) {
235-
final renamed = _getRenamedValue(meta);
236-
if (renamed != null) {
237-
_outputs.add('${variable.name}: ${renamed}');
238-
} else {
239-
_outputs.add('${variable.name}');
240-
}
235+
_addPropertyToType(_outputs, variable.name.toString(), meta);
241236
}
242237

243238
if (_isAnnotation(meta, 'Input')) {
244-
final renamed = _getRenamedValue(meta);
245-
if (renamed != null) {
246-
_inputs.add('${variable.name}: ${renamed}');
247-
} else {
248-
_inputs.add('${variable.name}');
249-
}
239+
_addPropertyToType(_inputs, variable.name.toString(), meta);
250240
}
251241

252242
if (_isAnnotation(meta, 'HostBinding')) {
@@ -265,6 +255,14 @@ class _DirectiveMetadataVisitor extends Object
265255
@override
266256
Object visitMethodDeclaration(MethodDeclaration node) {
267257
for (var meta in node.metadata) {
258+
if (_isAnnotation(meta, 'Output') && node.isGetter) {
259+
_addPropertyToType(_outputs, node.name.toString(), meta);
260+
}
261+
262+
if (_isAnnotation(meta, 'Input') && node.isSetter) {
263+
_addPropertyToType(_inputs, node.name.toString(), meta);
264+
}
265+
268266
if (_isAnnotation(meta, 'HostListener')) {
269267
if (meta.arguments.arguments.length == 0 ||
270268
meta.arguments.arguments.length > 2) {
@@ -280,6 +278,15 @@ class _DirectiveMetadataVisitor extends Object
280278
return null;
281279
}
282280

281+
void _addPropertyToType(List type, String name, Annotation meta) {
282+
final renamed = _getRenamedValue(meta);
283+
if (renamed != null) {
284+
type.add('${name}: ${renamed}');
285+
} else {
286+
type.add('${name}');
287+
}
288+
}
289+
283290
//TODO Use AnnotationMatcher instead of string matching
284291
bool _isAnnotation(Annotation node, String annotationName) {
285292
var id = node.name;

modules_dart/transform/test/transform/directive_processor/all_tests.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,14 +498,14 @@ void allTests() {
498498

499499
it('should merge `outputs` from the annotation and fields.', () async {
500500
var model = await _testCreateModel('directives_files/components.dart');
501-
expect(model.types['ComponentWithOutputs'].outputs)
502-
.toEqual({'a': 'a', 'b': 'b', 'c': 'renamed'});
501+
expect(model.types['ComponentWithOutputs'].outputs).toEqual(
502+
{'a': 'a', 'b': 'b', 'c': 'renamed', 'd': 'd', 'e': 'get-renamed'});
503503
});
504504

505505
it('should merge `inputs` from the annotation and fields.', () async {
506506
var model = await _testCreateModel('directives_files/components.dart');
507-
expect(model.types['ComponentWithInputs'].inputs)
508-
.toEqual({'a': 'a', 'b': 'b', 'c': 'renamed'});
507+
expect(model.types['ComponentWithInputs'].inputs).toEqual(
508+
{'a': 'a', 'b': 'b', 'c': 'renamed', 'd': 'd', 'e': 'set-renamed'});
509509
});
510510

511511
it('should merge host bindings from the annotation and fields.', () async {

modules_dart/transform/test/transform/directive_processor/directives_files/components.dart

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,41 @@ class ComponentOnly {}
2222
@Component(
2323
selector: 'component-with-outputs',
2424
template: '<dep1></dep1><dep2></dep2>',
25-
outputs: ['a']
26-
)
25+
outputs: ['a'])
2726
class ComponentWithOutputs {
2827
@Output() Object b;
2928
@Output('renamed') Object c;
29+
30+
Object _d;
31+
@Output() Object get d => _d;
32+
33+
Object _e;
34+
@Output('get-renamed') Object get e => _e;
3035
}
3136

3237
@Component(
3338
selector: 'component-with-inputs',
3439
template: '<dep1></dep1><dep2></dep2>',
35-
inputs: ['a']
36-
)
40+
inputs: ['a'])
3741
class ComponentWithInputs {
3842
@Input() Object b;
3943
@Input('renamed') Object c;
44+
45+
Object _d;
46+
@Input() void set d(Object value) {
47+
_d = value;
48+
}
49+
50+
Object _e;
51+
@Input('set-renamed') void set e(Object value) {
52+
_e = value;
53+
}
4054
}
4155

4256
@Component(
4357
selector: 'component-with-inputs',
4458
template: '<dep1></dep1><dep2></dep2>',
45-
host: {
46-
'[a]':'a'
47-
}
48-
)
59+
host: {'[a]': 'a'})
4960
class ComponentWithHostBindings {
5061
@HostBinding() Object b;
5162
@HostBinding('renamed') Object c;
@@ -54,11 +65,9 @@ class ComponentWithHostBindings {
5465
@Component(
5566
selector: 'component-with-inputs',
5667
template: '<dep1></dep1><dep2></dep2>',
57-
host: {
58-
'(a)':'onA()'
59-
}
60-
)
68+
host: {'(a)': 'onA()'})
6169
class ComponentWithHostListeners {
6270
@HostListener('b') void onB() {}
63-
@HostListener('c', ['\$event.target', '\$event.target.value']) void onC(t,v) {}
71+
@HostListener('c', ['\$event.target', '\$event.target.value']) void onC(
72+
t, v) {}
6473
}

0 commit comments

Comments
 (0)
X Tutup