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

Preprocessing before matching in custom rules? #671

Open
JaneX8 opened this issue Dec 7, 2024 · 3 comments
Open

Preprocessing before matching in custom rules? #671

JaneX8 opened this issue Dec 7, 2024 · 3 comments
Labels

Comments

@JaneX8
Copy link

JaneX8 commented Dec 7, 2024

I have a very specific use case where I would like to use custom DevSkim rules on.

I described the situation here: jqlang/jq#566 (comment) which I moved into a discussion here: jqlang/jq#3218.

For DevSkim purposes this for now can be considered a 'custom and unsupported language', JQ. JQ in itself is not valid JSON but in my use-case a large part of it is JSON. To avoid writing very complicated regular expressions on a complicated file structure only, if I enclosed it in placeholders like in below example.

{
  "name": "Example",
  "data": [
    # __JQ_START__
    .input | map({ key: .key, value: .value })
    # __JQ_END__
  ]
}

Is there a way in DevSkim to first use regex to remove everything in between the placeholders, including the placeholders itself, and then use JSONpath on the remainder, which is now fully JSON?

Or visa versa remove everything outside the placeholders including the placeholders, and do another regex on the remainder (JQ only)?

@gfs
Copy link
Contributor

gfs commented Dec 9, 2024

With the way you have currently formatted it, no.

The first option isn't possible as DevSkim only supports first running a path query and then a string compare/regex.

The second option won't work without changing your formatting, as you won't be able to run a JSONPath query against that file because it isn't valid json.

If you formatted it like this so the overall document is valid JSON I think you should be able to use a DevSkim JSON path query to select the value of the data key and then execute a regex against the content.

{
    "name": "Example",
    "data": [
        ".input | map({ key: .key, value: .value })"
    ]
}

@gfs gfs added the question label Dec 9, 2024
@JaneX8
Copy link
Author

JaneX8 commented Dec 10, 2024

I was afraid so. But this is unfortunately correct JQ syntax. I thought of enclosing it as a string too, but it would require extra steps in my program before passing it to a JQ processor and be less readable with multiline etc. And also it would effectively make it valid JSON meaning I won't be able to do any JQ processing in VScode anymore, and JQ syntax highlighting etc.

I think currently the only option to write rules for these .jq files in DevSkim is to go full regex or do you have any other ideas or helpful suggestions?

As far as I know there is no formal language description of JQ (yet) as in ABNF or equivalent, so it won't be possible to get this implemented in DevSkim as a language.

@gfs
Copy link
Contributor

gfs commented Dec 11, 2024

In that case, it sounds like your best option will unfortunately be falling back to regular regex parsing, which is a shame because it is very close to JSON syntax and JSONPath would I'm sure be helpful to strip down to just the jq portions for linting rather than writing regex against the structured data format. =\

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants