diff --git a/src/rules/arrayStyle/index.js b/src/rules/arrayStyle/index.js index 974eef48..15895765 100644 --- a/src/rules/arrayStyle/index.js +++ b/src/rules/arrayStyle/index.js @@ -8,27 +8,32 @@ const schema = [ } ]; -const fixShorthand = (context, node) => { +const fixShorthand = (node, type) => { return (fixer) => { - const rawElementType = context.getSourceCode().getText(node.elementType); - - return fixer.replaceText(node, 'Array<' + rawElementType + '>'); + return fixer.replaceText(node, 'Array<' + type + '>'); }; }; -const fixVerbose = (context, node) => { +const fixVerbose = (node, type, elementTypeNode) => { return (fixer) => { - const elementTypeNode = node.typeParameters.params[0]; - const rawElementType = context.getSourceCode().getText(elementTypeNode); - if (needWrap(elementTypeNode)) { - return fixer.replaceText(node, '(' + rawElementType + ')[]'); + return fixer.replaceText(node, '(' + type + ')[]'); } else { - return fixer.replaceText(node, rawElementType + '[]'); + return fixer.replaceText(node, type + '[]'); } }; }; +const inlineType = (type) => { + const inlined = type.replace(/\s+/g, ' '); + + if (inlined.length <= 50) { + return inlined; + } else { + return 'Type'; + } +}; + export default (defaultConfig, shorthandHandler, verboseHandler) => { const create = (context) => { const verbose = (context.options[0] || defaultConfig) === 'verbose'; @@ -36,13 +41,38 @@ export default (defaultConfig, shorthandHandler, verboseHandler) => { return { // shorthand ArrayTypeAnnotation (node) { - shorthandHandler(isSimpleType(node.elementType), verbose, context, node, fixShorthand(context, node)); + const rawElementType = context.getSourceCode().getText(node.elementType); + const inlinedType = inlineType(rawElementType); + const wrappedInlinedType = needWrap(node.elementType) ? '(' + inlinedType + ')' : inlinedType; + + shorthandHandler( + isSimpleType(node.elementType), + verbose, + context, + node, + fixShorthand(node, rawElementType), + inlinedType, + wrappedInlinedType + ); }, // verbose GenericTypeAnnotation (node) { if (node.id.name === 'Array') { if (node.typeParameters.params.length === 1) { - verboseHandler(isSimpleType(node.typeParameters.params[0]), verbose, context, node, fixVerbose(context, node)); + const elementTypeNode = node.typeParameters.params[0]; + const rawElementType = context.getSourceCode().getText(elementTypeNode); + const inlinedType = inlineType(rawElementType); + const wrappedInlinedType = needWrap(elementTypeNode) ? '(' + inlinedType + ')' : inlinedType; + + verboseHandler( + isSimpleType(elementTypeNode), + verbose, + context, + node, + fixVerbose(node, rawElementType, elementTypeNode), + inlinedType, + wrappedInlinedType + ); } } } diff --git a/src/rules/arrayStyleComplexType.js b/src/rules/arrayStyleComplexType.js index 0d67e5aa..998201b3 100644 --- a/src/rules/arrayStyleComplexType.js +++ b/src/rules/arrayStyleComplexType.js @@ -1,20 +1,28 @@ import makeArrayStyleRule from './arrayStyle'; -const shorthandHandler = (isSimpleType, verbose, context, node, fix) => { +const shorthandHandler = (isSimpleType, verbose, context, node, fix, inlinedType, wrappedInlinedType) => { if (!isSimpleType && verbose) { context.report({ + data: { + type: inlinedType, + wrappedType: wrappedInlinedType + }, fix, - message: 'Use "Array", not "ComplexType[]"', + message: 'Use "Array<{{ type }}>", not "{{ wrappedType }}[]"', node }); } }; -const verboseHandler = (isSimpleType, verbose, context, node, fix) => { +const verboseHandler = (isSimpleType, verbose, context, node, fix, inlinedType, wrappedInlinedType) => { if (!isSimpleType && !verbose) { context.report({ + data: { + type: inlinedType, + wrappedType: wrappedInlinedType + }, fix, - message: 'Use "ComplexType[]", not "Array"', + message: 'Use "{{ wrappedType }}[]", not "Array<{{ type }}>"', node }); } diff --git a/src/rules/arrayStyleSimpleType.js b/src/rules/arrayStyleSimpleType.js index 46904e9e..8cae4aa9 100644 --- a/src/rules/arrayStyleSimpleType.js +++ b/src/rules/arrayStyleSimpleType.js @@ -1,20 +1,28 @@ import makeArrayStyleRule from './arrayStyle'; -const shorthandHandler = (isSimpleType, verbose, context, node, fix) => { +const shorthandHandler = (isSimpleType, verbose, context, node, fix, inlinedType, wrappedInlinedType) => { if (isSimpleType && verbose) { context.report({ + data: { + type: inlinedType, + wrappedType: wrappedInlinedType + }, fix, - message: 'Use "Array", not "SimpleType[]"', + message: 'Use "Array<{{ type }}>", not "{{ wrappedType }}[]"', node }); } }; -const verboseHandler = (isSimpleType, verbose, context, node, fix) => { +const verboseHandler = (isSimpleType, verbose, context, node, fix, inlinedType, wrappedInlinedType) => { if (isSimpleType && !verbose) { context.report({ + data: { + type: inlinedType, + wrappedType: wrappedInlinedType + }, fix, - message: 'Use "SimpleType[]", not "Array"', + message: 'Use "{{ wrappedType }}[]", not "Array<{{ type }}>"', node }); } diff --git a/tests/rules/assertions/arrayStyleComplexType.js b/tests/rules/assertions/arrayStyleComplexType.js index 6347ca50..7e2f4617 100644 --- a/tests/rules/assertions/arrayStyleComplexType.js +++ b/tests/rules/assertions/arrayStyleComplexType.js @@ -2,51 +2,61 @@ export default { invalid: [ { code: 'type X = (?string)[]', - errors: [{message: 'Use "Array", not "ComplexType[]"'}], + errors: [{message: 'Use "Array", not "(?string)[]"'}], output: 'type X = Array' }, { code: 'type X = (?string)[]', - errors: [{message: 'Use "Array", not "ComplexType[]"'}], + errors: [{message: 'Use "Array", not "(?string)[]"'}], options: ['verbose'], output: 'type X = Array' }, { code: 'type X = Array', - errors: [{message: 'Use "ComplexType[]", not "Array"'}], + errors: [{message: 'Use "(?string)[]", not "Array"'}], options: ['shorthand'], output: 'type X = (?string)[]' }, { code: 'type X = Array<{foo: string}>', - errors: [{message: 'Use "ComplexType[]", not "Array"'}], + errors: [{message: 'Use "{foo: string}[]", not "Array<{foo: string}>"'}], options: ['shorthand'], output: 'type X = {foo: string}[]' }, { code: 'type X = (string | number)[]', - errors: [{message: 'Use "Array", not "ComplexType[]"'}], + errors: [{message: 'Use "Array", not "(string | number)[]"'}], output: 'type X = Array' }, { code: 'type X = (string & number)[]', - errors: [{message: 'Use "Array", not "ComplexType[]"'}], + errors: [{message: 'Use "Array", not "(string & number)[]"'}], output: 'type X = Array' }, { code: 'type X = [string, number][]', - errors: [{message: 'Use "Array", not "ComplexType[]"'}], + errors: [{message: 'Use "Array<[string, number]>", not "[string, number][]"'}], output: 'type X = Array<[string, number]>' }, { code: 'type X = {foo: string}[]', - errors: [{message: 'Use "Array", not "ComplexType[]"'}], + errors: [{message: 'Use "Array<{foo: string}>", not "{foo: string}[]"'}], output: 'type X = Array<{foo: string}>' }, { code: 'type X = (string => number)[]', - errors: [{message: 'Use "Array", not "ComplexType[]"'}], + errors: [{message: 'Use "Array number>", not "(string => number)[]"'}], output: 'type X = Array number>' + }, + { + code: 'type X = {\n foo: string,\n bar: number\n}[]', + errors: [{message: 'Use "Array<{ foo: string, bar: number }>", not "{ foo: string, bar: number }[]"'}], + output: 'type X = Array<{\n foo: string,\n bar: number\n}>' + }, + { + code: 'type X = {\n foo: string,\n bar: number,\n quo: boolean,\n hey: Date\n}[]', + errors: [{message: 'Use "Array", not "Type[]"'}], + output: 'type X = Array<{\n foo: string,\n bar: number,\n quo: boolean,\n hey: Date\n}>' } ], misconfigured: [ diff --git a/tests/rules/assertions/arrayStyleSimpleType.js b/tests/rules/assertions/arrayStyleSimpleType.js index b2bbf2a4..fba112a5 100644 --- a/tests/rules/assertions/arrayStyleSimpleType.js +++ b/tests/rules/assertions/arrayStyleSimpleType.js @@ -2,62 +2,72 @@ export default { invalid: [ { code: 'type X = string[]', - errors: [{message: 'Use "Array", not "SimpleType[]"'}], + errors: [{message: 'Use "Array", not "string[]"'}], output: 'type X = Array' }, { code: 'type X = string[]', - errors: [{message: 'Use "Array", not "SimpleType[]"'}], + errors: [{message: 'Use "Array", not "string[]"'}], options: ['verbose'], output: 'type X = Array' }, { code: 'type X = Array', - errors: [{message: 'Use "SimpleType[]", not "Array"'}], + errors: [{message: 'Use "string[]", not "Array"'}], options: ['shorthand'], output: 'type X = string[]' }, { code: 'type X = Date[]', - errors: [{message: 'Use "Array", not "SimpleType[]"'}], + errors: [{message: 'Use "Array", not "Date[]"'}], output: 'type X = Array' }, { code: 'type X = Promise[]', - errors: [{message: 'Use "Array", not "SimpleType[]"'}], + errors: [{message: 'Use "Array>", not "Promise[]"'}], output: 'type X = Array>' }, { - code: 'type X = $Keys<{ foo: string }>[]', - errors: [{message: 'Use "Array", not "SimpleType[]"'}], - output: 'type X = Array<$Keys<{ foo: string }>>' + code: 'type X = $Keys<{foo: string}>[]', + errors: [{message: 'Use "Array<$Keys<{foo: string}>>", not "$Keys<{foo: string}>[]"'}], + output: 'type X = Array<$Keys<{foo: string}>>' }, { code: 'type X = any[]', - errors: [{message: 'Use "Array", not "SimpleType[]"'}], + errors: [{message: 'Use "Array", not "any[]"'}], output: 'type X = Array' }, { code: 'type X = mixed[]', - errors: [{message: 'Use "Array", not "SimpleType[]"'}], + errors: [{message: 'Use "Array", not "mixed[]"'}], output: 'type X = Array' }, { code: 'type X = void[]', - errors: [{message: 'Use "Array", not "SimpleType[]"'}], + errors: [{message: 'Use "Array", not "void[]"'}], output: 'type X = Array' }, { code: 'type X = null[]', - errors: [{message: 'Use "Array", not "SimpleType[]"'}], + errors: [{message: 'Use "Array", not "null[]"'}], output: 'type X = Array' }, { code: 'type X = string[][]', errors: [ - {message: 'Use "Array", not "SimpleType[]"'}, - {message: 'Use "Array", not "SimpleType[]"'} + {message: 'Use "Array", not "string[][]"'}, + {message: 'Use "Array", not "string[]"'} ] + }, + { + code: 'type X = Promise<{\n foo: string,\n bar: number\n}>[]', + errors: [{message: 'Use "Array>", not "Promise<{ foo: string, bar: number }>[]"'}], + output: 'type X = Array>' + }, + { + code: 'type X = Promise<{\n foo: string,\n bar: number,\n quo: boolean\n}>[]', + errors: [{message: 'Use "Array", not "Type[]"'}], + output: 'type X = Array>' } ], misconfigured: [