From e17a63f201ba1c6fe7f0177b2dfc2c4bf5dc1228 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 29 Sep 2020 18:36:13 +0800 Subject: [PATCH] `no-console-spaces`: Make space position more specific (#838) --- rules/no-console-spaces.js | 29 ++++---- test/no-console-spaces.js | 63 ++++++++-------- test/snapshots/no-console-spaces.js.md | 89 ++++++++++++++++++++++- test/snapshots/no-console-spaces.js.snap | Bin 332 -> 720 bytes 4 files changed, 134 insertions(+), 47 deletions(-) diff --git a/rules/no-console-spaces.js b/rules/no-console-spaces.js index 77c512d5e0..9f2f1d0043 100644 --- a/rules/no-console-spaces.js +++ b/rules/no-console-spaces.js @@ -5,8 +5,7 @@ const replaceStringRaw = require('./utils/replace-string-raw'); const MESSAGE_ID = 'no-console-spaces'; const messages = { - // TODO: Make `leading/trailing` more specify - [MESSAGE_ID]: 'Do not use leading/trailing space between `console.{{method}}` parameters.' + [MESSAGE_ID]: 'Do not use {{positions}} space between `console.{{method}}` parameters.' }; const methods = [ @@ -24,16 +23,10 @@ const selector = methodSelector({ }); // Find exactly one leading space, allow exactly one space -const fixLeadingSpace = value => - value.length > 1 && value.charAt(0) === ' ' && value.charAt(1) !== ' ' ? - value.slice(1) : - value; +const hasLeadingSpace = value => value.length > 1 && value.charAt(0) === ' ' && value.charAt(1) !== ' '; // Find exactly one trailing space, allow exactly one space -const fixTrailingSpace = value => - value.length > 1 && value.charAt(value.length - 1) === ' ' && value.charAt(value.length - 2) !== ' ' ? - value.slice(0, -1) : - value; +const hasTrailingSpace = value => value.length > 1 && value.charAt(value.length - 1) === ' ' && value.charAt(value.length - 2) !== ' '; const create = context => { const sourceCode = context.getSourceCode(); @@ -47,19 +40,23 @@ const create = context => { } const raw = sourceCode.getText(node).slice(1, -1); + const positions = []; let fixed = raw; - if (index !== 0) { - fixed = fixLeadingSpace(fixed); + if (index !== 0 && hasLeadingSpace(fixed)) { + positions.push('leading'); + fixed = fixed.slice(1); } - if (index !== parameters.length - 1) { - fixed = fixTrailingSpace(fixed); + if (index !== parameters.length - 1 && hasTrailingSpace(fixed)) { + positions.push('trailing'); + fixed = fixed.slice(0, -1); } if (raw !== fixed) { return { + positions, node, fixed }; @@ -73,11 +70,11 @@ const create = context => { .map((parameter, index) => fixParamter(parameter, index, node.arguments)) .filter(Boolean); - for (const {node, fixed} of fixedParameters) { + for (const {node, fixed, positions} of fixedParameters) { context.report({ node, messageId: MESSAGE_ID, - data: {method}, + data: {method, positions: positions.join(' and ')}, fix: fixer => replaceStringRaw(fixer, node, fixed) }); } diff --git a/test/no-console-spaces.js b/test/no-console-spaces.js index 10008e0257..2e15ee1152 100644 --- a/test/no-console-spaces.js +++ b/test/no-console-spaces.js @@ -10,21 +10,11 @@ const ruleTester = avaRuleTester(test, { } }); -function buildError({method, column, line}) { - const error = { +function buildError({method, positions}) { + return { messageId: 'no-console-spaces', - data: {method} + data: {method, positions} }; - - if (column) { - error.column = column; - } - - if (line) { - error.line = line; - } - - return error; } ruleTester.run('no-console-spaces', rule, { @@ -105,66 +95,66 @@ ruleTester.run('no-console-spaces', rule, { invalid: [ { code: 'console.log("abc ", "def");', - errors: [buildError({method: 'log'})], + errors: [buildError({method: 'log', positions: 'trailing'})], output: 'console.log("abc", "def");' }, { code: 'console.log("abc", " def");', - errors: [buildError({method: 'log'})], + errors: [buildError({method: 'log', positions: 'leading'})], output: 'console.log("abc", "def");' }, { code: 'console.log(" abc ", "def");', - errors: [buildError({method: 'log'})], + errors: [buildError({method: 'log', positions: 'trailing'})], output: 'console.log(" abc", "def");' }, { code: 'console.debug("abc ", "def");', - errors: [buildError({method: 'debug'})], + errors: [buildError({method: 'debug', positions: 'trailing'})], output: 'console.debug("abc", "def");' }, { code: 'console.info("abc ", "def");', - errors: [buildError({method: 'info'})], + errors: [buildError({method: 'info', positions: 'trailing'})], output: 'console.info("abc", "def");' }, { code: 'console.warn("abc ", "def");', - errors: [buildError({method: 'warn'})], + errors: [buildError({method: 'warn', positions: 'trailing'})], output: 'console.warn("abc", "def");' }, { code: 'console.error("abc ", "def");', - errors: [buildError({method: 'error'})], + errors: [buildError({method: 'error', positions: 'trailing'})], output: 'console.error("abc", "def");' }, { code: 'console.log("abc", " def ", "ghi");', - errors: [buildError({method: 'log'})], + errors: [buildError({method: 'log', positions: 'leading and trailing'})], output: 'console.log("abc", "def", "ghi");' }, { code: 'console.log("abc ", "def ", "ghi");', errors: [ - buildError({method: 'log', column: 13}), - buildError({method: 'log', column: 21}) + buildError({method: 'log', positions: 'trailing'}), + buildError({method: 'log', positions: 'trailing'}) ], output: 'console.log("abc", "def", "ghi");' }, { code: 'console.log(\'abc \', "def");', - errors: [buildError({method: 'log'})], + errors: [buildError({method: 'log', positions: 'trailing'})], output: 'console.log(\'abc\', "def");' }, { code: 'console.log(`abc `, "def");', - errors: [buildError({method: 'log'})], + errors: [buildError({method: 'log', positions: 'trailing'})], output: 'console.log(`abc`, "def");' }, { // eslint-disable-next-line no-template-curly-in-string code: 'console.log(`abc ${1 + 2} `, "def");', - errors: [buildError({method: 'log'})], + errors: [buildError({method: 'log', positions: 'trailing'})], // eslint-disable-next-line no-template-curly-in-string output: 'console.log(`abc ${1 + 2}`, "def");' }, @@ -177,7 +167,7 @@ ruleTester.run('no-console-spaces', rule, { ); `, errors: [ - buildError({method: 'log', column: 2, line: 3}) + buildError({method: 'log', positions: 'trailing'}) ], output: outdent` console.log( @@ -197,7 +187,7 @@ ruleTester.run('no-console-spaces', rule, { ); `, errors: [ - buildError({method: 'error'}) + buildError({method: 'error', positions: 'trailing'}) ], output: outdent` console.error( @@ -219,5 +209,20 @@ visualizeTester.run('no-console-spaces', rule, [ 'Verifying "packaging" fixture\\n ', theme.error(errorMessage) ); - ` + `, + outdent` + console.log( + 'abc', + 'def ', + 'ghi' + ); + `, + 'console.log("_", " leading", "_")', + 'console.log("_", "trailing ", "_")', + 'console.log("_", " leading and trailing ", "_")', + 'console.log("_", " log ", "_")', + 'console.debug("_", " debug ", "_")', + 'console.info("_", " info ", "_")', + 'console.warn("_", " warn ", "_")', + 'console.error("_", " error ", "_")' ]); diff --git a/test/snapshots/no-console-spaces.js.md b/test/snapshots/no-console-spaces.js.md index e6fe6a15fa..46b7232089 100644 --- a/test/snapshots/no-console-spaces.js.md +++ b/test/snapshots/no-console-spaces.js.md @@ -10,7 +10,25 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | console.log("abc", " def ", "ghi");␊ - | ^^^^^^^ Do not use leading/trailing space between `console.log` parameters.␊ + | ^^^^^^^ Do not use leading and trailing space between `console.log` parameters.␊ + ` + +## no-console-spaces - #10 + +> Snapshot 1 + + `␊ + > 1 | console.warn("_", " warn ", "_")␊ + | ^^^^^^^^ Do not use leading and trailing space between `console.warn` parameters.␊ + ` + +## no-console-spaces - #11 + +> Snapshot 1 + + `␊ + > 1 | console.error("_", " error ", "_")␊ + | ^^^^^^^^^ Do not use leading and trailing space between `console.error` parameters.␊ ` ## no-console-spaces - #2 @@ -21,7 +39,74 @@ Generated by [AVA](https://avajs.dev). 1 | console.error(␊ 2 | theme.error('✗'),␊ > 3 | 'Verifying "packaging" fixture\\n ',␊ - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use leading/trailing space between `console.error` parameters.␊ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use trailing space between `console.error` parameters.␊ 4 | theme.error(errorMessage)␊ 5 | );␊ ` + +## no-console-spaces - #3 + +> Snapshot 1 + + `␊ + 1 | console.log(␊ + 2 | 'abc',␊ + > 3 | 'def ',␊ + | ^^^^^^ Do not use trailing space between `console.log` parameters.␊ + 4 | 'ghi'␊ + 5 | );␊ + ` + +## no-console-spaces - #4 + +> Snapshot 1 + + `␊ + > 1 | console.log("_", " leading", "_")␊ + | ^^^^^^^^^^ Do not use leading space between `console.log` parameters.␊ + ` + +## no-console-spaces - #5 + +> Snapshot 1 + + `␊ + > 1 | console.log("_", "trailing ", "_")␊ + | ^^^^^^^^^^^ Do not use trailing space between `console.log` parameters.␊ + ` + +## no-console-spaces - #6 + +> Snapshot 1 + + `␊ + > 1 | console.log("_", " leading and trailing ", "_")␊ + | ^^^^^^^^^^^^^^^^^^^^^^^^ Do not use leading and trailing space between `console.log` parameters.␊ + ` + +## no-console-spaces - #7 + +> Snapshot 1 + + `␊ + > 1 | console.log("_", " log ", "_")␊ + | ^^^^^^^ Do not use leading and trailing space between `console.log` parameters.␊ + ` + +## no-console-spaces - #8 + +> Snapshot 1 + + `␊ + > 1 | console.debug("_", " debug ", "_")␊ + | ^^^^^^^^^ Do not use leading and trailing space between `console.debug` parameters.␊ + ` + +## no-console-spaces - #9 + +> Snapshot 1 + + `␊ + > 1 | console.info("_", " info ", "_")␊ + | ^^^^^^^^ Do not use leading and trailing space between `console.info` parameters.␊ + ` diff --git a/test/snapshots/no-console-spaces.js.snap b/test/snapshots/no-console-spaces.js.snap index 3806178eb81985a2cb343bc84f279d02aa026b94..af199edbfc232453d9a1f637080a0e9d06454e98 100644 GIT binary patch literal 720 zcmV;>0x$hRRzV~$omG@W3*9%H(1^I$HMeTutK(H8y*`)U`_njfQr;`6!zs=Xb!i-?i1waLR zm>GWBfAV{{=zxRUC!LsGj9^g*CI*Jv;?dQ!13i?E+}!hg_S&^lj9}5LK2?wONc{(c?h8L3ZlP>ql<+zVVXVuw2^eE+bepmz9BG`A4OLeXcR<6yN18(APXV zlMyV+$Hu^L$YJ9uDa%98qBBL--L2rRX9SC`2V#{u?aS9^aog5$7gbDW@!!h`77b-* zU|?oo2ZskED}x}@6b3Fk1w(}zh2;Fa;{2Rcy`21X4W-1SWF;L1C54pKGzAbNJtI>| z)0#^G2x=6tfH(+HaLHH5%P&zVElyR)Nli@2%u82D%u7)yDN4-D0da~85|dLEl2S{` zQ&aO45|HdnP$)<&O3Y0yNi8bY<3e%bI|iV|$WBZxD#|a?016rb1vyJHQgdNq>W}73 zSJ%`5ddV0hr5=`Al$lluvRMgeb9Q1nkg24QmRV6!T9g`-r=YF_^%G|tL4fQzLLLIU z2GvUn3MS~T1(Uw1#l?x~shU6qra%S2P)70mM0C%C;s@q+bznTHBYX>rEp#7|;%ba& zfVx>782#!v+}DOR{ltS)4>SpY*zro5xKa=H5QZfmvQkVZZb!kxfM{1y7KAA7T2DBz zQ1c7%j>MT%sh1CX@wq;qSjW=FZPU@+mXexO3U?itL&$wd!AY(g!S>*B6Lce`E(BRZiUZ3Ni}GL&1hHxEK#(P*#51HMg}M;Tp+h`lDc%9H8TABt2mk=Z Ct4{6! literal 332 zcmV-S0ki%=RzVq9=N0GYr0V75r)wxBCM7HBC@3kUq^2o=80i_AN}ASO z3P4b!fCa=sfPzcDLSBA}LTPcTLQZO8N@iZVeo0YcW)6^{P+X9hoT`wNT2h{xnx~L} zWM6_pL1Iy2ZfZ$tQL!Euiu1lP04+s!UTRTMevt-H&l6VriAC55!iijvZz)R;U4bseaGIO7Ncay$lh6{^=16im=v e4JLh4i;EM}Q#FAKOo0l3Aq@b0p~m0d0RRA*y@_f7