Skip to content

Commit 80f6834

Browse files
pqcommit-bot@chromium.org
authored andcommitted
when fixing missing diagnostic refs, add debugFillProperties method if undefined
See: #38633 Change-Id: If82922a38243e144ad2ec0038d8642c17f458df8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/120140 Commit-Queue: Phil Quitslund <pquitslund@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
1 parent 04910d6 commit 80f6834

File tree

2 files changed

+101
-55
lines changed

2 files changed

+101
-55
lines changed

pkg/analysis_server/lib/src/services/correction/base_processor.dart

Lines changed: 83 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -98,83 +98,112 @@ abstract class BaseProcessor {
9898
return null;
9999
}
100100

101-
var constructorInvocation;
101+
var constructorName;
102102
var hasTypeArgs = false;
103103
if (type.isDartCoreBool) {
104-
constructorInvocation = 'DiagnosticsProperty<bool>';
104+
constructorName = 'DiagnosticsProperty<bool>';
105105
} else if (type.isDartCoreInt) {
106-
constructorInvocation = 'IntProperty';
106+
constructorName = 'IntProperty';
107107
} else if (type.isDartCoreDouble) {
108-
constructorInvocation = 'DoubleProperty';
108+
constructorName = 'DoubleProperty';
109109
} else if (type.isDartCoreString) {
110-
constructorInvocation = 'StringProperty';
110+
constructorName = 'StringProperty';
111111
} else if (isEnum(type)) {
112-
constructorInvocation = 'EnumProperty';
112+
constructorName = 'EnumProperty';
113113
} else if (isIterable(type)) {
114-
constructorInvocation = 'IterableProperty';
114+
constructorName = 'IterableProperty';
115115
hasTypeArgs = true;
116116
} else if (flutter.isColor(type)) {
117-
constructorInvocation = 'ColorProperty';
117+
constructorName = 'ColorProperty';
118118
} else if (flutter.isMatrix4(type)) {
119-
constructorInvocation = 'TransformProperty';
119+
constructorName = 'TransformProperty';
120120
}
121121

122-
if (constructorInvocation == null) {
122+
if (constructorName == null) {
123123
return null;
124124
}
125125

126-
ClassDeclaration classDeclaration =
127-
parent.thisOrAncestorOfType<ClassDeclaration>();
126+
void writePropertyReference(
127+
DartEditBuilder builder, {
128+
@required String prefix,
129+
@required String builderName,
130+
}) {
131+
builder.write("$prefix$builderName.add($constructorName");
132+
if (hasTypeArgs) {
133+
builder.write('<');
134+
builder.writeTypes((type as InterfaceType).typeArguments);
135+
builder.write('>');
136+
}
137+
builder.writeln("('${name.name}', ${name.name}));");
138+
}
139+
140+
final classDeclaration = parent.thisOrAncestorOfType<ClassDeclaration>();
128141
final debugFillProperties =
129142
classDeclaration.getMethod('debugFillProperties');
130-
if (debugFillProperties != null) {
131-
final body = debugFillProperties.body;
132-
if (body is BlockFunctionBody) {
133-
BlockFunctionBody functionBody = body;
134-
135-
var offset;
136-
var prefix;
137-
if (functionBody.block.statements.isEmpty) {
138-
offset = functionBody.block.leftBracket.offset;
139-
prefix = utils.getLinePrefix(offset) + utils.getIndent(1);
140-
} else {
141-
offset = functionBody.block.statements.last.endToken.offset;
142-
prefix = utils.getLinePrefix(offset);
143-
}
143+
if (debugFillProperties == null) {
144+
final insertOffset =
145+
utils.prepareNewMethodLocation(classDeclaration).offset;
146+
final changeBuilder = _newDartChangeBuilder();
147+
await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
148+
builder.addInsertion(utils.getLineNext(insertOffset),
149+
(DartEditBuilder builder) {
150+
final declPrefix =
151+
utils.getLinePrefix(classDeclaration.offset) + utils.getIndent(1);
152+
final bodyPrefix = declPrefix + utils.getIndent(1);
153+
154+
builder.writeln('$declPrefix@override');
155+
builder.writeln(
156+
'${declPrefix}void debugFillProperties(DiagnosticPropertiesBuilder properties) {');
157+
builder
158+
.writeln('${bodyPrefix}super.debugFillProperties(properties);');
159+
writePropertyReference(builder,
160+
prefix: bodyPrefix, builderName: 'properties');
161+
builder.writeln('$declPrefix}');
162+
});
163+
});
164+
return changeBuilder;
165+
}
166+
167+
final body = debugFillProperties.body;
168+
if (body is BlockFunctionBody) {
169+
BlockFunctionBody functionBody = body;
170+
171+
var offset;
172+
var prefix;
173+
if (functionBody.block.statements.isEmpty) {
174+
offset = functionBody.block.leftBracket.offset;
175+
prefix = utils.getLinePrefix(offset) + utils.getIndent(1);
176+
} else {
177+
offset = functionBody.block.statements.last.endToken.offset;
178+
prefix = utils.getLinePrefix(offset);
179+
}
144180

145-
var parameters = debugFillProperties.parameters.parameters;
146-
var propertiesBuilderName;
147-
for (var parameter in parameters) {
148-
if (parameter is SimpleFormalParameter) {
149-
final type = parameter.type;
150-
if (type is TypeName) {
151-
if (type.name.name == 'DiagnosticPropertiesBuilder') {
152-
propertiesBuilderName = parameter.identifier.name;
153-
break;
154-
}
181+
var parameters = debugFillProperties.parameters.parameters;
182+
var propertiesBuilderName;
183+
for (var parameter in parameters) {
184+
if (parameter is SimpleFormalParameter) {
185+
final type = parameter.type;
186+
if (type is TypeName) {
187+
if (type.name.name == 'DiagnosticPropertiesBuilder') {
188+
propertiesBuilderName = parameter.identifier.name;
189+
break;
155190
}
156191
}
157192
}
158-
if (propertiesBuilderName == null) {
159-
return null;
160-
}
193+
}
194+
if (propertiesBuilderName == null) {
195+
return null;
196+
}
161197

162-
final changeBuilder = _newDartChangeBuilder();
163-
await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
164-
builder.addInsertion(utils.getLineNext(offset),
165-
(DartEditBuilder builder) {
166-
builder.write(
167-
"$prefix$propertiesBuilderName.add($constructorInvocation");
168-
if (hasTypeArgs) {
169-
builder.write('<');
170-
builder.writeTypes((type as InterfaceType).typeArguments);
171-
builder.write('>');
172-
}
173-
builder.write("('${name.name}', ${name.name}));$eol");
174-
});
198+
final changeBuilder = _newDartChangeBuilder();
199+
await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
200+
builder.addInsertion(utils.getLineNext(offset),
201+
(DartEditBuilder builder) {
202+
writePropertyReference(builder,
203+
prefix: prefix, builderName: propertiesBuilderName);
175204
});
176-
return changeBuilder;
177-
}
205+
});
206+
return changeBuilder;
178207
}
179208

180209
return null;

pkg/analysis_server/test/src/services/correction/fix/add_diagnostic_property_reference_test.dart

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,24 @@ class A extends Widget {
299299
''');
300300
}
301301

302-
// todo (pq): tests for no debugFillProperties method
302+
test_stringField_noDebugFillProperties() async {
303+
await resolveTestUnit('''
304+
class A extends Widget {
305+
String /*LINT*/field;
306+
}
307+
''');
308+
await assertHasFix('''
309+
class A extends Widget {
310+
String /*LINT*/field;
311+
@override
312+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
313+
super.debugFillProperties(properties);
314+
properties.add(StringProperty('field', field));
315+
}
316+
}
317+
''');
318+
}
319+
303320
// todo (pq): consider a test for a body w/ no CR
304321
// todo (pq): support for DiagnosticsProperty for any T that doesn't match one of the other cases
305322
}

0 commit comments

Comments
 (0)