Skip to content

Commit

Permalink
refactor: add types to name-default-components.ts (#21291)
Browse files Browse the repository at this point in the history
- Add.type to variables and function, arguments
  • Loading branch information
tarunama committed Jan 26, 2021
1 parent 00d453d commit b40fd6c
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions 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
Expand Down

0 comments on commit b40fd6c

Please sign in to comment.