mvdan / sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
https://pkg.go.dev/mvdan.cc/sh/v3
BSD 3-Clause "New" or "Revised" License
6.99k stars 333 forks source link

cmd/shfmt: provide a jsonschema for --to-json and --from-json #967

Open loynoir opened 1 year ago

loynoir commented 1 year ago

feat

shfmt --dump-ast-schema ast.jsonschema.json --dump-ast ast.json

reason

I guess shellcheck and shfmt might be the largest two shellscript-AST-related repos. (Are they?)

Some shellscript AST parser I found seems incomplete.

It would be awesome to re-use shellcheck AST or shfmt AST, from other language.

So no need to workaround some other incomplete shellscript AST parsers.

related

https://github.com/koalaman/shellcheck/issues/2676

mvdan commented 1 year ago

We already have the --to-json flag, for example shfmt --to-json <<<'foo $bar'. There is no jsonschema, but the types are the nodes you can see in https://pkg.go.dev/mvdan.cc/sh/v3/syntax.

There's also --from-json, which ingests the same kind of AST in json form.

loynoir commented 1 year ago

@mvdan

The jsonschema request is for generating type definition.

For example, my use case, typescript have great tool to generate type definition from jsonschema.

As swagger and openapi use jsonschema, there should be tools to generate type definition for other language too.

mvdan commented 1 year ago

Sure, I'll look at a jsonschema.

loynoir commented 1 year ago

There is https://www.npmjs.com/package/@sinclair/typebox in typescript ecosystem, which write code to get jsonchema and typescript definition at the same time with no need to compile.

I guess there might be something similar within golang system?