Repository: remix-run/react-router
File: packages/react-router-dev/typegen/generate.ts (TypeScript)

Code before:
  // no extension
  path = Path.join(Path.dirname(path), Pathe.filename(path));
  if (!path.startsWith("../")) path = "./" + path;

  return path + ".js";
}

function rootDirsPath(ctx: Context, typesPath: string): string {
  const rel = Path.relative(typesDirectory(ctx), typesPath);
  return Path.join(ctx.rootDirectory, rel);
}

function paramsType(path: string) {
  const params = Params.parse(path);
  return t.tsTypeLiteral(
    Object.entries(params).map(([param, isRequired]) => {
      const property = t.tsPropertySignature(
        t.stringLiteral(param),
        t.tsTypeAnnotation(t.tsStringKeyword())
      );
      property.optional = !isRequired;
      return property;
    })
  );
}

// https://github.com/remix-run/react-router/blob/7a7f4b11ca8b26889ad328ba0ee5a749b0c6939e/packages/react-router/lib/router/utils.ts#L894C1-L937C2
function explodeOptionalSegments(path: string): string[] {
  let segments = path.split("/");
  if (segments.length === 0) return [];

  let [first, ...rest] = segments;

  // Optional path segments are denoted by a trailing `?`
  let isOptional = first.endsWith("?");
  // Compute the corresponding required segment: `foo?` -> `foo`
  let required = first.replace(/\?$/, "");

  if (rest.length === 0) {
    // Interpret empty string as omitting an optional segment
    // `["one", "", "three"]` corresponds to omitting `:two` from `/one/:two?/three` -> `/one/three`
    return isOptional ? [required, ""] : [required];
  }

  let restExploded = explodeOptionalSegments(rest.join("/"));

  let result: string[] = [];

  // All child paths with the prefix.  Do this for all children before the
  // optional version for all children, so we get consistent ordering where the
  // parent optional aspect is preferred as required.  Otherwise, we can get

Review comment (refactor):
have to copy from `react-router`. is there some way to not duplicate this w/o pulling in `react-router` as a dependency? maybe list `react-router` as a peer dep

Code after:
  path = Path.join(Path.dirname(path), Pathe.filename(path));
  if (!path.startsWith("../")) path = "./" + path;

  return path + ".js";
}

function rootDirsPath(ctx: Context, typesPath: string): string {
  const rel = Path.relative(typesDirectory(ctx), typesPath);
  return Path.join(ctx.rootDirectory, rel);
}

function paramsType(path: string) {
  const params = Params.parse(path);
  return t.tsTypeLiteral(
    Object.entries(params).map(([param, isRequired]) => {
      const property = t.tsPropertySignature(
        t.stringLiteral(param),
        t.tsTypeAnnotation(t.tsStringKeyword())
      );
      property.optional = !isRequired;
      return property;
    })
  );
}

// https://github.com/remix-run/react-router/blob/7a7f4b11ca8b26889ad328ba0ee5a749b0c6939e/packages/react-router/lib/router/utils.ts#L894C1-L937C2
function explodeOptionalSegments(path: string): string[] {
  let segments = path.split("/");
  if (segments.length === 0) return [];

  let [first, ...rest] = segments;

  // Optional path segments are denoted by a trailing `?`
  let isOptional = first.endsWith("?");
  // Compute the corresponding required segment: `foo?` -> `foo`
  let required = first.replace(/\?$/, "");

  if (rest.length === 0) {
    // Interpret empty string as omitting an optional segment
    // `["one", "", "three"]` corresponds to omitting `:two` from `/one/:two?/three` -> `/one/three`
    return isOptional ? [required, ""] : [required];
  }

  let restExploded = explodeOptionalSegments(rest.join("/"));

  let result: string[] = [];

  // All child paths with the prefix.  Do this for all children before the
  // optional version for all children, so we get consistent ordering where the
  // parent optional aspect is preferred as required.  Otherwise, we can get
  // child sections interspersed where deeper optional segments are higher than