diff --git a/packages/next-codemod/transforms/name-default-component.ts b/packages/next-codemod/transforms/name-default-component.ts index 745de4aaf4f10d7..bcbf7c34202a5b1 100644 --- a/packages/next-codemod/transforms/name-default-component.ts +++ b/packages/next-codemod/transforms/name-default-component.ts @@ -1,39 +1,44 @@ import { basename, extname } from 'path' -const camelCase = (value) => { +const camelCase = (value: string): string => { const val = value.replace(/[-_\s.]+(.)?/g, (_match, chr) => chr ? chr.toUpperCase() : '' ) return val.substr(0, 1).toUpperCase() + val.substr(1) } -const isValidIdentifier = (value) => /^[a-zA-ZÀ-ÿ][0-9a-zA-ZÀ-ÿ]+$/.test(value) +const isValidIdentifier = (value: string): boolean => + /^[a-zA-ZÀ-ÿ][0-9a-zA-ZÀ-ÿ]+$/.test(value) + +type Node = { + type: string + declaration: { + type: string + body: any + id?: any + } +} export default function transformer(file, api, options) { const j = api.jscodeshift const root = j(file.source) - let hasModifications + let hasModifications: boolean - const returnsJSX = (node) => + const returnsJSX = (node): boolean => node.type === 'JSXElement' || (node.type === 'BlockStatement' && j(node) .find(j.ReturnStatement) - .some( - (path) => - path.value.argument && path.value.argument.type === 'JSXElement' - )) + .some((path) => path.value.argument?.type === 'JSXElement')) - const hasRootAsParent = (path) => { + const hasRootAsParent = (path): boolean => { const program = path.parentPath.parentPath.parentPath.parentPath.parentPath - return ( - !program || (program && program.value && program.value.type === 'Program') - ) + return !program || program?.value?.type === 'Program' } - const nameFunctionComponent = (path) => { - const node = path.value + const nameFunctionComponent = (path): void => { + const node: Node = path.value if (!node.declaration) { return