Skip to content

Commit

Permalink
support simpler comment
Browse files Browse the repository at this point in the history
  • Loading branch information
lpaladin committed Nov 19, 2018
1 parent eb1bbda commit 3ac9f7b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 29 deletions.
51 changes: 28 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ All code is written in TypeScript which can be self-explanatory.

The abstract base class of all writers.

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

`writer.stringify(value, replacer, space)`

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

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

`writer = new CustomCommentWriter(configuration)`
**`writer = new CustomCommentWriter(configuration)`**
* Please refer to the constructor of [JSONCommentWriterBase](#JSONCommentWriterBase).

`writer.addComments(selector, comments)`

**`writer.addComments(selector, comments)`**
* Add custom `comments` to fields specified by `selector`.
* `selector`: (string | number | undefined)[] (required)

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

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

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

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

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

### Interfaces
### Types

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

Represents a single comment.

* `type`: 'block' | 'line' | 'end' (required)

Type of the comment:
* `block` - block comment wrapped with '/\*' and '\*\/' before the item
* `line` - line comment beginning with '//' before the item
* `end` - line comment at the end of the item on the same line
Represents a single comment. Can be an object or a string.

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

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

Type of the comment:
* `block` - block comment wrapped with '/\*' and '\*\/' before the item
* `line` - line comment beginning with '//' before the item
* `end` - line comment at the end of the item on the same line

`line` and `end` are not supported if `space` when calling `stringify` is 0.
(Except for comments of root object)
* `content`: string or function (required)
Content of the comment. Could be:
* A single-line or multi-line string
* A function to be called when stringifying the matched field.
* Signature: (matchedFieldPath: (string | number)[]) => string | undefined
* Return `undefined` to omit.
* '*\/' will be escaped automatically if type is `block`.

License
--------------------------
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-inline-doc",
"version": "1.0.1",
"version": "1.0.2",
"description": "Add inline comments on stringified JSON, or generate from JSON schema",
"main": "./lib/index.js",
"typings": "./lib/index.d.ts",
Expand Down
7 changes: 5 additions & 2 deletions src/jsonCommentWriterBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ export abstract class JSONCommentWriterBase<CommentDataNodeType> {
protected abstract getComments(currentNode: Readonly<CommentDataNodeType>): IJSONComment[];

private renderComment(gap: string, path: (string | number)[], comment: IJSONComment): string | undefined {
if (typeof comment === 'string') {
comment = { type: 'block', content: comment };
}
let content: string | undefined = typeof comment.content === 'function' ? comment.content(path.slice(1)) : comment.content;
if (content === undefined) {
return undefined;
Expand Down Expand Up @@ -136,11 +139,11 @@ ${gap} */`;
this.path.push(nextKey);
if (nextNode) {
for (const comment of this.getComments(nextNode)) {
if (comment.type === 'end' && lineEndComment !== undefined) {
if (typeof comment === 'object' && comment.type === 'end' && lineEndComment !== undefined) {
throw new Error('Comment of type `end` is expected to be unique for each field');
}
const commentString: string | undefined = this.renderComment(gap, this.path, comment);
if (comment.type === 'end') {
if (typeof comment === 'object' && comment.type === 'end') {
lineEndComment = commentString;
} else if (commentString !== undefined) {
parts.push(commentString);
Expand Down
2 changes: 1 addition & 1 deletion src/test/mixedComment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('mixed comments', () => {
it('should generate correct comments', () => {
const w: CustomCommentWriter = new CustomCommentWriter();
w.addComments([], [{ type: 'line', content: 'test' }]);
w.addComments(['test'], [{ type: 'block', content: 'test2' }]);
w.addComments(['test'], ['test2']);
w.addComments(['test', 1], [{ type: 'end', content: 'test3' }]);
w.addComments(['test', undefined, undefined], [{ type: 'line', content: 'test4' }]);
w.addComments(['test', undefined, 'test2'], [{ type: 'block', content: path => path.toString() }]);
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { JSONSchema4, JSONSchema6, JSONSchema7 } from 'json-schema';

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

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

export interface IJSONCommentConfiguration {
/**
Expand Down

0 comments on commit 3ac9f7b

Please sign in to comment.