-
-
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 8 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 |
---|---|---|
|
@@ -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), | ||
}, | ||
|
developer-bandi marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -397,6 +397,10 @@ function bazFunction(baz: Arr<ArrayClass<String>>) { | |
code: 'let a: readonly Array<number>[] = [[]];', | ||
options: [{ default: 'generic', readonly: 'array' }], | ||
}, | ||
{ | ||
code: 'let a: Readonly = [];', | ||
options: [{ default: 'generic', readonly: 'array' }], | ||
}, | ||
], | ||
invalid: [ | ||
// Base cases from https://github.com/typescript-eslint/typescript-eslint/issues/2323#issuecomment-663977655 | ||
|
@@ -1351,7 +1355,7 @@ let yyyy: Arr<Array<Array<Arr<string>>>> = [[[['2']]]]; | |
messageId: 'errorStringGenericSimple', | ||
data: { className: 'Array', readonlyPrefix: '', type: 'T' }, | ||
line: 3, | ||
column: 15, | ||
column: 19, | ||
}, | ||
], | ||
}, | ||
|
@@ -1378,7 +1382,7 @@ interface ArrayClass<T> { | |
messageId: 'errorStringArraySimple', | ||
data: { className: 'Array', readonlyPrefix: '', type: 'T' }, | ||
line: 3, | ||
column: 8, | ||
column: 12, | ||
}, | ||
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. [Tests] Could you revert these changed |
||
], | ||
}, | ||
|
@@ -1399,7 +1403,7 @@ function barFunction(bar: Array<ArrayClass<String>>) { | |
messageId: 'errorStringGenericSimple', | ||
data: { className: 'Array', readonlyPrefix: '', type: 'T' }, | ||
line: 2, | ||
column: 27, | ||
column: 31, | ||
}, | ||
], | ||
}, | ||
|
@@ -1539,7 +1543,7 @@ let yyyy: Arr<Arr<string>[][]> = [[[['2']]]]; | |
messageId: 'errorStringArray', | ||
data: { className: 'Array', readonlyPrefix: '', type: 'T' }, | ||
line: 3, | ||
column: 15, | ||
column: 19, | ||
}, | ||
], | ||
}, | ||
|
@@ -1564,7 +1568,7 @@ interface ArrayClass<T> { | |
messageId: 'errorStringArray', | ||
data: { className: 'Array', readonlyPrefix: '', type: 'T' }, | ||
line: 3, | ||
column: 8, | ||
column: 12, | ||
}, | ||
], | ||
}, | ||
|
@@ -1585,7 +1589,7 @@ function fooFunction(foo: ArrayClass<string>[]) { | |
messageId: 'errorStringArray', | ||
data: { className: 'Array', readonlyPrefix: '', type: 'T' }, | ||
line: 2, | ||
column: 27, | ||
column: 31, | ||
}, | ||
], | ||
}, | ||
|
@@ -1733,7 +1737,7 @@ let yyyy: Arr<Array<Array<Arr<string>>>> = [[[['2']]]]; | |
messageId: 'errorStringGeneric', | ||
data: { className: 'Array', readonlyPrefix: '', type: 'T' }, | ||
line: 3, | ||
column: 15, | ||
column: 19, | ||
}, | ||
], | ||
}, | ||
|
@@ -1758,7 +1762,7 @@ interface ArrayClass<T> { | |
messageId: 'errorStringGeneric', | ||
data: { className: 'Array', readonlyPrefix: '', type: 'T' }, | ||
line: 4, | ||
column: 8, | ||
column: 12, | ||
}, | ||
], | ||
}, | ||
|
@@ -1779,7 +1783,7 @@ function barFunction(bar: Array<ArrayClass<String>>) { | |
messageId: 'errorStringGeneric', | ||
data: { className: 'Array', readonlyPrefix: '', type: 'T' }, | ||
line: 2, | ||
column: 27, | ||
column: 31, | ||
}, | ||
], | ||
}, | ||
|
@@ -1839,7 +1843,7 @@ interface FooInterface { | |
messageId: 'errorStringGeneric', | ||
data: { className: 'Array', readonlyPrefix: '', type: 'string' }, | ||
line: 3, | ||
column: 18, | ||
column: 22, | ||
}, | ||
], | ||
}, | ||
|
@@ -1918,6 +1922,21 @@ interface FooInterface { | |
}, | ||
], | ||
}, | ||
{ | ||
code: "const x: Readonly<string[]> = ['a', 'b'];", | ||
output: "const x: readonly string[] = ['a', 'b'];", | ||
options: [{ default: 'array' }], | ||
errors: [ | ||
{ | ||
messageId: 'errorStringArrayReadonly', | ||
data: { | ||
className: 'Readonly', | ||
readonlyPrefix: 'readonly ', | ||
type: 'string[]', | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
|
||
|
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?