-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a42bcf8
commit 25fbd5a
Showing
2 changed files
with
120 additions
and
75 deletions.
There are no files selected for viewing
This file contains 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
99 changes: 99 additions & 0 deletions
99
packages/eslint-plugin-pf-codemods/src/rules/helpers/renameSinglePropOnNode.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { JSXAttribute, JSXIdentifier, JSXOpeningElement } from 'estree-jsx'; | ||
import { RenameConfig } from './renamePropsOnNode'; | ||
import { Rule } from 'eslint'; | ||
|
||
function isRenameConfig(obj: any): obj is RenameConfig { | ||
const hasValidObjectType = typeof obj === 'object' && obj !== null; | ||
|
||
const hasValidNewNameType = typeof obj.newName === 'string'; | ||
|
||
const isValidMessage = | ||
obj.message === undefined || | ||
typeof obj.message === 'string' || | ||
typeof obj.message === 'function'; | ||
|
||
const isValidReplace = | ||
obj.replace === undefined || typeof obj.replace === 'boolean'; | ||
|
||
return ( | ||
hasValidObjectType && | ||
hasValidNewNameType && | ||
isValidMessage && | ||
isValidReplace | ||
); | ||
} | ||
|
||
const getNewName = ( | ||
removed: boolean, | ||
propRename: RenameConfig | string | Record<PropertyKey, never> | ||
) => { | ||
if (removed) return ''; | ||
return isRenameConfig(propRename) ? propRename.newName : propRename; | ||
}; | ||
|
||
const getAction = ( | ||
removed: boolean, | ||
propRename: RenameConfig | string | Record<PropertyKey, never> | ||
) => { | ||
if (removed) return 'removed'; | ||
return propRename.replace ? 'replaced with ' : 'renamed to '; | ||
}; | ||
|
||
const getMessage = ( | ||
node: JSXOpeningElement, | ||
propRename: RenameConfig | string | Record<PropertyKey, never>, | ||
defaultMessage: string | ||
) => { | ||
if (!(isRenameConfig(propRename) && propRename.message)) | ||
return defaultMessage; | ||
|
||
return propRename.message instanceof Function | ||
? propRename.message(node) | ||
: propRename.message; | ||
}; | ||
|
||
export function renameSinglePropOnNode( | ||
context: Rule.RuleContext, | ||
attribute: JSXAttribute, | ||
node: JSXOpeningElement, | ||
propRename: RenameConfig | string | Record<PropertyKey, never> | ||
) { | ||
if (attribute.name.type !== 'JSXIdentifier') return; | ||
|
||
const isConfig = isRenameConfig(propRename); | ||
|
||
const isRemoved = !( | ||
(isConfig && propRename.newName !== '') || | ||
(typeof propRename === 'string' && propRename !== '') | ||
); | ||
|
||
const action = getAction(isRemoved, propRename); | ||
|
||
const newName = getNewName(isRemoved, propRename); | ||
|
||
const defaultMessage = `${attribute.name.name} prop for ${ | ||
(node.name as JSXIdentifier).name | ||
} has been ${action}${newName}`; | ||
const message = getMessage(node, propRename, defaultMessage); | ||
|
||
if (!isRemoved) { | ||
context.report({ | ||
node, | ||
message, | ||
fix(fixer) { | ||
return fixer.replaceText( | ||
propRename.replace ? attribute : (attribute as JSXAttribute).name, | ||
newName as string | ||
); | ||
}, | ||
}); | ||
} else { | ||
context.report({ | ||
node, | ||
message, | ||
fix(fixer) { | ||
return fixer.replaceText(attribute, ''); | ||
}, | ||
}); | ||
} | ||
} |