Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(212)

Side by Side Diff: pkg/analysis_server/lib/src/services/refactoring/rename_constructor.dart

Issue 2399533003: Issue 27475. Discourage users from naming constructors the same as the enclosing classes. (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/refactoring/rename_constructor_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library services.src.refactoring.rename_constructor; 5 library services.src.refactoring.rename_constructor;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:analysis_server/src/protocol_server.dart' hide Element; 9 import 'package:analysis_server/src/protocol_server.dart' hide Element;
10 import 'package:analysis_server/src/services/correction/source_range.dart'; 10 import 'package:analysis_server/src/services/correction/source_range.dart';
(...skipping 22 matching lines...) Expand all
33 ConstructorElement get element => super.element as ConstructorElement; 33 ConstructorElement get element => super.element as ConstructorElement;
34 34
35 @override 35 @override
36 String get refactoringName { 36 String get refactoringName {
37 return "Rename Constructor"; 37 return "Rename Constructor";
38 } 38 }
39 39
40 @override 40 @override
41 Future<RefactoringStatus> checkFinalConditions() { 41 Future<RefactoringStatus> checkFinalConditions() {
42 RefactoringStatus result = new RefactoringStatus(); 42 RefactoringStatus result = new RefactoringStatus();
43 _analyzePossibleConflicts(result);
44 return new Future.value(result); 43 return new Future.value(result);
45 } 44 }
46 45
47 @override 46 @override
48 RefactoringStatus checkNewName() { 47 RefactoringStatus checkNewName() {
49 RefactoringStatus result = super.checkNewName(); 48 RefactoringStatus result = super.checkNewName();
50 result.addStatus(validateConstructorName(newName)); 49 result.addStatus(validateConstructorName(newName));
50 if (newName != null) {
51 _analyzePossibleConflicts(result);
52 }
51 return result; 53 return result;
52 } 54 }
53 55
54 @override 56 @override
55 Future fillChange() async { 57 Future fillChange() async {
56 // prepare references 58 // prepare references
57 List<SearchMatch> matches = await searchEngine.searchReferences(element); 59 List<SearchMatch> matches = await searchEngine.searchReferences(element);
58 List<SourceReference> references = getSourceReferences(matches); 60 List<SourceReference> references = getSourceReferences(matches);
59 // append declaration 61 // append declaration
60 if (element.isSynthetic) { 62 if (element.isSynthetic) {
61 _replaceSynthetic(); 63 _replaceSynthetic();
62 } else { 64 } else {
63 references.add(_createDeclarationReference()); 65 references.add(_createDeclarationReference());
64 } 66 }
65 // update references 67 // update references
66 String replacement = newName.isEmpty ? '' : '.$newName'; 68 String replacement = newName.isEmpty ? '' : '.$newName';
67 for (SourceReference reference in references) { 69 for (SourceReference reference in references) {
68 reference.addEdit(change, replacement); 70 reference.addEdit(change, replacement);
69 } 71 }
70 } 72 }
71 73
72 void _analyzePossibleConflicts(RefactoringStatus result) { 74 void _analyzePossibleConflicts(RefactoringStatus result) {
75 ClassElement parentClass = element.enclosingElement;
76 // Check if the "newName" is the name of the enclosing class.
77 if (parentClass.name == newName) {
78 result.addError('The constructor should not have the same name '
79 'as the name of the enclosing class.');
80 }
73 // check if there are members with "newName" in the same ClassElement 81 // check if there are members with "newName" in the same ClassElement
74 ClassElement parentClass = element.enclosingElement;
75 for (Element newNameMember in getChildren(parentClass, newName)) { 82 for (Element newNameMember in getChildren(parentClass, newName)) {
76 String message = format( 83 String message = format(
77 "Class '{0}' already declares {1} with name '{2}'.", 84 "Class '{0}' already declares {1} with name '{2}'.",
78 parentClass.displayName, 85 parentClass.displayName,
79 getElementKindName(newNameMember), 86 getElementKindName(newNameMember),
80 newName); 87 newName);
81 result.addError(message, newLocation_fromElement(newNameMember)); 88 result.addError(message, newLocation_fromElement(newNameMember));
82 } 89 }
83 } 90 }
84 91
(...skipping 24 matching lines...) Expand all
109 change, 116 change,
110 classElement, 117 classElement,
111 new SourceEdit( 118 new SourceEdit(
112 location.offset, 119 location.offset,
113 0, 120 0,
114 location.prefix + 121 location.prefix +
115 '${classElement.name}.$newName();' + 122 '${classElement.name}.$newName();' +
116 location.suffix)); 123 location.suffix));
117 } 124 }
118 } 125 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/refactoring/rename_constructor_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698