Skip to content

Commit 36b575d

Browse files
committed
v0.4.0
- no longer uses esprima because of how slow it was - comment key is now the line number - strings are now parsed by default - `.fromFile()` must be used to parse files/globs
1 parent a38f4d8 commit 36b575d

File tree

4 files changed

+73
-39
lines changed

4 files changed

+73
-39
lines changed

.verbrc.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ tags: ['verb-tag-jscomments']
55
> {%= description %}
66
77
## Install
8-
{%= include("install") %}
8+
{%= include("install", {save: true}) %}
99

1010
## Run tests
1111

@@ -81,4 +81,10 @@ Or when `extract.fromString()` is used:
8181
8282
***
8383
84-
{%= include("footer") %}
84+
{%= include("footer") %}
85+
86+
87+
88+
[globby]: https://github.com/sindresorhus/globby
89+
[esprima]: https://github.com/ariya/esprima
90+
[map-files]: https://github.com/jonschlinkert/map-files

README.md

+16-11
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,33 @@ npm test
1818
```
1919

2020
## API
21-
### [extract](index.js#L32)
21+
### [extract](index.js#L31)
2222

23-
Extract code comments from a glob of files:
23+
Extract code comments from the given `string`.
2424

25-
* `patterns` **{String}**: Glob patterns to used.
26-
* `options` **{Object}**: Options to pass to [esprima] or [globby], or [map-files].
25+
* `string` **{String}**
2726
* `returns` **{Object}**: Object of code comments.
2827

2928
**Example:**
3029

3130
```js
3231
var extract = require('extract-comments');
33-
extract('lib/*.js');
32+
extract('// this is a code comment');
3433
```
3534

36-
### [.fromString](index.js#L62)
35+
### [.fromFiles](index.js#L74)
3736

38-
Extract code comments from the given `string`.
37+
Extract code comments from a file or glob of files:
3938

40-
* `string` **{String}**
41-
* `options` **{Object}**: Options to pass to esprima.
39+
* `patterns` **{String}**: Glob patterns to used.
40+
* `options` **{Object}**: Options to pass to [globby], or [map-files].
4241
* `returns` **{Object}**: Object of code comments.
4342

4443
**Example:**
4544

4645
```js
4746
var extract = require('extract-comments');
48-
extract.fromString('// this is a code comment');
47+
extract.fromFiles(['lib/*.js']);
4948
```
5049

5150
You may also pass a custom `rename` function on the options to change the key of each object returned. See [map-files](https://github.com/jonschlinkert/map-files) for more details and available options.
@@ -115,4 +114,10 @@ Released under the MIT license
115114
116115
***
117116
118-
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on September 20, 2014._
117+
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on September 24, 2014._
118+
119+
120+
121+
[globby]: https://github.com/sindresorhus/globby
122+
[esprima]: https://github.com/ariya/esprima
123+
[map-files]: https://github.com/jonschlinkert/map-files

index.js

+43-24
Original file line numberDiff line numberDiff line change
@@ -8,60 +8,79 @@
88
'use strict';
99

1010
var fs = require('fs');
11-
var esprima = require('esprima');
1211
var mapFiles = require('map-files');
13-
var _ = require('lodash');
12+
var lineCount = require('line-count');
13+
var extend = require('mixin-deep');
1414

1515

1616
/**
17-
* Extract code comments from a glob of files:
17+
* Extract code comments from the given `string`.
1818
*
1919
* **Example:**
2020
*
2121
* ```js
2222
* var extract = require('extract-comments');
23-
* extract('lib/*.js');
23+
* extract('// this is a code comment');
2424
* ```
2525
*
26-
* @param {String} `patterns` Glob patterns to used.
27-
* @param {Object} `options` Options to pass to [esprima] or [globby], or [map-files].
26+
* @param {String} `string`
2827
* @return {Object} Object of code comments.
2928
* @api public
3029
*/
3130

32-
function extract(patterns, options) {
33-
return mapFiles(patterns, _.extend({
34-
// noop for mapFiles
35-
rename: function(filepath) {
36-
return filepath;
37-
},
38-
parse: function (filepath, options) {
39-
var code = fs.readFileSync(filepath, 'utf8');
40-
return extract.fromString(code, options);
41-
}
42-
}, options));
31+
function extract(str) {
32+
var match, o = {};
33+
var line = 1;
34+
35+
while (match = (/\/\*{1,2}[^\*!]([\s\S]*?)\*\//g).exec(str)) {
36+
var start = str;
37+
38+
// add lines from before the comment
39+
line += lineCount(start.substr(0, match.index)) - 1;
40+
41+
// Update the string
42+
str = str.substr(match.index + match[1].length);
43+
44+
o[line] = {
45+
type: 'comment',
46+
comment: match[1],
47+
begin: line,
48+
end: line + lineCount(match[1]) - 1
49+
};
50+
51+
// add lines from the comment itself
52+
line += lineCount(start.substr(match.index, match[1].length)) - 1;
53+
}
54+
return o;
4355
}
4456

4557

4658
/**
47-
* Extract code comments from the given `string`.
59+
* Extract code comments from a file or glob of files:
4860
*
4961
* **Example:**
5062
*
5163
* ```js
5264
* var extract = require('extract-comments');
53-
* extract.fromString('// this is a code comment');
65+
* extract.fromFiles(['lib/*.js']);
5466
* ```
5567
*
56-
* @param {String} `string`
57-
* @param {Object} `options` Options to pass to esprima.
68+
* @param {String} `patterns` Glob patterns to used.
69+
* @param {Object} `options` Options to pass to [globby], or [map-files].
5870
* @return {Object} Object of code comments.
5971
* @api public
6072
*/
6173

62-
extract.fromString = function(string, options) {
63-
var opts = _.extend({comment: true, loc: true }, options);
64-
return esprima.parse(string, opts).comments;
74+
extract.fromFiles = function(patterns, options) {
75+
return mapFiles(patterns, extend({
76+
rename: function(filepath) {
77+
return filepath;
78+
},
79+
parse: function (filepath, options) {
80+
var code = fs.readFileSync(filepath, 'utf8');
81+
return extract(code, options);
82+
}
83+
}, options));
6584
};
6685

6786

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "extract-comments",
33
"description": "Extract code comments from string or from a glob of files.",
4-
"version": "0.3.0",
4+
"version": "0.4.0",
55
"homepage": "https://github.com/jonschlinkert/extract-comments",
66
"author": {
77
"name": "Jon Schlinkert",
@@ -23,6 +23,9 @@
2323
"keywords": [
2424
"code",
2525
"parse",
26+
"javascript",
27+
"block",
28+
"context",
2629
"comments",
2730
"comment",
2831
"extract",
@@ -43,7 +46,8 @@
4346
"verb-tag-jscomments": "^0.2.2"
4447
},
4548
"dependencies": {
46-
"esprima": "git+https://git@github.com/ariya/esprima",
49+
"mixin-deep": "^0.1.0",
50+
"line-count": "^0.1.0",
4751
"lodash": "^2.4.1",
4852
"map-files": "^0.1.1"
4953
}

0 commit comments

Comments
 (0)