-
-
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
Open
developer-bandi
wants to merge
12
commits into
typescript-eslint:main
Choose a base branch
from
developer-bandi:feature/array-type-allow-readonly
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
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 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1864644
feat: add new type
developer-bandi 18fff29
fix: test case
developer-bandi 2471ff2
test: add test case
developer-bandi 2ce7956
docs: add `ReadonlyArray<T>` description
developer-bandi 76d9f36
test: add droped test case
developer-bandi dbcd788
fix: revert comment line and fix error position
developer-bandi 8a361fe
fix: fix indent, docs description
developer-bandi 1a59bd0
fix: revert indent
developer-bandi 8fc0b82
fix: add exception and delete not related PR
developer-bandi 7482f0d
Merge branch 'main' of https://github.com/developer-bandi/typescript-…
developer-bandi f3e3cff
docs: move no correct example to incorrect example
developer-bandi ae91e94
docs: fix example document
developer-bandi File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
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,22 @@ 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' | ||
) || | ||
(node.typeName.name === 'Readonly' && | ||
node.typeArguments?.params[0].type !== AST_NODE_TYPES.TSArrayType) | ||
) { | ||
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 +230,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 +270,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), | ||
}, | ||
|
developer-bandi marked this conversation as resolved.
Show resolved
Hide resolved
|
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
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.
re #8752 (comment)
(One of) the CI failure(s) is due to this line being flagged, despite being in the "Correct" tab. (you can try it for yourself in the deploy preview by clicking the "Open in playground" button)
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 comment
The reason will be displayed to describe this comment to others. Learn more.
thank you for your advise!
but there is one problem. i use incorrect example
const z: Readonly<string[]> = ['a', 'b'];
but lint cannot use this type. Is there no way?