parse-code-context
Advanced tools
Comparing version 0.2.2 to 1.0.0
548
index.js
@@ -9,4 +9,6 @@ 'use strict'; | ||
* ```js | ||
* var parser = new Parser('function foo(a, b, c){}'); | ||
* const { Parser } = require('parse-code-context'); | ||
* const parser = new Parser('function foo(a, b, c) {}'); | ||
* ``` | ||
* @name Parser | ||
* @param {String} `str` | ||
@@ -17,293 +19,297 @@ * @param {String} `parent` | ||
function Parser(str, parent) { | ||
this.string = str; | ||
this.parent = parent; | ||
this.fns = []; | ||
} | ||
class Parser { | ||
constructor(parent) { | ||
this.parent = parent; | ||
this.fns = []; | ||
this.init(); | ||
} | ||
/** | ||
* Convenience method for creating a property name | ||
* that is prefixed with the parent namespace, if defined. | ||
* | ||
* @param {String} `name` | ||
* @return {String} | ||
* @api public | ||
*/ | ||
/** | ||
* Convenience method for creating a property name | ||
* that is prefixed with the parent namespace, if defined. | ||
* | ||
* @name .name | ||
* @param {String} `name` | ||
* @return {String} | ||
* @api public | ||
*/ | ||
Parser.prototype.name = function(name) { | ||
return this.parent ? (this.parent + name) : ''; | ||
}; | ||
name(name) { | ||
return this.parent ? this.parent + (name || '') : ''; | ||
} | ||
/** | ||
* Register a parser to use (in addition to those already | ||
* registered as default parsers) with the given `regex` and | ||
* function. | ||
* | ||
* ```js | ||
* var parser = new Parser('function foo(a, b, c){}'); | ||
* .use(/function\s*([\w$]+)\s*\(([^)]+)/, function(match) { | ||
* return { | ||
* name: match[1], | ||
* params: matc(h[2] || '').split(/[, ]/) | ||
* }; | ||
* }) | ||
* ``` | ||
* @param {RegExp} `regex` | ||
* @param {Function} `fn` | ||
* @return {Object} The instance for chaining | ||
* @api public | ||
*/ | ||
/** | ||
* Register a parser to use (in addition to those already | ||
* registered as default parsers) with the given `regex` and | ||
* function. | ||
* | ||
* ```js | ||
* const parser = new Parser('function foo(a, b, c){}'); | ||
* .capture(/function\s*([\w$]+)\s*\(([^)]+)/, (match) => { | ||
* return { | ||
* name: match[1], | ||
* params: matc(h[2] || '').split(/[,\s]/) | ||
* }; | ||
* }); | ||
* ``` | ||
* @name .capture | ||
* @param {RegExp} `regex` | ||
* @param {Function} `fn` | ||
* @return {Object} The instance for chaining | ||
* @api public | ||
*/ | ||
Parser.prototype.use = function(regex, fn) { | ||
this.fns.push({regex: regex, fn: fn}); | ||
return this; | ||
}; | ||
capture(regex, fn) { | ||
this.fns.push({ regex, fn }); | ||
return this; | ||
} | ||
/** | ||
* Parse the string passed to the constructor with | ||
* all registered parsers. | ||
* | ||
* @return {Object|Null} | ||
* @api public | ||
*/ | ||
/** | ||
* Parse the string passed to the constructor with all registered parsers. | ||
* | ||
* @name .parse | ||
* @return {Object|Null} | ||
* @api public | ||
*/ | ||
Parser.prototype.parse = function() { | ||
this.init(); | ||
var len = this.fns.length; | ||
var i = -1; | ||
while (++i < len) { | ||
var parser = this.fns[i]; | ||
var re = parser.regex; | ||
var fn = parser.fn; | ||
var match = re.exec(this.string); | ||
if (match) { | ||
var ctx = fn.call(this, match, this.parent); | ||
if (ctx) { | ||
this.value = ctx; | ||
return ctx; | ||
parse(str, parent) { | ||
this.parent = parent || this.parent; | ||
for (let parser of this.fns) { | ||
let re = parser.regex; | ||
let fn = parser.fn; | ||
let match = re.exec(str); | ||
if (match) { | ||
let ctx = fn.call(this, match, this.parent); | ||
if (ctx) { | ||
ctx.match = match; | ||
this.value = ctx; | ||
return ctx; | ||
} | ||
} | ||
} | ||
} | ||
return null; | ||
}; | ||
Parser.prototype.init = function() { | ||
// module.exports method | ||
this.use(/^(module\.exports)\s*=\s*function\s*\(([^)]+)/, function(m, parent) { | ||
return { | ||
type: 'method', | ||
receiver: m[1], | ||
name: '', | ||
params: (m[2] || '').split(/[, ]+/), | ||
string: m[1] + '.' + m[2] + '()', | ||
original: m.input | ||
}; | ||
}); | ||
init() { | ||
// module.exports method | ||
this.capture(/^(module\.exports)\s*=\s*function\s*\(([^)]+)/, (m, parent) => { | ||
return { | ||
type: 'method', | ||
receiver: m[1], | ||
name: '', | ||
params: params(m[2]), | ||
string: m[1] + '.' + m[2] + '()' | ||
}; | ||
}); | ||
this.use(/^(module\.exports)\s*=\s*function\s([\w$]+)\s*\(([^)]+)/, function(m, parent) { | ||
return { | ||
type: 'function', | ||
subtype: 'expression', | ||
receiver: m[1], | ||
name: m[2], | ||
params: (m[3] || '').split(/[, ]+/), | ||
string: m[2] + '()', | ||
original: m.input | ||
}; | ||
}); | ||
this.capture(/^(module\.exports)\s*=\s*function\s([\w$]+)\s*\(([^)]+)/, (m, parent) => { | ||
return { | ||
type: 'function', | ||
subtype: 'expression', | ||
receiver: m[1], | ||
name: m[2], | ||
params: params(m[3]), | ||
string: m[2] + '()' | ||
}; | ||
}); | ||
// class, possibly exported by name or as a default | ||
this.use(/^\s*(export(\s+default)?\s+)?class\s+([\w$]+)(\s+extends\s+([\w$.]+(?:\(.*\))?))?\s*{/, function(m, parent) { | ||
return { | ||
type: 'class', | ||
ctor: m[3], | ||
name: m[3], | ||
extends: m[5], | ||
string: 'new ' + m[3] + '()' | ||
}; | ||
}); | ||
// class, possibly exported by name or as a default | ||
this.capture(/^\s*(export(\s+default)?\s+)?class\s+([\w$]+)(\s+extends\s+([\w$.]+(?:\(.*\))?))?\s*{/, (m, parent) => { | ||
return { | ||
type: 'class', | ||
ctor: m[3], | ||
name: m[3], | ||
extends: m[5], | ||
string: 'new ' + m[3] + '()' | ||
}; | ||
}); | ||
// class constructor | ||
this.use(/^\s*constructor\s*\(([^)]+)/, function(m, parent) { | ||
return { | ||
type: 'constructor', | ||
ctor: this.parent, | ||
name: 'constructor', | ||
params: (m[4] || '').split(/[, ]+/), | ||
string: this.name('.prototype.') + 'constructor()' | ||
}; | ||
}); | ||
// class constructor | ||
this.capture(/^\s*constructor\s*\(([^)]+)/, (m, parent) => { | ||
return { | ||
type: 'constructor', | ||
ctor: this.parent, | ||
name: 'constructor', | ||
params: params(m[4]), | ||
string: this.name('.prototype.') + 'constructor()' | ||
}; | ||
}); | ||
// class method | ||
this.use(/^\s*(static)?\s*(\*)?\s*([\w$]+|\[.*\])\s*\(([^)]+)/, function(m, parent) { | ||
return { | ||
type: 'method', | ||
ctor: this.parent, | ||
name: m[2] + m[3], | ||
params: (m[4] || '').split(/[, ]+/), | ||
string: this.name(m[1] ? '.' : '.prototype.') + m[2] + m[3] + '()' | ||
}; | ||
}); | ||
// class method | ||
this.capture(/^\s*(static)?\s*(\*?)\s*(\[Symbol\.[^\]]+\]|[\w$]+|\[.*\])\s*\(([^)]*)/, (m, parent) => { | ||
return { | ||
type: 'method', | ||
ctor: this.parent, | ||
name: m[2] + m[3], | ||
params: params(m[4]), | ||
static: m[1] === 'static', | ||
generator: m[2] === '*', | ||
string: this.name(m[1] ? '.' : '.prototype.') + m[2] + m[3] + '()' | ||
}; | ||
}); | ||
// named function statement, possibly exported by name or as a default | ||
this.use(/^\s*(export(\s+default)?\s+)?function\s+([\w$]+)\s*\(([^)]+)/, function(m, parent) { | ||
return { | ||
type: 'function', | ||
subtype: 'statement', | ||
name: m[3], | ||
params: (m[4] || '').split(/[, ]+/), | ||
string: m[3] + '()' | ||
}; | ||
}); | ||
// named function statement, possibly exported by name or as a default | ||
this.capture(/^\s*(export(\s+default)?\s+)?function\s+([\w$]+)\s*\(([^)]+)/, (m, parent) => { | ||
return { | ||
type: 'function', | ||
subtype: 'statement', | ||
name: m[3], | ||
params: params(m[4]), | ||
string: m[3] + '()' | ||
}; | ||
}); | ||
// anonymous function expression exported as a default | ||
this.use(/^\s*export\s+default\s+function\s*\(([^)]+)/, function(m, parent) { | ||
return { | ||
type: 'function', | ||
name: m[1], // undefined | ||
params: (m[4] || '').split(/[, ]+/), | ||
string: m[1] + '()' | ||
}; | ||
}); | ||
// anonymous function expression exported as a default | ||
this.capture(/^\s*export\s+default\s+function\s*\(([^)]+)/, (m, parent) => { | ||
return { | ||
type: 'function', | ||
name: m[1], // undefined | ||
params: params(m[4]), | ||
string: m[1] + '()' | ||
}; | ||
}); | ||
// function expression | ||
this.use(/^return\s+function(?:\s+([\w$]+))?\s*\(([^)]+)/, function(m, parent) { | ||
return { | ||
type: 'function', | ||
subtype: 'expression', | ||
name: m[1], | ||
params: (m[4] || '').split(/[, ]+/), | ||
string: m[1] + '()' | ||
}; | ||
}); | ||
// function expression | ||
this.capture(/^return\s+function(?:\s+([\w$]+))?\s*\(([^)]+)/, (m, parent) => { | ||
return { | ||
type: 'function', | ||
subtype: 'expression', | ||
name: m[1], | ||
params: params(m[4]), | ||
string: m[1] + '()' | ||
}; | ||
}); | ||
// function expression | ||
this.use(/^\s*(?:const|let|var)\s+([\w$]+)\s*=\s*function\s*\(([^)]+)/, function(m, parent) { | ||
return { | ||
type: 'function', | ||
subtype: 'expression', | ||
name: m[1], | ||
params: (m[2] || '').split(/[, ]+/), | ||
string: (m[1] || '') + '()' | ||
}; | ||
}); | ||
// function expression | ||
this.capture(/^\s*(?:const|let|var)\s+([\w$]+)\s*=\s*function\s*\(([^)]+)/, (m, parent) => { | ||
return { | ||
type: 'function', | ||
subtype: 'expression', | ||
name: m[1], | ||
params: params(m[2]), | ||
string: (m[1] || '') + '()' | ||
}; | ||
}); | ||
// prototype method | ||
this.use(/^\s*([\w$.]+)\s*\.\s*prototype\s*\.\s*([\w$]+)\s*=\s*function\s*\(([^)]+)/, function(m, parent) { | ||
return { | ||
type: 'prototype method', | ||
category: 'method', | ||
ctor: m[1], | ||
name: m[2], | ||
params: (m[3] || '').split(/[, ]+/), | ||
string: m[1] + '.prototype.' + m[2] + '()' | ||
}; | ||
}); | ||
// prototype method | ||
this.capture(/^\s*([\w$.]+)\s*\.\s*prototype\s*\.\s*([\w$]+)\s*=\s*function\s*\(([^)]+)/, (m, parent) => { | ||
return { | ||
type: 'prototype method', | ||
category: 'method', | ||
ctor: m[1], | ||
name: m[2], | ||
params: params(m[3]), | ||
string: m[1] + '.prototype.' + m[2] + '()' | ||
}; | ||
}); | ||
// prototype property | ||
this.use(/^\s*([\w$.]+)\s*\.\s*prototype\s*\.\s*([\w$]+)\s*=\s*([^\n;]+)/, function(m, parent) { | ||
return { | ||
type: 'prototype property', | ||
ctor: m[1], | ||
name: m[2], | ||
value: trim(m[3]), | ||
string: m[1] + '.prototype.' + m[2] | ||
}; | ||
}); | ||
// prototype property | ||
this.capture(/^\s*([\w$.]+)\s*\.\s*prototype\s*\.\s*([\w$]+)\s*=\s*([^\n;]+)/, (m, parent) => { | ||
return { | ||
type: 'prototype property', | ||
ctor: m[1], | ||
name: m[2], | ||
value: trim(m[3]), | ||
string: m[1] + '.prototype.' + m[2] | ||
}; | ||
}); | ||
// prototype property without assignment | ||
this.use(/^\s*([\w$]+)\s*\.\s*prototype\s*\.\s*([\w$]+)\s*/, function(m, parent) { | ||
return { | ||
type: 'prototype property', | ||
ctor: m[1], | ||
name: m[2], | ||
string: m[1] + '.prototype.' + m[2] | ||
}; | ||
}); | ||
// prototype property without assignment | ||
this.capture(/^\s*([\w$]+)\s*\.\s*prototype\s*\.\s*([\w$]+)\s*/, (m, parent) => { | ||
return { | ||
type: 'prototype property', | ||
ctor: m[1], | ||
name: m[2], | ||
string: m[1] + '.prototype.' + m[2] | ||
}; | ||
}); | ||
// inline prototype | ||
this.use(/^\s*([\w$.]+)\s*\.\s*prototype\s*=\s*{/, function(m, parent) { | ||
return { | ||
type: 'prototype', | ||
ctor: m[1], | ||
name: m[1], | ||
string: m[1] + '.prototype' | ||
}; | ||
}); | ||
// inline prototype | ||
this.capture(/^\s*([\w$.]+)\s*\.\s*prototype\s*=\s*{/, (m, parent) => { | ||
return { | ||
type: 'prototype', | ||
ctor: m[1], | ||
name: m[1], | ||
string: m[1] + '.prototype' | ||
}; | ||
}); | ||
// Fat arrow function | ||
this.use(/^\s*\(*\s*([\w$.]+)\s*\)*\s*=>/, function(m, parent) { | ||
return { | ||
type: 'function', | ||
ctor: this.parent, | ||
name: m[1], | ||
string: this.name('.prototype.') + m[1] + '()' | ||
}; | ||
}); | ||
// Fat arrow function | ||
this.capture(/^\s*\(*\s*([\w$.]+)\s*\)*\s*=>/, (m, parent) => { | ||
return { | ||
type: 'function', | ||
ctor: this.parent, | ||
name: m[1], | ||
string: this.name('.prototype.') + m[1] + '()' | ||
}; | ||
}); | ||
// inline method | ||
this.use(/^\s*([\w$.]+)\s*:\s*function\s*\(([^)]+)/, function(m, parent) { | ||
return { | ||
type: 'method', | ||
ctor: this.parent, | ||
name: m[1], | ||
string: this.name('.prototype.') + m[1] + '()' | ||
}; | ||
}); | ||
// inline method | ||
this.capture(/^\s*([\w$.]+)\s*:\s*function\s*\(([^)]+)/, (m, parent) => { | ||
return { | ||
type: 'method', | ||
ctor: this.parent, | ||
name: m[1], | ||
string: this.name('.prototype.') + m[1] + '()' | ||
}; | ||
}); | ||
// inline property | ||
this.use(/^\s*([\w$.]+)\s*:\s*([^\n;]+)/, function(m, parent) { | ||
return { | ||
type: 'property', | ||
ctor: this.parent, | ||
name: m[1], | ||
value: trim(m[2]), | ||
string: this.name('.') + m[1] | ||
}; | ||
}); | ||
// inline property | ||
this.capture(/^\s*([\w$.]+)\s*:\s*([^\n;]+)/, (m, parent) => { | ||
return { | ||
type: 'property', | ||
ctor: this.parent, | ||
name: m[1], | ||
value: trim(m[2]), | ||
string: this.name('.') + m[1] | ||
}; | ||
}); | ||
// inline getter/setter | ||
this.use(/^\s*(get|set)\s*([\w$.]+)\s*\(([^)]+)/, function(m, parent) { | ||
return { | ||
type: 'property', | ||
ctor: this.parent, | ||
name: m[2], | ||
string: this.name('.prototype.') + m[2] | ||
}; | ||
}); | ||
// inline getter/setter | ||
this.capture(/^\s*(get|set)\s*([\w$.]+)\s*\(([^)]+)/, (m, parent) => { | ||
return { | ||
type: 'property', | ||
ctor: this.parent, | ||
name: m[2], | ||
string: this.name('.prototype.') + m[2] | ||
}; | ||
}); | ||
// method | ||
this.use(/^\s*([\w$.]+)\s*\.\s*([\w$]+)\s*=\s*function\s*\(([^)]+)/, function(m, parent) { | ||
return { | ||
type: 'method', | ||
receiver: m[1], | ||
name: m[2], | ||
params: (m[3] || '').split(/[, ]+/), | ||
string: m[1] + '.' + m[2] + '()' | ||
}; | ||
}); | ||
// method | ||
this.capture(/^\s*([\w$.]+)\s*\.\s*([\w$]+)\s*=\s*function\s*\(([^)]+)/, (m, parent) => { | ||
return { | ||
type: 'method', | ||
receiver: m[1], | ||
name: m[2], | ||
params: params(m[3]), | ||
string: m[1] + '.' + m[2] + '()' | ||
}; | ||
}); | ||
// property | ||
this.use(/^\s*([\w$.]+)\s*\.\s*([\w$]+)\s*=\s*([^\n;]+)/, function(m, parent) { | ||
return { | ||
type: 'property', | ||
receiver: m[1], | ||
name: m[2], | ||
value: trim(m[3]), | ||
string: m[1] + '.' + m[2] | ||
}; | ||
}); | ||
// property | ||
this.capture(/^\s*([\w$.]+)\s*\.\s*([\w$]+)\s*=\s*([^\n;]+)/, (m, parent) => { | ||
return { | ||
type: 'property', | ||
receiver: m[1], | ||
name: m[2], | ||
value: trim(m[3]), | ||
string: m[1] + '.' + m[2] | ||
}; | ||
}); | ||
// declaration | ||
this.use(/^\s*(?:const|let|var)\s+([\w$]+)\s*=\s*([^\n;]+)/, function(m, parent) { | ||
return { | ||
type: 'declaration', | ||
name: m[1], | ||
value: trim(m[2]), | ||
string: m[1] | ||
}; | ||
}); | ||
}; | ||
// declaration | ||
this.capture(/^\s*(?:const|let|var)\s+([\w$]+)\s*=\s*([^\n;]+)/, (m, parent) => { | ||
return { | ||
type: 'declaration', | ||
name: m[1], | ||
value: trim(m[2]), | ||
string: m[1] | ||
}; | ||
}); | ||
} | ||
} | ||
function params(val) { | ||
return trim(val).split(/[\s,]+/); | ||
} | ||
function trim(str) { | ||
@@ -314,19 +320,15 @@ return toString(str).trim(); | ||
function toString(str) { | ||
if (!str) return ''; | ||
return str; | ||
return str ? str.toString() : ''; | ||
} | ||
/** | ||
* Expose an instance of `Parser` | ||
* Expose `parse` | ||
*/ | ||
module.exports = function(str, ctx, i) { | ||
var parser = new Parser(str, ctx, i); | ||
return parser.parse(); | ||
const parse = (str, options) => { | ||
let parser = new Parser(options); | ||
return parser.parse(str); | ||
}; | ||
/** | ||
* Expose `Parser` | ||
*/ | ||
module.exports.Parser = Parser; | ||
parse.Parser = Parser; | ||
module.exports = parse; |
{ | ||
"name": "parse-code-context", | ||
"description": "Parse code context in a single line of javascript, for functions, variable declarations, methods, prototype properties, prototype methods etc.", | ||
"version": "0.2.2", | ||
"description": "Fast and simple way to parse code context for use with documentation from code comments. Parses context from a single line of JavaScript, for functions, variable declarations, methods, prototype properties, prototype methods etc.", | ||
"version": "1.0.0", | ||
"homepage": "https://github.com/jonschlinkert/parse-code-context", | ||
@@ -15,4 +15,5 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
], | ||
"main": "index.js", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=6" | ||
}, | ||
@@ -23,9 +24,4 @@ "scripts": { | ||
"devDependencies": { | ||
"gulp": "^3.9.0", | ||
"gulp-eslint": "^1.0.0", | ||
"gulp-format-md": "^0.1.4", | ||
"gulp-istanbul": "^0.10.2", | ||
"gulp-mocha": "^2.1.3", | ||
"mocha": "*", | ||
"should": "*" | ||
"gulp-format-md": "^2.0.0", | ||
"mocha": "^5.2.0" | ||
}, | ||
@@ -47,2 +43,7 @@ "keywords": [ | ||
"verb": { | ||
"toc": false, | ||
"layout": "default", | ||
"tasks": [ | ||
"readme" | ||
], | ||
"plugins": [ | ||
@@ -54,7 +55,10 @@ "gulp-format-md" | ||
"code-context", | ||
"strip-comments", | ||
"snapdragon" | ||
"snapdragon", | ||
"strip-comments" | ||
] | ||
}, | ||
"lint": { | ||
"reflinks": true | ||
} | ||
} | ||
} |
158
README.md
@@ -1,19 +0,85 @@ | ||
# parse-code-context [![NPM version](https://img.shields.io/npm/v/parse-code-context.svg)](https://www.npmjs.com/package/parse-code-context) [![Build Status](https://img.shields.io/travis/jonschlinkert/parse-code-context.svg)](https://travis-ci.org/jonschlinkert/parse-code-context) | ||
# parse-code-context [![NPM version](https://img.shields.io/npm/v/parse-code-context.svg?style=flat)](https://www.npmjs.com/package/parse-code-context) [![NPM monthly downloads](https://img.shields.io/npm/dm/parse-code-context.svg?style=flat)](https://npmjs.org/package/parse-code-context) [![NPM total downloads](https://img.shields.io/npm/dt/parse-code-context.svg?style=flat)](https://npmjs.org/package/parse-code-context) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/parse-code-context.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/parse-code-context) | ||
> Parse code context in a single line of javascript, for functions, variable declarations, methods, prototype properties, prototype methods etc. | ||
> Fast and simple way to parse code context for use with documentation from code comments. Parses context from a single line of JavaScript, for functions, variable declarations, methods, prototype properties, prototype methods etc. | ||
Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support. | ||
## Install | ||
Install with [npm](https://www.npmjs.com/) | ||
Install with [npm](https://www.npmjs.com/): | ||
```sh | ||
$ npm i parse-code-context --save | ||
$ npm install --save parse-code-context | ||
``` | ||
## Getting started | ||
* [Usage](#usage) | ||
* [API](#api) | ||
* [Examples](#examples) | ||
* [Custom parsers](#custom-parsers) | ||
## Usage | ||
```js | ||
var parse = require('parse-code-context'); | ||
const parse = require('parse-code-context'); | ||
console.log(parse('function app(a, b, c) {\n\n}')); | ||
``` | ||
## API | ||
### [Parser](index.js#L18) | ||
Create an instance of `Parser` with the given `string`, optionally passing a `parent` name for namespacing methods | ||
**Params** | ||
* `str` **{String}** | ||
* `parent` **{String}** | ||
**Example** | ||
```js | ||
const { Parser } = require('parse-code-context'); | ||
const parser = new Parser('function foo(a, b, c) {}'); | ||
``` | ||
### [.name](index.js#L35) | ||
Convenience method for creating a property name | ||
that is prefixed with the parent namespace, if defined. | ||
**Params** | ||
* `name` **{String}** | ||
* `returns` **{String}** | ||
### [.capture](index.js#L60) | ||
Register a parser to use (in addition to those already registered as default parsers) with the given `regex` and function. | ||
**Params** | ||
* `regex` **{RegExp}** | ||
* `fn` **{Function}** | ||
* `returns` **{Object}**: The instance for chaining | ||
**Example** | ||
```js | ||
const parser = new Parser('function foo(a, b, c){}'); | ||
.capture(/function\s*([\w$]+)\s*\(([^)]+)/, (match) => { | ||
return { | ||
name: match[1], | ||
params: matc(h[2] || '').split(/[,\s]/) | ||
}; | ||
}); | ||
``` | ||
### [.parse](index.js#L73) | ||
Parse the string passed to the constructor with all registered parsers. | ||
* `returns` **{Object|Null}** | ||
## Examples | ||
@@ -24,3 +90,4 @@ | ||
```js | ||
parse("function app(a, b, c) {\n\n}"); | ||
const context = parse('function app(a, b, c) {\n\n}'); | ||
console.log(context); | ||
``` | ||
@@ -170,2 +237,3 @@ | ||
original: 'var name = "delims";\nasdf' } | ||
``` | ||
@@ -238,35 +306,79 @@ | ||
## Related projects | ||
## Custom parsers | ||
* [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) | ||
* [snapdragon](https://www.npmjs.com/package/snapdragon): snapdragon is an extremely pluggable, powerful and easy-to-use parser-renderer factory. | [homepage](https://github.com/jonschlinkert/snapdragon) | ||
* [strip-comments](https://www.npmjs.com/package/strip-comments): Strip comments from code. Removes line comments, block comments, the first comment only, or all… [more](https://www.npmjs.com/package/strip-comments) | [homepage](https://github.com/jonschlinkert/strip-comments) | ||
Instantiate the `Parser` class to register custom parsers. | ||
## Running tests | ||
```js | ||
const { Parser} = require('parse-code-context'); | ||
const parser = new Parser(); | ||
Install dev dependencies: | ||
parser.capture(/foo\(([^)]+)\)/, match => { | ||
return { | ||
params: match[1].split(/[,\s]+/) | ||
}; | ||
}); | ||
console.log(parser.parse('foo(a, b, c)')); | ||
``` | ||
## Credit | ||
Regex was originally sourced and modified from [https://github.com/visionmedia/dox](https://github.com/visionmedia/dox). | ||
## About | ||
<details> | ||
<summary><strong>Contributing</strong></summary> | ||
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 i -d && npm test | ||
$ npm install && npm test | ||
``` | ||
## Contributing | ||
</details> | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/parse-code-context/issues/new). | ||
<details> | ||
<summary><strong>Building docs</strong></summary> | ||
## Author | ||
_(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.)_ | ||
To generate the readme, run the following command: | ||
```sh | ||
$ npm install -g verbose/verb#dev verb-generate-readme && verb | ||
``` | ||
</details> | ||
### Related projects | ||
You might also be interested in these projects: | ||
* [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://github.com/jonschlinkert/code-context) | [homepage](https://github.com/jonschlinkert/code-context "Parse a string of javascript to determine the context for functions, variables and comments based on the code that follows.") | ||
* [snapdragon](https://www.npmjs.com/package/snapdragon): Easy-to-use plugin system for creating powerful, fast and versatile parsers and compilers, with built-in source-map… [more](https://github.com/here-be/snapdragon) | [homepage](https://github.com/here-be/snapdragon "Easy-to-use plugin system for creating powerful, fast and versatile parsers and compilers, with built-in source-map support.") | ||
* [strip-comments](https://www.npmjs.com/package/strip-comments): Strip comments from code. Removes line comments, block comments, the first comment only, or all… [more](https://github.com/jonschlinkert/strip-comments) | [homepage](https://github.com/jonschlinkert/strip-comments "Strip comments from code. Removes line comments, block comments, the first comment only, or all comments. Optionally leave protected comments unharmed.") | ||
### Author | ||
**Jon Schlinkert** | ||
* [github/jonschlinkert](https://github.com/jonschlinkert) | ||
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
Regex originally sourced and modified from [https://github.com/visionmedia/dox](https://github.com/visionmedia/dox). | ||
* [GitHub Profile](https://github.com/jonschlinkert) | ||
* [Twitter Profile](https://twitter.com/jonschlinkert) | ||
* [LinkedIn Profile](https://linkedin.com/in/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 20, 2015._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on November 24, 2018._ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
19644
2
297
0
381