comment-parser 0.7.1 → 0.7.5

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/.editorconfig ADDED
@@ -0,0 +1,12 @@
1
+ # This file is for unifying the coding style for different editors and IDEs
2
+ # editorconfig.org
3
+
4
+ root = true
5
+
6
+ [*]
7
+ indent_style = space
8
+ end_of_line = lf
9
+ charset = utf-8
10
+ trim_trailing_whitespace = true
11
+ insert_final_newline = true
12
+ indent_size = 2
@@ -0,0 +1,18 @@
1
+ {
2
+ // Use IntelliSense to learn about possible attributes.
3
+ // Hover to view descriptions of existing attributes.
4
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+ {
8
+ "type": "node",
9
+ "request": "launch",
10
+ "name": "Test this",
11
+ "skipFiles": [
12
+ "<node_internals>/**"
13
+ ],
14
+ "program": "${workspaceFolder}/node_modules/.bin/mocha",
15
+ "args": ["${file}"]
16
+ }
17
+ ]
18
+ }
package/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ # v0.7.5
2
+ - name parsing fixes
3
+
4
+ # v0.7.4
5
+ - node 8 backward compatibility fixes
6
+
7
+ # v0.7.3
8
+ - make stringify result more close to the source
9
+
10
+ # v0.7.2
11
+ - make stringify to start each line with * in multiline comments
12
+
1
13
  # v0.7.1
2
14
  - ensure non-space characters after asterisk are included in source
3
15
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "comment-parser",
3
- "version": "0.7.1",
3
+ "version": "0.7.5",
4
4
  "description": "Generic JSDoc-like comment parser. ",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -10,15 +10,17 @@
10
10
  "dependencies": {},
11
11
  "devDependencies": {
12
12
  "chai": "^4.2.0",
13
- "eslint": "^6.6.0",
14
- "eslint-config-standard": "^14.1.0",
15
- "eslint-plugin-import": "^2.18.2",
16
- "eslint-plugin-node": "^10.0.0",
13
+ "eslint": "^6.8.0",
14
+ "eslint-config-standard": "^14.1.1",
15
+ "eslint-plugin-import": "^2.20.2",
16
+ "eslint-plugin-node": "^11.1.0",
17
17
  "eslint-plugin-promise": "^4.2.1",
18
18
  "eslint-plugin-standard": "^4.0.1",
19
- "mocha": "^6.2.2",
20
- "nodemon": "^1.19.4",
21
- "typescript": "^3.6.4"
19
+ "mocha": "^7.1.2",
20
+ "nodemon": "^2.0.3",
21
+ "nyc": "^15.0.1",
22
+ "readable-stream": "^3.6.0",
23
+ "typescript": "^3.8.3"
22
24
  },
23
25
  "engines": {
24
26
  "node": ">= 6.0.0"
@@ -28,10 +30,19 @@
28
30
  "lint:fix": "eslint --fix .",
29
31
  "test:lint": "eslint .",
30
32
  "test:typescript": "tsc index.d.ts",
31
- "test:unit": "mocha tests",
33
+ "test:unit": "nyc mocha tests",
32
34
  "test": "npm run test:typescript && npm run test:lint && npm run test:unit",
33
35
  "watch": "nodemon -q -i node_modules -x npm test"
34
36
  },
37
+ "nyc": {
38
+ "branches": 85,
39
+ "lines": 85,
40
+ "functions": 85,
41
+ "statements": 85,
42
+ "exclude": [
43
+ "tests"
44
+ ]
45
+ },
35
46
  "repository": {
36
47
  "type": "git",
37
48
  "url": "git@github.com:yavorskiy/comment-parser.git"
@@ -50,7 +61,8 @@
50
61
  "Evgeny Reznichenko (https://github.com/zxcabs)",
51
62
  "Javier \"Ciberma\" Mora (https://github.com/jhm-ciberman)",
52
63
  "Jordan Harband (https://github.com/ljharb)",
53
- "tengattack (https://github.com/tengattack)"
64
+ "tengattack (https://github.com/tengattack)",
65
+ "Jayden Seric (https://github.com/jaydenseric)"
54
66
  ],
55
67
  "license": "MIT",
56
68
  "bugs": {
package/parser.js CHANGED
@@ -33,8 +33,6 @@ function find (list, filter) {
33
33
  * @returns {object} parsed tag node
34
34
  */
35
35
  function parse_tag (str, parsers) {
36
- if (typeof str !== 'string' || str[0] !== '@') { return null }
37
-
38
36
  const data = parsers.reduce(function (state, parser) {
39
37
  let result
40
38
 
@@ -140,8 +138,6 @@ function parse_block (source, opts) {
140
138
  const tags = source.reduce(function (tags, tag) {
141
139
  const tag_node = parse_tag(tag.source, opts.parsers)
142
140
 
143
- if (!tag_node) { return tags }
144
-
145
141
  tag_node.line = tag.line
146
142
  tag_node.source = tag.source
147
143
 
package/parsers.js CHANGED
@@ -13,13 +13,12 @@ function skipws (str) {
13
13
  const PARSERS = {}
14
14
 
15
15
  PARSERS.parse_tag = function parse_tag (str) {
16
- const result = str.match(/^\s*@(\S+)/)
17
-
18
- if (!result) { throw new Error('Invalid `@tag`, missing @ symbol') }
16
+ const match = str.match(/^\s*@(\S+)/)
17
+ if (!match) { throw new SyntaxError('Invalid `@tag`, missing @ symbol') }
19
18
 
20
19
  return {
21
- source: result[0],
22
- data: { tag: result[1] }
20
+ source: match[0],
21
+ data: { tag: match[1] }
23
22
  }
24
23
  }
25
24
 
@@ -39,7 +38,7 @@ PARSERS.parse_type = function parse_type (str, data) {
39
38
  if (curlies === 0) { break }
40
39
  }
41
40
 
42
- if (curlies !== 0) { throw new Error('Invalid `{type}`, unpaired curlies') }
41
+ if (curlies !== 0) { throw new SyntaxError('Invalid `{type}`, unpaired curlies') }
43
42
 
44
43
  return {
45
44
  source: str.slice(0, pos),
@@ -69,19 +68,22 @@ PARSERS.parse_name = function parse_name (str, data) {
69
68
  if (brackets === 0 && /\s/.test(str[pos])) { break }
70
69
  }
71
70
 
72
- if (brackets !== 0) { throw new Error('Invalid `name`, unpaired brackets') }
71
+ if (brackets !== 0) { throw new SyntaxError('Invalid `name`, unpaired brackets') }
73
72
 
74
- res = { name: name, optional: false }
73
+ res = { name, optional: false }
75
74
 
76
75
  if (name[0] === '[' && name[name.length - 1] === ']') {
77
76
  res.optional = true
78
77
  name = name.slice(1, -1)
79
78
 
80
- if (name.indexOf('=') !== -1) {
81
- const parts = name.split('=')
82
- name = parts[0]
83
- res.default = parts[1].replace(/^(["'])(.+)(\1)$/, '$2')
84
- }
79
+ const match = name.match(
80
+ /^\s*([^=]+?)(?:\s*=\s*(.+?))?\s*(?=$)/
81
+ )
82
+
83
+ if (!match) throw new SyntaxError('Invalid `name`, bad syntax')
84
+
85
+ name = match[1]
86
+ if (match[2]) res.default = match[2]
85
87
  }
86
88
  }
87
89
 
@@ -96,12 +98,12 @@ PARSERS.parse_name = function parse_name (str, data) {
96
98
  PARSERS.parse_description = function parse_description (str, data) {
97
99
  if (data.errors && data.errors.length) { return null }
98
100
 
99
- const result = str.match(/^\s+((.|\s)+)?/)
101
+ const match = str.match(/^\s+((.|\s)+)?/)
100
102
 
101
- if (result) {
103
+ if (match) {
102
104
  return {
103
- source: result[0],
104
- data: { description: result[1] === undefined ? '' : result[1] }
105
+ source: match[0],
106
+ data: { description: match[1] === undefined ? '' : match[1] }
105
107
  }
106
108
  }
107
109
 
package/stringifier.js CHANGED
@@ -33,10 +33,16 @@ const stringifyBlock = exports.stringifyBlock = function stringifyBlock (
33
33
  ) {
34
34
  // block.line
35
35
  const indnt = getIndent(indent)
36
- return (block.description ? `${indnt}* ${block.description}\n${indnt}*\n` : '') +
37
- block.tags.reduce((s, tag) => {
38
- return s + stringifyTag(tag, { indent })
39
- }, '')
36
+
37
+ return (
38
+ block.description
39
+ ? block.description.replace(/^\n/, '').replace(/^(.*)\n?/gm, (n0, descLine) => {
40
+ return `${indnt}*${descLine ? ` ${descLine}` : ''}\n`
41
+ })
42
+ : ''
43
+ ) + block.tags.reduce((s, tag) => {
44
+ return s + stringifyTag(tag, { indent })
45
+ }, '')
40
46
  }
41
47
 
42
48
  const stringifyTag = exports.stringifyTag = function stringifyTag (
@@ -46,11 +52,12 @@ const stringifyTag = exports.stringifyTag = function stringifyTag (
46
52
  const {
47
53
  type, name, optional, description, tag: tagName, default: deflt //, line , source
48
54
  } = tag
55
+
49
56
  return indnt + `* @${tagName}` +
50
57
  (type ? ` {${type}}` : '') +
51
- (name ? ` ${
58
+ (name.trim() ? ` ${
52
59
  optional ? '[' : ''
53
- }${name}${deflt ? `=${deflt}` : ''}${
60
+ }${name.trimRight()}${deflt ? `=${deflt}` : ''}${
54
61
  optional ? ']' : ''
55
62
  }` : '') +
56
63
  (description ? ` ${description.replace(/\n/g, '\n' + indnt + '* ')}` : '') + '\n'
package/.travis.yml DELETED
@@ -1,10 +0,0 @@
1
- language: node_js
2
- node_js:
3
- - 12
4
- - 10
5
- - 8
6
- branches:
7
- except:
8
- - /^v\d+\.\d+\.\d+$/
9
- git:
10
- depth: 1
@@ -1,132 +0,0 @@
1
- /* eslint no-unused-vars:off */
2
- 'use strict'
3
-
4
- const { expect } = require('chai')
5
- const parse = require('./parse')
6
-
7
- describe('parse() with custom tag parsers', function () {
8
- function sample () {
9
- /**
10
- * @tag {type} name description
11
- */
12
- var a
13
- }
14
-
15
- it('should use `opts.parsers`', function () {
16
- const parsers = [
17
- function everything (str) {
18
- return {
19
- source: str,
20
- data: {
21
- tag: 'tag',
22
- type: 'type',
23
- name: 'name',
24
- optional: false,
25
- description: 'description'
26
- }
27
- }
28
- }
29
- ]
30
-
31
- expect(parse(sample, { parsers: parsers })[0])
32
- .to.eql({
33
- line: 1,
34
- description: '',
35
- source: '@tag {type} name description',
36
- tags: [{
37
- tag: 'tag',
38
- type: 'type',
39
- name: 'name',
40
- description: 'description',
41
- optional: false,
42
- source: '@tag {type} name description',
43
- line: 2
44
- }]
45
- })
46
- })
47
-
48
- it('should merge parsers result', function () {
49
- const parsers = [
50
- function parser1 (str) {
51
- return {
52
- source: '',
53
- data: { tag: 'tag' }
54
- }
55
- },
56
- function parser2 (str) {
57
- return {
58
- source: '',
59
- data: { type: 'type' }
60
- }
61
- },
62
- function parser3 (str) {
63
- return {
64
- source: '',
65
- data: {
66
- name: 'name',
67
- description: 'description'
68
- }
69
- }
70
- }
71
- ]
72
-
73
- expect(parse(sample, { parsers: parsers })[0])
74
- .to.eql({
75
- line: 1,
76
- description: '',
77
- source: '@tag {type} name description',
78
- tags: [{
79
- tag: 'tag',
80
- type: 'type',
81
- name: 'name',
82
- description: 'description',
83
- optional: false,
84
- source: '@tag {type} name description',
85
- line: 2
86
- }]
87
- })
88
- })
89
-
90
- it('should catch parser exceptions and populate `errors` field', function () {
91
- const parsers = [
92
- function parser1 (str) {
93
- return {
94
- source: '',
95
- data: { tag: 'tag' }
96
- }
97
- },
98
- function parser2 (str) {
99
- throw new Error('error 1')
100
- },
101
- function parser3 (str) {
102
- throw new Error('error 2')
103
- },
104
- function parser4 (str) {
105
- return {
106
- source: '',
107
- data: { name: 'name' }
108
- }
109
- }
110
- ]
111
-
112
- expect(parse(sample, { parsers: parsers })[0])
113
- .to.eql({
114
- line: 1,
115
- description: '',
116
- source: '@tag {type} name description',
117
- tags: [{
118
- tag: 'tag',
119
- type: '',
120
- name: 'name',
121
- description: '',
122
- optional: false,
123
- source: '@tag {type} name description',
124
- errors: [
125
- 'parser2: error 1',
126
- 'parser3: error 2'
127
- ],
128
- line: 2
129
- }]
130
- })
131
- })
132
- })
@@ -1,69 +0,0 @@
1
- /* eslint no-unused-vars:off */
2
- 'use strict'
3
-
4
- const fs = require('fs')
5
- const path = require('path')
6
- const stream = require('readable-stream')
7
- const { expect } = require('chai')
8
- const parse = require('../index')
9
-
10
- describe('File parsing', function () {
11
- it('should parse the file by path', function (done) {
12
- parse.file(path.resolve(__dirname, 'fixtures/sample.js'), function (err, parsed) {
13
- if (err) { done(err) }
14
-
15
- expect(parsed)
16
- .to.be.an('array')
17
-
18
- expect(parsed.length)
19
- .to.eq(4)
20
-
21
- expect(parsed[0].description)
22
- .to.eq('File description')
23
-
24
- expect(parsed[1].tags[0].tag)
25
- .to.eq('class')
26
-
27
- expect(parsed[2].tags[0].tag)
28
- .to.eq('property')
29
-
30
- expect(parsed[3].tags[0].tag)
31
- .to.eq('method')
32
-
33
- done()
34
- })
35
- })
36
-
37
- it('should path the error if file dows not exist', function (done) {
38
- parse.file('does/not/exists', function (err, parsed) {
39
- expect(err)
40
- .to.be.instanceof(Error)
41
- done()
42
- })
43
- })
44
-
45
- it('should return `Transform` stream', function (done) {
46
- let count = 0
47
-
48
- const readable = fs.createReadStream(path.resolve(__dirname, 'fixtures/sample.js'), { encoding: 'utf8' })
49
-
50
- const writable = new stream.Writable({ objectMode: true })
51
- writable._write = function (data, encoding, done) {
52
- count++
53
- done()
54
- }
55
-
56
- readable
57
- .on('error', done)
58
- .pipe(parse.stream())
59
- .on('error', done)
60
- .pipe(writable)
61
- .on('error', done)
62
- .on('finish', function () {
63
- expect(count)
64
- .to.eq(4)
65
-
66
- done()
67
- })
68
- })
69
- })
@@ -1,36 +0,0 @@
1
-
2
- /**
3
- * File description
4
- */
5
-
6
- /**
7
- * Sample class
8
- * @class Point
9
- * @param {Object} [coords]
10
- * @param {Int} coords.x
11
- * @param {Int} coords.y
12
- */
13
- function Point (options) {
14
-
15
- }
16
-
17
- /*
18
- * This should be skipped
19
- */
20
-
21
- /**
22
- * Tells if point was ever initilized with coordinates
23
- * @property {Boolean} [initilized=false]
24
- */
25
- Point.prototype.initilized = false
26
-
27
- /**
28
- * Moves point by x,y
29
- * @method move
30
- * @param {Object} [by]
31
- * @param {Int} by.x
32
- * @param {Int} by.y
33
- */
34
- Point.prototype.move = function (by) {
35
-
36
- }
package/tests/parse.js DELETED
@@ -1,20 +0,0 @@
1
- /**
2
- * Source lines numeration:
3
- *
4
- * 0 function() {
5
- * 1 // source with comments
6
- * 2 }
7
- *
8
- */
9
-
10
- 'use strict'
11
-
12
- const parse = require('../index')
13
-
14
- module.exports = function (func, opts) {
15
- let str = func.toString()
16
- str = str
17
- .slice(str.indexOf('{') + 1, str.lastIndexOf('}'))
18
- .replace(/\r\n/g, '\n')
19
- return parse(str, opts)
20
- }