Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-extract-comments

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-extract-comments - npm Package Compare versions

Comparing version 0.1.0 to 1.0.0

69

index.js
/*!
* babel-extract-comments <https://github.com/jonschlinkert/babel-extract-comments>
*
* Copyright (c) 2014 Jon Schlinkert, contributors.
* Licensed under the MIT license.
* Copyright (c) 2014-2018, Jon Schlinkert.
* Released under the MIT License.
*/

@@ -10,2 +10,6 @@

const fs = require('fs');
const path = require('path');
const babylon = require('babylon');
/**

@@ -16,31 +20,46 @@ * Extract code comments from the given `string`.

* var extract = require('babel-extract-comments');
* extract('// this is a code comment');
* console.log(extract('// this is a code comment'));
* // [{ type: 'CommentBlock',
* // value: '!\n * babel-extract-comments <https://github.com/jonschlinkert/babel-extract-comments>\n *\n *
* // Copyright (c) 2014-2018, Jon Schlinkert.\n * Released under the MIT License.\n ',
* // start: 0,
* // end: 173,
* // loc: SourceLocation { start: [Position], end: [Position] } }]
* ```
* @param {String} `string` String of javascript
* @param {Function} `fn` Optional. Functionto transform commend objects (AST tokens)
* @return {Object} Object of code comments.
* @return {Array} Array of code comment objects.
* @api public
*/
module.exports = function(str, fn) {
var babel = require('babel-core');
var res = babel.transform(str, {
ast: true,
comments: true,
code: false
});
function extract(str, options) {
const res = babylon.parse(str, options);
return res.comments;
}
var comments = res.ast.comments;
var len = comments.length, i = -1;
while (++i < len) {
var comment = comments[i];
comment.range = [comment.start, comment.end];
if (typeof fn === 'function') {
var obj = fn(comment);
if (obj) {
comment = obj;
}
}
}
return comments;
/**
* Extract code comments from a JavaScript file.
*
* ```js
* console.log(extract.file('some-file.js'), { cwd: 'some/path' });
* // [ { type: 'Line',
* // value: ' this is a line comment',
* // range: [ 0, 25 ],
* // loc: { start: { line: 1, column: 0 }, end: { line: 1, column: 25 } } } ]
* ```
* @param {String} `file` Filepath to the file to parse.
* @param {Object} `options` Options to pass to [esprima][].
* @return {Array} Array of code comment objects.
* @api public
*/
extract.file = function(file, options) {
const opts = Object.assign({ cwd: process.cwd() }, options);
const str = fs.readFileSync(path.resolve(opts.cwd, file), 'utf8');
return extract(str, options);
};
/**
* Expose `extract`
*/
module.exports = extract;
{
"name": "babel-extract-comments",
"description": "Uses babel to extract JavaScript code comments from a string. Returns an array of comment objects, with line, column, index, comment type and comment string.",
"version": "0.1.0",
"description": "Uses babel (babylon) to extract JavaScript code comments from a JavaScript string or file.",
"version": "1.0.0",
"homepage": "https://github.com/jonschlinkert/babel-extract-comments",

@@ -17,3 +17,3 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)",

"engines": {
"node": ">=0.8"
"node": ">=4"
},

@@ -24,8 +24,10 @@ "scripts": {

"dependencies": {
"babel-core": "^6.3.26"
"babylon": "^6.18.0"
},
"devDependencies": {
"gulp-format-md": "^0.1.4"
"gulp-format-md": "^1.0.0",
"mocha": "^3.5.3"
},
"keywords": [
"babel",
"block",

@@ -36,3 +38,2 @@ "code",

"context",
"babel",
"extract",

@@ -44,2 +45,10 @@ "glob",

"verb": {
"toc": false,
"layout": "default",
"tasks": [
"readme"
],
"plugins": [
"gulp-format-md"
],
"related": {

@@ -53,7 +62,6 @@ "list": [

},
"plugins": [
"gulp-format-md"
],
"layout": "default"
"lint": {
"reflinks": true
}
}
}

@@ -1,17 +0,7 @@

# babel-extract-comments [![NPM version](https://img.shields.io/npm/v/babel-extract-comments.svg)](https://www.npmjs.com/package/babel-extract-comments) [![Build Status](https://img.shields.io/travis/jonschlinkert/babel-extract-comments.svg)](https://travis-ci.org/jonschlinkert/babel-extract-comments)
# babel-extract-comments [![NPM version](https://img.shields.io/npm/v/babel-extract-comments.svg?style=flat)](https://www.npmjs.com/package/babel-extract-comments) [![NPM monthly downloads](https://img.shields.io/npm/dm/babel-extract-comments.svg?style=flat)](https://npmjs.org/package/babel-extract-comments) [![NPM total downloads](https://img.shields.io/npm/dt/babel-extract-comments.svg?style=flat)](https://npmjs.org/package/babel-extract-comments) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/babel-extract-comments.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/babel-extract-comments)
> Uses babel to extract JavaScript code comments from a string. Returns an array of comment objects, with line, column, index, comment type and comment string.
> Uses babel (babylon) to extract JavaScript code comments from a JavaScript string or file.
- [Install](#install)
- [API](#api)
* [Transform function](#transform-function)
- [Example results](#example-results)
- [Related projects](#related-projects)
- [Running tests](#running-tests)
- [Contributing](#contributing)
- [Author](#author)
- [License](#license)
Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
_(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_
## Install

@@ -22,12 +12,23 @@

```sh
$ npm i babel-extract-comments --save
$ npm install --save babel-extract-comments
```
## Usage
Add to your Node.js/JavaScript project with the following line of code:
```js
const extract = require('babel-extract-comments');
```
## API
### [extract](index.js#L32)
Extract code comments from the given `string`.
**Params**
* `string` **{String}**: String of javascript
* `fn` **{Function}**: Optional. Functionto transform commend objects (AST tokens)
* `returns` **{Object}**: Object of code comments.
* `returns` **{Array}**: Array of code comment objects.

@@ -38,64 +39,95 @@ **Example**

var extract = require('babel-extract-comments');
extract('// this is a code comment');
console.log(extract('// this is a code comment'));
// [{ type: 'CommentBlock',
// value: '!\n * babel-extract-comments <https://github.com/jonschlinkert/babel-extract-comments>\n *\n *
// Copyright (c) 2014-2018, Jon Schlinkert.\n * Released under the MIT License.\n ',
// start: 0,
// end: 173,
// loc: SourceLocation { start: [Position], end: [Position] } }]
```
### Transform function
### [.file](index.js#L53)
Optionally pass a function as the second argument to transform comment objects.
Extract code comments from a JavaScript file.
**Params**
* `file` **{String}**: Filepath to the file to parse.
* `options` **{Object}**: Options to pass to [esprima](http://esprima.org).
* `returns` **{Array}**: Array of code comment objects.
**Example**
```js
var comments = extract(javascriptString, function(comment) {
comment.foo = 'bar';
});
console.log(comments[0].foo);
//=> 'bar';
console.log(extract.file('some-file.js'), { cwd: 'some/path' });
// [ { type: 'Line',
// value: ' this is a line comment',
// range: [ 0, 25 ],
// loc: { start: { line: 1, column: 0 }, end: { line: 1, column: 25 } } } ]
```
## Example results
## About
See [fixtures/app.js](./fixtures/app.js) to see the code comments used to generate the following:
<details>
<summary><strong>Contributing</strong></summary>
```js
[{ type: 'Block',
value: '*\n * Create an instance of App with `options`.\n *\n * @param {Object} options\n * @api public\n ',
range: [ 15, 112 ],
loc: {
start: { line: 3, column: 0 },
end: { line: 8, column: 3 } }
}]
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
</details>
<details>
<summary><strong>Running Tests</strong></summary>
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
```sh
$ npm install && npm test
```
## Related projects
</details>
* [esprima-extract-comments](https://www.npmjs.com/package/esprima-extract-comments): Extract code comments from string or from a glob of files using esprima. | [homepage](https://github.com/jonschlinkert/esprima-extract-comments)
* [extract-comments](https://www.npmjs.com/package/extract-comments): Uses esprima to extract line and block comments from a string of JavaScript. Also optionally… [more](https://www.npmjs.com/package/extract-comments) | [homepage](https://github.com/jonschlinkert/extract-comments)
* [js-comments](https://www.npmjs.com/package/js-comments): Parse JavaScript code comments and generate API documentation. | [homepage](https://github.com/jonschlinkert/js-comments)
* [parse-comments](https://www.npmjs.com/package/parse-comments): Parse code comments from JavaScript or any language that uses the same format. | [homepage](https://github.com/jonschlinkert/parse-comments)
<details>
<summary><strong>Building docs</strong></summary>
## Running tests
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
Install dev dependencies:
To generate the readme, run the following command:
```sh
$ npm i -d && npm test
$ npm install -g verbose/verb#dev verb-generate-readme && verb
```
## Contributing
</details>
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/babel-extract-comments/issues/new).
### Related projects
## Author
You might also be interested in these projects:
* [esprima-extract-comments](https://www.npmjs.com/package/esprima-extract-comments): Extract code comments from string or from a glob of files using esprima. | [homepage](https://github.com/jonschlinkert/esprima-extract-comments "Extract code comments from string or from a glob of files using esprima.")
* [extract-comments](https://www.npmjs.com/package/extract-comments): Uses esprima to extract line and block comments from a string of JavaScript. Also optionally… [more](https://github.com/jonschlinkert/extract-comments) | [homepage](https://github.com/jonschlinkert/extract-comments "Uses esprima to extract line and block comments from a string of JavaScript. Also optionally parses code context (the next line of code after a comment).")
* [js-comments](https://www.npmjs.com/package/js-comments): Parse JavaScript code comments and generate API documentation. | [homepage](https://github.com/jonschlinkert/js-comments "Parse JavaScript code comments and generate API documentation.")
* [parse-comments](https://www.npmjs.com/package/parse-comments): Parse code comments from JavaScript or any language that uses the same format. | [homepage](https://github.com/jonschlinkert/parse-comments "Parse code comments from JavaScript or any language that uses the same format.")
### Contributors
| **Commits** | **Contributor** |
| --- | --- |
| 4 | [jonschlinkert](https://github.com/jonschlinkert) |
| 1 | [eventualbuddha](https://github.com/eventualbuddha) |
### Author
**Jon Schlinkert**
* [linkedin/in/jonschlinkert](https://linkedin.com/in/jonschlinkert)
* [github/jonschlinkert](https://github.com/jonschlinkert)
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
## License
### License
Copyright © 2015 [Jon Schlinkert](https://github.com/jonschlinkert)
Released under the MIT license.
Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
Released under the [MIT License](LICENSE).
***
_This file was generated by [verb](https://github.com/verbose/verb) on December 30, 2015._
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on February 12, 2018._

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc