From 136dd2b76fe780d75609531e54c92c2256d22b5f Mon Sep 17 00:00:00 2001 From: Moong0122 Date: Sun, 27 Sep 2020 15:51:24 +0900 Subject: [PATCH] try 1 --- lib/rules/jsx-indent-props.js | 31 +- tests/lib/rules/jsx-indent-props.js | 475 +++++++++++++++------------- 2 files changed, 277 insertions(+), 229 deletions(-) diff --git a/lib/rules/jsx-indent-props.js b/lib/rules/jsx-indent-props.js index 27a63fb4b1..1942b53605 100644 --- a/lib/rules/jsx-indent-props.js +++ b/lib/rules/jsx-indent-props.js @@ -56,13 +56,18 @@ module.exports = { }, create(context) { + debugger; const MESSAGE = 'Expected indentation of {{needed}} {{type}} {{characters}} but found {{gotten}}.'; const extraColumnStart = 0; let indentType = 'space'; /** @type {number|'first'} */ let indentSize = 4; - + // beforeLine을 추가해줬다! + const beforeLine = { + isOperator : false, + indent : 0, + }; if (context.options.length) { if (context.options[0] === 'first') { indentSize = 'first'; @@ -83,6 +88,7 @@ module.exports = { * @param {Number} gotten Indentation character count in the actual node/code */ function report(node, needed, gotten) { + debugger; const msgContext = { needed, type: indentType, @@ -90,7 +96,7 @@ module.exports = { gotten }; - context.report({ + context.report({ // 린트 에러를 호출하는 함수가 실행되지 않음 node, message: MESSAGE, data: msgContext, @@ -112,14 +118,25 @@ module.exports = { src = lines[0]; let regExp; + let useOperator; + let operatorIndent; if (indentType === 'space') { regExp = /^[ ]+/; } else { regExp = /^[\t]+/; } - const indent = regExp.exec(src); - return indent ? indent[0].length : 0; + useOperator = /^[:](?![:])/.test(src) || /^[?](?![?])/.test(src); // 연산자가 사용됐는지 확인해준다 + + if(useOperator) { // 만약 연산자가 사용됐다면 저장 해준다 + beforeLine.isOperator = true; + beforeLine.indent = indent[0].length; + } else if(!useOperator && beforeLine.isOperator) { // 현재 줄에서는 연산자가 사용되지 않고, 이 전 줄에 연산자가 사용됐다면 인덴트를 추가로 해주자 + beforeLine.isOperator = false; + operatorIndent = indentSize !== 'first' ? indentSize : 0; + } + debugger; + return indent ? indent[0].length + operatorIndent : 0; } /** @@ -127,9 +144,10 @@ module.exports = { * @param {ASTNode[]} nodes list of node objects * @param {Number} indent needed indent */ - function checkNodesIndent(nodes, indent) { + function checkNodesIndent(nodes, indent) { // 여기가 요주의 함수이다 여기서 조건문으로 report로 넘어가는데 여 nodes.forEach((node) => { const nodeIndent = getNodeIndent(node); + debugger; if ( node.type !== 'ArrayExpression' && node.type !== 'ObjectExpression' && nodeIndent !== indent && astUtil.isNodeFirstInLine(context, node) @@ -141,6 +159,7 @@ module.exports = { return { JSXOpeningElement(node) { + debugger; if (!node.attributes.length) { return; } @@ -148,9 +167,11 @@ module.exports = { if (indentSize === 'first') { const firstPropNode = node.attributes[0]; propIndent = firstPropNode.loc.start.column; + debugger; } else { const elementIndent = getNodeIndent(node); propIndent = elementIndent + indentSize; + debugger; } checkNodesIndent(node.attributes, propIndent); } diff --git a/tests/lib/rules/jsx-indent-props.js b/tests/lib/rules/jsx-indent-props.js index 05bc038a52..1a84436f4b 100644 --- a/tests/lib/rules/jsx-indent-props.js +++ b/tests/lib/rules/jsx-indent-props.js @@ -26,229 +26,256 @@ const parserOptions = { const ruleTester = new RuleTester({parserOptions}); ruleTester.run('jsx-indent-props', rule, { - valid: [{ - code: [ - '' - ].join('\n') - }, { - code: [ - '' - ].join('\n'), - options: [2] - }, { - code: [ - '' - ].join('\n'), - options: [0] - }, { - code: [ - ' ' - ].join('\n'), - options: [-2] - }, { - code: [ - '' - ].join('\n'), - options: ['tab'] - }, { - code: [ - '' - ].join('\n'), - options: ['first'] - }, { - code: [ - '' - ].join('\n'), - options: ['first'] - }, { - code: [ - '' - ].join('\n'), - options: ['first'] - }, { - code: [ - 'const test = ' - ].join('\n'), - options: ['first'] - }, { - code: [ - '' - ].join('\n'), - options: ['first'] - }, { - code: [ - 'const test = ' - ].join('\n'), - options: ['first'] - }, { - code: [ - '', - ' ', - '' - ].join('\n'), - options: ['first'] - }, { - code: [ - '', - ' ', - ' ', - '' - ].join('\n'), - options: ['first'] - }, { - code: [ - '' - ].join('\n'), - options: ['first'] - }], + valid: [ + // { + // code: [ + // '' + // ].join('\n') + // }, { + // code: [ + // '' + // ].join('\n'), + // options: [2] + // }, { + // code: [ + // '' + // ].join('\n'), + // options: [0] + // }, { + // code: [ + // ' ' + // ].join('\n'), + // options: [-2] + // }, { + // code: [ + // '' + // ].join('\n'), + // options: ['tab'] + // }, { + // code: [ + // '' + // ].join('\n'), + // options: ['first'] + // }, { + // code: [ + // '' + // ].join('\n'), + // options: ['first'] + // }, { + // code: [ + // '' + // ].join('\n'), + // options: ['first'] + // }, { + // code: [ + // 'const test = ' + // ].join('\n'), + // options: ['first'] + // }, { + // code: [ + // '' + // ].join('\n'), + // options: ['first'] + // }, { + // code: [ + // 'const test = ' + // ].join('\n'), + // options: ['first'] + // }, { + // code: [ + // '', + // ' ', + // '' + // ].join('\n'), + // options: ['first'] + // }, { + // code: [ + // '', + // ' ', + // ' ', + // '' + // ].join('\n'), + // options: ['first'] + // }, { + // code: [ + // '' + // ].join('\n'), + // options: ['first'] + // } + ], - invalid: [{ - code: [ - '' - ].join('\n'), - output: [ - '' - ].join('\n'), - errors: [{message: 'Expected indentation of 4 space characters but found 2.'}] - }, { - code: [ - '' - ].join('\n'), - output: [ - '' - ].join('\n'), - options: [2], - errors: [{message: 'Expected indentation of 2 space characters but found 4.'}] - }, { - code: [ - '' - ].join('\n'), - output: [ - '' - ].join('\n'), - options: ['tab'], - errors: [{message: 'Expected indentation of 1 tab character but found 0.'}] - }, { - code: [ - '' - ].join('\n'), - output: [ - '' - ].join('\n'), - options: ['tab'], - errors: [{message: 'Expected indentation of 1 tab character but found 3.'}] - }, { - code: [ - '' - ].join('\n'), - output: [ - '' - ].join('\n'), - options: ['first'], - errors: [{message: 'Expected indentation of 5 space characters but found 2.'}] - }, { - code: [ - '' - ].join('\n'), - output: [ - '' - ].join('\n'), - options: ['first'], - errors: [{message: 'Expected indentation of 6 space characters but found 3.'}] - }, { - code: [ - '' - ].join('\n'), - output: [ - '' - ].join('\n'), - options: ['first'], - errors: [{message: 'Expected indentation of 6 space characters but found 3.'}] - }, { - code: [ - '' - ].join('\n'), - output: [ - '' - ].join('\n'), - options: ['first'], - errors: [ - {message: 'Expected indentation of 2 space characters but found 1.'}, - {message: 'Expected indentation of 2 space characters but found 3.'} - ] - }] + invalid: [ + // { + // code: [ + // '' + // ].join('\n'), + // output: [ + // '' + // ].join('\n'), + // errors: [{message: 'Expected indentation of 4 space characters but found 2.'}] + // }, { + // code: [ + // '' + // ].join('\n'), + // output: [ + // '' + // ].join('\n'), + // options: [2], + // errors: [{message: 'Expected indentation of 2 space characters but found 4.'}] + // }, { + // code: [ + // '' + // ].join('\n'), + // output: [ + // '' + // ].join('\n'), + // options: ['tab'], + // errors: [{message: 'Expected indentation of 1 tab character but found 0.'}] + // }, { + // code: [ + // '' + // ].join('\n'), + // output: [ + // '' + // ].join('\n'), + // options: ['tab'], + // errors: [{message: 'Expected indentation of 1 tab character but found 3.'}] + // }, { + // code: [ + // '' + // ].join('\n'), + // output: [ + // '' + // ].join('\n'), + // options: ['first'], + // errors: [{message: 'Expected indentation of 5 space characters but found 2.'}] + // }, { + // code: [ + // '' + // ].join('\n'), + // output: [ + // '' + // ].join('\n'), + // options: ['first'], + // errors: [{message: 'Expected indentation of 6 space characters but found 3.'}] + // }, { + // code: [ + // '' + // ].join('\n'), + // output: [ + // '' + // ].join('\n'), + // options: ['first'], + // errors: [{message: 'Expected indentation of 6 space characters but found 3.'}] + // }, { + // code: [ + // '' + // ].join('\n'), + // output: [ + // '' + // ].join('\n'), + // options: ['first'], + // errors: [ + // {message: 'Expected indentation of 2 space characters but found 1.'}, + // {message: 'Expected indentation of 2 space characters but found 3.'} + // ] + // }, + { + code: [ + 'const test = true', + ' ? ', + ' : ' + ].join('\n'), + output: [ + 'const test = true', + ' ? ', + ' : ' + ].join('\n'), + options: [2], + errors: [ + {message: 'Expected indentation of 6 space characters but found 4.'} + ] + }] });