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

extract-comments

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

extract-comments - npm Package Compare versions

Comparing version 0.9.0 to 0.10.0

lib/comments.js

135

index.js

@@ -0,6 +1,12 @@

/*!
* extract-comments <https://github.com/jonschlinkert/extract-comments>
*
* Copyright (c) 2014 Jon Schlinkert, contributors.
* Licensed under the MIT license.
*/
'use strict';
var Block = require('./lib/block');
var Line = require('./lib/line');
var utils = require('./lib/utils');
var extend = require('extend-shallow');
var Comments = require('./lib/comments');

@@ -19,9 +25,10 @@ /**

function comments(str, options, fn) {
if (typeof str !== 'string') {
throw new TypeError('expected a string');
function extract(str, options, fn) {
if (typeof options === 'function') {
fn = options;
options = {};
}
return block(str, options, fn)
.concat(line(str, options, fn))
.sort(compare);
var extracted = new Comments(options, fn);
var res = extracted.extract(str);
return res.comments || [];
}

@@ -35,2 +42,3 @@

* ```
* @name .block
* @param {String} `string`

@@ -42,4 +50,4 @@ * @param {Object} `options` Pass `first: true` to return after the first comment is found.

function block(str, options, fn) {
return factory('/*', '*/', Block)(str, options, fn);
function block(str, options) {
return extract(str, extend({line: false}, options));
}

@@ -53,2 +61,3 @@

* ```
* @name .line
* @param {String} `string`

@@ -60,65 +69,10 @@ * @param {Object} `options` Pass `first: true` to return after the first comment is found.

function line(str, options, fn) {
return factory('//', '\n', Line)(str, options, fn);
function line(str, options) {
return extract(str, extend({block: false}, options));
}
/**
* Factory for extracting comments from a string.
*
* @param {String} `string`
* @return {String}
*/
function factory(open, close, Ctor) {
return function(str, options, fn) {
if (typeof str !== 'string') {
throw new TypeError('expected a string');
}
if (typeof options === 'function') {
fn = options;
options = {};
}
if (typeof fn !== 'function') {
fn = utils.identity;
}
var opts = utils.extend({}, options);
str = utils.normalize(str);
str = utils.escapeQuoted(str);
var res = [];
var start = str.indexOf(open);
// respect escaped slashes
if (str.charAt(start - 1) === '\\') {
start = str.indexOf(open, start + 2);
}
var end = str.indexOf(close, start);
var len = str.length;
if (end === -1) {
end = len;
}
while (start !== -1 && end <= len) {
var comment = fn(new Ctor(str, start, end, open, close));
res.push(comment);
if (opts.first && res.length === 1) {
return res;
}
start = str.indexOf(open, end + 1);
end = str.indexOf(close, start);
if (end === -1) {
end = len;
}
}
return res;
};
}
/**
* Extract the first comment from the given `string`.
*
* @name .first
* @param {String} `string`

@@ -131,51 +85,24 @@ * @param {Object} `options` Pass `first: true` to return after the first comment is found.

function first(str) {
if (typeof str !== 'string') {
throw new TypeError('expected a string');
}
var arr = comments(str, {first: true});
if (arr && arr.length) {
return arr[0].raw;
} else {
return null;
}
return extract(str, {first: true});
}
/**
* Utility for sorting line and block comments into
* the correct order.
* Expose `extract`
*/
function compare(a, b) {
return a.loc.start.pos - b.loc.start.pos;
}
module.exports = extract;
/**
* Expose `extract` module
* Expose convenience methods
*/
module.exports = comments;
/**
* Expose `extract.first` method
*/
module.exports.first = first;
/**
* Expose `extract.block` method
*/
module.exports.block = block;
/**
* Expose `extract.line` method
*/
module.exports.line = line;
module.exports.first = first;
/**
* Expose `extract.factory` method
* Expose `Comments` constructor, to
* allow custom plugins to be registered.
*/
module.exports.factory = factory;
module.exports.Comments = Comments;
'use strict';
var utils = require('./utils');
var Code = require('./code');
/**
* Create a new BlockComment with:
* - `str` the entire string
* - `idx` the starting index of the comment
* - `end` the ending index of the comment
* - `open` the opening character(s) of the comment
* - `close` the closing character(s) of the comment
* Create a new BlockComment
*
* @param {String} `str` string of JavaScript
* @param {Object} `token` Parsed AST token
*/
function BlockComment(str, idx, end, open, close) {
var cl = close.length;
var lineno = utils.linesCount(str, idx);
var value = utils.restore(str.slice(idx, end + cl));
this.type = 'block';
this.raw = value;
this.value = utils.stripStars(value);
this.loc = {
start: {
line: lineno,
pos: idx
},
end: {
line: lineno + utils.linesCount(value) - 1,
pos: end + cl
}
};
/**
* Add code context
*/
this.code = new Code(str, this);
function BlockComment(str, token) {
this.type = token.type;
this.range = token.range;
this.loc = token.loc;
this.raw = token.value;
this.value = utils.stripStars(this.raw);
}

@@ -41,0 +19,0 @@

'use strict';
var utils = require('./utils');
/**
* Create a new LineComment with:
* - `str` the entire string
* - `idx` the starting index of the comment
* - `end` the ending index of the comment
* - `open` the opening character(s) of the comment (e.g. '//')
* - `close` the closing character(s) of the comment (e.g. '\n')
* Create a new LineComment.
*
* @param {String} `str` string of JavaScript
* @param {Object} `token` Parsed AST token
*/
function LineComment(str, idx, end, open, close) {
var lineno = utils.linesCount(str, idx);
var value = utils.restore(str.slice(idx, end));
this.type = 'line';
this.raw = value;
this.value = this.raw.replace(/^\s*[\/\s]+/, '');
this.loc = {
start: {
line: lineno,
pos: idx
},
end: {
line: lineno + utils.linesCount(value) - 1,
pos: end
}
};
function LineComment(str, token) {
this.type = token.type;
this.range = token.range;
this.loc = token.loc;
this.raw = token.value;
this.value = this.raw.trim();
}

@@ -33,0 +17,0 @@

@@ -7,89 +7,25 @@ 'use strict';

var utils = require('lazy-cache')(require);
var fn = require;
require = utils;
var utils = module.exports;
/**
* Lazily required module dependencies
*/
require('cr');
require('strip-bom-string', 'bom');
require('quoted-string-regex', 'quotesRegex');
require('parse-code-context', 'codeContext');
require('noncharacters', 'nonchar');
require('extend-shallow', 'extend');
require = fn;
/**
* Normalize newlines, strip carriage returns and
* byte order marks from `str`
*/
utils.normalize = function(str) {
return utils.cr(utils.bom(str));
};
/**
* Return the given value unchanged
*/
utils.identity = function(val) {
return val;
};
/**
* Get the total number of lines from the start
* of a string to the given index.
*/
utils.linesCount = function(str, i) {
if (typeof i === 'number') {
return str.slice(0, i).split('\n').length;
}
return str.split('\n').length;
};
/**
* Utility for getting a sequence of non-characters. The
* goal is to return a non-character string that is the
* same length as the characters we're replacing.
* Create regex for matching JavaScript linting config comments.
*
* http://www.unicode.org/faq/private_use.html#noncharacters
* @param {Array} `vendors`
* @return {RegExp}
*/
function ch(num) {
return utils.nonchar[num] + utils.nonchar[num];
}
/**
* Escaped comment characters in quoted strings
*
* @param {String} str
* @return {String}
*/
utils.escapeQuoted = function(str) {
return str.replace(utils.quotesRegex(), function(val) {
val = val.split('//').join(ch(0));
val = val.split('/*').join(ch(1));
val = val.split('*/').join(ch(2));
return val;
});
utils.toPrefixRegex = function(vendors) {
var prefixes = '(' + vendors.join('|') + ')';
return new RegExp('^' + prefixes);
};
/**
* Restore comment characters in quoted strings
* Trim trailing whitespace
*
* @param {String} str
* @param {String} `str`
* @return {String}
*/
utils.restore = function(str) {
return str.replace(utils.quotesRegex(), function(val) {
val = val.split(ch(0)).join('//');
val = val.split(ch(1)).join('/*');
val = val.split(ch(2)).join('*/');
return val;
});
utils.trimRight = function(str) {
return str.replace(/\s+$/, '');
};

@@ -105,10 +41,5 @@

utils.stripStars = function(str) {
str = str.replace(/^[\/* ]*|[\/* ]*$/g, '');
str = str.replace(/^ *[\/*]/gm, '');
var m = /(?:^|\n)([ \t]*)[^\s]/.exec(str);
if (m) {
str = str.replace(new RegExp('(^|\\n)' + m[1], 'g'), '$1');
}
str = str.replace(/^ *@(?=[^\s])/gm, '@');
return str.replace(/\s+$/, '');
str = str.replace(/^[ \t]/gm, '');
str = str.replace(/^[ \t]*\*[ \t]?/gm, '');
return utils.trimRight(str);
};

@@ -115,0 +46,0 @@

{
"name": "extract-comments",
"description": "Extract code comments from string or from a glob of files.",
"version": "0.9.0",
"version": "0.10.0",
"homepage": "https://github.com/jonschlinkert/extract-comments",

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

"index.js",
"lib/",
"utils.js"
"lib/"
],

@@ -26,10 +25,6 @@ "main": "index.js",

"dependencies": {
"cr": "^0.1.0",
"export-files": "^2.1.0",
"define-property": "^0.2.5",
"esprima-extract-comments": "^0.2.1",
"extend-shallow": "^2.0.1",
"lazy-cache": "^1.0.3",
"noncharacters": "^1.1.0",
"parse-code-context": "^0.2.1",
"quoted-string-regex": "^0.1.1",
"strip-bom-string": "^0.1.2"
"parse-code-context": "^0.2.1"
},

@@ -39,6 +34,8 @@ "devDependencies": {

"gulp-eslint": "^1.1.1",
"gulp-format-md": "^0.1.4",
"gulp-istanbul": "^0.10.3",
"gulp-mocha": "^2.2.0",
"mocha": "*",
"should": "*"
"should": "*",
"time-diff": "^0.1.0"
},

@@ -45,0 +42,0 @@ "keywords": [

@@ -7,3 +7,3 @@ # extract-comments [![NPM version](https://img.shields.io/npm/v/extract-comments.svg)](https://www.npmjs.com/package/extract-comments)

Install with [npm](https://www.npmjs.com/)
Install with [npm](https://www.npmjs.com/):

@@ -44,3 +44,3 @@ ```sh

### [comments](index.js#L19)
### [extract](index.js#L25)

@@ -61,3 +61,3 @@ Extract comments from the given `string`.

### [block](index.js#L40)
### [.block](index.js#L48)

@@ -78,3 +78,3 @@ Extract block comments from the given `string`.

### [line](index.js#L56)
### [.line](index.js#L65)

@@ -95,3 +95,3 @@ Extract line comments from the given `string`.

### [first](index.js#L125)
### [.first](index.js#L79)

@@ -109,3 +109,3 @@ Extract the first comment from the given `string`.

* [code-context](https://www.npmjs.com/package/code-context): Parse a string of javascript to determine the context for functions, variables and comments based… [more](https://www.npmjs.com/package/code-context) | [homepage](https://github.com/jonschlinkert/code-context)
* [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)
+ [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)
* [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)

@@ -129,3 +129,3 @@

As of December 26, 2015:
As of December 30, 2015:

@@ -159,2 +159,2 @@ ```sh

_This file was generated by [verb](https://github.com/verbose/verb) on December 26, 2015._
_This file was generated by [verb](https://github.com/verbose/verb) on December 30, 2015._
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