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

Changing function signature of parse to accept the file content instead of a file #1706

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

oxisto
Copy link
Member

@oxisto oxisto commented Sep 20, 2024

This PR changes the way parse works (in a backwards compatible way). Instead of parsing a File, we parse the file contents (and a path). The reasoning behind this is that almost all language frontends currently need to read the file contents and we can harmonize this. This will also allow us to provide more common statistics about the parsing context in the future.

Language frontends that can potentially leverage this:

  • Python
  • Go
  • TypeScript
  • Ruby

…tead of a file

This PR changes the way `parse` works (in a backwards compatible way). Instead of parsing a `File`, we parse the file contents (and a path). The reasoning behind this is that almost all language frontends currently need to read the file contents and we can harmonize this. This will also allow us to provide more common statistics about the parsing context in the future.
Copy link

if (path != null) {
path.absolute().toString()
} else {
"<unknown>"
Copy link
Contributor

Choose a reason for hiding this comment

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

To we want this constant for cpg consistency? Python recommends <string> see https://docs.python.org/3/library/functions.html#compile

Copy link
Member Author

Choose a reason for hiding this comment

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

We are using ast.Parse and the default parameter here was <unknown>. See https://docs.python.org/3/library/ast.html#ast.parse

Copy link
Contributor

Choose a reason for hiding this comment

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

Funny, that the ast.parse doc says:

Equivalent to compile(source, filename, mode, PyCF_ONLY_AST).

But I'm ok with the "unknown". This should probably a sane constant for all frontends if there is no file available?

Copy link
Member Author

Choose a reason for hiding this comment

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

Funny, that the ast.parse doc says:

Equivalent to compile(source, filename, mode, PyCF_ONLY_AST).

But I'm ok with the "unknown". This should probably a sane constant for all frontends if there is no file available?

I was intentionally using the python specific value here, but I am fine with either way. Not sure if the SARIF standard defines something in this case, since we are using the location information from SARIF.

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.

2 participants