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

Request: Support preprocessor/postprocessor plugins #1844

Open
2 tasks done
gromgit opened this issue Feb 22, 2020 · 1 comment
Open
2 tasks done

Request: Support preprocessor/postprocessor plugins #1844

gromgit opened this issue Feb 22, 2020 · 1 comment

Comments

@gromgit
Copy link

gromgit commented Feb 22, 2020

For new checks and feature suggestions

There are instances where it would be useful to have a user-defined preprocessor flesh out script fragments for Shellcheck to lint, then a postprocessor to read and adjust the results before Shellcheck prepares final judgement. User-defined config scripts that are sourced by application scripts within their own environments are a broad class of files that need this functionality to be properly checked.

The workflow I'm thinking of would look like this (new bits in bold):

  1. Shellcheck opens source file (or stdin)
  2. Shellcheck writes source file to stdin of preprocessor, and reads modified source from its stdout
  3. Shellcheck parses and checks modified source
  4. Shellcheck sends JSON results to stdin of postprocessor, and reads modified JSON results from its stdout
  5. Shellcheck prints results in desired user format

#1843 is a typical example. Arch Linux's PKGBUILD files are really shell script fragments that are executed in an environment that's fairly well-defined, but not accessible outside the build process. A user wanting to use Shellcheck on PKGBUILD files would write the following:

  • A preprocessor script (pbwrap.sh) that takes the PKGBUILD source from stdin and wrap it like this: https://github.com/vn971/rua/blob/master/res/shellcheck-wrapper
  • A postprocessor script (pbadj.sh) that takes the JSON results from stdin and uses jq or some similar tool to subtract 7 from all line and endLine properties, to account for the wrapper's prelude, then drop any entries with line numbers that are negative or greater than the original input length. (For this and other reasons, it would be good for the postprocessor to also get the original source and the preprocessed input, probably as command-line arguments.)

Then shellcheck --pre pbwrap.sh --post pbadj.sh ... my.PKGBUILD should Get The Job Done.

Why not simply write a wrapper around Shellcheck instead?

A Shellcheck wrapper would have to parse all Shellcheck options, to "short-circuit" the above workflow when it doesn't make sense (e.g. --help, --list-optional, --version), and to "count option arguments" otherwise, to ensure all specified files are processed. This would require revisiting the wrapper each time the Shellcheck version changes, in case options are added/renamed/removed. It's not impossible, but it's highly inelegant.

Having this functionality be directly supported by Shellcheck also makes it easier to integrate with third-party tools like vscode-shellcheck, that would want to choose an output format best suited to their operations. Embedded pre-/post-processing in Shellcheck itself means not having to figure out how to adjust the final tty or diff output.

What about buggy plugins?

Poorly-written plugins could b0rk the workflow or confuse the user with results that don't map to the original source, but shouldn't be able to harm Shellcheck directly.

@KSXGitHub
Copy link

A preprocessor script (pbwrap.sh) that takes the PKGBUILD source from stdin and wrap it like this: https://github.com/vn971/rua/blob/master/res/shellcheck-wrapper

This is more convoluted than I thought. I expected shellcheck to have options to allow certain unused/undeclared variables.

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

No branches or pull requests

2 participants