Files
next.js/packages/next-codemod/transforms/name-default-component.ts
Arian Tron 61f56f997c
Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
first commit
2026-03-10 19:37:31 +03:30

105 lines
2.7 KiB
TypeScript

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 the generated name looks off, don't add a name
if (!isValidIdentifier(name)) {
return
}
// Add `Component` to the end of the name if an identifier with the
// same name already exists
while (root.find(j.Identifier, { name }).some(hasRootAsParent)) {
// If the name is still duplicated then don't add a name
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 {
// Anonymous Function
;(node.declaration as FunctionDeclaration).id = j.identifier(name)
}
}
root.find(j.ExportDefaultDeclaration).forEach(nameFunctionComponent)
return hasModifications ? root.toSource(options) : null
}