Skip to content

Instantly share code, notes, and snippets.

@patocallaghan
Created February 21, 2019 10:40
Show Gist options
  • Save patocallaghan/14f6df2b539d3cff5a7da37580082c95 to your computer and use it in GitHub Desktop.
Save patocallaghan/14f6df2b539d3cff5a7da37580082c95 to your computer and use it in GitHub Desktop.
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