From a5b6834ec63af6503cfee4fbec7413019985f48e Mon Sep 17 00:00:00 2001 From: fisker Date: Sat, 17 Oct 2020 04:39:14 +0800 Subject: [PATCH 1/5] `no-console-spaces`: Improve report location --- rules/no-console-spaces.js | 84 +++++++++++------------ test/no-console-spaces.js | 37 +++++----- test/snapshots/no-console-spaces.js.md | 57 ++++++++++----- test/snapshots/no-console-spaces.js.snap | Bin 878 -> 929 bytes 4 files changed, 101 insertions(+), 77 deletions(-) diff --git a/rules/no-console-spaces.js b/rules/no-console-spaces.js index 9f2f1d0043..1ba26fa29c 100644 --- a/rules/no-console-spaces.js +++ b/rules/no-console-spaces.js @@ -5,7 +5,7 @@ const replaceStringRaw = require('./utils/replace-string-raw'); const MESSAGE_ID = 'no-console-spaces'; const messages = { - [MESSAGE_ID]: 'Do not use {{positions}} space between `console.{{method}}` parameters.' + [MESSAGE_ID]: 'Do not use {{position}} space between `console.{{method}}` parameters.' }; const methods = [ @@ -23,60 +23,60 @@ const selector = methodSelector({ }); // Find exactly one leading space, allow exactly one space -const hasLeadingSpace = value => value.length > 1 && value.charAt(0) === ' ' && value.charAt(1) !== ' '; +const hasLeadingSpace = value => /^ [^ ]/.test(value); // Find exactly one trailing space, allow exactly one space -const hasTrailingSpace = value => value.length > 1 && value.charAt(value.length - 1) === ' ' && value.charAt(value.length - 2) !== ' '; +const hasTrailingSpace = value => /[^ ] $/.test(value); const create = context => { const sourceCode = context.getSourceCode(); + const report = (node, method, position) => { + let start; + let end; - const fixParamter = (node, index, parameters) => { - if ( - !(node.type === 'Literal' && typeof node.value === 'string') && - node.type !== 'TemplateLiteral' - ) { - return; + if (position === 'leading') { + const [index] = node.range; + start = index + 1; + end = index + 2; + } else { + const [, index] = node.range; + start = index - 2; + end = index - 1; } - const raw = sourceCode.getText(node).slice(1, -1); - const positions = []; - - let fixed = raw; - - if (index !== 0 && hasLeadingSpace(fixed)) { - positions.push('leading'); - fixed = fixed.slice(1); - } - - if (index !== parameters.length - 1 && hasTrailingSpace(fixed)) { - positions.push('trailing'); - fixed = fixed.slice(0, -1); - } - - if (raw !== fixed) { - return { - positions, - node, - fixed - }; - } + context.report({ + loc: { + start: sourceCode.getLocFromIndex(start), + end: sourceCode.getLocFromIndex(end) + }, + messageId: MESSAGE_ID, + data: {method, position}, + fix: fixer => fixer.removeRange([start, end]) + }); }; - return { [selector](node) { const method = node.callee.property.name; - const fixedParameters = node.arguments - .map((parameter, index) => fixParamter(parameter, index, node.arguments)) - .filter(Boolean); + const {arguments: messages} = node; + const {length} = messages; + for (const [index, node] of messages.entries()) { + const {type, value} = node; + if ( + !(type === 'Literal' && typeof value === 'string') && + type !== 'TemplateLiteral' + ) { + continue; + } + + const raw = sourceCode.getText(node).slice(1, -1); + + if (index !== 0 && hasLeadingSpace(raw)) { + report(node, method, 'leading'); + } - for (const {node, fixed, positions} of fixedParameters) { - context.report({ - node, - messageId: MESSAGE_ID, - data: {method, positions: positions.join(' and ')}, - fix: fixer => replaceStringRaw(fixer, node, fixed) - }); + if (index !== length - 1 && hasTrailingSpace(raw)) { + report(node, method, 'trailing'); + } } } }; diff --git a/test/no-console-spaces.js b/test/no-console-spaces.js index 6b4af81002..c40ebbeb54 100644 --- a/test/no-console-spaces.js +++ b/test/no-console-spaces.js @@ -1,10 +1,10 @@ import {outdent} from 'outdent'; import {test} from './utils/test'; -function buildError({method, positions}) { +function buildError({method, position}) { return { messageId: 'no-console-spaces', - data: {method, positions} + data: {method, position} }; } @@ -86,66 +86,69 @@ test({ invalid: [ { code: 'console.log("abc ", "def");', - errors: [buildError({method: 'log', positions: 'trailing'})], + errors: [buildError({method: 'log', position: 'trailing'})], output: 'console.log("abc", "def");' }, { code: 'console.log("abc", " def");', - errors: [buildError({method: 'log', positions: 'leading'})], + errors: [buildError({method: 'log', position: 'leading'})], output: 'console.log("abc", "def");' }, { code: 'console.log(" abc ", "def");', - errors: [buildError({method: 'log', positions: 'trailing'})], + errors: [buildError({method: 'log', position: 'trailing'})], output: 'console.log(" abc", "def");' }, { code: 'console.debug("abc ", "def");', - errors: [buildError({method: 'debug', positions: 'trailing'})], + errors: [buildError({method: 'debug', position: 'trailing'})], output: 'console.debug("abc", "def");' }, { code: 'console.info("abc ", "def");', - errors: [buildError({method: 'info', positions: 'trailing'})], + errors: [buildError({method: 'info', position: 'trailing'})], output: 'console.info("abc", "def");' }, { code: 'console.warn("abc ", "def");', - errors: [buildError({method: 'warn', positions: 'trailing'})], + errors: [buildError({method: 'warn', position: 'trailing'})], output: 'console.warn("abc", "def");' }, { code: 'console.error("abc ", "def");', - errors: [buildError({method: 'error', positions: 'trailing'})], + errors: [buildError({method: 'error', position: 'trailing'})], output: 'console.error("abc", "def");' }, { code: 'console.log("abc", " def ", "ghi");', - errors: [buildError({method: 'log', positions: 'leading and trailing'})], + errors: [ + buildError({method: 'log', position: 'leading'}), + buildError({method: 'log', position: 'trailing'}) + ], output: 'console.log("abc", "def", "ghi");' }, { code: 'console.log("abc ", "def ", "ghi");', errors: [ - buildError({method: 'log', positions: 'trailing'}), - buildError({method: 'log', positions: 'trailing'}) + buildError({method: 'log', position: 'trailing'}), + buildError({method: 'log', position: 'trailing'}) ], output: 'console.log("abc", "def", "ghi");' }, { code: 'console.log(\'abc \', "def");', - errors: [buildError({method: 'log', positions: 'trailing'})], + errors: [buildError({method: 'log', position: 'trailing'})], output: 'console.log(\'abc\', "def");' }, { code: 'console.log(`abc `, "def");', - errors: [buildError({method: 'log', positions: 'trailing'})], + errors: [buildError({method: 'log', position: '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', positions: 'trailing'})], + errors: [buildError({method: 'log', position: 'trailing'})], // eslint-disable-next-line no-template-curly-in-string output: 'console.log(`abc ${1 + 2}`, "def");' }, @@ -158,7 +161,7 @@ test({ ); `, errors: [ - buildError({method: 'log', positions: 'trailing'}) + buildError({method: 'log', position: 'trailing'}) ], output: outdent` console.log( @@ -178,7 +181,7 @@ test({ ); `, errors: [ - buildError({method: 'error', positions: 'trailing'}) + buildError({method: 'error', position: 'trailing'}) ], output: outdent` console.error( diff --git a/test/snapshots/no-console-spaces.js.md b/test/snapshots/no-console-spaces.js.md index a2d6a82db1..9ac5e5f14b 100644 --- a/test/snapshots/no-console-spaces.js.md +++ b/test/snapshots/no-console-spaces.js.md @@ -15,9 +15,12 @@ Generated by [AVA](https://avajs.dev). Output:␊ 1 | console.log("abc", "def", "ghi");␊ ␊ - Error 1/1:␊ + Error 1/2:␊ + > 1 | console.log("abc", " def ", "ghi");␊ + | ^ Do not use leading space between `console.log` parameters.␊ + Error 2/2:␊ > 1 | console.log("abc", " def ", "ghi");␊ - | ^^^^^^^ Do not use leading and trailing space between `console.log` parameters.␊ + | ^ Do not use trailing space between `console.log` parameters.␊ ` ## no-console-spaces - #02 @@ -43,7 +46,7 @@ Generated by [AVA](https://avajs.dev). 1 | console.error(␊ 2 | theme.error('✗'),␊ > 3 | 'Verifying "packaging" fixture\\n ',␊ - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use trailing space between `console.error` parameters.␊ + | ^ Do not use trailing space between `console.error` parameters.␊ 4 | theme.error(errorMessage)␊ 5 | );␊ ` @@ -71,7 +74,7 @@ Generated by [AVA](https://avajs.dev). 1 | console.log(␊ 2 | 'abc',␊ > 3 | 'def ',␊ - | ^^^^^^ Do not use trailing space between `console.log` parameters.␊ + | ^ Do not use trailing space between `console.log` parameters.␊ 4 | 'ghi'␊ 5 | );␊ ` @@ -89,7 +92,7 @@ Generated by [AVA](https://avajs.dev). ␊ Error 1/1:␊ > 1 | console.log("_", " leading", "_")␊ - | ^^^^^^^^^^ Do not use leading space between `console.log` parameters.␊ + | ^ Do not use leading space between `console.log` parameters.␊ ` ## no-console-spaces - #05 @@ -105,7 +108,7 @@ Generated by [AVA](https://avajs.dev). ␊ Error 1/1:␊ > 1 | console.log("_", "trailing ", "_")␊ - | ^^^^^^^^^^^ Do not use trailing space between `console.log` parameters.␊ + | ^ Do not use trailing space between `console.log` parameters.␊ ` ## no-console-spaces - #06 @@ -119,9 +122,12 @@ Generated by [AVA](https://avajs.dev). Output:␊ 1 | console.log("_", "leading and trailing", "_")␊ ␊ - Error 1/1:␊ + Error 1/2:␊ + > 1 | console.log("_", " leading and trailing ", "_")␊ + | ^ Do not use leading space between `console.log` parameters.␊ + Error 2/2:␊ > 1 | console.log("_", " leading and trailing ", "_")␊ - | ^^^^^^^^^^^^^^^^^^^^^^^^ Do not use leading and trailing space between `console.log` parameters.␊ + | ^ Do not use trailing space between `console.log` parameters.␊ ` ## no-console-spaces - #07 @@ -135,9 +141,12 @@ Generated by [AVA](https://avajs.dev). Output:␊ 1 | console.log("_", "log", "_")␊ ␊ - Error 1/1:␊ + Error 1/2:␊ + > 1 | console.log("_", " log ", "_")␊ + | ^ Do not use leading space between `console.log` parameters.␊ + Error 2/2:␊ > 1 | console.log("_", " log ", "_")␊ - | ^^^^^^^ Do not use leading and trailing space between `console.log` parameters.␊ + | ^ Do not use trailing space between `console.log` parameters.␊ ` ## no-console-spaces - #08 @@ -151,9 +160,12 @@ Generated by [AVA](https://avajs.dev). Output:␊ 1 | console.debug("_", "debug", "_")␊ ␊ - Error 1/1:␊ + Error 1/2:␊ > 1 | console.debug("_", " debug ", "_")␊ - | ^^^^^^^^^ Do not use leading and trailing space between `console.debug` parameters.␊ + | ^ Do not use leading space between `console.debug` parameters.␊ + Error 2/2:␊ + > 1 | console.debug("_", " debug ", "_")␊ + | ^ Do not use trailing space between `console.debug` parameters.␊ ` ## no-console-spaces - #09 @@ -167,9 +179,12 @@ Generated by [AVA](https://avajs.dev). Output:␊ 1 | console.info("_", "info", "_")␊ ␊ - Error 1/1:␊ + Error 1/2:␊ > 1 | console.info("_", " info ", "_")␊ - | ^^^^^^^^ Do not use leading and trailing space between `console.info` parameters.␊ + | ^ Do not use leading space between `console.info` parameters.␊ + Error 2/2:␊ + > 1 | console.info("_", " info ", "_")␊ + | ^ Do not use trailing space between `console.info` parameters.␊ ` ## no-console-spaces - #10 @@ -183,9 +198,12 @@ Generated by [AVA](https://avajs.dev). Output:␊ 1 | console.warn("_", "warn", "_")␊ ␊ - Error 1/1:␊ + Error 1/2:␊ > 1 | console.warn("_", " warn ", "_")␊ - | ^^^^^^^^ Do not use leading and trailing space between `console.warn` parameters.␊ + | ^ Do not use leading space between `console.warn` parameters.␊ + Error 2/2:␊ + > 1 | console.warn("_", " warn ", "_")␊ + | ^ Do not use trailing space between `console.warn` parameters.␊ ` ## no-console-spaces - #11 @@ -199,7 +217,10 @@ Generated by [AVA](https://avajs.dev). Output:␊ 1 | console.error("_", "error", "_")␊ ␊ - Error 1/1:␊ + Error 1/2:␊ + > 1 | console.error("_", " error ", "_")␊ + | ^ Do not use leading space between `console.error` parameters.␊ + Error 2/2:␊ > 1 | console.error("_", " error ", "_")␊ - | ^^^^^^^^^ Do not use leading and trailing space between `console.error` parameters.␊ + | ^ Do not use trailing space between `console.error` parameters.␊ ` diff --git a/test/snapshots/no-console-spaces.js.snap b/test/snapshots/no-console-spaces.js.snap index 850066d0883da40285c7871991a7d3e63c3ef507..8511e66ea8046752f0b62bf491889c7e8220ccaa 100644 GIT binary patch delta 836 zcmV-K1H1h02B8NcK~_N^Q*L2!b7*gLAa*he0s!a;2Gx>pA$&DASaWaJ1(1;&XZf{mjO|uzrVp;~A@t z^lVFxCxH*1&SM0N9^hbL_Kpn^$ zT#JhGixdp?jjXurh_*%n2x=6tfj9-1e1*LH5{1&@RE3<>#FWgubcN!A#Nf%f6yk{<1ihh_#DF)`~@ski`aemsyviIZsDR337BQukT$jm1t?(#{Ds{FwaGekIo%HiIOBivI` zlS<+40&@t3J+clkn{fE!u>`mGNr?rpnMB3|S&ktiKCrk6B}U3{#7JgdT0YEqb|5yP z_&`z*ViJxGoEG5pJ}FTFGLgubAl)IP#{#Bvh>C>r#G*Wy(?D!Ok$|Kg#3UREI4z(` zB!EmLG7?C42NYTkP&6Rx0J8~41RhIphX5&Yfu$>fGcw3>3>guE O#Z3UxaE$bd5dZ-DFn{R) delta 785 zcmV+s1Md8x2kr(UK~_N^Q*L2!b7*gLAa*he0szWmqo27AZhx7v{+c}X;%oni1JfA6qK7#c7)*N~+rJWS6XD%? zZpVYz;unly(W{&c43_2F0)J^u{*|F!emuA7{Ygf!=shk5hC>b;S4mkOdKR52vhHpL zcReFm^f@;JgUX!ta ztqx4!>I9=0zj3%t!x=|d475XcA5y@R61W&y3cK4-g7G5*o{}dXTp&PmHHaOrq=~oO z0c${4kH6T#Y6p%&2YZ@>l{#bIWr) zP>Xg_lLU%xa?%HO>j?)H&hm Date: Sat, 17 Oct 2020 11:38:17 +0800 Subject: [PATCH 2/5] Restore regex --- rules/no-console-spaces.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rules/no-console-spaces.js b/rules/no-console-spaces.js index 1ba26fa29c..37f0a24e32 100644 --- a/rules/no-console-spaces.js +++ b/rules/no-console-spaces.js @@ -23,10 +23,10 @@ const selector = methodSelector({ }); // Find exactly one leading space, allow exactly one space -const hasLeadingSpace = value => /^ [^ ]/.test(value); +const hasLeadingSpace = value => value.length > 1 && value.charAt(0) === ' ' && value.charAt(1) !== ' '; // Find exactly one trailing space, allow exactly one space -const hasTrailingSpace = value => /[^ ] $/.test(value); +const hasTrailingSpace = value => value.length > 1 && value.charAt(value.length - 1) === ' ' && value.charAt(value.length - 2) !== ' '; const create = context => { const sourceCode = context.getSourceCode(); From a69654d535e2a4333da85efa6ac49b8aa3b1dd6c Mon Sep 17 00:00:00 2001 From: fisker Date: Sat, 17 Oct 2020 11:39:01 +0800 Subject: [PATCH 3/5] xo --- rules/no-console-spaces.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/no-console-spaces.js b/rules/no-console-spaces.js index 37f0a24e32..dcf8b55d29 100644 --- a/rules/no-console-spaces.js +++ b/rules/no-console-spaces.js @@ -1,7 +1,6 @@ 'use strict'; const getDocumentationUrl = require('./utils/get-documentation-url'); const methodSelector = require('./utils/method-selector'); -const replaceStringRaw = require('./utils/replace-string-raw'); const MESSAGE_ID = 'no-console-spaces'; const messages = { @@ -54,6 +53,7 @@ const create = context => { fix: fixer => fixer.removeRange([start, end]) }); }; + return { [selector](node) { const method = node.callee.property.name; From 1c39dbfa53518353aa3ee1c4ff77caa15c01b330 Mon Sep 17 00:00:00 2001 From: fisker Date: Sat, 17 Oct 2020 11:42:38 +0800 Subject: [PATCH 4/5] Simplify range calculation --- rules/no-console-spaces.js | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/rules/no-console-spaces.js b/rules/no-console-spaces.js index dcf8b55d29..f0cb0bdd62 100644 --- a/rules/no-console-spaces.js +++ b/rules/no-console-spaces.js @@ -30,27 +30,18 @@ const hasTrailingSpace = value => value.length > 1 && value.charAt(value.length const create = context => { const sourceCode = context.getSourceCode(); const report = (node, method, position) => { - let start; - let end; - - if (position === 'leading') { - const [index] = node.range; - start = index + 1; - end = index + 2; - } else { - const [, index] = node.range; - start = index - 2; - end = index - 1; - } + const start = position === 'leading' ? + node.range[0] + 1 : + node.range[1] - 2; context.report({ loc: { start: sourceCode.getLocFromIndex(start), - end: sourceCode.getLocFromIndex(end) + end: sourceCode.getLocFromIndex(start + 1) }, messageId: MESSAGE_ID, data: {method, position}, - fix: fixer => fixer.removeRange([start, end]) + fix: fixer => fixer.removeRange([start, start + 1]) }); }; From a9865ab4535190139c42586a20d03fef08e91717 Mon Sep 17 00:00:00 2001 From: fisker Date: Sat, 17 Oct 2020 11:47:57 +0800 Subject: [PATCH 5/5] Rename `start` to `index` --- rules/no-console-spaces.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rules/no-console-spaces.js b/rules/no-console-spaces.js index f0cb0bdd62..579f1a1f6e 100644 --- a/rules/no-console-spaces.js +++ b/rules/no-console-spaces.js @@ -30,18 +30,18 @@ const hasTrailingSpace = value => value.length > 1 && value.charAt(value.length const create = context => { const sourceCode = context.getSourceCode(); const report = (node, method, position) => { - const start = position === 'leading' ? + const index = position === 'leading' ? node.range[0] + 1 : node.range[1] - 2; context.report({ loc: { - start: sourceCode.getLocFromIndex(start), - end: sourceCode.getLocFromIndex(start + 1) + start: sourceCode.getLocFromIndex(index), + end: sourceCode.getLocFromIndex(index + 1) }, messageId: MESSAGE_ID, data: {method, position}, - fix: fixer => fixer.removeRange([start, start + 1]) + fix: fixer => fixer.removeRange([index, index + 1]) }); };