-
-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b44534b
commit 5dd529f
Showing
5 changed files
with
325 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Do not use leading/trailing space between `console.log` parameters | ||
|
||
The [`console.log()` method](https://developer.mozilla.org/en-US/docs/Web/API/Console/log) and similar methods joins the parameters with a space, so adding a leading/trailing space to a parameter, results in two spaces being added. | ||
|
||
|
||
## Fail | ||
|
||
```js | ||
console.log('abc ', 'def'); | ||
console.log('abc', ' def'); | ||
|
||
console.log("abc ", " def"); | ||
console.log(`abc `, ` def`); | ||
|
||
console.debug('abc ', 'def'); | ||
console.info('abc ', 'def'); | ||
console.warn('abc ', 'def'); | ||
console.error('abc ', 'def'); | ||
``` | ||
|
||
|
||
## Pass | ||
|
||
```js | ||
console.log('abc'); | ||
console.log('abc', 'def'); | ||
|
||
console.log('abc '); | ||
console.log(' abc'); | ||
|
||
console.log('abc ', 'def'); | ||
console.log('abc\t', 'def'); | ||
console.log('abc\n', 'def'); | ||
|
||
console.log(` | ||
abc | ||
`); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
'use strict'; | ||
const getDocsUrl = require('./utils/get-docs-url'); | ||
|
||
const getConsoleMethod = node => { | ||
const methods = [ | ||
'log', | ||
'debug', | ||
'info', | ||
'warn', | ||
'error' | ||
]; | ||
|
||
const {callee} = node; | ||
|
||
if ( | ||
callee.type === 'MemberExpression' && | ||
callee.object.type === 'Identifier' && | ||
callee.object.name === 'console' && | ||
callee.property.type === 'Identifier' && | ||
methods.includes(callee.property.name) | ||
) { | ||
return callee.property.name; | ||
} | ||
}; | ||
|
||
const getArgumentValue = (context, nodeArgument) => { | ||
let value = null; | ||
|
||
if (nodeArgument.type === 'Literal' && typeof nodeArgument.value === 'string') { | ||
value = nodeArgument.value; | ||
} | ||
|
||
if (nodeArgument.type === 'TemplateLiteral') { | ||
const sourceCode = context.getSourceCode(); | ||
value = sourceCode.getText(nodeArgument); | ||
// Strip off backticks | ||
value = value.substring(1, value.length - 1); | ||
} | ||
|
||
return value; | ||
}; | ||
|
||
const fixValue = (value, { | ||
fixLeading = true, | ||
fixTrailing = true | ||
}) => { | ||
if (!value) { | ||
return value; | ||
} | ||
|
||
// Allow exactly one space | ||
if (value.length <= 1) { | ||
return value; | ||
} | ||
|
||
let fixed = value; | ||
|
||
// Find exactly one leading space | ||
if (fixLeading && fixed.startsWith(' ') && !fixed.startsWith(' ')) { | ||
fixed = fixed.slice(1); | ||
} | ||
|
||
// Find exactly one trailing space | ||
if (fixTrailing && fixed.endsWith(' ') && !fixed.endsWith(' ')) { | ||
fixed = fixed.slice(0, -1); | ||
} | ||
|
||
return fixed; | ||
}; | ||
|
||
const getFixableArguments = (context, node) => { | ||
const { | ||
arguments: args | ||
} = node; | ||
|
||
const fixables = args.map((nodeArgument, i) => { | ||
const fixLeading = i !== 0; | ||
const fixTrailing = i !== (args.length - 1); | ||
|
||
const value = getArgumentValue(context, nodeArgument); | ||
const fixed = fixValue(value, {fixLeading, fixTrailing}); | ||
|
||
return { | ||
nodeArgument, | ||
value, | ||
fixed, | ||
fixable: value !== fixed | ||
}; | ||
}); | ||
|
||
return fixables.filter(fixable => fixable.fixable); | ||
}; | ||
|
||
const fixArg = (context, fixable, fixer) => { | ||
const { | ||
nodeArgument, | ||
fixed | ||
} = fixable; | ||
|
||
// Ignore quotes and backticks | ||
const range = [ | ||
nodeArgument.range[0] + 1, | ||
nodeArgument.range[1] - 1 | ||
]; | ||
|
||
return fixer.replaceTextRange(range, fixed); | ||
}; | ||
|
||
const buildErrorMessage = method => { | ||
return `Do not use leading/trailing space between \`console.${method}\` parameters.`; | ||
}; | ||
|
||
const create = context => { | ||
return { | ||
CallExpression(node) { | ||
const method = getConsoleMethod(node); | ||
if (!method) { | ||
return; | ||
} | ||
|
||
const fixables = getFixableArguments(context, node); | ||
for (const fixable of fixables) { | ||
context.report({ | ||
node: fixable.nodeArgument, | ||
message: buildErrorMessage(method), | ||
fix: fixer => fixArg(context, fixable, fixer) | ||
}); | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
docs: { | ||
url: getDocsUrl(__filename) | ||
}, | ||
fixable: 'code' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import test from 'ava'; | ||
import avaRuleTester from 'eslint-ava-rule-tester'; | ||
import rule from '../rules/no-console-spaces'; | ||
|
||
const ruleTester = avaRuleTester(test, { | ||
parserOptions: { | ||
ecmaVersion: 2016 | ||
} | ||
}); | ||
|
||
function buildError({method, column, line}) { | ||
const error = { | ||
ruleId: 'no-console-spaces', | ||
message: `Do not use leading/trailing space between \`console.${method}\` parameters.` | ||
}; | ||
|
||
if (column) { | ||
error.column = column; | ||
} | ||
|
||
if (line) { | ||
error.line = line; | ||
} | ||
|
||
return error; | ||
} | ||
|
||
ruleTester.run('no-console-spaces', rule, { | ||
valid: [ | ||
'console.log("abc");', | ||
'console.log("abc", "def");', | ||
'console.log(\'abc\', "def");', | ||
'console.log(`abc`, "def");', | ||
'console.log("abc", "def");', | ||
'console.log(`\nabc\ndef\n`);', | ||
|
||
'console.log(\' \', "def");', | ||
'console.log(\' \', "def");', | ||
'console.log("abc ", "def");', | ||
'console.log("abc\\t", "def");', | ||
'console.log("abc\\n", "def");', | ||
'console.log(" abc", "def");', | ||
|
||
'console.log(" abc", "def");', | ||
'console.log("abc", "def ");', | ||
|
||
'console.log();', | ||
'console.log("");', | ||
'console.log(123);', | ||
'console.log(null);', | ||
'console.log(undefined);', | ||
|
||
'console.dir("abc ");' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'console.log("abc ", "def");', | ||
errors: [buildError({method: 'log'})], | ||
output: 'console.log("abc", "def");' | ||
}, | ||
{ | ||
code: 'console.log("abc", " def");', | ||
errors: [buildError({method: 'log'})], | ||
output: 'console.log("abc", "def");' | ||
}, | ||
{ | ||
code: 'console.log(" abc ", "def");', | ||
errors: [buildError({method: 'log'})], | ||
output: 'console.log(" abc", "def");' | ||
}, | ||
{ | ||
code: 'console.debug("abc ", "def");', | ||
errors: [buildError({method: 'debug'})], | ||
output: 'console.debug("abc", "def");' | ||
}, | ||
{ | ||
code: 'console.info("abc ", "def");', | ||
errors: [buildError({method: 'info'})], | ||
output: 'console.info("abc", "def");' | ||
}, | ||
{ | ||
code: 'console.warn("abc ", "def");', | ||
errors: [buildError({method: 'warn'})], | ||
output: 'console.warn("abc", "def");' | ||
}, | ||
{ | ||
code: 'console.error("abc ", "def");', | ||
errors: [buildError({method: 'error'})], | ||
output: 'console.error("abc", "def");' | ||
}, | ||
{ | ||
code: 'console.log("abc", " def ", "ghi");', | ||
errors: [buildError({method: 'log'})], | ||
output: 'console.log("abc", "def", "ghi");' | ||
}, | ||
{ | ||
code: 'console.log("abc ", "def ", "ghi");', | ||
errors: [ | ||
buildError({method: 'log', column: 13}), | ||
buildError({method: 'log', column: 21}) | ||
], | ||
output: 'console.log("abc", "def", "ghi");' | ||
}, | ||
{ | ||
code: 'console.log(\'abc \', "def");', | ||
errors: [buildError({method: 'log'})], | ||
output: 'console.log(\'abc\', "def");' | ||
}, | ||
{ | ||
code: 'console.log(`abc `, "def");', | ||
errors: [buildError({method: 'log'})], | ||
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'})], | ||
// eslint-disable-next-line no-template-curly-in-string | ||
output: 'console.log(`abc ${1 + 2}`, "def");' | ||
}, | ||
{ | ||
code: ` | ||
console.log( | ||
'abc', | ||
'def ', | ||
'ghi' | ||
); | ||
`, | ||
errors: [ | ||
buildError({method: 'log', column: 6, line: 4}) | ||
], | ||
output: ` | ||
console.log( | ||
'abc', | ||
'def', | ||
'ghi' | ||
); | ||
` | ||
} | ||
] | ||
}); |