Repository navigation
Prevent member name collisions in PowerShell classes #8372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mathias R. Jessen (IISResetMe)
wants to merge
12
commits into
PowerShell:master
Choose a base branch
from
IISResetMe:patch/class-member-name-collisions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bfd4be7
Disallow class definitions with duplicate member names
IISResetMe 5404f94
Move mutual check for duplicate class member names to PSType
IISResetMe 7a9e6ec
Update PSType.cs
IISResetMe 999bfcb
Exclude ctor definitions from member name collision checks
IISResetMe bca5cbd
Add negative parsing tests for member name collisions
IISResetMe cd680a7
Fix CodeFactor issue (SA1408)
IISResetMe 4eecf75
Exit early on property name collision checks for constructor definitions
iSazonov 1323dc9
Avoid unnecessary use of LINQ (.Any(this IEnumerable<T>)) in PSType
IISResetMe 94cb873
[noop] re-run failed tests
IISResetMe fbc76a3
Add comment regarding property name collision check logic
IISResetMe ab38d4d
Expand previous commit
IISResetMe 8bfff0f
Trigger tests
IISResetMe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -542,7 +542,15 @@ public void DefineMembers() | |
|
|
||
| private void DefineProperty(PropertyMemberAst propertyMemberAst) | ||
| { | ||
| if (_definedProperties.ContainsKey(propertyMemberAst.Name)) | ||
| // Report an error if the property name collides with an existing property or a non-constructor method definition | ||
| // Property naming collisions with constructors are fine though, "$this.C" unambiguously refers to the property in this example: | ||
| // | ||
| // class C { | ||
| // C() {} | ||
| // $C | ||
| // } | ||
| // | ||
| if (_definedProperties.ContainsKey(propertyMemberAst.Name) || (_definedMethods.ContainsKey(propertyMemberAst.Name) && !_definedMethods[propertyMemberAst.Name][0].Item1.IsConstructor)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Can you please define a local variable to hold |
||
| { | ||
| _parser.ReportError(propertyMemberAst.Extent, | ||
| nameof(ParserStrings.MemberAlreadyDefined), | ||
|
|
@@ -665,6 +673,15 @@ private PropertyBuilder EmitPropertyIl(PropertyMemberAst propertyMemberAst, Type | |
|
|
||
| private bool CheckForDuplicateOverload(FunctionMemberAst functionMemberAst, Type[] newParameters) | ||
| { | ||
| if (!functionMemberAst.IsConstructor && _definedProperties.ContainsKey(functionMemberAst.Name)) | ||
| { | ||
| _parser.ReportError(functionMemberAst.NameExtent ?? functionMemberAst.Extent, | ||
| nameof(ParserStrings.MemberAlreadyDefined), | ||
| ParserStrings.MemberAlreadyDefined, | ||
| functionMemberAst.Name); | ||
| return true; | ||
| } | ||
|
|
||
| List<Tuple<FunctionMemberAst, Type[]>> overloads; | ||
| if (!_definedMethods.TryGetValue(functionMemberAst.Name, out overloads)) | ||
| { | ||
|
|
@@ -700,7 +717,8 @@ private bool CheckForDuplicateOverload(FunctionMemberAst functionMemberAst, Type | |
| if (overload.Item1.IsStatic == functionMemberAst.IsStatic || | ||
| !functionMemberAst.IsConstructor) | ||
| { | ||
| _parser.ReportError(functionMemberAst.NameExtent ?? functionMemberAst.Extent, | ||
| _parser.ReportError( | ||
| functionMemberAst.NameExtent ?? functionMemberAst.Extent, | ||
| nameof(ParserStrings.MemberAlreadyDefined), | ||
| ParserStrings.MemberAlreadyDefined, | ||
| functionMemberAst.Name); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.