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 indentDSL to improve checkDSL parsing reliability #68

Merged
merged 11 commits into from
Oct 14, 2022
Merged

feat: add indentDSL to improve checkDSL parsing reliability #68

merged 11 commits into from
Oct 14, 2022

Conversation

dbrrt
Copy link
Contributor

@dbrrt dbrrt commented Oct 7, 2022

This PR introduces indent-dsl utilities to facilitate friendly DSL parsing.

Description

  • indent-dsl.ts added with corresponding tests

Review Checklist

  • I have clicked on "allow edits by maintainers".
  • I have added documentation for new/changed functionality in this PR or in a PR to openfga.dev [Provide a link to any relevant PRs in the references section above]
  • The correct base branch is being used, if not main
  • I have added tests to validate that the change in functionality is working as expected

@dbrrt dbrrt requested a review from a team as a code owner October 7, 2022 21:37
@CLAassistant
Copy link

CLAassistant commented Oct 7, 2022

CLA assistant check
All committers have signed the CLA.

tests/__snapshots__/dsl.test.ts.snap Outdated Show resolved Hide resolved
@adriantam
Copy link
Member

@dbrrt : thank you for your contribution! Instead of adding the indentDSL in check-dsl.ts, maybe it will be better to update the grammar file https://github.com/openfga/syntax-transformer/blob/main/src/openfga.ne instead. In this way, the line number parsed by the checkDSL's catch statement (https://github.com/openfga/syntax-transformer/blob/f2b3cc0a0fc53531e4d00b1a55484c32eeb46151/src/check-dsl.ts#L137) will be correct. Please let me know if you need further clarifications.

Thanks!

@dbrrt
Copy link
Contributor Author

dbrrt commented Oct 11, 2022

@dbrrt : thank you for your contribution! Instead of adding the indentDSL in check-dsl.ts, maybe it will be better to update the grammar file https://github.com/openfga/syntax-transformer/blob/main/src/openfga.ne instead. In this way, the line number parsed by the checkDSL's catch statement (

https://github.com/openfga/syntax-transformer/blob/f2b3cc0a0fc53531e4d00b1a55484c32eeb46151/src/check-dsl.ts#L137

) will be correct. Please let me know if you need further clarifications.
Thanks!

I don't think changing the grammar is fair here. The purpose of my function is to make sure that any non properly indented input (copy pasted from any IDE / text editor, FGA playground, Zoom etc...) will match 1:1 the grammar and make the parser more resilient. It also natively drops the new lines making nearley's job easier.

@adriantam Can you please elaborate on "why adding indenting to the catch statement"? I'm not sure how it could help with raw DSL parsing.

@rhamzeh Any thought?

@rhamzeh
Copy link
Member

rhamzeh commented Oct 14, 2022

Apologies for the late reply!

First, thanks for your contribution @dbrrt! This is definitely helpful while we decide whether we want to make indentation optional in the grammar or not.

A few notes:

  • Long term, I think what @adriantam mentioned is right - if we want to allow this, we should consider making different forms of indentation part of the grammar, this is especially true if users get trained not to indent properly and let the tool handle it for them, making indentation less important from a grammar perspective, only from a readability perspective.
  • Also long term I see the value of this as more than just fixing indentation, but more like prettifying/formatting (think of it like eslint --fix/go fmt)
  • I don't think this should run as part of checkDSL - checkDSL is concerned with validating and reporting back markers - if it modified the lines, the markers will be off (as @adriantam alluded)
  • Most likely this should be it's own util function that users can run separately if they chose to do so before passing it into checkDSL
  • Ideally, this should not affect any of the snapshots as those are related to checkDSL, not formatting
  • I would ask that you move this to it's own file: you can call it indent-dsl.ts (we will fix the naming and reorganize it after merge)
  • Also I would ask that for that file, you create a test file with a bunch of tests and cases to verify that it works as expected

TLDR:
Please:

  • move the indentDSL function into it's own file outside of checkDSL
  • add indent-dsl.test.ts with multiple tests for it 🙏🏽

src/check-dsl.ts Outdated Show resolved Hide resolved
@dbrrt dbrrt requested review from adriantam and rhamzeh and removed request for adriantam and rhamzeh October 14, 2022 14:17
@adriantam
Copy link
Member

Thanks @dbrrt . Do you mind running npm run format:fix to update the code so that it passes prettier validation? Otherwise, it looks good to me.

@dbrrt
Copy link
Contributor Author

dbrrt commented Oct 14, 2022

Thanks @dbrrt . Do you mind running npm run format:fix to update the code so that it passes prettier validation? Otherwise, it looks good to me.

Sure!

@adriantam
Copy link
Member

@dbrrt : It seems to be failing lint test (https://github.com/openfga/syntax-transformer/actions/runs/3251900056/jobs/5338708365#step:8:44). Can you take a look at it?

@dbrrt
Copy link
Contributor Author

dbrrt commented Oct 14, 2022

@dbrrt : It seems to be failing lint test (https://github.com/openfga/syntax-transformer/actions/runs/3251900056/jobs/5338708365#step:8:44). Can you take a look at it?

Yes, I missed the inferred type complaint from eslint, I only get warning from other files now, it should be fine.

@adriantam
Copy link
Member

@dbrrt
Copy link
Contributor Author

dbrrt commented Oct 14, 2022

Looks like the unit test failed - https://github.com/openfga/syntax-transformer/actions/runs/3252493163/jobs/5340130318#step:9:15

The last test I've added wasn't related to the functionality introduced by indent-dsl, so I've removed it.

Copy link
Member

@adriantam adriantam left a comment

Choose a reason for hiding this comment

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

LGTM

@adriantam adriantam merged commit 6b1b1bf into openfga:main Oct 14, 2022
@adriantam
Copy link
Member

@dbrrt : Thank you for your contribution. The changes have been merged into main.

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