Home / File/ transform-jsx.ts — ui Source File

transform-jsx.ts — ui Source File

Architecture documentation for transform-jsx.ts, a typescript file in the ui codebase. 5 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  b0b22f77_9304_6932_9684_47a29fa91040["transform-jsx.ts"]
  e69863ed_3e2f_ef94_648a_ef0155c386ef["transformers"]
  b0b22f77_9304_6932_9684_47a29fa91040 --> e69863ed_3e2f_ef94_648a_ef0155c386ef
  3574d2c0_f997_29ec_cf6e_83f0daeab126["core"]
  b0b22f77_9304_6932_9684_47a29fa91040 --> 3574d2c0_f997_29ec_cf6e_83f0daeab126
  222f3d9a_d7da_2ecb_70eb_7280696ec0f7["parser"]
  b0b22f77_9304_6932_9684_47a29fa91040 --> 222f3d9a_d7da_2ecb_70eb_7280696ec0f7
  09815072_34bf_ed9d_57ab_8d09c8afd02f["plugin-transform-typescript"]
  b0b22f77_9304_6932_9684_47a29fa91040 --> 09815072_34bf_ed9d_57ab_8d09c8afd02f
  f2974cb6_f296_9f14_d68f_efe1394447af["recast"]
  b0b22f77_9304_6932_9684_47a29fa91040 --> f2974cb6_f296_9f14_d68f_efe1394447af
  style b0b22f77_9304_6932_9684_47a29fa91040 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { type Transformer } from "@/src/utils/transformers"
import { transformFromAstSync } from "@babel/core"
import { ParserOptions, parse } from "@babel/parser"
// @ts-ignore
import transformTypescript from "@babel/plugin-transform-typescript"
import * as recast from "recast"

// TODO.
// I'm using recast for the AST here.
// Figure out if ts-morph AST is compatible with Babel.

// This is a copy of the babel options from recast/parser.
// The goal here is to tolerate as much syntax as possible.
// We want to be able to parse any valid tsx code.
// See https://github.com/benjamn/recast/blob/master/parsers/_babel_options.ts.
const PARSE_OPTIONS: ParserOptions = {
  sourceType: "module",
  allowImportExportEverywhere: true,
  allowReturnOutsideFunction: true,
  startLine: 1,
  tokens: true,
  plugins: [
    "asyncGenerators",
    "bigInt",
    "classPrivateMethods",
    "classPrivateProperties",
    "classProperties",
    "classStaticBlock",
    "decimal",
    "decorators-legacy",
    "doExpressions",
    "dynamicImport",
    "exportDefaultFrom",
    "exportNamespaceFrom",
    "functionBind",
    "functionSent",
    "importAssertions",
    "importMeta",
    "nullishCoalescingOperator",
    "numericSeparator",
    "objectRestSpread",
    "optionalCatchBinding",
    "optionalChaining",
    [
      "pipelineOperator",
      {
        proposal: "minimal",
      },
    ],
    [
      "recordAndTuple",
      {
        syntaxType: "hash",
      },
    ],
    "throwExpressions",
    "topLevelAwait",
    "v8intrinsic",
    "typescript",
    "jsx",
  ],
}

export const transformJsx: Transformer<string> = async ({
  sourceFile,
  config,
}) => {
  const output = sourceFile.getFullText()

  if (config.tsx) {
    return output
  }

  const ast = recast.parse(output, {
    parser: {
      parse: (code: string) => {
        return parse(code, PARSE_OPTIONS)
      },
    },
  })

  const result = transformFromAstSync(ast, output, {
    cloneInputAst: false,
    code: false,
    ast: true,
    plugins: [transformTypescript],
    configFile: false,
  })

  if (!result || !result.ast) {
    throw new Error("Failed to transform JSX")
  }

  return recast.print(result.ast).code
}

Subdomains

Functions

Dependencies

  • core
  • parser
  • plugin-transform-typescript
  • recast
  • transformers

Frequently Asked Questions

What does transform-jsx.ts do?
transform-jsx.ts is a source file in the ui codebase, written in typescript. It belongs to the FrameworkTooling domain, TemplateSync subdomain.
What functions are defined in transform-jsx.ts?
transform-jsx.ts defines 1 function(s): transformJsx.
What does transform-jsx.ts depend on?
transform-jsx.ts imports 5 module(s): core, parser, plugin-transform-typescript, recast, transformers.
Where is transform-jsx.ts in the architecture?
transform-jsx.ts is located at packages/shadcn/src/utils/transformers/transform-jsx.ts (domain: FrameworkTooling, subdomain: TemplateSync, directory: packages/shadcn/src/utils/transformers).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free