-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(eslint-plugin): [prefer-readonly] support modifiers of unions and intersections #8169
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
Merged
Merged
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -251,6 +251,13 @@ type ParameterOrPropertyDeclaration = | |
const OUTSIDE_CONSTRUCTOR = -1; | ||
const DIRECTLY_INSIDE_CONSTRUCTOR = 0; | ||
|
||
enum TypeToClassRelation { | ||
ClassAndInstance, | ||
Class, | ||
Instance, | ||
None, | ||
} | ||
|
||
class ClassScope { | ||
private readonly privateModifiableMembers = new Map< | ||
string, | ||
|
@@ -312,29 +319,75 @@ class ClassScope { | |
).set(node.name.getText(), node); | ||
} | ||
|
||
public getTypeToClassRelation(type: ts.Type): TypeToClassRelation { | ||
if (type.isIntersection()) { | ||
let result: TypeToClassRelation = TypeToClassRelation.None; | ||
for (const subType of type.types) { | ||
const subTypeResult = this.getTypeToClassRelation(subType); | ||
switch (subTypeResult) { | ||
case TypeToClassRelation.Class: | ||
if (result === TypeToClassRelation.Instance) { | ||
return TypeToClassRelation.ClassAndInstance; | ||
} | ||
result = TypeToClassRelation.Class; | ||
break; | ||
case TypeToClassRelation.Instance: | ||
if (result === TypeToClassRelation.Class) { | ||
return TypeToClassRelation.ClassAndInstance; | ||
} | ||
result = TypeToClassRelation.Instance; | ||
break; | ||
} | ||
} | ||
return result; | ||
} | ||
if (type.isUnion()) { | ||
// any union of class/instance and something else will prevent access to | ||
// private members, so we assume that union consists only of classes | ||
// or class instances, because otherwise tsc will report an error | ||
return this.getTypeToClassRelation(type.types[0]); | ||
} | ||
Comment on lines
+344
to
+349
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. class Test {
private foo: number = 1
private static foo: number
test() {
const that1 = {} as typeof Test | this
that1.foo = 2
// ^ 2339: Property 'foo' does not exist on type 'this | typeof Test'
const that2 = {} as this | { _foo: 'bar' }
that2.foo = 2
// ^ 2339: Property 'foo' does not exist on type 'this | { _foo: "bar"; }'
const that3 = {} as typeof Test | { _foo: 'bar' }
that3.foo = 2
// ^ 2339: Property 'foo' does not exist on type 'typeof Test | { _foo: "bar"; }'
}
} |
||
|
||
if (!type.getSymbol() || !typeIsOrHasBaseType(type, this.classType)) { | ||
return TypeToClassRelation.None; | ||
} | ||
|
||
const typeIsClass = | ||
tsutils.isObjectType(type) && | ||
tsutils.isObjectFlagSet(type, ts.ObjectFlags.Anonymous); | ||
|
||
if (typeIsClass) { | ||
return TypeToClassRelation.Class; | ||
} | ||
|
||
return TypeToClassRelation.Instance; | ||
} | ||
|
||
public addVariableModification(node: ts.PropertyAccessExpression): void { | ||
const modifierType = this.checker.getTypeAtLocation(node.expression); | ||
|
||
const relationOfModifierTypeToClass = | ||
this.getTypeToClassRelation(modifierType); | ||
|
||
if ( | ||
!modifierType.getSymbol() || | ||
!typeIsOrHasBaseType(modifierType, this.classType) | ||
relationOfModifierTypeToClass === TypeToClassRelation.Instance && | ||
this.constructorScopeDepth === DIRECTLY_INSIDE_CONSTRUCTOR | ||
) { | ||
return; | ||
} | ||
|
||
const modifyingStatic = | ||
tsutils.isObjectType(modifierType) && | ||
tsutils.isObjectFlagSet(modifierType, ts.ObjectFlags.Anonymous); | ||
if ( | ||
!modifyingStatic && | ||
this.constructorScopeDepth === DIRECTLY_INSIDE_CONSTRUCTOR | ||
relationOfModifierTypeToClass === TypeToClassRelation.Instance || | ||
relationOfModifierTypeToClass === TypeToClassRelation.ClassAndInstance | ||
) { | ||
return; | ||
this.memberVariableModifications.add(node.name.text); | ||
} | ||
if ( | ||
relationOfModifierTypeToClass === TypeToClassRelation.Class || | ||
relationOfModifierTypeToClass === TypeToClassRelation.ClassAndInstance | ||
) { | ||
this.staticVariableModifications.add(node.name.text); | ||
} | ||
|
||
(modifyingStatic | ||
? this.staticVariableModifications | ||
: this.memberVariableModifications | ||
).add(node.name.text); | ||
} | ||
|
||
public enterConstructor( | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Praise] I found this enum and the logic around it to be quite helpful in understanding the new logic. Very readable. Nice! 👏