MrLeebo / prisma-ast

Abstract Syntax Tree for parsing schema.prisma files
MIT License
140 stars 18 forks source link

Trailing comments not handled properly #7

Closed thecodeboss closed 2 years ago

thecodeboss commented 2 years ago

First off, this library is awesome, thanks for creating it! It's been really useful for customizing our Prisma schema formatting beyond what prisma format can do. I did run into this edge case with trailing comments though, I wonder how hard it would be to fix.

Summary

Trailing comments at the end of a line get forced onto the next line when parsing and then printing a schema.

Current behavior

The following code:

import { getSchema, printSchema } from '@mrleebo/prisma-ast';

const source = `
enum Role {
  ADMIN
  OWNER // similar to ADMIN, but can delete the project
  MEMBER
  USER // deprecated
}
`;

const schema = getSchema(source);
const output = printSchema(schema);
console.log(output);

outputs:

enum Role {
  ADMIN
  OWNER
  // similar to ADMIN, but can delete the project
  MEMBER
  USER
  // deprecated
}

Expected behavior

I would have expected that code to output one of these:

// Option 1: preserve trailing comments at end of line
enum Role {
  ADMIN
  OWNER // similar to ADMIN, but can delete the project
  MEMBER
  USER // deprecated
}

// Option 2: move trailing comments to the preceding line
enum Role {
  ADMIN
  // similar to ADMIN, but can delete the project
  OWNER
  MEMBER
  // deprecated
  USER
}
MrLeebo commented 2 years ago

@thecodeboss Good suggestion. Can you try the latest version (v0.3.1) and see if that fixes it for you?

thecodeboss commented 2 years ago

@MrLeebo it works great, thanks for such a fast turnaround on that! 🎉