comment-parser
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -35,2 +35,7 @@ var CommentParser = (function (exports) { | ||
} | ||
/** | ||
* Assures Block.tags[].source contains references to the Block.source items, | ||
* using Block.source as a source of truth. This is a counterpart of rewireSpecs | ||
* @param block parsed coments block | ||
*/ | ||
function rewireSource(block) { | ||
@@ -37,0 +42,0 @@ var source = block.source.reduce(function (acc, line) { return acc.set(line.number, line); }, new Map()); |
@@ -8,2 +8,13 @@ import { Block, Tokens, Spec } from './primitives'; | ||
export declare function seedTokens(tokens?: Partial<Tokens>): Tokens; | ||
/** | ||
* Assures Block.tags[].source contains references to the Block.source items, | ||
* using Block.source as a source of truth. This is a counterpart of rewireSpecs | ||
* @param block parsed coments block | ||
*/ | ||
export declare function rewireSource(block: Block): Block; | ||
/** | ||
* Assures Block.tags[].source contains references to the Block.source items, | ||
* using Block.tags[].source as a source of truth. This is a counterpart of rewireSource | ||
* @param block parsed coments block | ||
*/ | ||
export declare function rewireSpecs(block: Block): Block; |
@@ -36,2 +36,7 @@ var __assign = (this && this.__assign) || function () { | ||
} | ||
/** | ||
* Assures Block.tags[].source contains references to the Block.source items, | ||
* using Block.source as a source of truth. This is a counterpart of rewireSpecs | ||
* @param block parsed coments block | ||
*/ | ||
export function rewireSource(block) { | ||
@@ -45,1 +50,13 @@ var source = block.source.reduce(function (acc, line) { return acc.set(line.number, line); }, new Map()); | ||
} | ||
/** | ||
* Assures Block.tags[].source contains references to the Block.source items, | ||
* using Block.tags[].source as a source of truth. This is a counterpart of rewireSource | ||
* @param block parsed coments block | ||
*/ | ||
export function rewireSpecs(block) { | ||
var source = block.tags.reduce(function (acc, spec) { | ||
return spec.source.reduce(function (acc, line) { return acc.set(line.number, line); }, acc); | ||
}, new Map()); | ||
block.source = block.source.map(function (line) { return source.get(line.number) || line; }); | ||
return block; | ||
} |
@@ -8,2 +8,13 @@ import { Block, Tokens, Spec } from './primitives'; | ||
export declare function seedTokens(tokens?: Partial<Tokens>): Tokens; | ||
/** | ||
* Assures Block.tags[].source contains references to the Block.source items, | ||
* using Block.source as a source of truth. This is a counterpart of rewireSpecs | ||
* @param block parsed coments block | ||
*/ | ||
export declare function rewireSource(block: Block): Block; | ||
/** | ||
* Assures Block.tags[].source contains references to the Block.source items, | ||
* using Block.tags[].source as a source of truth. This is a counterpart of rewireSource | ||
* @param block parsed coments block | ||
*/ | ||
export declare function rewireSpecs(block: Block): Block; |
@@ -14,3 +14,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.rewireSource = exports.seedTokens = exports.seedSpec = exports.seedBlock = exports.splitLines = exports.splitSpace = exports.isSpace = void 0; | ||
exports.rewireSpecs = exports.rewireSource = exports.seedTokens = exports.seedSpec = exports.seedBlock = exports.splitLines = exports.splitSpace = exports.isSpace = void 0; | ||
function isSpace(source) { | ||
@@ -46,2 +46,7 @@ return /^\s+$/.test(source); | ||
exports.seedTokens = seedTokens; | ||
/** | ||
* Assures Block.tags[].source contains references to the Block.source items, | ||
* using Block.source as a source of truth. This is a counterpart of rewireSpecs | ||
* @param block parsed coments block | ||
*/ | ||
function rewireSource(block) { | ||
@@ -56,1 +61,14 @@ var source = block.source.reduce(function (acc, line) { return acc.set(line.number, line); }, new Map()); | ||
exports.rewireSource = rewireSource; | ||
/** | ||
* Assures Block.tags[].source contains references to the Block.source items, | ||
* using Block.tags[].source as a source of truth. This is a counterpart of rewireSource | ||
* @param block parsed coments block | ||
*/ | ||
function rewireSpecs(block) { | ||
var source = block.tags.reduce(function (acc, spec) { | ||
return spec.source.reduce(function (acc, line) { return acc.set(line.number, line); }, acc); | ||
}, new Map()); | ||
block.source = block.source.map(function (line) { return source.get(line.number) || line; }); | ||
return block; | ||
} | ||
exports.rewireSpecs = rewireSpecs; |
@@ -23,3 +23,3 @@ # Migrating 0.x to 1.x | ||
- `spacing: "preserve"` keeps line breaks and space around as is. Indentation space counts from `*` delimiter or from the start of the line if the delimiter is omitted | ||
- `spacing: (lines: Line[]) => string` completely freeform joining strategy, since all original spacing can be accessed, there is no limit to how this can be implemented. See [types.s.ts](./lib/types.s.ts) and [spacer.s.ts](./lib/spacer.s.ts) | ||
- `spacing: (lines: Line[]) => string` completely freeform joining strategy, since all original spacing can be accessed, there is no limit to how this can be implemented. See [primitives.ts](./src/primitives.ts) and [spacer.ts](./src/parser/spacer.ts) | ||
@@ -49,4 +49,4 @@ ### `join: string | number | boolean` | ||
- ` fence: '```' ` same as 0.x | ||
- `fencer: (source: string) => boolean` same as 0.x, see [block-parser.d.ts](./lib/block-parser.d.ts) | ||
- ```` fence: '```' ```` same as 0.x | ||
- `fencer: (source: string) => boolean` same as 0.x, see [parser/block-parser.ts](./src/parser/block-parser.ts) | ||
@@ -60,7 +60,7 @@ ### `parsers: Parser[]` | ||
- `tokenizers: []Tokenizer` is a list of functions extracting the `tag`, `type`, `name` and `description` tokens from this string. See [spec-parser.ts](./lib/spec-parser.ts) and [types.ts](./lib/types.ts) | ||
- `tokenizers: []Tokenizer` is a list of functions extracting the `tag`, `type`, `name` and `description` tokens from this string. See [parser/spec-parser.ts](./src/parser/spec-parser.ts) and [primitives.ts](./src/primitives.ts) | ||
Default tokenizers chain is | ||
``` | ||
```js | ||
[ | ||
@@ -76,3 +76,3 @@ tagTokenizer(), | ||
``` | ||
```ts | ||
type Tokenizer = (spec: Spec) => Spec | ||
@@ -105,3 +105,3 @@ | ||
- `format: "align"` aligns tag, name, type, and descriptions into fixed-width columns | ||
- `format: (tokens: Tokens) => string[]` do what you like, resulting lines will be concatenated into the output. Despite the simple interface, this can be turned into a complex stateful formatter, see `"align"` implementation in [stringifier.ts](./src/stringifier.ts) | ||
- `format: (tokens: Tokens) => string[]` do what you like, resulting lines will be concatenated into the output. Despite the simple interface, this can be turned into a complex stateful formatter, see `"align"` implementation in [transforms/align.ts](./src/transforms/align.ts) | ||
@@ -108,0 +108,0 @@ ## Stream |
{ | ||
"name": "comment-parser", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Generic JSDoc-like comment parser", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -5,5 +5,5 @@ # comment-parser | ||
- **language-agnostic** – no semantic enforced. You decide what tags are and what they mean. And it can be used with any language supporting `/** */` source comments. | ||
- **no dependencies** – it is compact and environment-agnostic, can be ran on both server and browser sides | ||
- **highly customizable** – with a little of code you can deeply customize how comments are parsed | ||
- **language-agnostic** – no semantics enforced. You decide what tags are and what they mean. And it can be used with any language supporting `/** */` source comments. | ||
- **no dependencies** – it is compact and environment-agnostic, can be run on both the server and browser sides | ||
- **highly customizable** – with a little code you can deeply customize how comments are parsed | ||
- **bidirectional** - you can write comment blocks back to the source after updating or formatting | ||
@@ -24,5 +24,5 @@ - **strictly typed** - comes with generated `d.ts` data definitions since written in TypeScript | ||
Lets go over string parsing | ||
Let's go over string parsing: | ||
``` | ||
```js | ||
const { parse } = require('comment-parser/lib') | ||
@@ -34,4 +34,4 @@ | ||
* over few lines followed by @tags | ||
* @param name {string} name parameter | ||
* @param value {any} value of any type | ||
* @param {string} name name parameter | ||
* @param {any} value value of any type | ||
*/` | ||
@@ -44,7 +44,7 @@ | ||
The input source is fist parsed into lines, then lines split into tokens, and finally, tokens are processed into blocks of tags | ||
The input source is first parsed into lines, then lines split into tokens, and finally, tokens are processed into blocks of tags | ||
### Block | ||
``` | ||
```js | ||
/** | ||
@@ -60,3 +60,3 @@ * Description may go | ||
``` | ||
```js | ||
/** | ||
@@ -69,7 +69,7 @@ * Description may go | ||
``` | ||
```js | ||
* @param {string} name the name parameter | ||
``` | ||
``` | ||
```js | ||
* @param {any} value the value parameter | ||
@@ -82,3 +82,3 @@ */ | ||
``` | ||
| ... | * | ... | @param | ... | value | ... | {any} | ... | value of any type | ||
| ... | * | ... | @param | ... | {any} | ... | value | ... | the value of any type | ||
``` | ||
@@ -169,3 +169,3 @@ | ||
examples | ||
examples | ||
- [default config](https://syavorsky.github.io/comment-parser/#parse-defaults) | ||
@@ -184,3 +184,3 @@ - [line numbers control](https://syavorsky.github.io/comment-parser/#parse-line-numbering) | ||
```js | ||
const { parse, stringify, transforms: {flow, align, indent} } = require('./lib/'); | ||
const { parse, stringify, transforms: {flow, align, indent} } = require('comment-parser'); | ||
@@ -191,3 +191,3 @@ const source = ` | ||
* over multiple lines followed by @tags | ||
* | ||
* | ||
* @my-tag {my.type} my-name description line 1 | ||
@@ -205,3 +205,3 @@ description line 2 | ||
``` | ||
```js | ||
/** | ||
@@ -208,0 +208,0 @@ * Description may go |
@@ -58,2 +58,7 @@ import { Block, Tokens, Spec, Line } from './primitives'; | ||
/** | ||
* Assures Block.tags[].source contains references to the Block.source items, | ||
* using Block.source as a source of truth. This is a counterpart of rewireSpecs | ||
* @param block parsed coments block | ||
*/ | ||
export function rewireSource(block: Block): Block { | ||
@@ -69,1 +74,16 @@ const source = block.source.reduce( | ||
} | ||
/** | ||
* Assures Block.tags[].source contains references to the Block.source items, | ||
* using Block.tags[].source as a source of truth. This is a counterpart of rewireSource | ||
* @param block parsed coments block | ||
*/ | ||
export function rewireSpecs(block: Block): Block { | ||
const source = block.tags.reduce( | ||
(acc, spec) => | ||
spec.source.reduce((acc, line) => acc.set(line.number, line), acc), | ||
new Map<number, Line>() | ||
); | ||
block.source = block.source.map((line) => source.get(line.number) || line); | ||
return block; | ||
} |
@@ -8,3 +8,2 @@ import { | ||
seedSpec, | ||
rewireSource, | ||
} from '../../src/util'; | ||
@@ -121,48 +120,1 @@ | ||
}); | ||
test('rewireSource', () => { | ||
const source = () => [ | ||
{ | ||
number: 42, | ||
source: '/** @my-tag */', | ||
tokens: { | ||
start: '', | ||
delimiter: '/**', | ||
postDelimiter: ' ', | ||
tag: '@my-tag', | ||
postTag: ' ', | ||
name: '', | ||
postName: '', | ||
type: '', | ||
postType: '', | ||
description: '', | ||
end: '*/', | ||
}, | ||
}, | ||
]; | ||
const parsed = { | ||
description: '', | ||
tags: [ | ||
{ | ||
tag: 'my-tag', | ||
name: '', | ||
type: '', | ||
optional: false, | ||
description: '', | ||
problems: [], | ||
source: source(), | ||
}, | ||
], | ||
source: source(), | ||
problems: [], | ||
}; | ||
expect(parsed.source[0] === parsed.tags[0].source[0]).toBe(false); | ||
rewireSource(parsed); | ||
expect(parsed.source[0] === parsed.tags[0].source[0]).toBe(true); | ||
parsed.source[0].tokens.name = 'test'; | ||
expect(parsed.tags[0].source[0].tokens.name).toBe('test'); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
226773
91
7857