Skip to content

Commit

Permalink
feat(eslint-plugin): array-type distinguish whether readonly or not (
Browse files Browse the repository at this point in the history
…#4066)

* feat(eslint-plugin): distinguish whether readonly or not

Signed-off-by: koooge <koooooge@gmail.com>

* refactor: template messages

Signed-off-by: koooge <koooooge@gmail.com>
  • Loading branch information
koooge committed Nov 23, 2021
1 parent 3c89e42 commit 314af44
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 99 deletions.
20 changes: 13 additions & 7 deletions packages/eslint-plugin/src/rules/array-type.ts
Expand Up @@ -80,9 +80,9 @@ type Options = [
];
type MessageIds =
| 'errorStringGeneric'
| 'errorStringGenericSimple'
| 'errorStringArray'
| 'errorStringArraySimple';
| 'errorStringArraySimple'
| 'errorStringGenericSimple';

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

Expand All @@ -98,13 +98,13 @@ export default util.createRule<Options, MessageIds>({
fixable: 'code',
messages: {
errorStringGeneric:
"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 '{{readonlyPrefix}}{{type}}[]' is forbidden. Use '{{className}}<{{type}}>' instead.",
errorStringArray:
"Array type using 'Array<{{type}}>' is forbidden. Use '{{type}}[]' instead.",
"Array type using '{{className}}<{{type}}>' is forbidden. Use '{{readonlyPrefix}}{{type}}[]' instead.",
errorStringArraySimple:
"Array type using 'Array<{{type}}>' is forbidden for simple types. Use '{{type}}[]' instead.",
"Array type using '{{className}}<{{type}}>' is forbidden for simple types. Use '{{readonlyPrefix}}{{type}}[]' instead.",
errorStringGenericSimple:
"Array type using '{{readonlyPrefix}}{{type}}[]' is forbidden for non-simple types. Use '{{className}}<{{type}}>' instead.",
},
schema: [
{
Expand Down Expand Up @@ -163,6 +163,8 @@ export default util.createRule<Options, MessageIds>({
node: errorNode,
messageId,
data: {
className: isReadonly ? 'ReadonlyArray' : 'Array',
readonlyPrefix: isReadonly ? 'readonly ' : '',
type: getMessageType(node.elementType),
},
fix(fixer) {
Expand Down Expand Up @@ -216,6 +218,8 @@ export default util.createRule<Options, MessageIds>({
node,
messageId,
data: {
className: isReadonlyArrayType ? 'ReadonlyArray' : 'Array',
readonlyPrefix,
type: 'any',
},
fix(fixer) {
Expand Down Expand Up @@ -250,6 +254,8 @@ export default util.createRule<Options, MessageIds>({
node,
messageId,
data: {
className: isReadonlyArrayType ? 'ReadonlyArray' : 'Array',
readonlyPrefix,
type: getMessageType(type),
},
fix(fixer) {
Expand Down

0 comments on commit 314af44

Please sign in to comment.