Skip to content

Commit

Permalink
no-console-spaces: Improve report location (#866)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Oct 17, 2020
1 parent 3cdc5ec commit 190cbac
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 77 deletions.
75 changes: 33 additions & 42 deletions 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 = [
Expand All @@ -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');
}
}
}
};
Expand Down
37 changes: 20 additions & 17 deletions 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}
};
}

Expand Down Expand Up @@ -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");'
},
Expand All @@ -158,7 +161,7 @@ test({
);
`,
errors: [
buildError({method: 'log', positions: 'trailing'})
buildError({method: 'log', position: 'trailing'})
],
output: outdent`
console.log(
Expand All @@ -178,7 +181,7 @@ test({
);
`,
errors: [
buildError({method: 'error', positions: 'trailing'})
buildError({method: 'error', position: 'trailing'})
],
output: outdent`
console.error(
Expand Down
57 changes: 39 additions & 18 deletions test/snapshots/no-console-spaces.js.md
Expand Up @@ -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
Expand All @@ -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 | );␊
`
Expand Down Expand Up @@ -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 | );␊
`
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.␊
`
Binary file modified test/snapshots/no-console-spaces.js.snap
Binary file not shown.

0 comments on commit 190cbac

Please sign in to comment.