Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit c9d89d5

Browse files
bwilkersoncommit-bot@chromium.org
authored andcommitted
Stop generating unnecessary new and const in server generators
This doesn't remove other uses of new or const, only those that are generated. I'll get the rest in other CLs. Change-Id: I59c296da3d2da0f45789df156a682b9c37398087 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/128840 Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
1 parent b6e051e commit c9d89d5

File tree

11 files changed

+2211
-2332
lines changed

11 files changed

+2211
-2332
lines changed

pkg/analysis_server/lib/protocol/protocol_generated.dart

Lines changed: 776 additions & 809 deletions
Large diffs are not rendered by default.

pkg/analysis_server/test/integration/support/integration_test_methods.dart

Lines changed: 186 additions & 201 deletions
Large diffs are not rendered by default.

pkg/analysis_server/test/integration/support/protocol_matchers.dart

Lines changed: 413 additions & 449 deletions
Large diffs are not rendered by default.

pkg/analysis_server/tool/lsp_spec/codegen_dart.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ void _writeEnumClass(IndentableStringBuffer buffer, Namespace namespace) {
335335
_writeDocCommentsAndAnnotations(buffer, cons);
336336
buffer
337337
..writeIndentedln(
338-
'static const ${_makeValidIdentifier(cons.name)} = const ${namespace.name}$constructorName(${cons.valueAsLiteral});');
338+
'static const ${_makeValidIdentifier(cons.name)} = ${namespace.name}$constructorName(${cons.valueAsLiteral});');
339339
});
340340
buffer
341341
..writeln()
@@ -415,7 +415,7 @@ void _writeFromJsonCode(
415415
} else if (type is MapType) {
416416
// Maps need to be map()'d so we can recursively call writeFromJsonCode as
417417
// they may need fromJson on each key or value.
418-
buffer.write('$valueCode?.map((key, value) => new MapEntry(');
418+
buffer.write('$valueCode?.map((key, value) => MapEntry(');
419419
_writeFromJsonCode(buffer, type.indexType, 'key', allowsNull: allowsNull);
420420
buffer.write(', ');
421421
_writeFromJsonCode(buffer, type.valueType, 'value', allowsNull: allowsNull);
@@ -446,7 +446,7 @@ void _writeFromJsonCodeForUnion(
446446
}
447447

448448
// The code to construct a value with this "side" of the union.
449-
buffer.write('new ${union.dartTypeWithTypeArgs}.t${i + 1}(');
449+
buffer.write('${union.dartTypeWithTypeArgs}.t${i + 1}(');
450450
_writeFromJsonCode(buffer, type, valueCode,
451451
allowsNull: allowsNull); // Call recursively!
452452
buffer.write(')');
@@ -498,7 +498,7 @@ void _writeFromJsonConstructor(
498498
buffer.writeln(';');
499499
}
500500
buffer
501-
..writeIndented('return new ${interface.nameWithTypeArgs}(')
501+
..writeIndented('return ${interface.nameWithTypeArgs}(')
502502
..write(allFields.map((field) => '${field.name}').join(', '))
503503
..writeln(');')
504504
..outdent()

pkg/analysis_server/tool/lsp_spec/generate_all.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ import 'package:analysis_server/src/protocol/protocol_internal.dart'
175175
show listEqual, mapEqual;
176176
import 'package:analyzer/src/generated/utilities_general.dart';
177177
178-
const jsonEncoder = const JsonEncoder.withIndent(' ');
178+
const jsonEncoder = JsonEncoder.withIndent(' ');
179179
180180
''';
181181

pkg/analysis_server/tool/spec/codegen_dart_notification_handler.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ class CodegenNotificationHandlerVisitor extends DartCodegenVisitor
4949
mixin NotificationHandler {
5050
void handleEvent(Notification notification) {
5151
Map<String, Object> params = notification.params;
52-
ResponseDecoder decoder = new ResponseDecoder(null);
52+
ResponseDecoder decoder = ResponseDecoder(null);
5353
switch (notification.event) {
5454
''');
5555
for (_Notification notification in notifications) {
5656
writeln(' case ${notification.constName}:');
5757
writeln(' ${notification.methodName}(');
58-
writeln(' new ${notification.paramsTypeName}');
58+
writeln(' ${notification.paramsTypeName}');
5959
writeln(" .fromJson(decoder, 'params', params));");
6060
writeln(' break;');
6161
}

pkg/analysis_server/tool/spec/codegen_dart_protocol.dart

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -190,29 +190,29 @@ class CodegenProtocolVisitor extends DartCodegenVisitor with CodeGenerator {
190190
inputType = 'Request';
191191
inputName = 'request';
192192
fieldName = 'params';
193-
makeDecoder = 'new RequestDecoder(request)';
193+
makeDecoder = 'RequestDecoder(request)';
194194
constructorName = 'fromRequest';
195195
break;
196196
case 'requestResult':
197197
inputType = 'Response';
198198
inputName = 'response';
199199
fieldName = 'result';
200200
makeDecoder =
201-
'new ResponseDecoder(REQUEST_ID_REFACTORING_KINDS.remove(response.id))';
201+
'ResponseDecoder(REQUEST_ID_REFACTORING_KINDS.remove(response.id))';
202202
constructorName = 'fromResponse';
203203
break;
204204
case 'notificationParams':
205205
inputType = 'Notification';
206206
inputName = 'notification';
207207
fieldName = 'params';
208-
makeDecoder = 'new ResponseDecoder(null)';
208+
makeDecoder = 'ResponseDecoder(null)';
209209
constructorName = 'fromNotification';
210210
break;
211211
case 'refactoringOptions':
212212
inputType = 'EditGetRefactoringParams';
213213
inputName = 'refactoringParams';
214214
fieldName = 'options';
215-
makeDecoder = 'new RequestDecoder(request)';
215+
makeDecoder = 'RequestDecoder(request)';
216216
constructorName = 'fromRefactoringParams';
217217
extraArgs.add('Request request');
218218
break;
@@ -226,12 +226,12 @@ class CodegenProtocolVisitor extends DartCodegenVisitor with CodeGenerator {
226226
String fieldNameString =
227227
literalString(fieldName.replaceFirst(new RegExp('^_'), ''));
228228
if (className == 'EditGetRefactoringParams') {
229-
writeln('var params = new $className.fromJson(');
229+
writeln('var params = $className.fromJson(');
230230
writeln(' $makeDecoder, $fieldNameString, $inputName.$fieldName);');
231231
writeln('REQUEST_ID_REFACTORING_KINDS[request.id] = params.kind;');
232232
writeln('return params;');
233233
} else {
234-
writeln('return new $className.fromJson(');
234+
writeln('return $className.fromJson(');
235235
writeln(' $makeDecoder, $fieldNameString, $inputName.$fieldName);');
236236
}
237237
});
@@ -321,7 +321,7 @@ class CodegenProtocolVisitor extends DartCodegenVisitor with CodeGenerator {
321321
}));
322322
String valueString = literalString(value.value);
323323
writeln(
324-
'static const $className ${value.value} = const $className._($valueString);');
324+
'static const $className ${value.value} = $className._($valueString);');
325325
writeln();
326326
}
327327

@@ -330,7 +330,7 @@ class CodegenProtocolVisitor extends DartCodegenVisitor with CodeGenerator {
330330
writeln(' */');
331331
write('static const List<');
332332
write(className);
333-
write('> VALUES = const <');
333+
write('> VALUES = <');
334334
write(className);
335335
write('>[');
336336
bool first = true;
@@ -388,7 +388,7 @@ class CodegenProtocolVisitor extends DartCodegenVisitor with CodeGenerator {
388388
}
389389
});
390390
writeln('}');
391-
writeln(r"throw new Exception('Illegal enum value: $name');");
391+
writeln(r"throw Exception('Illegal enum value: $name');");
392392
});
393393
writeln('}');
394394
}
@@ -405,7 +405,7 @@ class CodegenProtocolVisitor extends DartCodegenVisitor with CodeGenerator {
405405
indent(() {
406406
writeln('try {');
407407
indent(() {
408-
writeln('return new $className(json);');
408+
writeln('return $className(json);');
409409
});
410410
writeln('} catch(_) {');
411411
indent(() {
@@ -721,7 +721,7 @@ class CodegenProtocolVisitor extends DartCodegenVisitor with CodeGenerator {
721721
}
722722
}
723723
args.addAll(optionalArgs);
724-
writeln('return new $className(${args.join(', ')});');
724+
writeln('return $className(${args.join(', ')});');
725725
});
726726
writeln('} else {');
727727
indent(() {
@@ -960,7 +960,7 @@ class CodegenProtocolVisitor extends DartCodegenVisitor with CodeGenerator {
960960
String eventString =
961961
literalString((impliedType.apiNode as Notification).longEvent);
962962
String jsonPart = impliedType.type != null ? 'toJson()' : 'null';
963-
writeln('return new Notification($eventString, $jsonPart);');
963+
writeln('return Notification($eventString, $jsonPart);');
964964
});
965965
writeln('}');
966966
return true;
@@ -980,7 +980,7 @@ class CodegenProtocolVisitor extends DartCodegenVisitor with CodeGenerator {
980980
String methodString =
981981
literalString((impliedType.apiNode as Request).longMethod);
982982
String jsonPart = impliedType.type != null ? 'toJson()' : 'null';
983-
writeln('return new Request(id, $methodString, $jsonPart);');
983+
writeln('return Request(id, $methodString, $jsonPart);');
984984
});
985985
writeln('}');
986986
return true;
@@ -1003,9 +1003,9 @@ class CodegenProtocolVisitor extends DartCodegenVisitor with CodeGenerator {
10031003
indent(() {
10041004
String jsonPart = impliedType.type != null ? 'toJson()' : 'null';
10051005
if (responseRequiresRequestTime) {
1006-
writeln('return new Response(id, requestTime, result: $jsonPart);');
1006+
writeln('return Response(id, requestTime, result: $jsonPart);');
10071007
} else {
1008-
writeln('return new Response(id, result: $jsonPart);');
1008+
writeln('return Response(id, result: $jsonPart);');
10091009
}
10101010
});
10111011
writeln('}');
@@ -1026,11 +1026,11 @@ class CodegenProtocolVisitor extends DartCodegenVisitor with CodeGenerator {
10261026
return new FromJsonSnippet((String jsonPath, String json) {
10271027
String typeName = dartType(type);
10281028
if (typeName == 'RefactoringFeedback') {
1029-
return 'new $typeName.fromJson(jsonDecoder, $jsonPath, $json, json)';
1029+
return '$typeName.fromJson(jsonDecoder, $jsonPath, $json, json)';
10301030
} else if (typeName == 'RefactoringOptions') {
1031-
return 'new $typeName.fromJson(jsonDecoder, $jsonPath, $json, kind)';
1031+
return '$typeName.fromJson(jsonDecoder, $jsonPath, $json, kind)';
10321032
} else {
1033-
return 'new $typeName.fromJson(jsonDecoder, $jsonPath, $json)';
1033+
return '$typeName.fromJson(jsonDecoder, $jsonPath, $json)';
10341034
}
10351035
});
10361036
} else {

pkg/analysis_server/tool/spec/codegen_inttest_methods.dart

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class CodegenInttestMethodsVisitor extends DartCodegenVisitor
142142
}));
143143
writeln('void dispatchNotification(String event, params) {');
144144
indent(() {
145-
writeln('ResponseDecoder decoder = new ResponseDecoder(null);');
145+
writeln('ResponseDecoder decoder = ResponseDecoder(null);');
146146
writeln('switch (event) {');
147147
indent(() {
148148
write(notificationSwitchContents.join());
@@ -178,7 +178,7 @@ class CodegenInttestMethodsVisitor extends DartCodegenVisitor
178178
}));
179179
writeln('StreamController<$className> _$streamName;');
180180
fieldInitializationCode.add(collectCode(() {
181-
writeln('_$streamName = new StreamController<$className>(sync: true);');
181+
writeln('_$streamName = StreamController<$className>(sync: true);');
182182
writeln('$streamName = _$streamName.stream.asBroadcastStream();');
183183
}));
184184
notificationSwitchContents.add(collectCode(() {
@@ -189,10 +189,9 @@ class CodegenInttestMethodsVisitor extends DartCodegenVisitor
189189
writeln('outOfTestExpect(params, $paramsValidator);');
190190
String constructorCall;
191191
if (notification.params == null) {
192-
constructorCall = 'new $className()';
192+
constructorCall = '$className()';
193193
} else {
194-
constructorCall =
195-
"new $className.fromJson(decoder, 'params', params)";
194+
constructorCall = "$className.fromJson(decoder, 'params', params)";
196195
}
197196
writeln('_$streamName.add($constructorCall);');
198197
writeln('break;');
@@ -253,7 +252,7 @@ class CodegenInttestMethodsVisitor extends DartCodegenVisitor
253252
}
254253
}
255254
args.addAll(optionalArgs);
256-
writeln('var params = new $requestClass(${args.join(', ')}).toJson();');
255+
writeln('var params = $requestClass(${args.join(', ')}).toJson();');
257256
}
258257
String methodJson = json.encode(request.longMethod);
259258
writeln('var result = await server.send($methodJson, $paramsVar);');
@@ -262,8 +261,8 @@ class CodegenInttestMethodsVisitor extends DartCodegenVisitor
262261
if (requestClass == 'EditGetRefactoringParams') {
263262
kind = 'kind';
264263
}
265-
writeln('ResponseDecoder decoder = new ResponseDecoder($kind);');
266-
writeln("return new $resultClass.fromJson(decoder, 'result', result);");
264+
writeln('ResponseDecoder decoder = ResponseDecoder($kind);');
265+
writeln("return $resultClass.fromJson(decoder, 'result', result);");
267266
} else {
268267
writeln('outOfTestExpect(result, isNull);');
269268
writeln('return null;');

pkg/analysis_server/tool/spec/codegen_matchers.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class CodegenMatchersVisitor extends HierarchicalApiVisitor with CodeGenerator {
115115

116116
@override
117117
visitTypeEnum(TypeEnum typeEnum) {
118-
writeln('new MatchesEnum(${json.encode(context)}, [');
118+
writeln('MatchesEnum(${json.encode(context)}, [');
119119
indent(() {
120120
bool commaNeeded = false;
121121
for (TypeEnumValue value in typeEnum.values) {
@@ -148,7 +148,7 @@ class CodegenMatchersVisitor extends HierarchicalApiVisitor with CodeGenerator {
148148

149149
@override
150150
void visitTypeObject(TypeObject typeObject) {
151-
writeln('new LazyMatcher(() => new MatchesJsonObject(');
151+
writeln('LazyMatcher(() => MatchesJsonObject(');
152152
indent(() {
153153
write('${json.encode(context)}, ');
154154
Iterable<TypeObjectField> requiredFields =

pkg/analysis_server_client/lib/handler/notification_handler.dart

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,96 +17,93 @@ import 'package:analysis_server_client/protocol.dart';
1717
mixin NotificationHandler {
1818
void handleEvent(Notification notification) {
1919
Map<String, Object> params = notification.params;
20-
ResponseDecoder decoder = new ResponseDecoder(null);
20+
ResponseDecoder decoder = ResponseDecoder(null);
2121
switch (notification.event) {
2222
case ANALYSIS_NOTIFICATION_ANALYZED_FILES:
23-
onAnalysisAnalyzedFiles(new AnalysisAnalyzedFilesParams.fromJson(
24-
decoder, 'params', params));
23+
onAnalysisAnalyzedFiles(
24+
AnalysisAnalyzedFilesParams.fromJson(decoder, 'params', params));
2525
break;
2626
case ANALYSIS_NOTIFICATION_CLOSING_LABELS:
27-
onAnalysisClosingLabels(new AnalysisClosingLabelsParams.fromJson(
28-
decoder, 'params', params));
27+
onAnalysisClosingLabels(
28+
AnalysisClosingLabelsParams.fromJson(decoder, 'params', params));
2929
break;
3030
case ANALYSIS_NOTIFICATION_ERRORS:
3131
onAnalysisErrors(
32-
new AnalysisErrorsParams.fromJson(decoder, 'params', params));
32+
AnalysisErrorsParams.fromJson(decoder, 'params', params));
3333
break;
3434
case ANALYSIS_NOTIFICATION_FLUSH_RESULTS:
3535
onAnalysisFlushResults(
36-
new AnalysisFlushResultsParams.fromJson(decoder, 'params', params));
36+
AnalysisFlushResultsParams.fromJson(decoder, 'params', params));
3737
break;
3838
case ANALYSIS_NOTIFICATION_FOLDING:
3939
onAnalysisFolding(
40-
new AnalysisFoldingParams.fromJson(decoder, 'params', params));
40+
AnalysisFoldingParams.fromJson(decoder, 'params', params));
4141
break;
4242
case ANALYSIS_NOTIFICATION_HIGHLIGHTS:
4343
onAnalysisHighlights(
44-
new AnalysisHighlightsParams.fromJson(decoder, 'params', params));
44+
AnalysisHighlightsParams.fromJson(decoder, 'params', params));
4545
break;
4646
case ANALYSIS_NOTIFICATION_IMPLEMENTED:
4747
onAnalysisImplemented(
48-
new AnalysisImplementedParams.fromJson(decoder, 'params', params));
48+
AnalysisImplementedParams.fromJson(decoder, 'params', params));
4949
break;
5050
case ANALYSIS_NOTIFICATION_INVALIDATE:
5151
onAnalysisInvalidate(
52-
new AnalysisInvalidateParams.fromJson(decoder, 'params', params));
52+
AnalysisInvalidateParams.fromJson(decoder, 'params', params));
5353
break;
5454
case ANALYSIS_NOTIFICATION_NAVIGATION:
5555
onAnalysisNavigation(
56-
new AnalysisNavigationParams.fromJson(decoder, 'params', params));
56+
AnalysisNavigationParams.fromJson(decoder, 'params', params));
5757
break;
5858
case ANALYSIS_NOTIFICATION_OCCURRENCES:
5959
onAnalysisOccurrences(
60-
new AnalysisOccurrencesParams.fromJson(decoder, 'params', params));
60+
AnalysisOccurrencesParams.fromJson(decoder, 'params', params));
6161
break;
6262
case ANALYSIS_NOTIFICATION_OUTLINE:
6363
onAnalysisOutline(
64-
new AnalysisOutlineParams.fromJson(decoder, 'params', params));
64+
AnalysisOutlineParams.fromJson(decoder, 'params', params));
6565
break;
6666
case ANALYSIS_NOTIFICATION_OVERRIDES:
6767
onAnalysisOverrides(
68-
new AnalysisOverridesParams.fromJson(decoder, 'params', params));
68+
AnalysisOverridesParams.fromJson(decoder, 'params', params));
6969
break;
7070
case COMPLETION_NOTIFICATION_AVAILABLE_SUGGESTIONS:
7171
onCompletionAvailableSuggestions(
72-
new CompletionAvailableSuggestionsParams.fromJson(
72+
CompletionAvailableSuggestionsParams.fromJson(
7373
decoder, 'params', params));
7474
break;
7575
case COMPLETION_NOTIFICATION_EXISTING_IMPORTS:
76-
onCompletionExistingImports(
77-
new CompletionExistingImportsParams.fromJson(
78-
decoder, 'params', params));
76+
onCompletionExistingImports(CompletionExistingImportsParams.fromJson(
77+
decoder, 'params', params));
7978
break;
8079
case COMPLETION_NOTIFICATION_RESULTS:
8180
onCompletionResults(
82-
new CompletionResultsParams.fromJson(decoder, 'params', params));
81+
CompletionResultsParams.fromJson(decoder, 'params', params));
8382
break;
8483
case EXECUTION_NOTIFICATION_LAUNCH_DATA:
8584
onExecutionLaunchData(
86-
new ExecutionLaunchDataParams.fromJson(decoder, 'params', params));
85+
ExecutionLaunchDataParams.fromJson(decoder, 'params', params));
8786
break;
8887
case FLUTTER_NOTIFICATION_OUTLINE:
8988
onFlutterOutline(
90-
new FlutterOutlineParams.fromJson(decoder, 'params', params));
89+
FlutterOutlineParams.fromJson(decoder, 'params', params));
9190
break;
9291
case SEARCH_NOTIFICATION_RESULTS:
9392
onSearchResults(
94-
new SearchResultsParams.fromJson(decoder, 'params', params));
93+
SearchResultsParams.fromJson(decoder, 'params', params));
9594
break;
9695
case SERVER_NOTIFICATION_CONNECTED:
9796
onServerConnected(
98-
new ServerConnectedParams.fromJson(decoder, 'params', params));
97+
ServerConnectedParams.fromJson(decoder, 'params', params));
9998
break;
10099
case SERVER_NOTIFICATION_ERROR:
101-
onServerError(
102-
new ServerErrorParams.fromJson(decoder, 'params', params));
100+
onServerError(ServerErrorParams.fromJson(decoder, 'params', params));
103101
break;
104102
case SERVER_NOTIFICATION_LOG:
105-
onServerLog(new ServerLogParams.fromJson(decoder, 'params', params));
103+
onServerLog(ServerLogParams.fromJson(decoder, 'params', params));
106104
break;
107105
case SERVER_NOTIFICATION_STATUS:
108-
onServerStatus(
109-
new ServerStatusParams.fromJson(decoder, 'params', params));
106+
onServerStatus(ServerStatusParams.fromJson(decoder, 'params', params));
110107
break;
111108
default:
112109
onUnknownNotification(notification.event, params);

0 commit comments

Comments
 (0)