Skip to content
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

fix(eslint-plugin): [array-type] correct error message for readonly #4193

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 24 additions & 3 deletions packages/eslint-plugin/src/rules/array-type.ts
Expand Up @@ -82,7 +82,11 @@ type MessageIds =
| 'errorStringGeneric'
| 'errorStringGenericSimple'
| 'errorStringArray'
| 'errorStringArraySimple';
| 'errorStringArraySimple'
| 'errorStringGenericReadonly'
| 'errorStringGenericSimpleReadonly'
| 'errorStringArrayReadonly'
| 'errorStringArraySimpleReadonly';

const arrayOption = { enum: ['array', 'generic', 'array-simple'] };

Expand All @@ -105,6 +109,14 @@ export default util.createRule<Options, MessageIds>({
"Array type using 'Array<{{type}}>' is forbidden. Use '{{type}}[]' instead.",
errorStringArraySimple:
"Array type using 'Array<{{type}}>' is forbidden for simple types. Use '{{type}}[]' instead.",
errorStringGenericReadonly:
"Array type using 'readonly {{type}}[]' is forbidden. Use 'ReadonlyArray<{{type}}>' instead.",
errorStringGenericSimpleReadonly:
"Array type using 'readonly {{type}}[]' is forbidden for non-simple types. Use 'ReadonlyArray<{{type}}>' instead.",
errorStringArrayReadonly:
"Array type using 'ReadonlyArray<{{type}}>' is forbidden. Use 'readonly {{type}}[]' instead.",
errorStringArraySimpleReadonly:
"Array type using 'ReadonlyArray<{{type}}>' is forbidden for simple types. Use 'readonly {{type}}[]' instead.",
},
schema: [
{
Expand Down Expand Up @@ -155,7 +167,11 @@ export default util.createRule<Options, MessageIds>({

const messageId =
currentOption === 'generic'
? 'errorStringGeneric'
? isReadonly
? 'errorStringGenericReadonly'
: 'errorStringGeneric'
: isReadonly
? 'errorStringGenericSimpleReadonly'
: 'errorStringGenericSimple';
const errorNode = isReadonly ? node.parent! : node;

Expand Down Expand Up @@ -205,9 +221,14 @@ export default util.createRule<Options, MessageIds>({

const readonlyPrefix = isReadonlyArrayType ? 'readonly ' : '';
const typeParams = node.typeParameters?.params;

const messageId =
currentOption === 'array'
? 'errorStringArray'
? isReadonlyArrayType
? 'errorStringArrayReadonly'
: 'errorStringArray'
: isReadonlyArrayType
? 'errorStringArraySimpleReadonly'
: 'errorStringArraySimple';

if (!typeParams || typeParams.length === 0) {
Expand Down
51 changes: 26 additions & 25 deletions packages/eslint-plugin/tests/rules/array-type.test.ts
Expand Up @@ -400,7 +400,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'array' }],
errors: [
{
messageId: 'errorStringArray',
messageId: 'errorStringArrayReadonly',
data: { type: 'number' },
line: 1,
column: 8,
Expand All @@ -413,7 +413,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'array' }],
errors: [
{
messageId: 'errorStringArray',
messageId: 'errorStringArrayReadonly',
data: { type: 'T' },
line: 1,
column: 8,
Expand Down Expand Up @@ -452,7 +452,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'array', readonly: 'array' }],
errors: [
{
messageId: 'errorStringArray',
messageId: 'errorStringArrayReadonly',
data: { type: 'number' },
line: 1,
column: 8,
Expand All @@ -465,7 +465,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'array', readonly: 'array' }],
errors: [
{
messageId: 'errorStringArray',
messageId: 'errorStringArrayReadonly',
data: { type: 'T' },
line: 1,
column: 8,
Expand Down Expand Up @@ -504,7 +504,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'array', readonly: 'array-simple' }],
errors: [
{
messageId: 'errorStringArraySimple',
messageId: 'errorStringArraySimpleReadonly',
data: { type: 'number' },
line: 1,
column: 8,
Expand All @@ -517,7 +517,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'array', readonly: 'array-simple' }],
errors: [
{
messageId: 'errorStringGenericSimple',
messageId: 'errorStringGenericSimpleReadonly',
data: { type: 'T' },
line: 1,
column: 8,
Expand Down Expand Up @@ -556,7 +556,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'array', readonly: 'generic' }],
errors: [
{
messageId: 'errorStringGeneric',
messageId: 'errorStringGenericReadonly',
data: { type: 'number' },
line: 1,
column: 8,
Expand All @@ -569,7 +569,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'array', readonly: 'generic' }],
errors: [
{
messageId: 'errorStringGeneric',
messageId: 'errorStringGenericReadonly',
data: { type: 'T' },
line: 1,
column: 8,
Expand Down Expand Up @@ -608,7 +608,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'array-simple' }],
errors: [
{
messageId: 'errorStringArraySimple',
messageId: 'errorStringArraySimpleReadonly',
data: { type: 'number' },
line: 1,
column: 8,
Expand All @@ -621,7 +621,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'array-simple' }],
errors: [
{
messageId: 'errorStringGenericSimple',
messageId: 'errorStringGenericSimpleReadonly',
data: { type: 'T' },
line: 1,
column: 8,
Expand Down Expand Up @@ -660,7 +660,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'array-simple', readonly: 'array' }],
errors: [
{
messageId: 'errorStringArray',
messageId: 'errorStringArrayReadonly',
data: { type: 'number' },
line: 1,
column: 8,
Expand All @@ -673,7 +673,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'array-simple', readonly: 'array' }],
errors: [
{
messageId: 'errorStringArray',
messageId: 'errorStringArrayReadonly',
data: { type: 'T' },
line: 1,
column: 8,
Expand Down Expand Up @@ -712,7 +712,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'array-simple', readonly: 'array-simple' }],
errors: [
{
messageId: 'errorStringArraySimple',
messageId: 'errorStringArraySimpleReadonly',
data: { type: 'number' },
line: 1,
column: 8,
Expand All @@ -725,7 +725,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'array-simple', readonly: 'array-simple' }],
errors: [
{
messageId: 'errorStringGenericSimple',
messageId: 'errorStringGenericSimpleReadonly',
data: { type: 'T' },
line: 1,
column: 8,
Expand Down Expand Up @@ -764,7 +764,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'array-simple', readonly: 'generic' }],
errors: [
{
messageId: 'errorStringGeneric',
messageId: 'errorStringGenericReadonly',
data: { type: 'number' },
line: 1,
column: 8,
Expand All @@ -777,7 +777,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'array-simple', readonly: 'generic' }],
errors: [
{
messageId: 'errorStringGeneric',
messageId: 'errorStringGenericReadonly',
data: { type: 'T' },
line: 1,
column: 8,
Expand Down Expand Up @@ -816,7 +816,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'generic' }],
errors: [
{
messageId: 'errorStringGeneric',
messageId: 'errorStringGenericReadonly',
data: { type: 'number' },
line: 1,
column: 8,
Expand All @@ -829,7 +829,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'generic' }],
errors: [
{
messageId: 'errorStringGeneric',
messageId: 'errorStringGenericReadonly',
data: { type: 'T' },
line: 1,
column: 8,
Expand Down Expand Up @@ -868,7 +868,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'generic', readonly: 'array' }],
errors: [
{
messageId: 'errorStringArray',
messageId: 'errorStringArrayReadonly',
data: { type: 'number' },
line: 1,
column: 8,
Expand All @@ -881,7 +881,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'generic', readonly: 'array' }],
errors: [
{
messageId: 'errorStringArray',
messageId: 'errorStringArrayReadonly',
data: { type: 'T' },
line: 1,
column: 8,
Expand Down Expand Up @@ -920,7 +920,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'generic', readonly: 'array-simple' }],
errors: [
{
messageId: 'errorStringArraySimple',
messageId: 'errorStringArraySimpleReadonly',
data: { type: 'number' },
line: 1,
column: 8,
Expand All @@ -933,7 +933,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'generic', readonly: 'array-simple' }],
errors: [
{
messageId: 'errorStringGenericSimple',
messageId: 'errorStringGenericSimpleReadonly',
data: { type: 'T' },
line: 1,
column: 8,
Expand Down Expand Up @@ -972,7 +972,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'generic', readonly: 'generic' }],
errors: [
{
messageId: 'errorStringGeneric',
messageId: 'errorStringGenericReadonly',
data: { type: 'number' },
line: 1,
column: 8,
Expand All @@ -985,7 +985,7 @@ function bazFunction(baz: Arr<ArrayClass<String>>) {
options: [{ default: 'generic', readonly: 'generic' }],
errors: [
{
messageId: 'errorStringGeneric',
messageId: 'errorStringGenericReadonly',
data: { type: 'T' },
line: 1,
column: 8,
Expand Down Expand Up @@ -1451,6 +1451,7 @@ function fooFunction(foo: ArrayClass<string>[]) {
errors: [
{
messageId: 'errorStringArraySimple',
data: { type: 'any' },
line: 1,
column: 8,
},
Expand Down Expand Up @@ -1654,7 +1655,7 @@ interface FooInterface {
options: [{ default: 'array' }],
errors: [
{
messageId: 'errorStringArray',
messageId: 'errorStringArrayReadonly',
data: { type: 'object' },
line: 1,
column: 12,
Expand Down