Skip to content

Commit 3ac9f7b

Browse files
committed
support simpler comment
1 parent eb1bbda commit 3ac9f7b

5 files changed

Lines changed: 37 additions & 29 deletions

File tree

README.md

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ All code is written in TypeScript which can be self-explanatory.
110110

111111
The abstract base class of all writers.
112112

113-
`writer = new JSONCommentWriterBase(configuration)`
113+
**`writer = new JSONCommentWriterBase(configuration)`**
114114
* Note: The above line of code is only for explanation. This class is abstract - do not try to `new` a instance by yourself!
115115
* `configuration`: object (optional)
116116
* `spaceAroundCommentSymbol`: boolean (default true)
@@ -135,7 +135,8 @@ The abstract base class of all writers.
135135
Not supported if `space` when calling `stringify` is 0.
136136
(Except for comments of root object)
137137

138-
`writer.stringify(value, replacer, space)`
138+
139+
**`writer.stringify(value, replacer, space)`**
139140
* Convert value to JSON string with comments.
140141
* The signature is the same as [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)
141142
* JSON stringify implementation is based on the following code: https://github.com/douglascrockford/JSON-js/blob/2a76286e00cdc1e98fbc9e9ec6589563a3a4c3bb/json2.js
@@ -145,10 +146,11 @@ The abstract base class of all writers.
145146

146147
A class of JSON comment writer which supports custom comments for fields specified by path.
147148

148-
`writer = new CustomCommentWriter(configuration)`
149+
**`writer = new CustomCommentWriter(configuration)`**
149150
* Please refer to the constructor of [JSONCommentWriterBase](#JSONCommentWriterBase).
150151

151-
`writer.addComments(selector, comments)`
152+
153+
**`writer.addComments(selector, comments)`**
152154
* Add custom `comments` to fields specified by `selector`.
153155
* `selector`: (string | number | undefined)[] (required)
154156

@@ -164,7 +166,7 @@ A class of JSON comment writer which supports custom comments for fields specifi
164166

165167
A class of JSON comment writer generating comments for fields specified in JSON schema.
166168

167-
`writer = new SchemaMetadataWriter(schemaObject, commentGenerator, configuration)`
169+
**`writer = new SchemaMetadataWriter(schemaObject, commentGenerator, configuration)`**
168170
* Construct a new SchemaMetadataWriter.
169171
* `schemaObject`: [JSONSchema](https://json-schema.org/) (required)
170172

@@ -182,30 +184,33 @@ A class of JSON comment writer generating comments for fields specified in JSON
182184

183185
Please refer to the constructor of [JSONCommentWriterBase](#JSONCommentWriterBase).
184186

185-
### Interfaces
187+
### Types
186188

187189
#### [IJSONComment](src/types.ts)
188190

189-
Represents a single comment.
190-
191-
* `type`: 'block' | 'line' | 'end' (required)
192-
193-
Type of the comment:
194-
* `block` - block comment wrapped with '/\*' and '\*\/' before the item
195-
* `line` - line comment beginning with '//' before the item
196-
* `end` - line comment at the end of the item on the same line
191+
Represents a single comment. Can be an object or a string.
197192

198-
`line` and `end` are not supported if `space` when calling `stringify` is 0.
199-
(Except for comments of root object)
200-
* `content`: string or function (required)
193+
* When being a string:
201194

202-
Content of the comment. Could be:
203-
* A single-line or multi-line string
204-
* A function to be called when stringifying the matched field.
205-
* Signature: (matchedFieldPath: (string | number)[]) => string | undefined
206-
* Return `undefined` to omit.
207-
* '*\/' will be escaped automatically if type is `block`.
195+
The comment will be assumed to be a block comment (see below), and the string will be the content of comment.
196+
* When being an object:
197+
* `type`: 'block' | 'line' | 'end' (required)
208198

199+
Type of the comment:
200+
* `block` - block comment wrapped with '/\*' and '\*\/' before the item
201+
* `line` - line comment beginning with '//' before the item
202+
* `end` - line comment at the end of the item on the same line
203+
204+
`line` and `end` are not supported if `space` when calling `stringify` is 0.
205+
(Except for comments of root object)
206+
* `content`: string or function (required)
207+
208+
Content of the comment. Could be:
209+
* A single-line or multi-line string
210+
* A function to be called when stringifying the matched field.
211+
* Signature: (matchedFieldPath: (string | number)[]) => string | undefined
212+
* Return `undefined` to omit.
213+
* '*\/' will be escaped automatically if type is `block`.
209214

210215
License
211216
--------------------------

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-inline-doc",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Add inline comments on stringified JSON, or generate from JSON schema",
55
"main": "./lib/index.js",
66
"typings": "./lib/index.d.ts",

src/jsonCommentWriterBase.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ export abstract class JSONCommentWriterBase<CommentDataNodeType> {
8080
protected abstract getComments(currentNode: Readonly<CommentDataNodeType>): IJSONComment[];
8181

8282
private renderComment(gap: string, path: (string | number)[], comment: IJSONComment): string | undefined {
83+
if (typeof comment === 'string') {
84+
comment = { type: 'block', content: comment };
85+
}
8386
let content: string | undefined = typeof comment.content === 'function' ? comment.content(path.slice(1)) : comment.content;
8487
if (content === undefined) {
8588
return undefined;
@@ -136,11 +139,11 @@ ${gap} */`;
136139
this.path.push(nextKey);
137140
if (nextNode) {
138141
for (const comment of this.getComments(nextNode)) {
139-
if (comment.type === 'end' && lineEndComment !== undefined) {
142+
if (typeof comment === 'object' && comment.type === 'end' && lineEndComment !== undefined) {
140143
throw new Error('Comment of type `end` is expected to be unique for each field');
141144
}
142145
const commentString: string | undefined = this.renderComment(gap, this.path, comment);
143-
if (comment.type === 'end') {
146+
if (typeof comment === 'object' && comment.type === 'end') {
144147
lineEndComment = commentString;
145148
} else if (commentString !== undefined) {
146149
parts.push(commentString);

src/test/mixedComment.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('mixed comments', () => {
2020
it('should generate correct comments', () => {
2121
const w: CustomCommentWriter = new CustomCommentWriter();
2222
w.addComments([], [{ type: 'line', content: 'test' }]);
23-
w.addComments(['test'], [{ type: 'block', content: 'test2' }]);
23+
w.addComments(['test'], ['test2']);
2424
w.addComments(['test', 1], [{ type: 'end', content: 'test3' }]);
2525
w.addComments(['test', undefined, undefined], [{ type: 'line', content: 'test4' }]);
2626
w.addComments(['test', undefined, 'test2'], [{ type: 'block', content: path => path.toString() }]);

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { JSONSchema4, JSONSchema6, JSONSchema7 } from 'json-schema';
33

44
export type JSONCommentType = 'block' | 'line' | 'end';
55

6-
export interface IJSONComment {
6+
export type IJSONComment = {
77
/**
88
* @description Type of the comment:
99
* * `block` - block comment wrapped with '/\*' and '\*\/' before the item
@@ -22,7 +22,7 @@ export interface IJSONComment {
2222
* '*\/' will be escaped automatically if type is `block`.
2323
*/
2424
content: ((matchedFieldPath: (string | number)[]) => string | undefined) | string;
25-
}
25+
} | string;
2626

2727
export interface IJSONCommentConfiguration {
2828
/**

0 commit comments

Comments
 (0)