Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no-console-spaces: Improve report location #866

Merged
merged 5 commits into from Oct 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
84 changes: 42 additions & 42 deletions rules/no-console-spaces.js
Expand Up @@ -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 = [
Expand All @@ -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);
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved

// 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');
}
}
}
};
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.