for personal reasons I want console.(log|error|…) always to be written in one line.
is there a eslint rule for that which is tested and works?
my approach below introduces some arbitrary syntax problems
module.exports = {
meta: {
fixable: 'code',
},
create(context) {
return {
CallExpression(node) {
if (
node.callee.object &&
node.callee.object.name === 'console' &&
node.callee.property
) {
const sourceCode = context.getSourceCode()
const text = sourceCode.getText(node)
// Check if there are comments inside the console statement
const comments = sourceCode.getCommentsInside(node)
if (text.includes('n')) {
if (comments.length > 0) {
// Raise a warning if there are comments inside
context.report({
node,
message:
'console.' +
node.callee.property.name +
' contains comments and should be checked manually',
})
} else {
// Fix the text if there are no comments
context.report({
node,
message:
'console.' +
node.callee.property.name +
' should be in one line',
fix(fixer) {
const fixedText = text.replace(/n/g, ' ').replace(/s+/g, ' ')
return fixer.replaceText(node, fixedText)
},
})
}
}
}
},
}
},
}