mirror of
https://github.com/google/go-jsonnet.git
synced 2025-08-07 23:07:14 +02:00
Expose "unparse", FormatNode and RawAST (#710)
* Export FormatNode so external users can pretty print an AST Signed-off-by: Tom Wilkie <tom@grafana.com> * Expose RawAST function. Signed-off-by: Tom Wilkie <tom@grafana.com> * Add comment Signed-off-by: Tom Wilkie <tom@grafana.com> --------- Signed-off-by: Tom Wilkie <tom@grafana.com> Co-authored-by: Tom Wilkie <tom@grafana.com>
This commit is contained in:
parent
9639773cf0
commit
7b9f5fd4b4
@ -4,7 +4,11 @@
|
|||||||
// customized using formatter.Options.
|
// customized using formatter.Options.
|
||||||
package formatter
|
package formatter
|
||||||
|
|
||||||
import "github.com/google/go-jsonnet/internal/formatter"
|
import (
|
||||||
|
"github.com/google/go-jsonnet/ast"
|
||||||
|
"github.com/google/go-jsonnet/internal/formatter"
|
||||||
|
"github.com/google/go-jsonnet/internal/parser"
|
||||||
|
)
|
||||||
|
|
||||||
// StringStyle controls how the reformatter rewrites string literals.
|
// StringStyle controls how the reformatter rewrites string literals.
|
||||||
// Strings that contain a ' or a " use the optimal syntax to avoid escaping
|
// Strings that contain a ' or a " use the optimal syntax to avoid escaping
|
||||||
@ -46,3 +50,14 @@ func DefaultOptions() Options {
|
|||||||
func Format(filename string, input string, options Options) (string, error) {
|
func Format(filename string, input string, options Options) (string, error) {
|
||||||
return formatter.Format(filename, input, options)
|
return formatter.Format(filename, input, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FormatNode returns code that is equivalent to its input but better formatted
|
||||||
|
// according to the given options.
|
||||||
|
func FormatNode(node ast.Node, finalFodder ast.Fodder, options Options) (string, error) {
|
||||||
|
return formatter.FormatNode(node, finalFodder, options)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SnippetToRawAST parses a snippet and returns the resulting AST.
|
||||||
|
func SnippetToRawAST(filename string, snippet string) (ast.Node, ast.Fodder, error) {
|
||||||
|
return parser.SnippetToRawAST(ast.DiagnosticFileName(filename), "", snippet)
|
||||||
|
}
|
||||||
|
@ -152,6 +152,12 @@ func Format(filename string, input string, options Options) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return FormatNode(node, finalFodder, options)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FormatNode returns code that is equivalent to its input but better formatted
|
||||||
|
// according to the given options.
|
||||||
|
func FormatNode(node ast.Node, finalFodder ast.Fodder, options Options) (string, error) {
|
||||||
// Passes to enforce style on the AST.
|
// Passes to enforce style on the AST.
|
||||||
if options.SortImports {
|
if options.SortImports {
|
||||||
SortImports(&node)
|
SortImports(&node)
|
||||||
|
Loading…
Reference in New Issue
Block a user