From 8c2122ac0f54063bb050650a8b009da873723d1e Mon Sep 17 00:00:00 2001 From: Armano Date: Fri, 19 Nov 2021 20:31:28 +0100 Subject: [PATCH 1/3] fix: [array-type] correct error message for readonly --- .../eslint-plugin/src/rules/array-type.ts | 15 +- .../tests/rules/array-type.test.ts | 185 +++++++++--------- 2 files changed, 102 insertions(+), 98 deletions(-) diff --git a/packages/eslint-plugin/src/rules/array-type.ts b/packages/eslint-plugin/src/rules/array-type.ts index da2bfdef518..f825c3a90df 100644 --- a/packages/eslint-plugin/src/rules/array-type.ts +++ b/packages/eslint-plugin/src/rules/array-type.ts @@ -98,13 +98,13 @@ export default util.createRule({ fixable: 'code', messages: { errorStringGeneric: - "Array type using '{{type}}[]' is forbidden. Use 'Array<{{type}}>' instead.", + "Array type using '{{type}}[]' is forbidden. Use '{{array}}<{{type}}>' instead.", errorStringGenericSimple: - "Array type using '{{type}}[]' is forbidden for non-simple types. Use 'Array<{{type}}>' instead.", + "Array type using '{{type}}[]' is forbidden for non-simple types. Use '{{array}}<{{type}}>' instead.", errorStringArray: - "Array type using 'Array<{{type}}>' is forbidden. Use '{{type}}[]' instead.", + "Array type using '{{array}}<{{type}}>' is forbidden. Use '{{type}}[]' instead.", errorStringArraySimple: - "Array type using 'Array<{{type}}>' is forbidden for simple types. Use '{{type}}[]' instead.", + "Array type using '{{array}}<{{type}}>' is forbidden for simple types. Use '{{type}}[]' instead.", }, schema: [ { @@ -159,16 +159,17 @@ export default util.createRule({ : 'errorStringGenericSimple'; const errorNode = isReadonly ? node.parent! : node; + const arrayType = isReadonly ? 'ReadonlyArray' : 'Array'; + context.report({ node: errorNode, messageId, data: { + array: arrayType, type: getMessageType(node.elementType), }, fix(fixer) { const typeNode = node.elementType; - const arrayType = isReadonly ? 'ReadonlyArray' : 'Array'; - return [ fixer.replaceTextRange( [errorNode.range[0], typeNode.range[0]], @@ -217,6 +218,7 @@ export default util.createRule({ messageId, data: { type: 'any', + array: node.typeName.name, }, fix(fixer) { return fixer.replaceText(node, `${readonlyPrefix}any[]`); @@ -251,6 +253,7 @@ export default util.createRule({ messageId, data: { type: getMessageType(type), + array: node.typeName.name, }, fix(fixer) { return [ diff --git a/packages/eslint-plugin/tests/rules/array-type.test.ts b/packages/eslint-plugin/tests/rules/array-type.test.ts index 3c2c1893a3f..6e88f77cdee 100644 --- a/packages/eslint-plugin/tests/rules/array-type.test.ts +++ b/packages/eslint-plugin/tests/rules/array-type.test.ts @@ -375,7 +375,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'number' }, + data: { type: 'number', array: 'Array' }, line: 1, column: 8, }, @@ -388,7 +388,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 8, }, @@ -401,7 +401,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'number' }, + data: { type: 'number', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -414,7 +414,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T' }, + data: { type: 'T', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -427,7 +427,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'number' }, + data: { type: 'number', array: 'Array' }, line: 1, column: 8, }, @@ -440,7 +440,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 8, }, @@ -453,7 +453,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'number' }, + data: { type: 'number', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -466,7 +466,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T' }, + data: { type: 'T', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -479,7 +479,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'number' }, + data: { type: 'number', array: 'Array' }, line: 1, column: 8, }, @@ -492,7 +492,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 8, }, @@ -505,7 +505,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'number' }, + data: { type: 'number', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -518,7 +518,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T' }, + data: { type: 'T', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -531,7 +531,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'number' }, + data: { type: 'number', array: 'Array' }, line: 1, column: 8, }, @@ -544,7 +544,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 8, }, @@ -557,7 +557,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'number' }, + data: { type: 'number', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -570,7 +570,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T' }, + data: { type: 'T', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -583,7 +583,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'number' }, + data: { type: 'number', array: 'Array' }, line: 1, column: 8, }, @@ -596,7 +596,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 8, }, @@ -609,7 +609,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'number' }, + data: { type: 'number', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -622,7 +622,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T' }, + data: { type: 'T', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -635,7 +635,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'number' }, + data: { type: 'number', array: 'Array' }, line: 1, column: 8, }, @@ -648,7 +648,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 8, }, @@ -661,7 +661,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'number' }, + data: { type: 'number', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -674,7 +674,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T' }, + data: { type: 'T', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -687,7 +687,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'number' }, + data: { type: 'number', array: 'Array' }, line: 1, column: 8, }, @@ -700,7 +700,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 8, }, @@ -713,7 +713,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'number' }, + data: { type: 'number', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -726,7 +726,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T' }, + data: { type: 'T', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -739,7 +739,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'number' }, + data: { type: 'number', array: 'Array' }, line: 1, column: 8, }, @@ -752,7 +752,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 8, }, @@ -765,7 +765,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'number' }, + data: { type: 'number', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -778,7 +778,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T' }, + data: { type: 'T', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -791,7 +791,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'number' }, + data: { type: 'number', array: 'Array' }, line: 1, column: 8, }, @@ -804,7 +804,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 8, }, @@ -817,7 +817,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'number' }, + data: { type: 'number', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -830,7 +830,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T' }, + data: { type: 'T', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -843,7 +843,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'number' }, + data: { type: 'number', array: 'Array' }, line: 1, column: 8, }, @@ -856,7 +856,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 8, }, @@ -869,7 +869,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'number' }, + data: { type: 'number', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -882,7 +882,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T' }, + data: { type: 'T', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -895,7 +895,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'number' }, + data: { type: 'number', array: 'Array' }, line: 1, column: 8, }, @@ -908,7 +908,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 8, }, @@ -921,7 +921,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'number' }, + data: { type: 'number', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -934,7 +934,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T' }, + data: { type: 'T', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -947,7 +947,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'number' }, + data: { type: 'number', array: 'Array' }, line: 1, column: 8, }, @@ -960,7 +960,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 8, }, @@ -973,7 +973,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'number' }, + data: { type: 'number', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -986,7 +986,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T' }, + data: { type: 'T', array: 'ReadonlyArray' }, line: 1, column: 8, }, @@ -1001,7 +1001,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'Bar' }, + data: { type: 'Bar', array: 'Array' }, line: 1, column: 15, }, @@ -1014,7 +1014,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'Bar' }, + data: { type: 'Bar', array: 'Array' }, line: 1, column: 21, }, @@ -1027,7 +1027,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'Bar' }, + data: { type: 'Bar', array: 'Array' }, line: 1, column: 27, }, @@ -1040,13 +1040,13 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'Bar' }, + data: { type: 'Bar', array: 'Array' }, line: 1, column: 17, }, { messageId: 'errorStringArray', - data: { type: 'Bar' }, + data: { type: 'Bar', array: 'Array' }, line: 1, column: 30, }, @@ -1059,7 +1059,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'undefined' }, + data: { type: 'undefined', array: 'Array' }, line: 1, column: 8, }, @@ -1072,7 +1072,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'string' }, + data: { type: 'string', array: 'Array' }, line: 1, column: 20, }, @@ -1085,7 +1085,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'any' }, + data: { type: 'any', array: 'Array' }, line: 1, column: 8, }, @@ -1098,7 +1098,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 24, }, @@ -1111,7 +1111,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 15, }, @@ -1130,7 +1130,7 @@ let yyyy: Arr>>> = [[[['2']]]]; errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 3, column: 15, }, @@ -1157,7 +1157,7 @@ interface ArrayClass { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 3, column: 8, }, @@ -1178,7 +1178,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 2, column: 27, }, @@ -1191,7 +1191,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 13, }, @@ -1204,7 +1204,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 17, }, @@ -1217,7 +1217,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 24, }, @@ -1230,7 +1230,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'fooName.BarType' }, + data: { type: 'fooName.BarType', array: 'Array' }, line: 1, column: 8, }, @@ -1243,7 +1243,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 8, }, @@ -1256,7 +1256,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'undefined' }, + data: { type: 'undefined', array: 'Array' }, line: 1, column: 8, }, @@ -1269,7 +1269,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'string' }, + data: { type: 'string', array: 'Array' }, line: 1, column: 20, }, @@ -1282,7 +1282,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'any' }, + data: { type: 'any', array: 'Array' }, line: 1, column: 8, }, @@ -1295,7 +1295,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 15, }, @@ -1314,7 +1314,7 @@ let yyyy: Arr[][]> = [[[['2']]]]; errors: [ { messageId: 'errorStringArray', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 3, column: 15, }, @@ -1339,7 +1339,7 @@ interface ArrayClass { errors: [ { messageId: 'errorStringArray', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 3, column: 8, }, @@ -1360,7 +1360,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 2, column: 27, }, @@ -1373,7 +1373,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 13, }, @@ -1386,7 +1386,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 17, }, @@ -1399,7 +1399,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 24, }, @@ -1412,7 +1412,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringArray', - data: { type: 'any' }, + data: { type: 'any', array: 'Array' }, line: 1, column: 8, }, @@ -1425,7 +1425,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringArray', - data: { type: 'any' }, + data: { type: 'any', array: 'Array' }, line: 1, column: 8, }, @@ -1438,7 +1438,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'any' }, + data: { type: 'any', array: 'Array' }, line: 1, column: 8, }, @@ -1451,6 +1451,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringArraySimple', + data: { type: 'any', array: 'Array' }, line: 1, column: 8, }, @@ -1463,7 +1464,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'number' }, + data: { type: 'number', array: 'Array' }, line: 1, column: 31, }, @@ -1476,7 +1477,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'string' }, + data: { type: 'string', array: 'Array' }, line: 1, column: 8, }, @@ -1489,7 +1490,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 24, }, @@ -1508,7 +1509,7 @@ let yyyy: Arr>>> = [[[['2']]]]; errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 3, column: 15, }, @@ -1533,7 +1534,7 @@ interface ArrayClass { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 4, column: 8, }, @@ -1554,7 +1555,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 2, column: 27, }, @@ -1567,7 +1568,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 13, }, @@ -1580,7 +1581,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 17, }, @@ -1593,7 +1594,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 24, }, @@ -1614,7 +1615,7 @@ interface FooInterface { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'string' }, + data: { type: 'string', array: 'Array' }, line: 3, column: 18, }, @@ -1628,7 +1629,7 @@ interface FooInterface { errors: [ { messageId: 'errorStringArray', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 28, }, @@ -1642,7 +1643,7 @@ interface FooInterface { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T' }, + data: { type: 'T', array: 'Array' }, line: 1, column: 28, }, @@ -1655,7 +1656,7 @@ interface FooInterface { errors: [ { messageId: 'errorStringArray', - data: { type: 'object' }, + data: { type: 'object', array: 'ReadonlyArray' }, line: 1, column: 12, }, From ecf2cf26e3d97a34bedc7d3ba15a80fd1e0527bb Mon Sep 17 00:00:00 2001 From: Armano Date: Fri, 19 Nov 2021 21:02:28 +0100 Subject: [PATCH 2/3] fix: [array-type] correct error message for readonly part 2 --- .../eslint-plugin/src/rules/array-type.ts | 42 +++- .../tests/rules/array-type.test.ts | 234 +++++++++--------- 2 files changed, 147 insertions(+), 129 deletions(-) diff --git a/packages/eslint-plugin/src/rules/array-type.ts b/packages/eslint-plugin/src/rules/array-type.ts index f825c3a90df..37f1a14ca6a 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'] }; @@ -98,13 +102,21 @@ export default util.createRule({ fixable: 'code', messages: { errorStringGeneric: - "Array type using '{{type}}[]' is forbidden. Use '{{array}}<{{type}}>' instead.", + "Array type using '{{type}}[]' is forbidden. Use 'Array<{{type}}>' instead.", errorStringGenericSimple: - "Array type using '{{type}}[]' is forbidden for non-simple types. Use '{{array}}<{{type}}>' instead.", + "Array type using '{{type}}[]' is forbidden for non-simple types. Use 'Array<{{type}}>' instead.", errorStringArray: - "Array type using '{{array}}<{{type}}>' is forbidden. Use '{{type}}[]' instead.", + "Array type using 'Array<{{type}}>' is forbidden. Use '{{type}}[]' instead.", errorStringArraySimple: - "Array type using '{{array}}<{{type}}>' is forbidden for simple types. Use '{{type}}[]' instead.", + "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,20 +167,23 @@ export default util.createRule({ const messageId = currentOption === 'generic' - ? 'errorStringGeneric' + ? isReadonly + ? 'errorStringGenericReadonly' + : 'errorStringGeneric' + : isReadonly + ? 'errorStringGenericSimpleReadonly' : 'errorStringGenericSimple'; const errorNode = isReadonly ? node.parent! : node; - const arrayType = isReadonly ? 'ReadonlyArray' : 'Array'; - context.report({ node: errorNode, messageId, data: { - array: arrayType, type: getMessageType(node.elementType), }, fix(fixer) { + const arrayType = isReadonly ? 'ReadonlyArray' : 'Array'; + const typeNode = node.elementType; return [ fixer.replaceTextRange( @@ -206,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) { @@ -218,7 +238,6 @@ export default util.createRule({ messageId, data: { type: 'any', - array: node.typeName.name, }, fix(fixer) { return fixer.replaceText(node, `${readonlyPrefix}any[]`); @@ -253,7 +272,6 @@ export default util.createRule({ messageId, data: { type: getMessageType(type), - array: node.typeName.name, }, fix(fixer) { return [ diff --git a/packages/eslint-plugin/tests/rules/array-type.test.ts b/packages/eslint-plugin/tests/rules/array-type.test.ts index 6e88f77cdee..d658ffd4998 100644 --- a/packages/eslint-plugin/tests/rules/array-type.test.ts +++ b/packages/eslint-plugin/tests/rules/array-type.test.ts @@ -375,7 +375,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'number', array: 'Array' }, + data: { type: 'number' }, line: 1, column: 8, }, @@ -388,7 +388,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 8, }, @@ -400,8 +400,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array' }], errors: [ { - messageId: 'errorStringArray', - data: { type: 'number', array: 'ReadonlyArray' }, + messageId: 'errorStringArrayReadonly', + data: { type: 'number' }, line: 1, column: 8, }, @@ -413,8 +413,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array' }], errors: [ { - messageId: 'errorStringArray', - data: { type: 'T', array: 'ReadonlyArray' }, + messageId: 'errorStringArrayReadonly', + data: { type: 'T' }, line: 1, column: 8, }, @@ -427,7 +427,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'number', array: 'Array' }, + data: { type: 'number' }, line: 1, column: 8, }, @@ -440,7 +440,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 8, }, @@ -452,8 +452,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array', readonly: 'array' }], errors: [ { - messageId: 'errorStringArray', - data: { type: 'number', array: 'ReadonlyArray' }, + messageId: 'errorStringArrayReadonly', + data: { type: 'number' }, line: 1, column: 8, }, @@ -465,8 +465,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array', readonly: 'array' }], errors: [ { - messageId: 'errorStringArray', - data: { type: 'T', array: 'ReadonlyArray' }, + messageId: 'errorStringArrayReadonly', + data: { type: 'T' }, line: 1, column: 8, }, @@ -479,7 +479,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'number', array: 'Array' }, + data: { type: 'number' }, line: 1, column: 8, }, @@ -492,7 +492,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 8, }, @@ -504,8 +504,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array', readonly: 'array-simple' }], errors: [ { - messageId: 'errorStringArraySimple', - data: { type: 'number', array: 'ReadonlyArray' }, + messageId: 'errorStringArraySimpleReadonly', + data: { type: 'number' }, line: 1, column: 8, }, @@ -517,8 +517,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array', readonly: 'array-simple' }], errors: [ { - messageId: 'errorStringGenericSimple', - data: { type: 'T', array: 'ReadonlyArray' }, + messageId: 'errorStringGenericSimpleReadonly', + data: { type: 'T' }, line: 1, column: 8, }, @@ -531,7 +531,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'number', array: 'Array' }, + data: { type: 'number' }, line: 1, column: 8, }, @@ -544,7 +544,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 8, }, @@ -556,8 +556,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array', readonly: 'generic' }], errors: [ { - messageId: 'errorStringGeneric', - data: { type: 'number', array: 'ReadonlyArray' }, + messageId: 'errorStringGenericReadonly', + data: { type: 'number' }, line: 1, column: 8, }, @@ -569,8 +569,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array', readonly: 'generic' }], errors: [ { - messageId: 'errorStringGeneric', - data: { type: 'T', array: 'ReadonlyArray' }, + messageId: 'errorStringGenericReadonly', + data: { type: 'T' }, line: 1, column: 8, }, @@ -583,7 +583,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'number', array: 'Array' }, + data: { type: 'number' }, line: 1, column: 8, }, @@ -596,7 +596,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 8, }, @@ -608,8 +608,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array-simple' }], errors: [ { - messageId: 'errorStringArraySimple', - data: { type: 'number', array: 'ReadonlyArray' }, + messageId: 'errorStringArraySimpleReadonly', + data: { type: 'number' }, line: 1, column: 8, }, @@ -621,8 +621,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array-simple' }], errors: [ { - messageId: 'errorStringGenericSimple', - data: { type: 'T', array: 'ReadonlyArray' }, + messageId: 'errorStringGenericSimpleReadonly', + data: { type: 'T' }, line: 1, column: 8, }, @@ -635,7 +635,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'number', array: 'Array' }, + data: { type: 'number' }, line: 1, column: 8, }, @@ -648,7 +648,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 8, }, @@ -660,8 +660,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array-simple', readonly: 'array' }], errors: [ { - messageId: 'errorStringArray', - data: { type: 'number', array: 'ReadonlyArray' }, + messageId: 'errorStringArrayReadonly', + data: { type: 'number' }, line: 1, column: 8, }, @@ -673,8 +673,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array-simple', readonly: 'array' }], errors: [ { - messageId: 'errorStringArray', - data: { type: 'T', array: 'ReadonlyArray' }, + messageId: 'errorStringArrayReadonly', + data: { type: 'T' }, line: 1, column: 8, }, @@ -687,7 +687,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'number', array: 'Array' }, + data: { type: 'number' }, line: 1, column: 8, }, @@ -700,7 +700,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 8, }, @@ -712,8 +712,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array-simple', readonly: 'array-simple' }], errors: [ { - messageId: 'errorStringArraySimple', - data: { type: 'number', array: 'ReadonlyArray' }, + messageId: 'errorStringArraySimpleReadonly', + data: { type: 'number' }, line: 1, column: 8, }, @@ -725,8 +725,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array-simple', readonly: 'array-simple' }], errors: [ { - messageId: 'errorStringGenericSimple', - data: { type: 'T', array: 'ReadonlyArray' }, + messageId: 'errorStringGenericSimpleReadonly', + data: { type: 'T' }, line: 1, column: 8, }, @@ -739,7 +739,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'number', array: 'Array' }, + data: { type: 'number' }, line: 1, column: 8, }, @@ -752,7 +752,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 8, }, @@ -764,8 +764,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array-simple', readonly: 'generic' }], errors: [ { - messageId: 'errorStringGeneric', - data: { type: 'number', array: 'ReadonlyArray' }, + messageId: 'errorStringGenericReadonly', + data: { type: 'number' }, line: 1, column: 8, }, @@ -777,8 +777,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'array-simple', readonly: 'generic' }], errors: [ { - messageId: 'errorStringGeneric', - data: { type: 'T', array: 'ReadonlyArray' }, + messageId: 'errorStringGenericReadonly', + data: { type: 'T' }, line: 1, column: 8, }, @@ -791,7 +791,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'number', array: 'Array' }, + data: { type: 'number' }, line: 1, column: 8, }, @@ -804,7 +804,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 8, }, @@ -816,8 +816,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'generic' }], errors: [ { - messageId: 'errorStringGeneric', - data: { type: 'number', array: 'ReadonlyArray' }, + messageId: 'errorStringGenericReadonly', + data: { type: 'number' }, line: 1, column: 8, }, @@ -829,8 +829,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'generic' }], errors: [ { - messageId: 'errorStringGeneric', - data: { type: 'T', array: 'ReadonlyArray' }, + messageId: 'errorStringGenericReadonly', + data: { type: 'T' }, line: 1, column: 8, }, @@ -843,7 +843,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'number', array: 'Array' }, + data: { type: 'number' }, line: 1, column: 8, }, @@ -856,7 +856,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 8, }, @@ -868,8 +868,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'generic', readonly: 'array' }], errors: [ { - messageId: 'errorStringArray', - data: { type: 'number', array: 'ReadonlyArray' }, + messageId: 'errorStringArrayReadonly', + data: { type: 'number' }, line: 1, column: 8, }, @@ -881,8 +881,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'generic', readonly: 'array' }], errors: [ { - messageId: 'errorStringArray', - data: { type: 'T', array: 'ReadonlyArray' }, + messageId: 'errorStringArrayReadonly', + data: { type: 'T' }, line: 1, column: 8, }, @@ -895,7 +895,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'number', array: 'Array' }, + data: { type: 'number' }, line: 1, column: 8, }, @@ -908,7 +908,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 8, }, @@ -920,8 +920,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'generic', readonly: 'array-simple' }], errors: [ { - messageId: 'errorStringArraySimple', - data: { type: 'number', array: 'ReadonlyArray' }, + messageId: 'errorStringArraySimpleReadonly', + data: { type: 'number' }, line: 1, column: 8, }, @@ -933,8 +933,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'generic', readonly: 'array-simple' }], errors: [ { - messageId: 'errorStringGenericSimple', - data: { type: 'T', array: 'ReadonlyArray' }, + messageId: 'errorStringGenericSimpleReadonly', + data: { type: 'T' }, line: 1, column: 8, }, @@ -947,7 +947,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'number', array: 'Array' }, + data: { type: 'number' }, line: 1, column: 8, }, @@ -960,7 +960,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 8, }, @@ -972,8 +972,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'generic', readonly: 'generic' }], errors: [ { - messageId: 'errorStringGeneric', - data: { type: 'number', array: 'ReadonlyArray' }, + messageId: 'errorStringGenericReadonly', + data: { type: 'number' }, line: 1, column: 8, }, @@ -985,8 +985,8 @@ function bazFunction(baz: Arr>) { options: [{ default: 'generic', readonly: 'generic' }], errors: [ { - messageId: 'errorStringGeneric', - data: { type: 'T', array: 'ReadonlyArray' }, + messageId: 'errorStringGenericReadonly', + data: { type: 'T' }, line: 1, column: 8, }, @@ -1001,7 +1001,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'Bar', array: 'Array' }, + data: { type: 'Bar' }, line: 1, column: 15, }, @@ -1014,7 +1014,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'Bar', array: 'Array' }, + data: { type: 'Bar' }, line: 1, column: 21, }, @@ -1027,7 +1027,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'Bar', array: 'Array' }, + data: { type: 'Bar' }, line: 1, column: 27, }, @@ -1040,13 +1040,13 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'Bar', array: 'Array' }, + data: { type: 'Bar' }, line: 1, column: 17, }, { messageId: 'errorStringArray', - data: { type: 'Bar', array: 'Array' }, + data: { type: 'Bar' }, line: 1, column: 30, }, @@ -1059,7 +1059,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'undefined', array: 'Array' }, + data: { type: 'undefined' }, line: 1, column: 8, }, @@ -1072,7 +1072,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'string', array: 'Array' }, + data: { type: 'string' }, line: 1, column: 20, }, @@ -1085,7 +1085,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'any', array: 'Array' }, + data: { type: 'any' }, line: 1, column: 8, }, @@ -1098,7 +1098,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 24, }, @@ -1111,7 +1111,7 @@ function bazFunction(baz: Arr>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 15, }, @@ -1130,7 +1130,7 @@ let yyyy: Arr>>> = [[[['2']]]]; errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 3, column: 15, }, @@ -1157,7 +1157,7 @@ interface ArrayClass { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 3, column: 8, }, @@ -1178,7 +1178,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 2, column: 27, }, @@ -1191,7 +1191,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 13, }, @@ -1204,7 +1204,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 17, }, @@ -1217,7 +1217,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 24, }, @@ -1243,7 +1243,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringGenericSimple', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 8, }, @@ -1256,7 +1256,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'undefined', array: 'Array' }, + data: { type: 'undefined' }, line: 1, column: 8, }, @@ -1269,7 +1269,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'string', array: 'Array' }, + data: { type: 'string' }, line: 1, column: 20, }, @@ -1282,7 +1282,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'any', array: 'Array' }, + data: { type: 'any' }, line: 1, column: 8, }, @@ -1295,7 +1295,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 15, }, @@ -1314,7 +1314,7 @@ let yyyy: Arr[][]> = [[[['2']]]]; errors: [ { messageId: 'errorStringArray', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 3, column: 15, }, @@ -1339,7 +1339,7 @@ interface ArrayClass { errors: [ { messageId: 'errorStringArray', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 3, column: 8, }, @@ -1360,7 +1360,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 2, column: 27, }, @@ -1373,7 +1373,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 13, }, @@ -1386,7 +1386,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 17, }, @@ -1399,7 +1399,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringArray', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 24, }, @@ -1412,7 +1412,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringArray', - data: { type: 'any', array: 'Array' }, + data: { type: 'any' }, line: 1, column: 8, }, @@ -1425,7 +1425,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringArray', - data: { type: 'any', array: 'Array' }, + data: { type: 'any' }, line: 1, column: 8, }, @@ -1438,7 +1438,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'any', array: 'Array' }, + data: { type: 'any' }, line: 1, column: 8, }, @@ -1451,7 +1451,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'any', array: 'Array' }, + data: { type: 'any' }, line: 1, column: 8, }, @@ -1464,7 +1464,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'number', array: 'Array' }, + data: { type: 'number' }, line: 1, column: 31, }, @@ -1477,7 +1477,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'string', array: 'Array' }, + data: { type: 'string' }, line: 1, column: 8, }, @@ -1490,7 +1490,7 @@ function fooFunction(foo: ArrayClass[]) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 24, }, @@ -1509,7 +1509,7 @@ let yyyy: Arr>>> = [[[['2']]]]; errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 3, column: 15, }, @@ -1534,7 +1534,7 @@ interface ArrayClass { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 4, column: 8, }, @@ -1555,7 +1555,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 2, column: 27, }, @@ -1568,7 +1568,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 13, }, @@ -1581,7 +1581,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 17, }, @@ -1594,7 +1594,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 24, }, @@ -1615,7 +1615,7 @@ interface FooInterface { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'string', array: 'Array' }, + data: { type: 'string' }, line: 3, column: 18, }, @@ -1629,7 +1629,7 @@ interface FooInterface { errors: [ { messageId: 'errorStringArray', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 28, }, @@ -1643,7 +1643,7 @@ interface FooInterface { errors: [ { messageId: 'errorStringGeneric', - data: { type: 'T', array: 'Array' }, + data: { type: 'T' }, line: 1, column: 28, }, @@ -1655,8 +1655,8 @@ interface FooInterface { options: [{ default: 'array' }], errors: [ { - messageId: 'errorStringArray', - data: { type: 'object', array: 'ReadonlyArray' }, + messageId: 'errorStringArrayReadonly', + data: { type: 'object' }, line: 1, column: 12, }, From b67e62682f93f3c19b99034b942a248c04d7cd64 Mon Sep 17 00:00:00 2001 From: Armano Date: Fri, 19 Nov 2021 21:04:24 +0100 Subject: [PATCH 3/3] fix: [array-type] revert unnecessary changes --- packages/eslint-plugin/src/rules/array-type.ts | 2 +- packages/eslint-plugin/tests/rules/array-type.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/array-type.ts b/packages/eslint-plugin/src/rules/array-type.ts index 37f1a14ca6a..998e4b7d5aa 100644 --- a/packages/eslint-plugin/src/rules/array-type.ts +++ b/packages/eslint-plugin/src/rules/array-type.ts @@ -182,9 +182,9 @@ export default util.createRule({ type: getMessageType(node.elementType), }, fix(fixer) { + const typeNode = node.elementType; const arrayType = isReadonly ? 'ReadonlyArray' : 'Array'; - const typeNode = node.elementType; return [ fixer.replaceTextRange( [errorNode.range[0], typeNode.range[0]], diff --git a/packages/eslint-plugin/tests/rules/array-type.test.ts b/packages/eslint-plugin/tests/rules/array-type.test.ts index d658ffd4998..c19c945c4e3 100644 --- a/packages/eslint-plugin/tests/rules/array-type.test.ts +++ b/packages/eslint-plugin/tests/rules/array-type.test.ts @@ -1230,7 +1230,7 @@ function barFunction(bar: Array>) { errors: [ { messageId: 'errorStringArraySimple', - data: { type: 'fooName.BarType', array: 'Array' }, + data: { type: 'fooName.BarType' }, line: 1, column: 8, },