Created
February 1, 2019 09:08
-
-
Save hjzheng/1dafb4b488fa17d77bbac431d25d53f8 to your computer and use it in GitHub Desktop.
looksLike
This file contains hidden or 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
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