json-schema-to-typescript icon indicating copy to clipboard operation
json-schema-to-typescript copied to clipboard

Formatting seems to use non-standard Prettier options by default

Open robcresswell opened this issue 5 years ago • 3 comments

I'm seeing an issue where the formatting doesn't follow the standard Prettier rules, in this instance regarding line length.

I have an enum that looks something like this, as part of a larger schema:

"state": {
  "type": "string",
  "enum": [
    "processing",
    "success",
    "error",
    "cancelled",
    "failure",
    "skipped"
  ]
},

Everything builds fine, but the output is changed when we run Prettier. This is mildly annoying, because it means when you generate the types, they aren't correct-by-default.

The output here is something like:

state: 'processing' | 'success'  | 'error' | 'cancelled' | 'failure' | 'skipped';

In context, that goes over 80 characters (as its indented as part of the object), but isn't wrapped by json-schema-to-typescript. It does get wrapped when using Prettier from the CLI.

The call site for json-schema-to-typescript looks like:

const style = await loadJSON(
  path.join(__dirname, '..', '.prettierrc.json'),
);

const typedef = await compile(schema, typeFileName, {
  bannerComment,
  style,
  cwd: schemaDir,
  enableConstEnums: false,
});

Where loadJSON is some trivial utility to read a file and parse it. I edited our prettier config to have printWidth: 80, and that fixed the issue, causing the output to be formatted correctly.

This makes me think that something else is setting the default Prettier width, as it appears that when manually overriding it from the callsite, it went back to 80 as expected.

My suspicion is, because you are publishing your prettierrc as part of the package (https://unpkg.com/browse/[email protected]/) those options are getting picked up as the default.

robcresswell avatar Nov 30 '20 16:11 robcresswell

Hey @bcherny, any thoughts on this?

robcresswell avatar Jan 08 '21 18:01 robcresswell

Contributions welcome! I’d suggest starting with a good repro case.

bcherny avatar Jan 08 '21 18:01 bcherny

The Prettier default options can be used by providing --style '{ bracketSpacing: true, printWidth: 80, trailingComma: "es5" }' on the command line. It would indeed be nice if these were the default in this package as well 👍

ohueter avatar Mar 26 '21 12:03 ohueter