Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map @defaulValue back to @default in generation #609

Closed
nschonni opened this issue Jan 2, 2021 · 3 comments
Closed

Map @defaulValue back to @default in generation #609

nschonni opened this issue Jan 2, 2021 · 3 comments

Comments

@nschonni
Copy link

nschonni commented Jan 2, 2021

TSDoc uses/recommends @defaultValue instead of @default https://tsdoc.org/pages/tags/defaultvalue/
It would be good to map it back to the more common default key on generation

@domoritz
Copy link
Member

domoritz commented Jan 2, 2021

Can you send a pull request?

@arthurfiorette
Copy link
Collaborator

This is not in the TSDoc RFC yet and is only part of
https://tsdoc.org/pages/spec/standardization_groups/#extended-group

microsoft/tsdoc#27

@dimitropoulos
Copy link

to anyone else struggling to make TSDoc's @defaultValue play nice here, after trying a few things (a custom parser, modifying the value with TypeScript's compiler API, etc.) I came to a simpler solution - postprocess.

Here's an example script
#!/usr/bin/env node

import { writeFileSync } from "node:fs";
import { join } from "node:path";
import { createGenerator } from "ts-json-schema-generator";

const __dirname = new URL(".", import.meta.url).pathname;
const packageRoot = join(__dirname, "..", "src");

/**
 * post-process the schema recursively to:
 * 1. replace any key named `defaultValue` with `default`
 * 1. remove any backticks from the value
 * 1. attempt to parsing the value as JSON (falling back, if not)
 */
const postProcess = <T>(item: T): T => {
  if (typeof item !== "object" || item === null) {
    return item;
  }
  if (Array.isArray(item)) {
    return item.map(postProcess) as unknown as T;
  }
  return Object.fromEntries(
    Object.entries(item).map(([key, value]) => {
      if (key === "defaultValue" && typeof value === "string") {
        const replaced = value.replaceAll(/`/g, "");
        try {
          return ["default", JSON.parse(replaced)];
        } catch (e) {
          return ["default", replaced];
        }
      }
      return [key, postProcess(value)];
    })
  ) as T;
};

const create = (fileName: string, typeName: string) => {
  const generator = createGenerator({
    path: join(packageRoot, "index.ts"),
    tsconfig: join(__dirname, "../tsconfig.json"),
    type: "Schema",
    extraTags: ["defaultValue"],
  });
  const schema = postProcess(generator.createSchema(typeName));
  const filePath = join(__dirname, "..", "schemas", fileName);
  writeFileSync(filePath, JSON.stringify(schema, null, 2));
};

create("schema.v1.json", "SchemaV1");
create("schema.v2.json", "Schema");
create("schema.json", "Schema");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants