Created
February 21, 2019 10:40
-
-
Save patocallaghan/14f6df2b539d3cff5a7da37580082c95 to your computer and use it in GitHub Desktop.
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 ESLINT_RULE_NAME = 'intercom/no-mockjax'; | |
const AST_TYPE = 'MemberExpression'; | |
function shouldAddDisableComment(path) { | |
return path.object && path.object.name === '$' && path.property.name === 'mockjax'; | |
} | |
function closestAncestorOfType(startPath, typeRegex) { | |
let current = startPath.parentPath; | |
while (current.parentPath && !typeRegex.test(current.value.type)) { | |
current = current.parentPath; | |
} | |
return current || null; | |
} | |
export default function transformer(file, api) { | |
let j = api.jscodeshift; | |
let comment = j.commentLine(` eslint-disable-next-line ${ESLINT_RULE_NAME}`, true, false); | |
return j(file.source) | |
.find(j[AST_TYPE], path => { | |
return shouldAddDisableComment(path); | |
}) | |
.forEach(path => { | |
let ancestor = closestAncestorOfType(path, /VariableDeclaration|Statement/); | |
ancestor.node.comments = ancestor.node.comments || []; | |
ancestor.node.comments.push(comment); | |
}) | |
.toSource(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment