Comments (9)
It sounds intriguing. prisma-ast is built with chevrotain.js, but I'll admit I don't really know that much about building syntax trees and mostly just stumbled my way through this project. It produces an AST because they were simpler to implement than CSTs.
I believe chevrotain does support location info for tokens, so it would be possible to surface that info.
What I'd like to do is have a better API for querying the document, but until now I never really had a good idea for the sorts of use cases an API like that would need to serve. I'm open to your suggestions on what kinds of features a prisma-ast Query interface would be most helpful to provide.
from prisma-ast.
I believe chevrotain does support location info for tokens, so it would be possible to surface that info
Makes sense! Let me look into this a little bit and circle back. I haven't thought too deeply beyond the idea of mirroring ESLint and RuboCop developer experience.
What I'd like to do is have a better API for querying the document, but until now I never really had a good idea for the sorts of use cases an API like that would need to serve.
The visitor pattern implemented in the linter works well for traversal:
But maybe there is a better approach, or other use cases for a query API.
from prisma-ast.
@maxh I've created a PR with the option to enable node location tracking. It's only sort of partially implemented, to fit in with the way the schema is currently shaped, but perhaps it will be enough for your use cases.
Can you try it out and see if it works for your usage?
Location tracking is disabled by default, since there is an impact on parsing performance, but you can turn it on by adding a configuration option to your package.json
file.
{
"prisma-ast": {
"parser": {
"nodeLocationTracking": "full"
}
}
}
After that, "location" statistics will appear on certain parts of your schema output from getSchema()
. The available statistics will appear as such:
{
type: 'generator',
name: 'client',
assignments: [...],
location: {
startLine: 7,
startColumn: 1,
startOffset: 79,
endLine: 7,
endColumn: 16,
endOffset: 94
}
}
Hopefully that will be sufficient for your linter.
from prisma-ast.
Looks promising! I'm testing it out here:
But running into problems with types; not sure if your side or mine:
There are types at '/home/runner/work/prisma-lint/prisma-lint/node_modules/@mrleebo/prisma-ast/dist/index.d.ts', but this result could not be resolved when respecting package.json "exports". The '@mrleebo/prisma-ast' library may need to update its package.json or typings.
https://github.com/loop-payments/prisma-lint/actions/runs/5502248068/jobs/10026376231?pr=84
from prisma-ast.
@maxh Please try again.
I had added a "exports" section to the package.json, but I guess it broke the TypeScript typings so I rolled it back.
from prisma-ast.
Ok, it is looking good:
In the test scenario, I have it printing:
model Users {
^^^^^^^^^^^
It seems that model nodes have location nodes but field nodes do not. Is that expected? I think ideally if there is a field-level violation we can underline that field specifically.
Given that the variance in position and context for model names and field names is pretty standardized (models are top-level nodes, fields are nested within them), I'm second-guessing whether it makes to mirror ESLint and RuboCop for the CLI rendered output of the linter.
But there are other use cases for these locations -- for example an LSP implementation for highlighting errors inside an editor.
from prisma-ast.
Fixed the presence of location data for field and attribute node types.
from prisma-ast.
Looking ready to go! One small thing -- I wasn't able to import the CstNodeLocation type when annotating a function:
https://github.com/loop-payments/prisma-lint/pull/84/files#r1266591956
Otherwise LGTM. I have another library in the pipeline this will be perfect for too. Thanks!!
from prisma-ast.
Added CstNodeLocation
to the export, but it is just a pass-thru from chevrotain, so you can also get it from there.
import type { CstNodeLocation } from '@mrleebo/prisma-ast';
// is the same as
import type { CstNodeLocation } from 'chevrotain';
from prisma-ast.
Related Issues (14)
- Schema Builder API
- 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
- @@map in Enums can not be parsed properly HOT 1
- `getSchema` - model attributes are not parsed HOT 1
- Renaming a model attribute or deleting an attribute and creating a new attribute with the same args of the deleted one HOT 4
- getSchema doesn't produce break-nodes HOT 3
- Thank you! HOT 2
- MismatchedTokenException: Expecting --> '}' <-- but found --> 'model' <--
- Trailing comments not handled properly 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.