-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): added related-getter-setter-pairs rule #10192
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
JoshuaKGoldberg
merged 7 commits into
typescript-eslint:main
from
JoshuaKGoldberg:related-getter-setter-pairs
Nov 11, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5e9817f
feat(eslint-plugin): added related-getter-setter-pairs rule
JoshuaKGoldberg c74b5ea
Merge branch 'main' into related-getter-setter-pairs
JoshuaKGoldberg ac1f0e8
Fixed stack popping
JoshuaKGoldberg ab93e7f
Fixed stack popping
JoshuaKGoldberg 51971cd
Correction: reported getter always has return type annotation
JoshuaKGoldberg 3348863
Correction: reported getter always has return type annotation
JoshuaKGoldberg e76d87b
Update packages/eslint-plugin/docs/rules/related-getter-setter-pairs.mdx
JoshuaKGoldberg 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
61 changes: 61 additions & 0 deletions
61
packages/eslint-plugin/docs/rules/related-getter-setter-pairs.mdx
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
description: 'Enforce that `get()` types should be assignable to their equivalent `set()` type.' | ||
--- | ||
|
||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
|
||
> 🛑 This file is source code, not the primary documentation location! 🛑 | ||
> | ||
> See **https://typescript-eslint.io/rules/related-getter-setter-pairs** for documentation. | ||
|
||
TypeScript allows defining different types for a `get` parameter and its corresponding `set` return. | ||
Prior to TypeScript 4.3, the types had to be identical. | ||
From TypeScript 4.3 to 5.0, the `get` type had to be a subtype of the `set` type. | ||
As of TypeScript 5.1, the types may be completely unrelated as long as there is an explicit type annotation. | ||
|
||
Defining drastically different types for a `get` and `set` pair can be confusing. | ||
It means that assigning a property to itself would not work: | ||
|
||
```ts | ||
// Assumes box.value's get() return is assignable to its set() parameter | ||
box.value = box.value; | ||
``` | ||
|
||
This rule reports cases where a `get()` and `set()` have the same name, but the `get()`'s type is not assignable to the `set()`'s. | ||
|
||
## Examples | ||
|
||
<Tabs> | ||
<TabItem value="❌ Incorrect"> | ||
|
||
```ts | ||
interface Box { | ||
get value(): string; | ||
set value(newValue: number); | ||
} | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="✅ Correct"> | ||
|
||
```ts | ||
interface Box { | ||
get value(): string; | ||
set value(newValue: string); | ||
} | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
## When Not To Use It | ||
|
||
If your project needs to model unusual relationships between data, such as older DOM types, this rule may not be useful for you. | ||
You might consider using [ESLint disable comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) for those specific situations instead of completely disabling this rule. | ||
|
||
## Further Reading | ||
|
||
- [MDN documentation on `get`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get) | ||
- [MDN documentation on `set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set) | ||
- [TypeScript 5.1 Release Notes > Unrelated Types for Getters and Setters](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-1.html#unrelated-types-for-getters-and-setters) |
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
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
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
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
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
113 changes: 113 additions & 0 deletions
113
packages/eslint-plugin/src/rules/related-getter-setter-pairs.ts
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 |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import type { TSESTree } from '@typescript-eslint/utils'; | ||
|
||
import { AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
|
||
import { createRule, getNameFromMember, getParserServices } from '../util'; | ||
|
||
type Method = TSESTree.MethodDefinition | TSESTree.TSMethodSignature; | ||
|
||
type GetMethod = { | ||
kind: 'get'; | ||
returnType: TSESTree.TSTypeAnnotation; | ||
} & Method; | ||
|
||
type GetMethodRaw = { | ||
returnType: TSESTree.TSTypeAnnotation | undefined; | ||
} & GetMethod; | ||
|
||
type SetMethod = { kind: 'set'; params: [TSESTree.Node] } & Method; | ||
|
||
interface MethodPair { | ||
get?: GetMethod; | ||
set?: SetMethod; | ||
} | ||
|
||
export default createRule({ | ||
name: 'related-getter-setter-pairs', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: | ||
'Enforce that `get()` types should be assignable to their equivalent `set()` type', | ||
recommended: 'strict', | ||
requiresTypeChecking: true, | ||
}, | ||
messages: { | ||
mismatch: | ||
'`get()` type should be assignable to its equivalent `set()` type.', | ||
}, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const services = getParserServices(context); | ||
const checker = services.program.getTypeChecker(); | ||
const methodPairsStack: Map<string, MethodPair>[] = []; | ||
|
||
function addPropertyNode( | ||
member: GetMethod | SetMethod, | ||
inner: TSESTree.Node, | ||
kind: 'get' | 'set', | ||
): void { | ||
const methodPairs = methodPairsStack[methodPairsStack.length - 1]; | ||
const { name } = getNameFromMember(member, context.sourceCode); | ||
|
||
methodPairs.set(name, { | ||
...methodPairs.get(name), | ||
[kind]: inner, | ||
}); | ||
} | ||
|
||
return { | ||
':matches(ClassBody, TSInterfaceBody, TSTypeLiteral):exit'(): void { | ||
const methodPairs = methodPairsStack[methodPairsStack.length - 1]; | ||
|
||
for (const pair of methodPairs.values()) { | ||
if (!pair.get || !pair.set) { | ||
continue; | ||
} | ||
|
||
const getter = pair.get; | ||
|
||
const getType = services.getTypeAtLocation(getter); | ||
const setType = services.getTypeAtLocation(pair.set.params[0]); | ||
|
||
if (!checker.isTypeAssignableTo(getType, setType)) { | ||
context.report({ | ||
node: getter.returnType.typeAnnotation, | ||
messageId: 'mismatch', | ||
}); | ||
} | ||
} | ||
|
||
methodPairsStack.pop(); | ||
}, | ||
':matches(MethodDefinition, TSMethodSignature)[kind=get]'( | ||
node: GetMethodRaw, | ||
): void { | ||
const getter = getMethodFromNode(node); | ||
|
||
if (getter.returnType) { | ||
addPropertyNode(node, getter, 'get'); | ||
} | ||
}, | ||
':matches(MethodDefinition, TSMethodSignature)[kind=set]'( | ||
node: SetMethod, | ||
): void { | ||
const setter = getMethodFromNode(node); | ||
|
||
if (setter.params.length === 1) { | ||
addPropertyNode(node, setter, 'set'); | ||
} | ||
}, | ||
|
||
'ClassBody, TSInterfaceBody, TSTypeLiteral'(): void { | ||
methodPairsStack.push(new Map()); | ||
}, | ||
}; | ||
}, | ||
}); | ||
|
||
function getMethodFromNode(node: GetMethodRaw | SetMethod) { | ||
return node.type === AST_NODE_TYPES.TSMethodSignature ? node : node.value; | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/eslint-plugin/tests/docs-eslint-output-snapshots/related-getter-setter-pairs.shot
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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.
🤷