diff --git a/packages/eslint-plugin/src/rules/array-type.ts b/packages/eslint-plugin/src/rules/array-type.ts index da2bfdef518..998e4b7d5aa 100644 --- a/packages/eslint-plugin/src/rules/array-type.ts +++ b/packages/eslint-plugin/src/rules/array-type.ts @@ -82,7 +82,11 @@ type MessageIds = | 'errorStringGeneric' | 'errorStringGenericSimple' | 'errorStringArray' - | 'errorStringArraySimple'; + | 'errorStringArraySimple' + | 'errorStringGenericReadonly' + | 'errorStringGenericSimpleReadonly' + | 'errorStringArrayReadonly' + | 'errorStringArraySimpleReadonly'; const arrayOption = { enum: ['array', 'generic', 'array-simple'] }; @@ -105,6 +109,14 @@ export default util.createRule({ "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: [ { @@ -155,7 +167,11 @@ export default util.createRule({ const messageId = currentOption === 'generic' - ? 'errorStringGeneric' + ? isReadonly + ? 'errorStringGenericReadonly' + : 'errorStringGeneric' + : isReadonly + ? 'errorStringGenericSimpleReadonly' : 'errorStringGenericSimple'; const errorNode = isReadonly ? node.parent! : node; @@ -205,9 +221,14 @@ export default util.createRule({ 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) { diff --git a/packages/eslint-plugin/tests/rules/array-type.test.ts b/packages/eslint-plugin/tests/rules/array-type.test.ts index 3c2c1893a3f..c19c945c4e3 100644 --- a/packages/eslint-plugin/tests/rules/array-type.test.ts +++ b/packages/eslint-plugin/tests/rules/array-type.test.ts @@ -400,7 +400,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array' }], errors: [ { - messageId: 'errorStringArray', + messageId: 'errorStringArrayReadonly', data: { type: 'number' }, line: 1, column: 8, @@ -413,7 +413,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array' }], errors: [ { - messageId: 'errorStringArray', + messageId: 'errorStringArrayReadonly', data: { type: 'T' }, line: 1, column: 8, @@ -452,7 +452,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array', readonly: 'array' }], errors: [ { - messageId: 'errorStringArray', + messageId: 'errorStringArrayReadonly', data: { type: 'number' }, line: 1, column: 8, @@ -465,7 +465,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array', readonly: 'array' }], errors: [ { - messageId: 'errorStringArray', + messageId: 'errorStringArrayReadonly', data: { type: 'T' }, line: 1, column: 8, @@ -504,7 +504,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array', readonly: 'array-simple' }], errors: [ { - messageId: 'errorStringArraySimple', + messageId: 'errorStringArraySimpleReadonly', data: { type: 'number' }, line: 1, column: 8, @@ -517,7 +517,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array', readonly: 'array-simple' }], errors: [ { - messageId: 'errorStringGenericSimple', + messageId: 'errorStringGenericSimpleReadonly', data: { type: 'T' }, line: 1, column: 8, @@ -556,7 +556,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array', readonly: 'generic' }], errors: [ { - messageId: 'errorStringGeneric', + messageId: 'errorStringGenericReadonly', data: { type: 'number' }, line: 1, column: 8, @@ -569,7 +569,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array', readonly: 'generic' }], errors: [ { - messageId: 'errorStringGeneric', + messageId: 'errorStringGenericReadonly', data: { type: 'T' }, line: 1, column: 8, @@ -608,7 +608,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array-simple' }], errors: [ { - messageId: 'errorStringArraySimple', + messageId: 'errorStringArraySimpleReadonly', data: { type: 'number' }, line: 1, column: 8, @@ -621,7 +621,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array-simple' }], errors: [ { - messageId: 'errorStringGenericSimple', + messageId: 'errorStringGenericSimpleReadonly', data: { type: 'T' }, line: 1, column: 8, @@ -660,7 +660,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array-simple', readonly: 'array' }], errors: [ { - messageId: 'errorStringArray', + messageId: 'errorStringArrayReadonly', data: { type: 'number' }, line: 1, column: 8, @@ -673,7 +673,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array-simple', readonly: 'array' }], errors: [ { - messageId: 'errorStringArray', + messageId: 'errorStringArrayReadonly', data: { type: 'T' }, line: 1, column: 8, @@ -712,7 +712,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array-simple', readonly: 'array-simple' }], errors: [ { - messageId: 'errorStringArraySimple', + messageId: 'errorStringArraySimpleReadonly', data: { type: 'number' }, line: 1, column: 8, @@ -725,7 +725,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array-simple', readonly: 'array-simple' }], errors: [ { - messageId: 'errorStringGenericSimple', + messageId: 'errorStringGenericSimpleReadonly', data: { type: 'T' }, line: 1, column: 8, @@ -764,7 +764,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array-simple', readonly: 'generic' }], errors: [ { - messageId: 'errorStringGeneric', + messageId: 'errorStringGenericReadonly', data: { type: 'number' }, line: 1, column: 8, @@ -777,7 +777,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array-simple', readonly: 'generic' }], errors: [ { - messageId: 'errorStringGeneric', + messageId: 'errorStringGenericReadonly', data: { type: 'T' }, line: 1, column: 8, @@ -816,7 +816,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'generic' }], errors: [ { - messageId: 'errorStringGeneric', + messageId: 'errorStringGenericReadonly', data: { type: 'number' }, line: 1, column: 8, @@ -829,7 +829,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'generic' }], errors: [ { - messageId: 'errorStringGeneric', + messageId: 'errorStringGenericReadonly', data: { type: 'T' }, line: 1, column: 8, @@ -868,7 +868,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'generic', readonly: 'array' }], errors: [ { - messageId: 'errorStringArray', + messageId: 'errorStringArrayReadonly', data: { type: 'number' }, line: 1, column: 8, @@ -881,7 +881,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'generic', readonly: 'array' }], errors: [ { - messageId: 'errorStringArray', + messageId: 'errorStringArrayReadonly', data: { type: 'T' }, line: 1, column: 8, @@ -920,7 +920,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'generic', readonly: 'array-simple' }], errors: [ { - messageId: 'errorStringArraySimple', + messageId: 'errorStringArraySimpleReadonly', data: { type: 'number' }, line: 1, column: 8, @@ -933,7 +933,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'generic', readonly: 'array-simple' }], errors: [ { - messageId: 'errorStringGenericSimple', + messageId: 'errorStringGenericSimpleReadonly', data: { type: 'T' }, line: 1, column: 8, @@ -972,7 +972,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'generic', readonly: 'generic' }], errors: [ { - messageId: 'errorStringGeneric', + messageId: 'errorStringGenericReadonly', data: { type: 'number' }, line: 1, column: 8, @@ -985,7 +985,7 @@ function bazFunction(baz: Arr>) { options: [{ default: 'generic', readonly: 'generic' }], errors: [ { - messageId: 'errorStringGeneric', + messageId: 'errorStringGenericReadonly', data: { type: 'T' }, line: 1, column: 8, @@ -1451,6 +1451,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringArraySimple', + data: { type: 'any' }, line: 1, column: 8, }, @@ -1654,7 +1655,7 @@ interface FooInterface { options: [{ default: 'array' }], errors: [ { - messageId: 'errorStringArray', + messageId: 'errorStringArrayReadonly', data: { type: 'object' }, line: 1, column: 12,