| import type { |
| API, |
| ArrowFunctionExpression, |
| ASTPath, |
| ExportDefaultDeclaration, |
| FileInfo, |
| FunctionDeclaration, |
| Options, |
| } from 'jscodeshift' |
| import { basename, extname } from 'path' |
| import { createParserFromPath } from '../lib/parser' |
|
|
| const camelCase = (value: string): string => { |
| const val = value.replace(/[-_\s.]+(.)?/g, (_match, chr) => |
| chr ? chr.toUpperCase() : '' |
| ) |
| return val.slice(0, 1).toUpperCase() + val.slice(1) |
| } |
|
|
| const isValidIdentifier = (value: string): boolean => |
| /^[a-zA-Z脌-每][0-9a-zA-Z脌-每]+$/.test(value) |
|
|
| export default function transformer( |
| file: FileInfo, |
| _api: API, |
| options: Options |
| ) { |
| const j = createParserFromPath(file.path) |
| const root = j(file.source) |
|
|
| let hasModifications: boolean |
|
|
| const returnsJSX = (node): boolean => |
| node.type === 'JSXElement' || |
| (node.type === 'BlockStatement' && |
| j(node) |
| .find(j.ReturnStatement) |
| .some((path) => path.value.argument?.type === 'JSXElement')) |
|
|
| const hasRootAsParent = (path): boolean => { |
| const program = path.parentPath.parentPath.parentPath.parentPath.parentPath |
| return !program || program?.value?.type === 'Program' |
| } |
|
|
| const nameFunctionComponent = ( |
| path: ASTPath<ExportDefaultDeclaration> |
| ): void => { |
| const node = path.value |
|
|
| if (!node.declaration) { |
| return |
| } |
|
|
| const isArrowFunction = |
| node.declaration.type === 'ArrowFunctionExpression' && |
| returnsJSX(node.declaration.body) |
| const isAnonymousFunction = |
| node.declaration.type === 'FunctionDeclaration' && !node.declaration.id |
|
|
| if (!(isArrowFunction || isAnonymousFunction)) { |
| return |
| } |
|
|
| const fileName = basename(file.path, extname(file.path)) |
| let name = camelCase(fileName) |
|
|
| |
| if (!isValidIdentifier(name)) { |
| return |
| } |
|
|
| |
| |
| while (root.find(j.Identifier, { name }).some(hasRootAsParent)) { |
| |
| if (name.endsWith('Component')) { |
| return |
| } |
| name += 'Component' |
| } |
|
|
| hasModifications = true |
|
|
| if (isArrowFunction) { |
| path.insertBefore( |
| j.variableDeclaration('const', [ |
| j.variableDeclarator( |
| j.identifier(name), |
| node.declaration as ArrowFunctionExpression |
| ), |
| ]) |
| ) |
|
|
| node.declaration = j.identifier(name) |
| } else { |
| |
| ;(node.declaration as FunctionDeclaration).id = j.identifier(name) |
| } |
| } |
|
|
| root.find(j.ExportDefaultDeclaration).forEach(nameFunctionComponent) |
|
|
| return hasModifications ? root.toSource(options) : null |
| } |
|
|