comment-parser 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/browser/index.js CHANGED
@@ -225,8 +225,10 @@ var CommentParser = (function (exports) {
225
225
  * and populates the `spec.name`
226
226
  */
227
227
  function nameTokenizer() {
228
+ const typeEnd = (num, { tokens }, i) => tokens.type === '' ? num : i;
228
229
  return (spec) => {
229
- const { tokens } = spec.source[0];
230
+ // look for the name in the line where {type} ends
231
+ const { tokens } = spec.source[spec.source.reduce(typeEnd, 0)];
230
232
  const source = tokens.description.trimLeft();
231
233
  const quotedGroups = source.split('"');
232
234
  // if it starts with quoted group, assume it is a literal
@@ -5,8 +5,10 @@ const isQuoted = (s) => s && s.startsWith('"') && s.endsWith('"');
5
5
  * and populates the `spec.name`
6
6
  */
7
7
  export default function nameTokenizer() {
8
+ const typeEnd = (num, { tokens }, i) => tokens.type === '' ? num : i;
8
9
  return (spec) => {
9
- const { tokens } = spec.source[0];
10
+ // look for the name in the line where {type} ends
11
+ const { tokens } = spec.source[spec.source.reduce(typeEnd, 0)];
10
12
  const source = tokens.description.trimLeft();
11
13
  const quotedGroups = source.split('"');
12
14
  // if it starts with quoted group, assume it is a literal
package/es6/util.d.ts CHANGED
@@ -12,7 +12,7 @@ export declare function seedTokens(tokens?: Partial<Tokens>): Tokens;
12
12
  */
13
13
  export declare function rewireSource(block: Block): Block;
14
14
  /**
15
- * Assures Block.tags[].source contains references to the Block.source items,
15
+ * Assures Block.source contains references to the Block.tags[].source items,
16
16
  * using Block.tags[].source as a source of truth. This is a counterpart of rewireSource
17
17
  * @param block parsed coments block
18
18
  */
package/es6/util.js CHANGED
@@ -32,7 +32,7 @@ export function rewireSource(block) {
32
32
  return block;
33
33
  }
34
34
  /**
35
- * Assures Block.tags[].source contains references to the Block.source items,
35
+ * Assures Block.source contains references to the Block.tags[].source items,
36
36
  * using Block.tags[].source as a source of truth. This is a counterpart of rewireSource
37
37
  * @param block parsed coments block
38
38
  */
@@ -7,8 +7,10 @@ const isQuoted = (s) => s && s.startsWith('"') && s.endsWith('"');
7
7
  * and populates the `spec.name`
8
8
  */
9
9
  function nameTokenizer() {
10
+ const typeEnd = (num, { tokens }, i) => tokens.type === '' ? num : i;
10
11
  return (spec) => {
11
- const { tokens } = spec.source[0];
12
+ // look for the name in the line where {type} ends
13
+ const { tokens } = spec.source[spec.source.reduce(typeEnd, 0)];
12
14
  const source = tokens.description.trimLeft();
13
15
  const quotedGroups = source.split('"');
14
16
  // if it starts with quoted group, assume it is a literal
package/lib/util.d.ts CHANGED
@@ -12,7 +12,7 @@ export declare function seedTokens(tokens?: Partial<Tokens>): Tokens;
12
12
  */
13
13
  export declare function rewireSource(block: Block): Block;
14
14
  /**
15
- * Assures Block.tags[].source contains references to the Block.source items,
15
+ * Assures Block.source contains references to the Block.tags[].source items,
16
16
  * using Block.tags[].source as a source of truth. This is a counterpart of rewireSource
17
17
  * @param block parsed coments block
18
18
  */
package/lib/util.js CHANGED
@@ -42,7 +42,7 @@ function rewireSource(block) {
42
42
  }
43
43
  exports.rewireSource = rewireSource;
44
44
  /**
45
- * Assures Block.tags[].source contains references to the Block.source items,
45
+ * Assures Block.source contains references to the Block.tags[].source items,
46
46
  * using Block.tags[].source as a source of truth. This is a counterpart of rewireSource
47
47
  * @param block parsed coments block
48
48
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "comment-parser",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Generic JSDoc-like comment parser",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -1,4 +1,4 @@
1
- import { Spec } from '../../primitives';
1
+ import { Spec, Line } from '../../primitives';
2
2
  import { splitSpace, isSpace, seedBlock } from '../../util';
3
3
  import { Tokenizer } from './index';
4
4
 
@@ -9,8 +9,12 @@ const isQuoted = (s: string) => s && s.startsWith('"') && s.endsWith('"');
9
9
  * and populates the `spec.name`
10
10
  */
11
11
  export default function nameTokenizer(): Tokenizer {
12
+ const typeEnd = (num: number, { tokens }: Line, i: number) =>
13
+ tokens.type === '' ? num : i;
14
+
12
15
  return (spec: Spec): Spec => {
13
- const { tokens } = spec.source[0];
16
+ // look for the name in the line where {type} ends
17
+ const { tokens } = spec.source[spec.source.reduce(typeEnd, 0)];
14
18
  const source = tokens.description.trimLeft();
15
19
 
16
20
  const quotedGroups = source.split('"');
package/src/util.ts CHANGED
@@ -72,7 +72,7 @@ export function rewireSource(block: Block): Block {
72
72
  }
73
73
 
74
74
  /**
75
- * Assures Block.tags[].source contains references to the Block.source items,
75
+ * Assures Block.source contains references to the Block.tags[].source items,
76
76
  * using Block.tags[].source as a source of truth. This is a counterpart of rewireSource
77
77
  * @param block parsed coments block
78
78
  */
@@ -15,31 +15,35 @@ const source = `
15
15
  test('default', () => {
16
16
  const parsed = parse(source);
17
17
  // console.log(inspect(parsed[0]));
18
- expect(parsed[0].problems).toEqual([]);
19
- expect(parsed[0].tags[1].problems).toEqual([]);
20
- expect(parsed[0].tags[1].type).toEqual('function(number,{x:string})');
18
+ expect(parsed[0].tags[1]).toMatchObject({
19
+ tag: 'property',
20
+ type: 'function(number,{x:string})',
21
+ name: 'numberEater',
22
+ description: 'Method which takes a number.',
23
+ problems: [],
24
+ });
21
25
  });
22
26
 
23
27
  test('preserve', () => {
24
28
  const parsed = parse(source, { spacing: 'preserve' });
25
29
  // console.log(inspect(parsed[0]));
26
- expect(parsed[0].problems).toEqual([]);
27
- expect(parsed[0].tags[1].problems).toEqual([]);
28
- expect(parsed[0].tags[1].type).toEqual(
29
- 'function(\n number,\n {x:string}\n)'
30
- );
31
- expect(parsed[0].tags[1].description).toEqual(
32
- 'numberEater Method\n which takes a number.'
33
- );
30
+ expect(parsed[0].tags[1]).toMatchObject({
31
+ tag: 'property',
32
+ type: 'function(\n number,\n {x:string}\n)',
33
+ name: 'numberEater',
34
+ description: 'Method\n which takes a number.',
35
+ problems: [],
36
+ });
34
37
  });
35
38
 
36
39
  test('compact', () => {
37
40
  const parsed = parse(source, { spacing: 'compact' });
38
41
  // console.log(inspect(parsed[0]));
39
- expect(parsed[0].problems).toEqual([]);
40
- expect(parsed[0].tags[1].problems).toEqual([]);
41
- expect(parsed[0].tags[1].type).toEqual('function(number,{x:string})');
42
- expect(parsed[0].tags[1].description).toEqual(
43
- 'numberEater Method which takes a number.'
44
- );
42
+ expect(parsed[0].tags[1]).toMatchObject({
43
+ tag: 'property',
44
+ type: 'function(number,{x:string})',
45
+ name: 'numberEater',
46
+ description: 'Method which takes a number.',
47
+ problems: [],
48
+ });
45
49
  });
@@ -0,0 +1,23 @@
1
+ const { parse, inspect } = require('../../lib');
2
+
3
+ const source = `
4
+ /** Multi-line typedef for an options object type.
5
+ *
6
+ * @typedef {{
7
+ * prop: number
8
+ * }} MyOptions description text
9
+ */`;
10
+
11
+ test('name after multiline tag', () => {
12
+ const parsed = parse(source);
13
+ // console.log(inspect(parsed[0]));
14
+
15
+ expect(parsed[0].problems).toEqual([]);
16
+ expect(parsed[0].tags[0]).toMatchObject({
17
+ tag: 'typedef',
18
+ name: 'MyOptions',
19
+ type: '{prop: number}',
20
+ description: 'description text',
21
+ problems: [],
22
+ });
23
+ });
@@ -692,3 +692,45 @@ test('default with arrow', () => {
692
692
  })
693
693
  );
694
694
  });
695
+
696
+ test('after multiline {type}', () => {
697
+ const sourceIn = [
698
+ {
699
+ number: 0,
700
+ source: '...',
701
+ tokens: seedTokens({
702
+ tag: '@aram',
703
+ postTag: ' ',
704
+ type: '{function(',
705
+ }),
706
+ },
707
+ {
708
+ number: 1,
709
+ source: '...',
710
+ tokens: seedTokens({ type: ' number' }),
711
+ },
712
+ {
713
+ number: 2,
714
+ source: '...',
715
+ tokens: seedTokens({
716
+ type: ')}',
717
+ postType: ' ',
718
+ description: 'paramname description text',
719
+ }),
720
+ },
721
+ ];
722
+
723
+ const sourceOut = JSON.parse(JSON.stringify(sourceIn));
724
+ Object.assign(sourceOut[2].tokens, {
725
+ name: 'paramname',
726
+ postName: ' ',
727
+ description: 'description text',
728
+ });
729
+
730
+ expect(tokenize(seedSpec({ source: sourceIn }))).toEqual(
731
+ seedSpec({
732
+ name: 'paramname',
733
+ source: sourceOut,
734
+ })
735
+ );
736
+ });