Browse Source

AddAnotherAccessorAction: don't try to assign to readonly fields

pull/45/merge
Daniel Grunwald 12 years ago
parent
commit
fb493193e3
  1. 2
      ICSharpCode.NRefactory.CSharp/OutputVisitor/CodeDomConvertVisitor.cs
  2. 2
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/AddAnotherAccessorAction.cs
  3. 2
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/RemoveBackingStoreAction.cs
  4. 2
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/ConstantConditionIssue.cs
  5. 31
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/AddAnotherAccessorTests.cs

2
ICSharpCode.NRefactory.CSharp/OutputVisitor/CodeDomConvertVisitor.cs

@ -1356,7 +1356,7 @@ namespace ICSharpCode.NRefactory.CSharp
CodeObject IAstVisitor<CodeObject>.VisitPreProcessorDirective (PreProcessorDirective preProcessorDirective) CodeObject IAstVisitor<CodeObject>.VisitPreProcessorDirective (PreProcessorDirective preProcessorDirective)
{ {
return new CodeComment ("#" + preProcessorDirective.Type.ToString ().ToLower ()); return new CodeComment ("#" + preProcessorDirective.Type.ToString ().ToLowerInvariant ());
} }
CodeObject IAstVisitor<CodeObject>.VisitTypeParameterDeclaration(TypeParameterDeclaration typeParameterDeclaration) CodeObject IAstVisitor<CodeObject>.VisitTypeParameterDeclaration(TypeParameterDeclaration typeParameterDeclaration)

2
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/AddAnotherAccessorAction.cs

@ -73,7 +73,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{ {
if (pdecl.Setter.IsNull && !pdecl.Getter.IsNull) { if (pdecl.Setter.IsNull && !pdecl.Getter.IsNull) {
var field = RemoveBackingStoreAction.ScanGetter (context, pdecl); var field = RemoveBackingStoreAction.ScanGetter (context, pdecl);
if (field != null) if (field != null && !field.IsReadOnly && !field.IsConst)
return new ExpressionStatement (new AssignmentExpression (new IdentifierExpression (field.Name), AssignmentOperatorType.Assign, new IdentifierExpression ("value"))); return new ExpressionStatement (new AssignmentExpression (new IdentifierExpression (field.Name), AssignmentOperatorType.Assign, new IdentifierExpression ("value")));
} }

2
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/RemoveBackingStoreAction.cs

@ -89,7 +89,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var setterField = ScanSetter (context, propertyDeclaration); var setterField = ScanSetter (context, propertyDeclaration);
if (setterField == null) if (setterField == null)
return null; return null;
if (getterField.Region != setterField.Region) if (!getterField.Equals(setterField))
return null; return null;
return getterField; return getterField;
} }

2
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/ConstantConditionIssue.cs

@ -95,7 +95,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var value = (bool)resolveResult.ConstantValue; var value = (bool)resolveResult.ConstantValue;
var conditionalExpr = condition.Parent as ConditionalExpression; var conditionalExpr = condition.Parent as ConditionalExpression;
var ifElseStatement = condition.Parent as IfElseStatement; var ifElseStatement = condition.Parent as IfElseStatement;
var valueStr = value.ToString ().ToLower (); var valueStr = value.ToString ().ToLowerInvariant ();
CodeAction action; CodeAction action;
if (conditionalExpr != null) { if (conditionalExpr != null) {

31
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/AddAnotherAccessorTests.cs

@ -64,6 +64,37 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
"}", result); "}", result);
} }
[Test]
public void TestAddSet_ReadOnlyField ()
{
string result = RunContextAction (
new AddAnotherAccessorAction (),
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" readonly int field;" + Environment.NewLine +
" public int $Field {" + Environment.NewLine +
" get {" + Environment.NewLine +
" return field;" + Environment.NewLine +
" }" + Environment.NewLine +
" }" + Environment.NewLine +
"}"
);
Assert.AreEqual (
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" readonly int field;" + Environment.NewLine +
" public int Field {" + Environment.NewLine +
" get {" + Environment.NewLine +
" return field;" + Environment.NewLine +
" }" + Environment.NewLine +
" set {" + Environment.NewLine +
" throw new System.NotImplementedException ();" + Environment.NewLine +
" }" + Environment.NewLine +
" }" + Environment.NewLine +
"}", result);
}
[Test] [Test]
public void TestAddGet () public void TestAddGet ()
{ {

Loading…
Cancel
Save