Skip to content

Instantly share code, notes, and snippets.

@KevinTCoughlin
Forked from jdhuntington/renameProp.ts
Created April 11, 2019 03:21
Show Gist options
  • Save KevinTCoughlin/cb72a94a5b1ec084c0ec774c29944915 to your computer and use it in GitHub Desktop.
Save KevinTCoughlin/cb72a94a5b1ec084c0ec774c29944915 to your computer and use it in GitHub Desktop.
rename a prop for OUFR
import ts from 'typescript';
import { migration } from '../../migration';
import { mod } from 'riceburn';
const inFooJsxTag = (node: ts.Node): boolean => {
if (!node) {
return false;
}
if (ts.isJsxSelfClosingElement(node) || ts.isJsxOpeningElement(node)) {
const tagName = node.tagName;
if (ts.isIdentifier(tagName)) {
return tagName.escapedText === 'Foo';
} else {
return false;
}
}
if (!node.parent) {
return false;
}
return inFooJsxTag(node.parent);
};
export default migration('rename prop bar to prop baz inside component foo', () => {
mod('**/*.tsx').asTypescript((node, modder) => {
if (ts.isIdentifier(node) && ts.isJsxAttribute(node.parent) && inFooJsxTag(node)) {
if (node.escapedText === 'isSelected') {
return modder.replace(node, 'selected');
}
}
return undefined;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment