Skip to content

Commit b95dd31

Browse files
committed
feat: Improve error messages
1 parent 687f82b commit b95dd31

File tree

5 files changed

+109
-43
lines changed

5 files changed

+109
-43
lines changed

src/rules/arrayStyle/index.js

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,71 @@ const schema = [
88
}
99
];
1010

11-
const fixShorthand = (context, node) => {
11+
const fixShorthand = (node, type) => {
1212
return (fixer) => {
13-
const rawElementType = context.getSourceCode().getText(node.elementType);
14-
15-
return fixer.replaceText(node, 'Array<' + rawElementType + '>');
13+
return fixer.replaceText(node, 'Array<' + type + '>');
1614
};
1715
};
1816

19-
const fixVerbose = (context, node) => {
17+
const fixVerbose = (node, type, elementTypeNode) => {
2018
return (fixer) => {
21-
const elementTypeNode = node.typeParameters.params[0];
22-
const rawElementType = context.getSourceCode().getText(elementTypeNode);
23-
2419
if (needWrap(elementTypeNode)) {
25-
return fixer.replaceText(node, '(' + rawElementType + ')[]');
20+
return fixer.replaceText(node, '(' + type + ')[]');
2621
} else {
27-
return fixer.replaceText(node, rawElementType + '[]');
22+
return fixer.replaceText(node, type + '[]');
2823
}
2924
};
3025
};
3126

27+
const inlineType = (type) => {
28+
const inlined = type.replace(/\s+/g, ' ');
29+
30+
if (inlined.length <= 50) {
31+
return inlined;
32+
} else {
33+
return 'Type';
34+
}
35+
};
36+
3237
export default (defaultConfig, shorthandHandler, verboseHandler) => {
3338
const create = (context) => {
3439
const verbose = (context.options[0] || defaultConfig) === 'verbose';
3540

3641
return {
3742
// shorthand
3843
ArrayTypeAnnotation (node) {
39-
shorthandHandler(isSimpleType(node.elementType), verbose, context, node, fixShorthand(context, node));
44+
const rawElementType = context.getSourceCode().getText(node.elementType);
45+
const inlinedType = inlineType(rawElementType);
46+
const wrappedInlinedType = needWrap(node.elementType) ? '(' + inlinedType + ')' : inlinedType;
47+
48+
shorthandHandler(
49+
isSimpleType(node.elementType),
50+
verbose,
51+
context,
52+
node,
53+
fixShorthand(node, rawElementType),
54+
inlinedType,
55+
wrappedInlinedType
56+
);
4057
},
4158
// verbose
4259
GenericTypeAnnotation (node) {
4360
if (node.id.name === 'Array') {
4461
if (node.typeParameters.params.length === 1) {
45-
verboseHandler(isSimpleType(node.typeParameters.params[0]), verbose, context, node, fixVerbose(context, node));
62+
const elementTypeNode = node.typeParameters.params[0];
63+
const rawElementType = context.getSourceCode().getText(elementTypeNode);
64+
const inlinedType = inlineType(rawElementType);
65+
const wrappedInlinedType = needWrap(elementTypeNode) ? '(' + inlinedType + ')' : inlinedType;
66+
67+
verboseHandler(
68+
isSimpleType(elementTypeNode),
69+
verbose,
70+
context,
71+
node,
72+
fixVerbose(node, rawElementType, elementTypeNode),
73+
inlinedType,
74+
wrappedInlinedType
75+
);
4676
}
4777
}
4878
}

src/rules/arrayStyleComplexType.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
import makeArrayStyleRule from './arrayStyle';
22

3-
const shorthandHandler = (isSimpleType, verbose, context, node, fix) => {
3+
const shorthandHandler = (isSimpleType, verbose, context, node, fix, inlinedType, wrappedInlinedType) => {
44
if (!isSimpleType && verbose) {
55
context.report({
6+
data: {
7+
type: inlinedType,
8+
wrappedType: wrappedInlinedType
9+
},
610
fix,
7-
message: 'Use "Array<ComplexType>", not "ComplexType[]"',
11+
message: 'Use "Array<{{ type }}>", not "{{ wrappedType }}[]"',
812
node
913
});
1014
}
1115
};
1216

13-
const verboseHandler = (isSimpleType, verbose, context, node, fix) => {
17+
const verboseHandler = (isSimpleType, verbose, context, node, fix, inlinedType, wrappedInlinedType) => {
1418
if (!isSimpleType && !verbose) {
1519
context.report({
20+
data: {
21+
type: inlinedType,
22+
wrappedType: wrappedInlinedType
23+
},
1624
fix,
17-
message: 'Use "ComplexType[]", not "Array<ComplexType>"',
25+
message: 'Use "{{ wrappedType }}[]", not "Array<{{ type }}>"',
1826
node
1927
});
2028
}

src/rules/arrayStyleSimpleType.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
import makeArrayStyleRule from './arrayStyle';
22

3-
const shorthandHandler = (isSimpleType, verbose, context, node, fix) => {
3+
const shorthandHandler = (isSimpleType, verbose, context, node, fix, inlinedType, wrappedInlinedType) => {
44
if (isSimpleType && verbose) {
55
context.report({
6+
data: {
7+
type: inlinedType,
8+
wrappedType: wrappedInlinedType
9+
},
610
fix,
7-
message: 'Use "Array<SimpleType>", not "SimpleType[]"',
11+
message: 'Use "Array<{{ type }}>", not "{{ wrappedType }}[]"',
812
node
913
});
1014
}
1115
};
1216

13-
const verboseHandler = (isSimpleType, verbose, context, node, fix) => {
17+
const verboseHandler = (isSimpleType, verbose, context, node, fix, inlinedType, wrappedInlinedType) => {
1418
if (isSimpleType && !verbose) {
1519
context.report({
20+
data: {
21+
type: inlinedType,
22+
wrappedType: wrappedInlinedType
23+
},
1624
fix,
17-
message: 'Use "SimpleType[]", not "Array<SimpleType>"',
25+
message: 'Use "{{ wrappedType }}[]", not "Array<{{ type }}>"',
1826
node
1927
});
2028
}

tests/rules/assertions/arrayStyleComplexType.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,61 @@ export default {
22
invalid: [
33
{
44
code: 'type X = (?string)[]',
5-
errors: [{message: 'Use "Array<ComplexType>", not "ComplexType[]"'}],
5+
errors: [{message: 'Use "Array<?string>", not "(?string)[]"'}],
66
output: 'type X = Array<?string>'
77
},
88
{
99
code: 'type X = (?string)[]',
10-
errors: [{message: 'Use "Array<ComplexType>", not "ComplexType[]"'}],
10+
errors: [{message: 'Use "Array<?string>", not "(?string)[]"'}],
1111
options: ['verbose'],
1212
output: 'type X = Array<?string>'
1313
},
1414
{
1515
code: 'type X = Array<?string>',
16-
errors: [{message: 'Use "ComplexType[]", not "Array<ComplexType>"'}],
16+
errors: [{message: 'Use "(?string)[]", not "Array<?string>"'}],
1717
options: ['shorthand'],
1818
output: 'type X = (?string)[]'
1919
},
2020
{
2121
code: 'type X = Array<{foo: string}>',
22-
errors: [{message: 'Use "ComplexType[]", not "Array<ComplexType>"'}],
22+
errors: [{message: 'Use "{foo: string}[]", not "Array<{foo: string}>"'}],
2323
options: ['shorthand'],
2424
output: 'type X = {foo: string}[]'
2525
},
2626
{
2727
code: 'type X = (string | number)[]',
28-
errors: [{message: 'Use "Array<ComplexType>", not "ComplexType[]"'}],
28+
errors: [{message: 'Use "Array<string | number>", not "(string | number)[]"'}],
2929
output: 'type X = Array<string | number>'
3030
},
3131
{
3232
code: 'type X = (string & number)[]',
33-
errors: [{message: 'Use "Array<ComplexType>", not "ComplexType[]"'}],
33+
errors: [{message: 'Use "Array<string & number>", not "(string & number)[]"'}],
3434
output: 'type X = Array<string & number>'
3535
},
3636
{
3737
code: 'type X = [string, number][]',
38-
errors: [{message: 'Use "Array<ComplexType>", not "ComplexType[]"'}],
38+
errors: [{message: 'Use "Array<[string, number]>", not "[string, number][]"'}],
3939
output: 'type X = Array<[string, number]>'
4040
},
4141
{
4242
code: 'type X = {foo: string}[]',
43-
errors: [{message: 'Use "Array<ComplexType>", not "ComplexType[]"'}],
43+
errors: [{message: 'Use "Array<{foo: string}>", not "{foo: string}[]"'}],
4444
output: 'type X = Array<{foo: string}>'
4545
},
4646
{
4747
code: 'type X = (string => number)[]',
48-
errors: [{message: 'Use "Array<ComplexType>", not "ComplexType[]"'}],
48+
errors: [{message: 'Use "Array<string => number>", not "(string => number)[]"'}],
4949
output: 'type X = Array<string => number>'
50+
},
51+
{
52+
code: 'type X = {\n foo: string,\n bar: number\n}[]',
53+
errors: [{message: 'Use "Array<{ foo: string, bar: number }>", not "{ foo: string, bar: number }[]"'}],
54+
output: 'type X = Array<{\n foo: string,\n bar: number\n}>'
55+
},
56+
{
57+
code: 'type X = {\n foo: string,\n bar: number,\n quo: boolean,\n hey: Date\n}[]',
58+
errors: [{message: 'Use "Array<Type>", not "Type[]"'}],
59+
output: 'type X = Array<{\n foo: string,\n bar: number,\n quo: boolean,\n hey: Date\n}>'
5060
}
5161
],
5262
misconfigured: [

tests/rules/assertions/arrayStyleSimpleType.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,72 @@ export default {
22
invalid: [
33
{
44
code: 'type X = string[]',
5-
errors: [{message: 'Use "Array<SimpleType>", not "SimpleType[]"'}],
5+
errors: [{message: 'Use "Array<string>", not "string[]"'}],
66
output: 'type X = Array<string>'
77
},
88
{
99
code: 'type X = string[]',
10-
errors: [{message: 'Use "Array<SimpleType>", not "SimpleType[]"'}],
10+
errors: [{message: 'Use "Array<string>", not "string[]"'}],
1111
options: ['verbose'],
1212
output: 'type X = Array<string>'
1313
},
1414
{
1515
code: 'type X = Array<string>',
16-
errors: [{message: 'Use "SimpleType[]", not "Array<SimpleType>"'}],
16+
errors: [{message: 'Use "string[]", not "Array<string>"'}],
1717
options: ['shorthand'],
1818
output: 'type X = string[]'
1919
},
2020
{
2121
code: 'type X = Date[]',
22-
errors: [{message: 'Use "Array<SimpleType>", not "SimpleType[]"'}],
22+
errors: [{message: 'Use "Array<Date>", not "Date[]"'}],
2323
output: 'type X = Array<Date>'
2424
},
2525
{
2626
code: 'type X = Promise<string>[]',
27-
errors: [{message: 'Use "Array<SimpleType>", not "SimpleType[]"'}],
27+
errors: [{message: 'Use "Array<Promise<string>>", not "Promise<string>[]"'}],
2828
output: 'type X = Array<Promise<string>>'
2929
},
3030
{
31-
code: 'type X = $Keys<{ foo: string }>[]',
32-
errors: [{message: 'Use "Array<SimpleType>", not "SimpleType[]"'}],
33-
output: 'type X = Array<$Keys<{ foo: string }>>'
31+
code: 'type X = $Keys<{foo: string}>[]',
32+
errors: [{message: 'Use "Array<$Keys<{foo: string}>>", not "$Keys<{foo: string}>[]"'}],
33+
output: 'type X = Array<$Keys<{foo: string}>>'
3434
},
3535
{
3636
code: 'type X = any[]',
37-
errors: [{message: 'Use "Array<SimpleType>", not "SimpleType[]"'}],
37+
errors: [{message: 'Use "Array<any>", not "any[]"'}],
3838
output: 'type X = Array<any>'
3939
},
4040
{
4141
code: 'type X = mixed[]',
42-
errors: [{message: 'Use "Array<SimpleType>", not "SimpleType[]"'}],
42+
errors: [{message: 'Use "Array<mixed>", not "mixed[]"'}],
4343
output: 'type X = Array<mixed>'
4444
},
4545
{
4646
code: 'type X = void[]',
47-
errors: [{message: 'Use "Array<SimpleType>", not "SimpleType[]"'}],
47+
errors: [{message: 'Use "Array<void>", not "void[]"'}],
4848
output: 'type X = Array<void>'
4949
},
5050
{
5151
code: 'type X = null[]',
52-
errors: [{message: 'Use "Array<SimpleType>", not "SimpleType[]"'}],
52+
errors: [{message: 'Use "Array<null>", not "null[]"'}],
5353
output: 'type X = Array<null>'
5454
},
5555
{
5656
code: 'type X = string[][]',
5757
errors: [
58-
{message: 'Use "Array<SimpleType>", not "SimpleType[]"'},
59-
{message: 'Use "Array<SimpleType>", not "SimpleType[]"'}
58+
{message: 'Use "Array<string[]>", not "string[][]"'},
59+
{message: 'Use "Array<string>", not "string[]"'}
6060
]
61+
},
62+
{
63+
code: 'type X = Promise<{\n foo: string,\n bar: number\n}>[]',
64+
errors: [{message: 'Use "Array<Promise<{ foo: string, bar: number }>>", not "Promise<{ foo: string, bar: number }>[]"'}],
65+
output: 'type X = Array<Promise<{\n foo: string,\n bar: number\n}>>'
66+
},
67+
{
68+
code: 'type X = Promise<{\n foo: string,\n bar: number,\n quo: boolean\n}>[]',
69+
errors: [{message: 'Use "Array<Type>", not "Type[]"'}],
70+
output: 'type X = Array<Promise<{\n foo: string,\n bar: number,\n quo: boolean\n}>>'
6171
}
6272
],
6373
misconfigured: [

0 commit comments

Comments
 (0)