Skip to content

Commit 0da3d81

Browse files
📝 Add docstrings to feat/new_options (#105)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: JounQin <[email protected]>
1 parent 1aa72b1 commit 0da3d81

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ func Print(originalText string, filepath string, syntaxOptions processor.SyntaxO
5050
return processor.Print(originalText, filepath, syntaxOptions)
5151
}
5252

53+
// `process` processes the input file path and text by performing either formatting (printing) or parsing based on the print flag.
54+
//
55+
// It converts the input byte slices to strings and configures parser options—including comment retention, language variant, stop marker, and error recovery settings. When printing is enabled, it applies printer options (indentation, binary next line, switch case indentation, space redirects, padding, minification, single-line formatting, and function next line) to format the text via the Print function. Otherwise, it parses the text with Parse and maps the resulting AST into a file representation.
56+
//
57+
// The function then encapsulates the file, the processed text, and any parsing error information into a result structure, marshals it to JSON, appends a null terminator, and returns a pointer to the first byte of the JSON output.
58+
//
5359
//export process
5460
func process(
5561
filepathBytes []byte,

processor/main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ type SyntaxOptions struct {
3535
PrinterOptions
3636
}
3737

38+
// `Parse` converts shell script text into a structured syntax tree.
39+
// It assembles parser options based on the provided configuration—such as whether to keep comments,
40+
// the shell syntax variant to use, an optional stopping point, and the desired error recovery level.
41+
// The supplied file path is used for contextual error reporting.
42+
// It returns a syntax.File representing the parsed script, or an error if parsing fails.
3843
func Parse(text string, filepath string, parserOptions ParserOptions) (*syntax.File, error) {
3944
var options []syntax.ParserOption
4045

@@ -53,6 +58,11 @@ func Parse(text string, filepath string, parserOptions ParserOptions) (*syntax.F
5358
return parser.Parse(bytes.NewReader([]byte(text)), filepath)
5459
}
5560

61+
// `Print` returns the formatted shell script defined in originalText.
62+
// It first parses the input using the parser options in syntaxOptions and then prints the resulting
63+
// syntax tree using printer options—including indentation, single-line formatting, and others.
64+
// The filepath parameter is used for context in error messages. On success, Print returns the formatted
65+
// script as a string, or an error if parsing or printing fails.
5666
func Print(originalText string, filepath string, syntaxOptions SyntaxOptions) (string, error) {
5767
file, err := Parse(originalText, filepath, syntaxOptions.ParserOptions)
5868

processor/structs.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func mapNode(node syntax.Node) *Node {
115115
}
116116
}
117117

118+
// `mapComments` transforms a slice of syntax.Comment into a slice of Comment by converting each comment's hash, text, start, and end positions using mapPos. It preserves the order of the comments and returns an empty slice if the input is nil or empty.
118119
func mapComments(comments []syntax.Comment) []Comment {
119120
commentsSize := len(comments)
120121
commentList := make([]Comment, commentsSize)
@@ -130,6 +131,8 @@ func mapComments(comments []syntax.Comment) []Comment {
130131
return commentList
131132
}
132133

134+
// `mapWord` converts a *syntax.Word into a custom *Word structure. It maps each part of the syntax.Word using mapNode,
135+
// extracts the literal via Lit(), and maps the start and end positions using mapPos. If the input word is nil, it returns nil.
133136
func mapWord(word *syntax.Word) *Word {
134137
if word == nil {
135138
return nil
@@ -150,6 +153,9 @@ func mapWord(word *syntax.Word) *Word {
150153
}
151154
}
152155

156+
// `mapRedirects` converts a slice of syntax.Redirect pointers into a slice of custom Redirect structures.
157+
// It maps each redirect’s operator position, associated literal (if present), word, heredoc, and overall positional data using helper functions.
158+
// If the literal component (N) is non-nil, it is transformed into a Lit structure that encapsulates both its value and positional information.
153159
func mapRedirects(redirects []*syntax.Redirect) []Redirect {
154160
redirsSize := len(redirects)
155161
redirs := make([]Redirect, redirsSize)
@@ -180,6 +186,7 @@ func mapRedirects(redirects []*syntax.Redirect) []Redirect {
180186
return redirs
181187
}
182188

189+
// `mapStmts` converts a slice of *syntax.Stmt into a slice of Stmt by mapping each statement's components—including comments, command node, positional information, semicolon, redirections, and execution flags (negated, background, coprocess).
183190
func mapStmts(stmts []*syntax.Stmt) []Stmt {
184191
stmtsSize := len(stmts)
185192
stmtList := make([]Stmt, stmtsSize)

src/processor.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,34 @@ export const getProcessor = (
4545
},
4646
): Promise<string>
4747

48+
/**
49+
* Processes a shell script input using a WebAssembly module.
50+
*
51+
* This asynchronous function accepts shell script input either as a string or as an AST File, along with a set of options
52+
* that control formatting, error recovery, and output. It ensures that the WebAssembly module is loaded and instantiated,
53+
* allocates memory for the file path and text content, and then calls the module's processing function with the provided options.
54+
* Depending on the `print` flag, it returns either the processed text or a File representing the parsed AST.
55+
*
56+
* @param textOrAst - The shell script input as a string or as an AST File. When providing a non-string input and `print` is false,
57+
* the `originalText` option must be supplied.
58+
* @param options - An object containing processing options:
59+
* - filepath: The file path associated with the input, used primarily for error reporting.
60+
* - print: If true, the function returns the processed text; otherwise, it returns the processed AST as a File.
61+
* - originalText: The original text of the shell script, required when `textOrAst` is not a string.
62+
* - keepComments: Determines whether comments should be preserved in the output.
63+
* - variant: Specifies the shell scripting variant (e.g., {@link LangVariant.LangBash}).
64+
* - stopAt: A token indicating where to halt further processing.
65+
* - recoverErrors: Sets the level of error recovery during processing (default is 0).
66+
* - useTabs, tabWidth, indent: Options to control indentation formatting.
67+
* - binaryNextLine, switchCaseIndent, spaceRedirects, keepPadding, minify, singleLine, functionNextLine:
68+
* Additional flags that influence formatting details and output structure.
69+
*
70+
* @returns A promise that resolves to either the processed text (if `print` is true) or a File (if `print` is false).
71+
*
72+
* @throws {TypeError} If the original text is required but not provided.
73+
* @throws {ParseError} If the processed output is not valid JSON or indicates a parsing error.
74+
* @throws {SyntaxError} If a syntax error is detected without an associated parse error object.
75+
*/
4876
async function processor(
4977
textOrAst: File | string,
5078
{

0 commit comments

Comments
 (0)