Skip to content

Commit

Permalink
no-console-spaces: Make space position more specific (#838)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Sep 29, 2020
1 parent 2105707 commit e17a63f
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 47 deletions.
29 changes: 13 additions & 16 deletions rules/no-console-spaces.js
Expand Up @@ -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 = [
Expand All @@ -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();
Expand All @@ -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
};
Expand All @@ -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)
});
}
Expand Down
63 changes: 34 additions & 29 deletions test/no-console-spaces.js
Expand Up @@ -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, {
Expand Down Expand Up @@ -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");'
},
Expand All @@ -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(
Expand All @@ -197,7 +187,7 @@ ruleTester.run('no-console-spaces', rule, {
);
`,
errors: [
buildError({method: 'error'})
buildError({method: 'error', positions: 'trailing'})
],
output: outdent`
console.error(
Expand All @@ -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 ", "_")'
]);
89 changes: 87 additions & 2 deletions test/snapshots/no-console-spaces.js.md
Expand Up @@ -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
Expand All @@ -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.␊
`
Binary file modified test/snapshots/no-console-spaces.js.snap
Binary file not shown.

0 comments on commit e17a63f

Please sign in to comment.