Skip to content

Commit

Permalink
Migrate several lib/src/services/correction/dart/
Browse files Browse the repository at this point in the history
Change-Id: I49f43e801f13dbbfe67be79fea19073990acb658
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/194962
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
scheglov authored and commit-bot@chromium.org committed Apr 10, 2021
1 parent 5b6d517 commit e1539dc
Show file tree
Hide file tree
Showing 45 changed files with 591 additions and 506 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart = 2.9

import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';

class MakeClassAbstract extends CorrectionProducer {
String _className;
String _className = '';

@override
List<Object> get fixArguments => [_className];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart = 2.9

import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analysis_server/src/utilities/extensions/ast.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/src/dart/ast/extensions.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';

class MakeFieldNotFinal extends CorrectionProducer {
String _fieldName;
String _fieldName = '';

@override
List<Object> get fixArguments => [_fieldName];
Expand All @@ -26,40 +24,72 @@ class MakeFieldNotFinal extends CorrectionProducer {
@override
Future<void> compute(ChangeBuilder builder) async {
var node = this.node;
if (node is SimpleIdentifier &&
node.writeOrReadElement is PropertyAccessorElement) {
PropertyAccessorElement getter = node.writeOrReadElement;
if (getter.isGetter &&
getter.isSynthetic &&
!getter.variable.isSynthetic &&
getter.variable.setter == null &&
getter.enclosingElement is ClassElement) {
var declarationResult =
await sessionHelper.getElementDeclaration(getter.variable);
var variable = declarationResult.node;
if (variable is VariableDeclaration &&
variable.parent is VariableDeclarationList &&
variable.parent.parent is FieldDeclaration) {
VariableDeclarationList declarationList = variable.parent;
var keywordToken = declarationList.keyword;
if (declarationList.variables.length == 1 &&
keywordToken.keyword == Keyword.FINAL) {
await builder.addDartFileEdit(file, (builder) {
if (declarationList.type != null) {
builder.addDeletion(
range.startStart(keywordToken, declarationList.type));
} else {
builder.addReplacement(range.startStart(keywordToken, variable),
(builder) {
builder.write('var ');
});
}
});
_fieldName = getter.variable.displayName;
}
}
}
if (node is! SimpleIdentifier) {
return;
}

var getter = node.writeOrReadElement;
if (getter is! PropertyAccessorElement) {
return;
}

// The accessor must be a getter, and it must be synthetic.
if (!(getter.isGetter && getter.isSynthetic)) {
return;
}

// The variable must be not synthetic, and have no setter yet.
var variable = getter.variable;
if (variable.isSynthetic || variable.setter != null) {
return;
}

// It must be a field declaration.
if (getter.enclosingElement is! ClassElement) {
return;
}

var declaration = await sessionHelper.getElementDeclaration(variable);
var variableNode = declaration?.node;
if (variableNode is! VariableDeclaration) {
return;
}

// The declaration list must have exactly one variable.
var declarationList = variableNode.parent;
if (declarationList is! VariableDeclarationList) {
return;
}
if (declarationList.variables.length != 1) {
return;
}

// It must be a field declaration.
if (declarationList.parent is! FieldDeclaration) {
return;
}

var finalKeyword = declarationList.finalKeyword;
if (finalKeyword == null) {
return;
}

_fieldName = variable.displayName;
await builder.addDartFileEdit(file, (builder) {
var typeAnnotation = declarationList.type;
if (typeAnnotation != null) {
builder.addDeletion(
range.startStart(finalKeyword, typeAnnotation),
);
} else {
builder.addReplacement(
range.startStart(finalKeyword, variableNode),
(builder) {
builder.write('var ');
},
);
}
});
}

/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart = 2.9

import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
Expand All @@ -22,41 +20,47 @@ class MakeFinal extends CorrectionProducer {
@override
Future<void> compute(ChangeBuilder builder) async {
var node = this.node;
var parent = node.parent;
var grandParent = parent?.parent;

if (node is SimpleIdentifier &&
node.parent is DeclaredIdentifier &&
node.parent.parent is ForEachPartsWithDeclaration) {
var declaration = node.parent as DeclaredIdentifier;
parent is DeclaredIdentifier &&
grandParent is ForEachPartsWithDeclaration) {
await builder.addDartFileEdit(file, (builder) {
if (declaration.keyword?.keyword == Keyword.VAR) {
builder.addSimpleReplacement(
range.token(declaration.keyword), 'final');
} else if (declaration.keyword == null) {
builder.addSimpleInsertion(declaration.offset, 'final ');
var keyword = parent.keyword;
if (keyword != null && keyword.keyword == Keyword.VAR) {
builder.addSimpleReplacement(range.token(keyword), 'final');
} else if (keyword == null) {
builder.addSimpleInsertion(parent.offset, 'final ');
}
});
return;
}

VariableDeclarationList list;
if (node is SimpleIdentifier &&
node.parent is VariableDeclaration &&
node.parent.parent is VariableDeclarationList) {
list = node.parent.parent;
parent is VariableDeclaration &&
grandParent is VariableDeclarationList) {
list = grandParent;
} else if (node is VariableDeclaration &&
node.parent is VariableDeclarationList) {
list = node.parent;
parent is VariableDeclarationList) {
list = parent;
} else {
return;
}
if (list != null) {
if (list.variables.length == 1) {
await builder.addDartFileEdit(file, (builder) {
if (list.keyword?.keyword == Keyword.VAR) {
builder.addSimpleReplacement(range.token(list.keyword), 'final');
} else if (list.lateKeyword != null) {
builder.addSimpleInsertion(list.lateKeyword.end, ' final');
} else if (list.keyword == null) {
builder.addSimpleInsertion(list.offset, 'final ');
}
});
}

if (list.variables.length == 1) {
await builder.addDartFileEdit(file, (builder) {
var keyword = list.keyword;
var lateKeyword = list.lateKeyword;
if (keyword != null && keyword.keyword == Keyword.VAR) {
builder.addSimpleReplacement(range.token(keyword), 'final');
} else if (lateKeyword != null) {
builder.addSimpleInsertion(lateKeyword.end, ' final');
} else if (keyword == null) {
builder.addSimpleInsertion(list.offset, 'final ');
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart = 2.9

import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/src/dart/ast/extensions.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';

Expand All @@ -20,10 +19,16 @@ class MakeReturnTypeNullable extends CorrectionProducer {
if (!unit.featureSet.isEnabled(Feature.non_nullable)) {
return;
}

var node = this.node;
if (node is! Expression) {
return;
}

var body = node.thisOrAncestorOfType<FunctionBody>();
if (body == null) {
return;
}

var returnType = _getReturnTypeNode(body);
if (returnType == null) {
Expand All @@ -34,7 +39,7 @@ class MakeReturnTypeNullable extends CorrectionProducer {
if (returnType is! NamedType) {
return null;
}
var typeArguments = (returnType as NamedType).typeArguments;
var typeArguments = returnType.typeArguments;
if (typeArguments == null) {
return null;
}
Expand All @@ -44,20 +49,23 @@ class MakeReturnTypeNullable extends CorrectionProducer {
}
returnType = arguments[0];
}

if (node is! NullLiteral &&
!typeSystem.isAssignableTo(returnType.type,
typeSystem.promoteToNonNull((node as Expression).staticType))) {
!typeSystem.isAssignableTo(returnType.typeOrThrow,
typeSystem.promoteToNonNull(node.typeOrThrow))) {
return;
}

final returnType_final = returnType;
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleInsertion(returnType.end, '?');
builder.addSimpleInsertion(returnType_final.end, '?');
});
}

/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
static MakeReturnTypeNullable newInstance() => MakeReturnTypeNullable();

static TypeAnnotation _getReturnTypeNode(FunctionBody body) {
static TypeAnnotation? _getReturnTypeNode(FunctionBody body) {
var function = body.parent;
if (function is FunctionExpression) {
function = function.parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart = 2.9

import 'package:_fe_analyzer_shared/src/scanner/token.dart';
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
Expand All @@ -15,7 +13,7 @@ import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';

class MakeVariableNotFinal extends CorrectionProducer {
String _variableName;
String _variableName = '';

@override
List<Object> get fixArguments => [_variableName];
Expand All @@ -26,28 +24,36 @@ class MakeVariableNotFinal extends CorrectionProducer {
@override
Future<void> compute(ChangeBuilder builder) async {
var node = this.node;
if (node is SimpleIdentifier &&
node.staticElement is LocalVariableElement) {
LocalVariableElement variable = node.staticElement;
var id = NodeLocator(variable.nameOffset).searchWithin(unit);
var decl = id?.parent;
if (decl is VariableDeclaration &&
decl.parent is VariableDeclarationList) {
VariableDeclarationList declarationList = decl.parent;
var keywordToken = declarationList.keyword;
if (declarationList.variables.length == 1 &&
keywordToken.keyword == Keyword.FINAL) {
await builder.addDartFileEdit(file, (builder) {
if (declarationList.type != null) {
builder.addDeletion(
range.startStart(keywordToken, declarationList.type));
} else {
builder.addSimpleReplacement(range.token(keywordToken), 'var');
}
});
declarationList.variables[0].name.name;
_variableName = declarationList.variables[0].name.name;
}
if (node is! SimpleIdentifier) {
return;
}

var variable = node.staticElement;
if (variable is! LocalVariableElement) {
return;
}

var id = NodeLocator(variable.nameOffset).searchWithin(unit);
var declaration = id?.parent;
var declarationList = declaration?.parent;

if (declaration is VariableDeclaration &&
declarationList is VariableDeclarationList) {
var keywordToken = declarationList.keyword;
if (declarationList.variables.length == 1 &&
keywordToken != null &&
keywordToken.keyword == Keyword.FINAL) {
await builder.addDartFileEdit(file, (builder) {
var typeAnnotation = declarationList.type;
if (typeAnnotation != null) {
builder.addDeletion(
range.startStart(keywordToken, typeAnnotation),
);
} else {
builder.addSimpleReplacement(range.token(keywordToken), 'var');
}
});
_variableName = variable.name;
}
}
}
Expand Down
Loading

0 comments on commit e1539dc

Please sign in to comment.