diff --git a/rules/no-console-spaces.js b/rules/no-console-spaces.js index 9f2f1d0043..579f1a1f6e 100644 --- a/rules/no-console-spaces.js +++ b/rules/no-console-spaces.js @@ -1,11 +1,10 @@ '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 = { - [MESSAGE_ID]: 'Do not use {{positions}} space between `console.{{method}}` parameters.' + [MESSAGE_ID]: 'Do not use {{position}} space between `console.{{method}}` parameters.' }; const methods = [ @@ -30,53 +29,45 @@ const hasTrailingSpace = value => value.length > 1 && value.charAt(value.length const create = context => { const sourceCode = context.getSourceCode(); + const report = (node, method, position) => { + const index = position === 'leading' ? + node.range[0] + 1 : + node.range[1] - 2; - const fixParamter = (node, index, parameters) => { - if ( - !(node.type === 'Literal' && typeof node.value === 'string') && - node.type !== 'TemplateLiteral' - ) { - return; - } - - 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(index), + end: sourceCode.getLocFromIndex(index + 1) + }, + messageId: MESSAGE_ID, + data: {method, position}, + fix: fixer => fixer.removeRange([index, index + 1]) + }); }; 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 850066d088..8511e66ea8 100644 Binary files a/test/snapshots/no-console-spaces.js.snap and b/test/snapshots/no-console-spaces.js.snap differ