Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add prism parser #114

Merged
merged 16 commits into from
Dec 3, 2024
Merged

feat: add prism parser #114

merged 16 commits into from
Dec 3, 2024

Conversation

sasamuku
Copy link
Member

@sasamuku sasamuku commented Nov 29, 2024

Summary

Add prism parser for schema.rb.
This is alternative to pegjs.

vs pegjs

  • Type safe
  • Easy to understand (because it is a common pattern to parse AST)
  • Highly scalable (same as pegjs maybe)

Related Issue

Changes

Testing

Other Information

@sasamuku sasamuku force-pushed the add_prism_parser branch 3 times, most recently from cb7e6da to e3b4857 Compare November 29, 2024 05:22
private extractTableName(argNodes: Node[]): string {
const nameNode = argNodes.find((node) => node instanceof StringNode)
// @ts-ignore: unescaped is defined as string but it is actually object
return (nameNode as StringNode).unescaped.value
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unescaped is defined as string but it is actually object.
We might need to fix ruby/prism package. btw, let me use ts-ignore in this pr.

StringNode {
  nodeID: 3,
  location: { startOffset: 22, length: 7 },
  openingLoc: { startOffset: 22, length: 1 },
  contentLoc: { startOffset: 23, length: 5 },
  closingLoc: { startOffset: 28, length: 1 },
  unescaped: { encoding: 'utf-8', validEncoding: true, value: 'users' }
}

@sasamuku sasamuku marked this pull request as ready for review November 29, 2024 05:22
@sasamuku sasamuku requested a review from a team as a code owner November 29, 2024 05:22
@sasamuku sasamuku requested review from hoshinotsuyoshi, FunamaYukina, junkisai and MH4GF and removed request for a team November 29, 2024 05:22
Copy link
Member

@MH4GF MH4GF left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I commented lightly!

import { processor as schemarbProcessor } from './schemarb/index.js'
import { processor as postgresqlProcessor } from './sql/index.js'

type SupportedFormat = 'schemarb' | 'postgres'
type SupportedFormat = 'schemarb' | 'postgres' | 'schemarb-prism'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the default should be prism, how about the following?

Suggested change
type SupportedFormat = 'schemarb' | 'postgres' | 'schemarb-prism'
type SupportedFormat = 'schemarb' | 'postgres' | 'schemarb-deprecated-pegjs'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be fine with a renaming with a different PR 👍🏻

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll replace them in another pr 👍

frontend/packages/db-structure/src/parser/types.ts Outdated Show resolved Hide resolved
@MH4GF
Copy link
Member

MH4GF commented Nov 29, 2024

The conversion part of AST will be checked in an hour 🙏🏻

@sasamuku sasamuku mentioned this pull request Nov 29, 2024
1 task
@sasamuku sasamuku force-pushed the add_prism_parser branch 2 times, most recently from d755752 to b64136d Compare November 29, 2024 11:22
@sasamuku sasamuku requested a review from MH4GF November 29, 2024 11:29
@sasamuku
Copy link
Member Author

@MH4GF
several commits were pushed, pls review them 😄
ci is failed for some reason, I'll check it out later.

Copy link
Member

@MH4GF MH4GF left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!
Nice work!!!

})

expect(result).toEqual(expected)
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice test 😄

Comment on lines +56 to +57
// Since 5.1 PostgreSQL adapter uses bigserial type for primary key in default
// See:https://github.com/rails/rails/blob/v8.0.0/activerecord/lib/active_record/migration/compatibility.rb#L377
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an awesome comment!

@sasamuku
Copy link
Member Author

sasamuku commented Dec 2, 2024

@MH4GF @hoshinotsuyoshi @FunamaYukina

If anyone has idea, please let me know 🙏
I am getting the following error in frontend-ci.
It looks like importing path is wrong, but it doesn't look so.

 FAIL  src/cli/runPreprocess.test.ts [ src/cli/runPreprocess.test.ts ]
Error: Failed to load url src/schema/factories (resolved id: src/schema/factories) in /home/runner/work/liam/liam/frontend/packages/db-structure/dist/parser/schemarb-prism/parser.js. Does the file exist?

@MH4GF
Copy link
Member

MH4GF commented Dec 2, 2024

@sasamuku

It seems to be a pattern that works well during development but causes problems after build.

https://github.com/liam-hq/liam/pull/114/files#diff-a7e513747c262e49f364a0ee804085556c818f22f6cb5e56dc82337b3ff1c432R16-R17

Perhaps it is necessary to make this an absolute reference with .js.
Also, maybe ../schema/factories.js instead of src/schema/factories.

@sasamuku
Copy link
Member Author

sasamuku commented Dec 2, 2024

@MH4GF
You're absolutely right👍
Fix on 5f6d3ef

But there is still some problem on test. I'll handle this💪

@sasamuku
Copy link
Member Author

sasamuku commented Dec 2, 2024

📝 the cli build process seems to have failed.

│ x Build failed in 972ms
│ error during build:
│ [vite]: Rollup failed to resolve import "@liam/ui" from "/Users/XXXX/liam/frontend/packages/erd-core/src/components/ERDRenderer/ERDContent/TableNode/TableNode.tsx".

Cannot find module '@liam/ui' or its corresponding type declarations.

image

@MH4GF
Copy link
Member

MH4GF commented Dec 2, 2024

@sasamuku Maybe this is why it's in devDependencies? 💭

"@liam/ui": "workspace:*",

@sasamuku
Copy link
Member Author

sasamuku commented Dec 2, 2024

@MH4GF
Wow, Wonderful. Indeed, you are right.
Thanks a lot!

Then, another error happened...

│ x Build failed in 1.62s
│ error during build:
│ ../../node_modules/.pnpm/@[email protected]/node_modules/@ruby/prism/src/index.js (2:9): "readFile" is not exported by "__vite-browser-external", imported by "../../node_modules/.pnpm/@[email protected]/node_modules/@ruby/prism/sr…
│ file: /Users/ryota.sasazawa/works/route06/liam/frontend/node_modules/.pnpm/@[email protected]/node_modules/@ruby/prism/src/index.js:2:9
│ 1: import { WASI } from "wasi";
│ 2: import { readFile } from "node:fs/promises";
│             ^
│ 3: import { fileURLToPath } from "node:url";
│     at getRollupError (file:///Users/ryota.sasazawa/works/route06/liam/frontend/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/parseAst.js:396:41)
│     at error (file:///Users/ryota.sasazawa/works/route06/liam/frontend/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/parseAst.js:392:42)

This is similar to error on #90 maybe.

the CLI build test is failing with a "getRollupError"

@MH4GF
Copy link
Member

MH4GF commented Dec 2, 2024

@sasamuku Oh, Perhaps this fix is needed:

https://github.com/ruby/prism/blob/main/docs/javascript.md#browser


One could view it as a bad idea to include it in the build for browsers since this time it is running in the CLI, but it should be better to be able to run it in both.
So, please try to fix it!

Comment on lines 8 to 15
"@liam/ui": "workspace:*",
"@xyflow/react": "12.3.5",
"react": "18"
},
"devDependencies": {
"@biomejs/biome": "1.9.3",
"@liam/configs": "workspace:*",
"@liam/db-structure": "workspace:*",
"@liam/ui": "workspace:*",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

( Oh, sorry, but I wonder why the CI results for #108 show success. 💭 )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔💭

Copy link
Member

@FunamaYukina FunamaYukina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I apologize for the delay in the review🙇‍♀️. I've checked the parsing process and it looks good to me (LGTM). I think we can skip the smoke test portion for now and address it in a separate issue.💭

It seems like there's a WASI-related error...

Some results of running pnpm dev

[plugin:vite:resolve] [plugin vite:resolve] Module "wasi" has been externalized for browser compatibility

Copy link
Member

@hoshinotsuyoshi hoshinotsuyoshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this fix is needed:
https://github.com/ruby/prism/blob/main/docs/javascript.md#browser

I see, I understand that this is the issue with frontend-ci. I didn’t notice anything else concerning in the implementation!

@@ -1,4 +1,3 @@
export { parse } from './parser/index.js'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parser is removed from index.ts so that it is not referenced at build.
b0a12dd
thank you @MH4GF 🙏

@sasamuku sasamuku force-pushed the add_prism_parser branch 3 times, most recently from aef7680 to 94f3c88 Compare December 3, 2024 02:17
Comment on lines 46 to 57
// NOTE: suppress the following warning:
if (!stderr.includes('ExperimentalWarning: WASI is an experimental feature and might change at any time')) {
expect(stderr).toBe('')
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Workaround for WASI warning.
We could disable warning on npx command, but it would be better to suppress in case of only this waring.

@sasamuku sasamuku added this pull request to the merge queue Dec 3, 2024
Merged via the queue into main with commit 0502d8f Dec 3, 2024
10 checks passed
@sasamuku sasamuku deleted the add_prism_parser branch December 3, 2024 02:28
@sasamuku sasamuku mentioned this pull request Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants