Comments (3)
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.
@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.
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)
- Schema Builder API
- @@map in Enums can not be parsed properly HOT 1
- Thank you! HOT 2
- MismatchedTokenException: Expecting --> '}' <-- but found --> 'model' <--
- Trailing comments not handled properly HOT 2
- Throws when passing an empty array into an attribute HOT 3
- Doesn't extend existing model HOT 6
- Issue with documentation example HOT 2
- Bug with db.ObjectId HOT 1
- Add support for views HOT 4
- `getSchema` - model attributes are not parsed HOT 1
- feature request: Include source code location for each node HOT 9
- Renaming a model attribute or deleting an attribute and creating a new attribute with the same args of the deleted one HOT 4
- getSchema- Empty comments are not parsed correctly HOT 1
- Missing `produceSchema` in the bundle? HOT 1
- MismatchedTokenException if certain keywords are used in the generator name HOT 1
- Possible issue parsing when hyphen in generator name HOT 2
- Support for @map on enum fields HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from prisma-ast.