Installations
npm install parse-code-context
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=6
Node Version
11.1.0
NPM Version
6.4.1
Score
96.6
Supply Chain
100
Quality
75.5
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
Download Statistics
Total Downloads
9,315,196
Last Day
9,622
Last Week
51,385
Last Month
211,449
Last Year
2,392,129
GitHub Statistics
MIT License
11 Stars
23 Commits
1 Forks
4 Watchers
1 Branches
2 Contributors
Updated on Jan 10, 2022
Bundle Size
3.88 kB
Minified
995.00 B
Minified + Gzipped
Package Meta Information
Latest Version
1.0.0
Package Id
parse-code-context@1.0.0
Size
4.99 kB
NPM Version
6.4.1
Node Version
11.1.0
Published on
Nov 24, 2018
Total Downloads
Cumulative downloads
Total Downloads
9,315,196
Last Day
11%
9,622
Compared to previous day
Last Week
3.8%
51,385
Compared to previous week
Last Month
37.7%
211,449
Compared to previous month
Last Year
15.5%
2,392,129
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
2
parse-code-context

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, and consider starring the project to show your :heart: and support.
Install
Install with npm:
1$ npm install --save parse-code-context
Getting started
Usage
1const parse = require('parse-code-context'); 2console.log(parse('function app(a, b, c) {\n\n}'));
API
Parser
Create an instance of Parser
with the given string
, optionally passing a parent
name for namespacing methods
Params
str
{String}parent
{String}
Example
1const { Parser } = require('parse-code-context');
2const parser = new Parser('function foo(a, b, c) {}');
.name
Convenience method for creating a property name that is prefixed with the parent namespace, if defined.
Params
name
{String}returns
{String}
.capture
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
1const parser = new Parser('function foo(a, b, c){}'); 2 .capture(/function\s*([\w$]+)\s*\(([^)]+)/, (match) => { 3 return { 4 name: match[1], 5 params: matc(h[2] || '').split(/[,\s]/) 6 }; 7 });
.parse
Parse the string passed to the constructor with all registered parsers.
returns
{Object|Null}
Examples
function statement
1const context = parse('function app(a, b, c) {\n\n}'); 2console.log(context);
Results in:
1{ type: 'function statement', 2 name: 'app', 3 params: [ 'a', 'b', 'c' ], 4 string: 'app()', 5 original: 'function app() {\n\n}' }
function expression
1parse("var app = function(a, b, c) {\n\n}");
Results in:
1{ type: 'function expression', 2 name: 'app', 3 params: [ 'a', 'b', 'c' ], 4 string: 'app()', 5 original: 'var app = function() {\n\n}' }
module.exports
function expression
1parse("module.exports = function foo(a, b, c) {\n\n}");
Results in:
1{ type: 'function expression', 2 receiver: 'module.exports', 3 name: 'foo', 4 params: [ 'a', 'b', 'c' ], 5 string: 'module.exports()', 6 original: 'module.exports = function foo(a, b, c) {\n\n}' }
module.exports
method
1parse("module.exports = function() {\n\n}");
Results in:
1{ type: 'method', 2 receiver: 'module.exports', 3 name: '', 4 params: [], 5 string: 'module.exports.() {\n\n}()', 6 original: 'module.exports = function() {\n\n}' }
prototype method
1parse("Template.prototype.get = function() {}");
Results in:
1{ type: 'prototype method', 2 class: 'Template', 3 name: 'get', 4 params: [], 5 string: 'Template.prototype.get()', 6 original: 'Template.prototype.get = function() {}' }
prototype property
1parse("Template.prototype.enabled = true;\nasdf");
Results in:
1{ type: 'prototype property', 2 class: 'Template', 3 name: 'enabled', 4 value: 'true', 5 string: 'Template.prototype.enabled', 6 original: 'Template.prototype.enabled = true;\nasdf' }
method
1parse("option.get = function() {}");
Results in:
1{ type: 'method', 2 receiver: 'option', 3 name: 'get', 4 params: [], 5 string: 'option.get()', 6 original: 'option.get = function() {}' }
property
1parse("option.name = \"delims\";\nasdf");
Results in:
1{ type: 'property', 2 receiver: 'option', 3 name: 'name', 4 value: '"delims"', 5 string: 'option.name', 6 original: 'option.name = "delims";\nasdf' }
declaration
1parse("var name = \"delims\";\nasdf");
Results in:
1{ type: 'declaration', 2 name: 'name', 3 value: '"delims"', 4 string: 'name', 5 original: 'var name = "delims";\nasdf' } 6
function statement params
1parse("function app(a, b) {\n\n}");
Results in:
1{ type: 'function statement', 2 name: 'app', 3 params: [ 'a', 'b' ], 4 string: 'app()', 5 original: 'function app(a, b) {\n\n}' }
function expression params
1parse("var app = function(foo, bar) {\n\n}");
Results in:
1{ type: 'function expression', 2 name: 'app', 3 params: [ 'foo', 'bar' ], 4 string: 'app()', 5 original: 'var app = function(foo, bar) {\n\n}' }
function expression params
1parse("var app=function(foo,bar) {\n\n}");
Results in:
1{ type: 'function expression', 2 name: 'app', 3 params: [ 'foo', 'bar' ], 4 string: 'app()', 5 original: 'var app=function(foo,bar) {\n\n}' }
prototype method params
1parse("Template.prototype.get = function(key, value, options) {}");
Results in:
1{ type: 'prototype method', 2 class: 'Template', 3 name: 'get', 4 params: [ 'key', 'value', 'options' ], 5 string: 'Template.prototype.get()', 6 original: 'Template.prototype.get = function(key, value, options) {}' }
Custom parsers
Instantiate the Parser
class to register custom parsers.
1const { Parser} = require('parse-code-context'); 2const parser = new Parser(); 3 4parser.capture(/foo\(([^)]+)\)/, match => { 5 return { 6 params: match[1].split(/[,\s]+/) 7 }; 8}); 9 10console.log(parser.parse('foo(a, b, c)'));
Credit
Regex was originally sourced and modified from https://github.com/visionmedia/dox.
About
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Running Tests
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:
1$ npm install && npm test
Building docs
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
1$ npm install -g verbose/verb#dev verb-generate-readme && verb
Related projects
You might also be interested in these projects:
- code-context: Parse a string of javascript to determine the context for functions, variables and comments based… more | homepage
- snapdragon: Easy-to-use plugin system for creating powerful, fast and versatile parsers and compilers, with built-in source-map… more | homepage
- strip-comments: Strip comments from code. Removes line comments, block comments, the first comment only, or all… more | homepage
Author
Jon Schlinkert
License
Copyright © 2018, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.8.0, on November 24, 2018.

No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 0/23 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Score
3
/10
Last Scanned on 2025-02-10
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to parse-code-context
babel-extract-comments
Uses babel (babylon) to extract JavaScript code comments from a JavaScript string or file.
esprima-extract-comments
Extract code comments from string or from a glob of files using esprima.
js-code-context
RegExp expressions to parse javascript code context - for node and the browser.
@diotoborg/architecto-at
> [Gulp](http://gulpjs.com) plugin to preprocess HTML, JavaScript, and other files based on custom context or environment configuration