Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
fbf2e40f
R
roslyn
项目概览
lwm1986
/
roslyn
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
roslyn
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
fbf2e40f
编写于
3月 25, 2020
作者:
C
CyrusNajmabadi
提交者:
GitHub
3月 25, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #42254 from CyrusNajmabadi/doNotGenerateReadOnlyWhenInsideLocalFunction
Do not generate read-only when inside local function
上级
806a26cc
f4909b47
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
58 addition
and
11 deletion
+58
-11
src/EditorFeatures/CSharpTest/GenerateVariable/GenerateVariableTests.cs
...ures/CSharpTest/GenerateVariable/GenerateVariableTests.cs
+46
-1
src/Features/Core/Portable/GenerateMember/GenerateVariable/AbstractGenerateVariableService.State.cs
...GenerateVariable/AbstractGenerateVariableService.State.cs
+12
-10
未找到文件。
src/EditorFeatures/CSharpTest/GenerateVariable/GenerateVariableTests.cs
浏览文件 @
fbf2e40f
...
...
@@ -11,7 +11,6 @@
using
Microsoft.CodeAnalysis.CSharp
;
using
Microsoft.CodeAnalysis.CSharp.CodeStyle
;
using
Microsoft.CodeAnalysis.CSharp.GenerateVariable
;
using
Microsoft.CodeAnalysis.CSharp.Test.Utilities
;
using
Microsoft.CodeAnalysis.Diagnostics
;
using
Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics
;
using
Microsoft.CodeAnalysis.Options
;
...
...
@@ -9270,5 +9269,51 @@ public void Method(int goo)
void M1(int a);
}"
,
index
:
ParameterAndOverrides
);
}
[
WorkItem
(
26502
,
"https://github.com/dotnet/roslyn/issues/26502"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateVariable
)]
public
async
Task
TestNoReadOnlyMembersWhenInLambdaInConstructor
()
{
await
TestExactActionSetOfferedAsync
(
@"using System;
class C
{
public C()
{
Action a = () =>
{
this.[|Field|] = 1;
};
}
}"
,
new
[]
{
string
.
Format
(
FeaturesResources
.
Generate_property_1_0
,
"Field"
,
"C"
),
string
.
Format
(
FeaturesResources
.
Generate_field_1_0
,
"Field"
,
"C"
),
});
}
[
WorkItem
(
26502
,
"https://github.com/dotnet/roslyn/issues/26502"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsGenerateVariable
)]
public
async
Task
TestNoReadOnlyMembersWhenInLocalFunctionInConstructor
()
{
await
TestExactActionSetOfferedAsync
(
@"using System;
class C
{
public C()
{
void Goo()
{
this.[|Field|] = 1;
};
}
}"
,
new
[]
{
string
.
Format
(
FeaturesResources
.
Generate_property_1_0
,
"Field"
,
"C"
),
string
.
Format
(
FeaturesResources
.
Generate_field_1_0
,
"Field"
,
"C"
),
});
}
}
}
src/Features/Core/Portable/GenerateMember/GenerateVariable/AbstractGenerateVariableService.State.cs
浏览文件 @
fbf2e40f
...
...
@@ -32,7 +32,6 @@ private partial class State
// Just the name of the method. i.e. "Goo" in "Goo" or "X.Goo"
public
SyntaxToken
IdentifierToken
{
get
;
private
set
;
}
public
TSimpleNameSyntax
SimpleNameOpt
{
get
;
private
set
;
}
// The entire expression containing the name. i.e. "X.Goo"
public
TExpressionSyntax
SimpleNameOrMemberAccessExpressionOpt
{
get
;
private
set
;
}
...
...
@@ -209,7 +208,6 @@ internal bool CanGenerateParameter()
return
false
;
}
SimpleNameOpt
=
simpleName
;
IdentifierToken
=
identifierToken
;
SimpleNameOrMemberAccessExpressionOpt
=
simpleNameOrMemberAccessExpression
;
IsInExecutableBlock
=
isInExecutableBlock
;
...
...
@@ -267,9 +265,10 @@ internal bool CanGenerateParameter()
IsInOutContext
=
semanticFacts
.
IsInOutContext
(
semanticModel
,
SimpleNameOrMemberAccessExpressionOpt
,
cancellationToken
);
IsWrittenTo
=
semanticFacts
.
IsWrittenTo
(
semanticModel
,
SimpleNameOrMemberAccessExpressionOpt
,
cancellationToken
);
IsOnlyWrittenTo
=
semanticFacts
.
IsOnlyWrittenTo
(
semanticModel
,
SimpleNameOrMemberAccessExpressionOpt
,
cancellationToken
);
IsInConstructor
=
DetermineIsInConstructor
(
semanticDocument
);
IsInMemberContext
=
SimpleNameOpt
!=
SimpleNameOrMemberAccessExpressionOpt
||
syntaxFacts
.
IsObjectInitializerNamedAssignmentIdentifier
(
SimpleNameOrMemberAccessExpressionOpt
);
IsInConstructor
=
DetermineIsInConstructor
(
semanticDocument
,
simpleName
);
IsInMemberContext
=
simpleName
!=
SimpleNameOrMemberAccessExpressionOpt
||
syntaxFacts
.
IsObjectInitializerNamedAssignmentIdentifier
(
SimpleNameOrMemberAccessExpressionOpt
);
ContainingMethod
=
semanticModel
.
GetEnclosingSymbol
<
IMethodSymbol
>(
IdentifierToken
.
SpanStart
,
cancellationToken
);
...
...
@@ -428,15 +427,18 @@ private int GetStatementIndex(ChildSyntaxList children, SyntaxNode statement)
}
}
private
bool
DetermineIsInConstructor
(
SemanticDocument
semanticDocument
)
private
bool
DetermineIsInConstructor
(
SemanticDocument
semanticDocument
,
SyntaxNode
simpleName
)
{
if
(!
ContainingType
.
OriginalDefinition
.
Equals
(
TypeToGenerateIn
.
OriginalDefinition
))
{
return
false
;
}
var
syntaxFacts
=
semanticDocument
.
Document
.
GetLanguageService
<
ISyntaxFactsService
>();
return
syntaxFacts
.
IsInConstructor
(
SimpleNameOpt
);
// If we're in an lambda/local function we're not actually 'in' the constructor.
// i.e. we can't actually write to read-only fields here.
var
syntaxFacts
=
semanticDocument
.
Document
.
GetRequiredLanguageService
<
ISyntaxFactsService
>();
if
(
simpleName
.
AncestorsAndSelf
().
Any
(
n
=>
syntaxFacts
.
IsAnonymousOrLocalFunction
(
n
)))
return
false
;
return
syntaxFacts
.
IsInConstructor
(
simpleName
);
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录