-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): added related-getter-setter-pairs rule (#10192)
* feat(eslint-plugin): added related-getter-setter-pairs rule * Fixed stack popping * Fixed stack popping * Correction: reported getter always has return type annotation * Correction: reported getter always has return type annotation * Update packages/eslint-plugin/docs/rules/related-getter-setter-pairs.mdx Co-authored-by: Joshua Chen <sidachen2003@gmail.com> --------- Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
- Loading branch information
1 parent
16fba0a
commit 0409851
Showing
14 changed files
with
474 additions
and
1 deletion.
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 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 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 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 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 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 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 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.