1+ /**
2+ * @fileoverview Go Prettier Format Plugin (CommonJS)
3+ * A Prettier plugin for formatting Go code using WebAssembly.
4+ * This plugin leverages Go's native formatting capabilities through WASM.
5+ * This is the CommonJS version for compatibility with older Node.js environments.
6+ */
7+
18"use strict" ;
29
310const fs = require ( "fs" ) ;
411const path = require ( "path" ) ;
512
13+ /** @type {Promise<void>|null } */
614let initializePromise ;
715
16+ /**
17+ * Initializes the Go WebAssembly module for formatting Go code.
18+ * This function sets up the WASM runtime and makes the formatGo function
19+ * available on the global object.
20+ *
21+ * @async
22+ * @function initialize
23+ * @returns {Promise<void> } A promise that resolves when the WASM module is ready
24+ * @throws {Error } If the WASM file cannot be loaded or instantiated
25+ */
826function initialize ( ) {
927 if ( initializePromise ) {
1028 return initializePromise ;
@@ -36,6 +54,16 @@ function initialize() {
3654 return initializePromise ;
3755}
3856
57+ /**
58+ * Prettier language configuration for Go.
59+ * Defines the language settings, file extensions, and parser mappings.
60+ *
61+ * @type {Array<Object> }
62+ * @property {string } name - The display name of the language
63+ * @property {string[] } parsers - Array of parser names for this language
64+ * @property {string[] } extensions - File extensions associated with this language
65+ * @property {string[] } vscodeLanguageIds - VSCode language identifier mappings
66+ */
3967const languages = [
4068 {
4169 name : "Go" ,
@@ -45,18 +73,67 @@ const languages = [
4573 } ,
4674] ;
4775
76+ /**
77+ * Prettier parser configuration for Go.
78+ * Defines how Go source code should be parsed and processed.
79+ *
80+ * @type {Object<string, Object> }
81+ * @property {Object } go - Go language parser configuration
82+ * @property {Function } go.parse - Parser function that returns the input text as-is
83+ * @property {string } go.astFormat - AST format identifier for the printer
84+ * @property {Function } go.locStart - Function to get the start location of a node
85+ * @property {Function } go.locEnd - Function to get the end location of a node
86+ */
4887const parsers = {
4988 go : {
89+ /**
90+ * Parse Go source code. For this plugin, we pass through the text as-is
91+ * since the actual formatting is handled by the Go WASM module.
92+ *
93+ * @param {string } text - The Go source code to parse
94+ * @returns {string } The input text unchanged
95+ */
5096 parse : ( text ) => text ,
5197 astFormat : "go-format" ,
5298 // These are required for Prettier to work
99+ /**
100+ * Get the start location of a node in the source code.
101+ *
102+ * @param {string } node - The node (in this case, the source text)
103+ * @returns {number } Always returns 0 as we treat the entire text as one node
104+ */
53105 locStart : ( node ) => 0 ,
106+ /**
107+ * Get the end location of a node in the source code.
108+ *
109+ * @param {string } node - The node (in this case, the source text)
110+ * @returns {number } The length of the text
111+ */
54112 locEnd : ( node ) => node . length ,
55113 } ,
56114} ;
57115
116+ /**
117+ * Prettier printer configuration for Go.
118+ * Defines how the parsed Go AST should be formatted back to text.
119+ *
120+ * @type {Object<string, Object> }
121+ * @property {Object } go-format - Go formatting printer configuration
122+ * @property {Function } go-format.print - Async function that formats Go code
123+ */
58124const printers = {
59125 "go-format" : {
126+ /**
127+ * Format Go source code using the WebAssembly Go formatter.
128+ * This function initializes the WASM module if needed and calls the
129+ * global formatGo function exposed by the Go program.
130+ *
131+ * @async
132+ * @param {Object } path - Prettier's path object containing the source code
133+ * @param {Function } path.getValue - Function to get the current node value
134+ * @returns {Promise<string> } The formatted Go source code
135+ * @throws {Error } If the WASM module fails to initialize or format the code
136+ */
60137 print : async ( path ) => {
61138 // The WASM module must be initialized before we can format.
62139 await initialize ( ) ;
@@ -67,6 +144,13 @@ const printers = {
67144 } ,
68145} ;
69146
147+ /**
148+ * @module go-prettier-format
149+ * @description Prettier plugin for formatting Go source code using WebAssembly
150+ * @exports {Object } languages - Language configuration for Prettier
151+ * @exports {Object } parsers - Parser configuration for Go language
152+ * @exports {Object } printers - Printer configuration for Go formatting
153+ */
70154module . exports = {
71155 languages,
72156 parsers,
0 commit comments