Skip to content

Commit

Permalink
refactor for compatibility with eslint v7.7.0+
Browse files Browse the repository at this point in the history
  • Loading branch information
Pascal Fong Kye committed Aug 29, 2020
1 parent 7174219 commit 0b0c734
Showing 1 changed file with 65 additions and 63 deletions.
128 changes: 65 additions & 63 deletions scripts/eslint-rules/no-production-logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,75 +9,77 @@

'use strict';

module.exports = function(context) {
function isInDEVBlock(node) {
let done = false;
while (!done) {
let parent = node.parent;
if (!parent) {
return false;
}
if (
parent.type === 'IfStatement' &&
node === parent.consequent &&
parent.test.type === 'Identifier' &&
// This is intentionally strict so we can
// see blocks of DEV-only code at once.
parent.test.name === '__DEV__'
) {
return true;
module.exports = {
meta: {
fixable: 'code',
},
create: function(context) {
function isInDEVBlock(node) {
let done = false;
while (!done) {
let parent = node.parent;
if (!parent) {
return false;
}
if (
parent.type === 'IfStatement' &&
node === parent.consequent &&
parent.test.type === 'Identifier' &&
// This is intentionally strict so we can
// see blocks of DEV-only code at once.
parent.test.name === '__DEV__'
) {
return true;
}
node = parent;
}
node = parent;
}
}

function reportWrapInDEV(node) {
context.report({
node: node,
message: `Wrap console.{{identifier}}() in an "if (__DEV__) {}" check`,
data: {
identifier: node.property.name,
},
fix: function(fixer) {
return [
fixer.insertTextBefore(node.parent, `if (__DEV__) {`),
fixer.insertTextAfter(node.parent, '}'),
];
},
});
}
function reportWrapInDEV(node) {
context.report({
node: node,
message: `Wrap console.{{identifier}}() in an "if (__DEV__) {}" check`,
data: {
identifier: node.property.name,
},
fix: function(fixer) {
return [
fixer.insertTextBefore(node.parent, `if (__DEV__) {`),
fixer.insertTextAfter(node.parent, '}'),
];
},
});
}

function reportUnexpectedConsole(node) {
context.report({
node: node,
message: `Unexpected use of console`,
});
}
function reportUnexpectedConsole(node) {
context.report({
node: node,
message: `Unexpected use of console`,
});
}

return {
meta: {
fixable: 'code',
},
MemberExpression: function(node) {
if (
node.object.type === 'Identifier' &&
node.object.name === 'console' &&
node.property.type === 'Identifier'
) {
switch (node.property.name) {
case 'error':
case 'warn': {
if (!isInDEVBlock(node)) {
reportWrapInDEV(node);
return {
MemberExpression: function(node) {
if (
node.object.type === 'Identifier' &&
node.object.name === 'console' &&
node.property.type === 'Identifier'
) {
switch (node.property.name) {
case 'error':
case 'warn': {
if (!isInDEVBlock(node)) {
reportWrapInDEV(node);
}
break;
}
default: {
reportUnexpectedConsole(node);
break;
}
break;
}
default: {
reportUnexpectedConsole(node);
break;
}
}
}
},
};
},
};
},
};

0 comments on commit 0b0c734

Please sign in to comment.