Skip to content

Instantly share code, notes, and snippets.

@hjzheng
Created February 1, 2019 09:08
Show Gist options
  • Save hjzheng/1dafb4b488fa17d77bbac431d25d53f8 to your computer and use it in GitHub Desktop.
Save hjzheng/1dafb4b488fa17d77bbac431d25d53f8 to your computer and use it in GitHub Desktop.
looksLike
const disallowedMethods = ['log', 'info', 'warn', 'error', 'dir']
module.exports = {
create(context) {
return {
Identifier(node) {
if (
!looksLike(node, {
name: 'console',
parent: {
type: 'MemberExpression',
parent: {type: 'CallExpression'},
property: {
name: val => disallowedMethods.includes(val),
},
},
})
) {
return
}
context.report({
node: node.parent.property,
message: 'Using console is not allowed',
})
},
}
},
}
function looksLike(a, b) {
debugger;
return (
a &&
b &&
Object.keys(b).every(bKey => {
const bVal = b[bKey]
const aVal = a[bKey]
if (typeof bVal === 'function') {
return bVal(aVal)
}
return isPrimitive(bVal) ? bVal === aVal : looksLike(aVal, bVal)
})
)
}
function isPrimitive(val) {
return val == null || /^[sbn]/.test(typeof val)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment