Giter Club home page Giter Club logo

Comments (3)

iagurban avatar iagurban commented on May 12, 2024

Workaround is not very big, heh

const breakComment = '//<[LINE BREAK]>';

const replaceNLComments = <T extends { type: `comment`; text: string } | { type: string; text?: never }>(
  p: T
) => (p.type === `comment` && p.text === breakComment ? ({ type: `break` } as const) : p);

const replaceAllNLComments = (schema: Schema) => {
  schema.list = schema.list.map(replaceNLComments);
  for (const block of schema.list) {
    switch (block.type) {
      case `model`:
      case `view`: {
        block.properties = block.properties.map(replaceNLComments);
        break;
      }
      case `datasource`:
      case `generator`: {
        block.assignments = block.assignments.map(replaceNLComments);
        break;
      }
      case `enum`: {
        block.enumerators = block.enumerators.map(replaceNLComments);
        break;
      }
    }
  }

  return schema;
};

const getSchemaWithBreaks = (source: string) =>
  replaceAllNLComments(
    getSchema(
      source
        .split(`\n`)
        .map(s => (s.trim().length ? s : breakComment))
        .join(`\n`)
    )
  );

from prisma-ast.

MrLeebo avatar MrLeebo commented on May 12, 2024

@iagurban can you describe what you're trying to accomplish? If you're trying to do some kind of linter / IDE plugin, you can enable nodeTrackingLocation to get the full position of each of the parsed nodes.

The default AST only adds { type: "break" } for double spaced breaks. Single breaks are implicit, because the formatter is going to only render one field or property per line anyway and for most usages, people are going to ignore the line breaks. E.g.

      model User {
        id    Int     @id @default(autoincrement())
        email String  @unique
        name  String?
        role  Role    @default(MEMBER)

        createdAt DateTime @default(now())
        updatedAt DateTime @default(now())
      }    

the user model properties are going to appear as:

[
  { type: "field", name: "id" },
  { type: "field", name: "email" },
  { type: "field", name: "name" },
  { type: "field", name: "role" },
  { type: "break" },
  { type: "field", name: "createdAt" },
  { type: "field", name: "updatedAt" },
]

In terms of formatting, the line break before createdAt is significant, because that resets the sorting / indenting rules, but the other breaks don't really matter since the formatter is going overwrite those anyway.

If you'd like to see an option to include ALL break nodes, I'm not opposed to seeing a PR to add it as an option, but I'm not sure that it should be part of the default config, because most users are going to use this library to manipulate their schemas and then auto-format, so the individual break nodes are going to get replaced anyway.

from prisma-ast.

MrLeebo avatar MrLeebo commented on May 12, 2024

Closing this since there hasn't been any follow-up and regular line breaks can be inferred to be between each entry in model.properties.

from prisma-ast.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.