Skip to content

Commit 0b25580

Browse files
committed
doc: adds jsdocs for public methods
1 parent 85c002f commit 0b25580

File tree

4 files changed

+161
-7
lines changed

4 files changed

+161
-7
lines changed

src/array.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,35 @@ const get_mapped = (map, key) => {
8484
return mapped
8585
}
8686

87+
/**
88+
* An Array subclass that preserves comments when array operations are performed.
89+
*
90+
* CommentArray extends the native Array class and automatically handles comment
91+
* preservation during array mutations like splice, slice, push, pop, etc.
92+
* Comments are stored as symbol properties and are moved/copied appropriately
93+
* when the array structure changes.
94+
*
95+
* @extends Array
96+
*
97+
* @example
98+
* const arr = parse('[1, 2, 3]') // with comments
99+
* // arr is a CommentArray instance
100+
* arr.splice(1, 1) // Comments are preserved and repositioned correctly
101+
*/
87102
class CommentArray extends Array {
88103
// - deleteCount + items.length
89104

90105
// We should avoid `splice(begin, deleteCount, ...items)`,
91106
// because `splice(0, undefined)` is not equivalent to `splice(0)`,
92107
// as well as:
93108
// - slice
109+
/**
110+
* Changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
111+
* Comments are automatically preserved and repositioned during the operation.
112+
*
113+
* @param {...*} args Arguments passed to Array.prototype.splice
114+
* @returns {CommentArray} A new CommentArray containing the deleted elements.
115+
*/
94116
splice (...args) {
95117
const {length} = this
96118
const ret = super.splice(...args)
@@ -136,6 +158,13 @@ class CommentArray extends Array {
136158
return ret
137159
}
138160

161+
/**
162+
* Returns a shallow copy of a portion of an array into a new CommentArray object.
163+
* Comments are copied to the appropriate positions in the new array.
164+
*
165+
* @param {...*} args Arguments passed to Array.prototype.slice
166+
* @returns {CommentArray} A new CommentArray containing the extracted elements with their comments.
167+
*/
139168
slice (...args) {
140169
const {length} = this
141170
const array = super.slice(...args)

src/common.js

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,34 @@ module.exports = {
167167

168168
is_raw_json,
169169

170-
// TODO: js doc and usage examples
170+
/**
171+
* Assign properties and comments from source to target object.
172+
*
173+
* @param {Object} target The target object to assign properties and comments
174+
* to.
175+
* @param {Object} source The source object to copy properties and comments
176+
* from.
177+
* @param {Array<string|number>} [keys] Optional array of keys to assign. If
178+
* not provided, all keys and non-property comments are assigned. If empty
179+
* array, only non-property comments are assigned.
180+
* @returns {Object} The target object with assigned properties and comments.
181+
*
182+
* @throws {TypeError} If target cannot be converted to object or keys is not
183+
* array or undefined.
184+
*
185+
* @example
186+
* const source = parse('{"a": 1 // comment a, "b": 2 // comment b}')
187+
* const target = {}
188+
*
189+
* // Copy all properties and comments
190+
* assign(target, source)
191+
*
192+
* // Copy only specific properties and their comments
193+
* assign(target, source, ['a'])
194+
*
195+
* // Copy only non-property comments
196+
* assign(target, source, [])
197+
*/
171198
assign (target, source, keys) {
172199
if (!isObject(target)) {
173200
throw new TypeError('Cannot convert undefined or null to object')
@@ -199,7 +226,43 @@ module.exports = {
199226
return assign(target, source, keys)
200227
},
201228

202-
// TODO: js doc and usage examples
229+
/**
230+
* Move comments from one location to another within objects.
231+
*
232+
* @param {Object} source The source object containing comments to move.
233+
* @param {Object} [target] The target object to move comments to. If not
234+
* provided, defaults to source (move within same object).
235+
* @param {Object} from The source comment location.
236+
* @param {string} from.kind The kind of comment prefix (e.g., 'before',
237+
* 'after', 'before-all', etc.).
238+
* @param {string} [from.key] The property key for property-specific comments.
239+
* Omit for non-property comments.
240+
* @param {Object} to The target comment location.
241+
* @param {string} to.kind The kind of comment prefix (e.g., 'before',
242+
* 'after', 'before-all', etc.).
243+
* @param {string} [to.key] The property key for property-specific comments.
244+
* Omit for non-property comments.
245+
* @param {boolean} [override=false] Whether to override existing comments at
246+
* the target location. If false, comments will be appended.
247+
*
248+
* @throws {TypeError} If source is not an object.
249+
*
250+
* @example
251+
* const obj = parse('{"a": 1 // comment on a}')
252+
*
253+
* // Move comment from after 'a' to before 'a'
254+
* moveComments(obj, obj,
255+
* { kind: 'after', key: 'a' },
256+
* { kind: 'before', key: 'a' }
257+
* )
258+
*
259+
* @example
260+
* // Move non-property comment
261+
* moveComments(obj, obj,
262+
* { kind: 'before-all' },
263+
* { kind: 'after-all' }
264+
* )
265+
*/
203266
moveComments (source, target, {
204267
kind: from_kind,
205268
key: from_key

src/parse.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ const {
3232
assign_non_prop_comments
3333
} = require('./common')
3434

35+
/**
36+
* Tokenize JSON string with comments into an array of tokens.
37+
*
38+
* @param {string} code The JSON string with comments to tokenize.
39+
* @returns {Array} Array of token objects containing type, value, and location
40+
* information.
41+
*
42+
* @example
43+
* const tokens = tokenize('{"a": 1 // comment}')
44+
* // Returns array of tokens including comment tokens
45+
*/
3546
const tokenize = code => esprima.tokenize(code, {
3647
comment: true,
3748
loc: true
@@ -417,7 +428,35 @@ function walk () {
417428

418429
const isObject = subject => Object(subject) === subject
419430

420-
// TODO: js doc and usage examples
431+
/**
432+
* Converts a JavaScript Object Notation (JSON) string with comments into an
433+
* object.
434+
*
435+
* @param {string} code A valid JSON string with comments.
436+
* @param {function} [rev] A function that transforms the results. This function
437+
* is called for each member of the object. If a member contains nested
438+
* objects, the nested objects are transformed before the parent object is.
439+
* @param {boolean} [no_comments=false] If true, the comments won't be
440+
* maintained, which is often used when we want to get a clean object.
441+
* @returns {*} The JavaScript object corresponding to the given JSON text with
442+
* comments preserved as symbol properties.
443+
*
444+
* @example
445+
* const result = parse('{"a": 1 // This is a comment}')
446+
* // result.a === 1
447+
* // Comments are stored in symbol properties
448+
*
449+
* @example
450+
* // With reviver function
451+
* const result = parse('{"a": "1"}', (key, value) => {
452+
* return typeof value === 'string' ? parseInt(value) : value
453+
* })
454+
*
455+
* @example
456+
* // Without comments
457+
* const clean = parse('{"a": 1 // comment}', null, true)
458+
* // Returns clean object without comment symbols
459+
*/
421460
const parse = (code, rev, no_comments) => {
422461
// Clean variables in closure
423462
clean()

src/stringify.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,33 @@ const is_primitive_object = subject => {
332332
return PRIMITIVE_OBJECT_TYPES.includes(str)
333333
}
334334

335-
// TODO: js doc and usage examples
336-
337-
// @param {function()|Array} replacer
338-
// @param {string|number} space
335+
/**
336+
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string
337+
* with comments preserved.
338+
*
339+
* @param {*} value A JavaScript value, usually an object or array, to be
340+
* converted.
341+
* @param {function|Array|null} [replacer_] A function that transforms the
342+
* results or an array of strings and numbers that acts as an approved list
343+
* for selecting the object properties that will be stringified.
344+
* @param {string|number} [space] Adds indentation, white space, and line
345+
* break characters to the return-value JSON text to make it easier to read.
346+
* @returns {string} A JSON string representing the given value with comments
347+
* preserved.
348+
*
349+
* @example
350+
* const obj = parse('{"a": 1 // comment}')
351+
* stringify(obj, null, 2)
352+
* // Returns: '{\n "a": 1 // comment\n}'
353+
*
354+
* @example
355+
* // With replacer function
356+
* stringify(obj, (key, value) => typeof value === 'number' ? value * 2 : value)
357+
*
358+
* @example
359+
* // With replacer array
360+
* stringify(obj, ['a', 'b']) // Only include 'a' and 'b' properties
361+
*/
339362
module.exports = (value, replacer_, space) => {
340363
// The stringify method takes a value and an optional replacer, and an optional
341364
// space parameter, and returns a JSON text. The replacer can be a function

0 commit comments

Comments
 (0)