-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
feat(eslint-plugin): [array-type] detect Readonly<string[]>
case
#8752
base: main
Are you sure you want to change the base?
Changes from 5 commits
1864644
18fff29
2471ff2
2ce7956
76d9f36
dbcd788
8a361fe
1a59bd0
8fc0b82
7482f0d
f3e3cff
ae91e94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,8 @@ const y: readonly string[] = ['a', 'b']; | |
|
||
### `"generic"` | ||
|
||
Always use `Array<T>` or `ReadonlyArray<T>` for all array types. | ||
Always use `Array<T>` or `ReadonlyArray<T>` or `Readonly<T[]>` for all array types. | ||
However, `readonly T[]` is modified to `ReadonlyArray<T>`. | ||
|
||
<Tabs> | ||
<TabItem value="❌ Incorrect"> | ||
|
@@ -58,6 +59,7 @@ const y: readonly string[] = ['a', 'b']; | |
```ts option='{ "default": "generic" }' | ||
const x: Array<string> = ['a', 'b']; | ||
const y: ReadonlyArray<string> = ['a', 'b']; | ||
const z: Readonly<string[]> = ['a', 'b']; | ||
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. re #8752 (comment)
Shoutout to @auvred's for his work in #8382 / https://github.com/typescript-eslint/typescript-eslint/pull/8497/files#diff-62842d0740c234c02c26a4ccd3b3fe920b872cff1479f0703c6ec44f9cfb2f3dR407 that caught this! 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. 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. Hi, sorry for the slow response. I'm not really familiar with this change, and, I apologize, but I'm having a bit of a hard time understanding what you mean in the question... I'm wondering if the issue is that, now that the snapshot has been set, the snapshot tests are failing due to the examples being changed? If so, you may just need to run Let me know if that resolves your issue or if the problem is something else! 🙂 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. BTW - looking closer at the screenshot, i think you might also need to run |
||
``` | ||
|
||
</TabItem> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,7 +82,8 @@ type MessageIds = | |
| 'errorStringArray' | ||
| 'errorStringArraySimple' | ||
| 'errorStringGeneric' | ||
| 'errorStringGenericSimple'; | ||
| 'errorStringGenericSimple' | ||
| 'errorStringArrayReadonly'; | ||
|
||
export default createRule<Options, MessageIds>({ | ||
name: 'array-type', | ||
|
@@ -99,6 +100,8 @@ export default createRule<Options, MessageIds>({ | |
"Array type using '{{readonlyPrefix}}{{type}}[]' is forbidden. Use '{{className}}<{{type}}>' instead.", | ||
errorStringArray: | ||
"Array type using '{{className}}<{{type}}>' is forbidden. Use '{{readonlyPrefix}}{{type}}[]' instead.", | ||
errorStringArrayReadonly: | ||
"Array type using '{{className}}<{{type}}>' is forbidden. Use '{{readonlyPrefix}}{{type}}' instead.", | ||
errorStringArraySimple: | ||
"Array type using '{{className}}<{{type}}>' is forbidden for simple types. Use '{{readonlyPrefix}}{{type}}[]' instead.", | ||
errorStringGenericSimple: | ||
|
@@ -199,13 +202,20 @@ export default createRule<Options, MessageIds>({ | |
node.typeName.type !== AST_NODE_TYPES.Identifier || | ||
!( | ||
node.typeName.name === 'Array' || | ||
node.typeName.name === 'ReadonlyArray' | ||
node.typeName.name === 'ReadonlyArray' || | ||
node.typeName.name === 'Readonly' | ||
) | ||
) { | ||
return; | ||
} | ||
|
||
const isReadonlyArrayType = node.typeName.name === 'ReadonlyArray'; | ||
const isReadonlyWithGenericArrayType = | ||
node.typeName.name === 'Readonly' && | ||
node.typeArguments?.params[0].type === AST_NODE_TYPES.TSArrayType; | ||
Comment on lines
+214
to
+216
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. [Bug] If the type name is const x: Readonly<string> = 'a' |
||
const isReadonlyArrayType = | ||
node.typeName.name === 'ReadonlyArray' || | ||
isReadonlyWithGenericArrayType; | ||
|
||
const currentOption = isReadonlyArrayType | ||
? readonlyOption | ||
: defaultOption; | ||
|
@@ -218,7 +228,9 @@ export default createRule<Options, MessageIds>({ | |
const typeParams = node.typeArguments?.params; | ||
const messageId = | ||
currentOption === 'array' | ||
? 'errorStringArray' | ||
? isReadonlyWithGenericArrayType | ||
? 'errorStringArrayReadonly' | ||
: 'errorStringArray' | ||
: 'errorStringArraySimple'; | ||
|
||
if (!typeParams || typeParams.length === 0) { | ||
|
@@ -256,13 +268,13 @@ export default createRule<Options, MessageIds>({ | |
const start = `${parentParens ? '(' : ''}${readonlyPrefix}${ | ||
typeParens ? '(' : '' | ||
}`; | ||
const end = `${typeParens ? ')' : ''}[]${parentParens ? ')' : ''}`; | ||
const end = `${typeParens ? ')' : ''}${isReadonlyWithGenericArrayType ? '' : `[]`}${parentParens ? ')' : ''}`; | ||
|
||
context.report({ | ||
node, | ||
messageId, | ||
data: { | ||
className: isReadonlyArrayType ? 'ReadonlyArray' : 'Array', | ||
className: isReadonlyArrayType ? node.typeName.name : 'Array', | ||
readonlyPrefix, | ||
type: getMessageType(type), | ||
}, | ||
|
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.
[Docs] A bit of clarity ("however" implies contrast with the previous statement)