{"id":15679781,"url":"https://github.com/jonschlinkert/parse-code-context","last_synced_at":"2025-05-07T08:13:01.182Z","repository":{"id":27296760,"uuid":"30770576","full_name":"jonschlinkert/parse-code-context","owner":"jonschlinkert","description":"Parse code context in a single line of javascript, for functions, variable declarations, methods, prototype properties, prototype methods etc.","archived":false,"fork":false,"pushed_at":"2018-11-24T10:48:24.000Z","size":28,"stargazers_count":11,"open_issues_count":1,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-07T08:12:51.344Z","etag":null,"topics":["code-comments","code-context","comments","context","docs","documentation","javascript","jonschlinkert","js","nodejs","parse","parser"],"latest_commit_sha":null,"homepage":"https://github.com/jonschlinkert","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jonschlinkert.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-02-13T18:49:57.000Z","updated_at":"2022-01-10T17:55:42.000Z","dependencies_parsed_at":"2022-08-25T22:42:20.492Z","dependency_job_id":null,"html_url":"https://github.com/jonschlinkert/parse-code-context","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fparse-code-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fparse-code-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fparse-code-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fparse-code-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonschlinkert","download_url":"https://codeload.github.com/jonschlinkert/parse-code-context/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252839296,"owners_count":21812090,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code-comments","code-context","comments","context","docs","documentation","javascript","jonschlinkert","js","nodejs","parse","parser"],"created_at":"2024-10-03T16:36:17.669Z","updated_at":"2025-05-07T08:13:01.152Z","avatar_url":"https://github.com/jonschlinkert.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 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\u0026label=Travis)](https://travis-ci.org/jonschlinkert/parse-code-context)\n\n\u003e 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.\n\nPlease consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.\n\n## Install\n\nInstall with [npm](https://www.npmjs.com/):\n\n```sh\n$ npm install --save parse-code-context\n```\n\n## Getting started\n\n* [Usage](#usage)\n* [API](#api)\n* [Examples](#examples)\n* [Custom parsers](#custom-parsers)\n\n## Usage\n\n```js\nconst parse = require('parse-code-context');\nconsole.log(parse('function app(a, b, c) {\\n\\n}'));\n```\n\n## API\n\n### [Parser](index.js#L18)\n\nCreate an instance of `Parser` with the given `string`, optionally passing a `parent` name for namespacing methods\n\n**Params**\n\n* `str` **{String}**\n* `parent` **{String}**\n\n**Example**\n\n```js\nconst { Parser } = require('parse-code-context');\nconst parser = new Parser('function foo(a, b, c) {}');\n```\n\n### [.name](index.js#L35)\n\nConvenience method for creating a property name\nthat is prefixed with the parent namespace, if defined.\n\n**Params**\n\n* `name` **{String}**\n* `returns` **{String}**\n\n### [.capture](index.js#L60)\n\nRegister a parser to use (in addition to those already registered as default parsers) with the given `regex` and function.\n\n**Params**\n\n* `regex` **{RegExp}**\n* `fn` **{Function}**\n* `returns` **{Object}**: The instance for chaining\n\n**Example**\n\n```js\nconst parser = new Parser('function foo(a, b, c){}');\n  .capture(/function\\s*([\\w$]+)\\s*\\(([^)]+)/, (match) =\u003e {\n    return {\n       name: match[1],\n       params: matc(h[2] || '').split(/[,\\s]/)\n    };\n  });\n```\n\n### [.parse](index.js#L73)\n\nParse the string passed to the constructor with all registered parsers.\n\n* `returns` **{Object|Null}**\n\n## Examples\n\n### function statement\n\n```js\nconst context = parse('function app(a, b, c) {\\n\\n}');\nconsole.log(context);\n```\n\nResults in:\n\n```js\n{ type: 'function statement',\n  name: 'app',\n  params: [ 'a', 'b', 'c' ],\n  string: 'app()',\n  original: 'function app() {\\n\\n}' }\n```\n\n### function expression\n\n```js\nparse(\"var app = function(a, b, c) {\\n\\n}\");\n```\n\nResults in:\n\n```js\n{ type: 'function expression',\n  name: 'app',\n  params: [ 'a', 'b', 'c' ],\n  string: 'app()',\n  original: 'var app = function() {\\n\\n}' }\n```\n\n### `module.exports` function expression\n\n```js\nparse(\"module.exports = function foo(a, b, c) {\\n\\n}\");\n```\n\nResults in:\n\n```js\n{ type: 'function expression',\n  receiver: 'module.exports',\n  name: 'foo',\n  params: [ 'a', 'b', 'c' ],\n  string: 'module.exports()',\n  original: 'module.exports = function foo(a, b, c) {\\n\\n}' }\n```\n\n### `module.exports` method\n\n```js\nparse(\"module.exports = function() {\\n\\n}\");\n```\n\nResults in:\n\n```js\n{ type: 'method',\n  receiver: 'module.exports',\n  name: '',\n  params: [],\n  string: 'module.exports.() {\\n\\n}()',\n  original: 'module.exports = function() {\\n\\n}' }\n```\n\n### prototype method\n\n```js\nparse(\"Template.prototype.get = function() {}\");\n```\n\nResults in:\n\n```js\n{ type: 'prototype method',\n  class: 'Template',\n  name: 'get',\n  params: [],\n  string: 'Template.prototype.get()',\n  original: 'Template.prototype.get = function() {}' }\n```\n\n### prototype property\n\n```js\nparse(\"Template.prototype.enabled = true;\\nasdf\");\n```\n\nResults in:\n\n```js\n{ type: 'prototype property',\n  class: 'Template',\n  name: 'enabled',\n  value: 'true',\n  string: 'Template.prototype.enabled',\n  original: 'Template.prototype.enabled = true;\\nasdf' }\n```\n\n### method\n\n```js\nparse(\"option.get = function() {}\");\n```\n\nResults in:\n\n```js\n{ type: 'method',\n  receiver: 'option',\n  name: 'get',\n  params: [],\n  string: 'option.get()',\n  original: 'option.get = function() {}' }\n```\n\n### property\n\n```js\nparse(\"option.name = \\\"delims\\\";\\nasdf\");\n```\n\nResults in:\n\n```js\n{ type: 'property',\n  receiver: 'option',\n  name: 'name',\n  value: '\"delims\"',\n  string: 'option.name',\n  original: 'option.name = \"delims\";\\nasdf' }\n```\n\n### declaration\n\n```js\nparse(\"var name = \\\"delims\\\";\\nasdf\");\n```\n\nResults in:\n\n```js\n{ type: 'declaration',\n  name: 'name',\n  value: '\"delims\"',\n  string: 'name',\n  original: 'var name = \"delims\";\\nasdf' }\n\n```\n\n### function statement params\n\n```js\nparse(\"function app(a, b) {\\n\\n}\");\n```\n\nResults in:\n\n```js\n{ type: 'function statement',\n  name: 'app',\n  params: [ 'a', 'b' ],\n  string: 'app()',\n  original: 'function app(a, b) {\\n\\n}' }\n```\n\n### function expression params\n\n```js\nparse(\"var app = function(foo, bar) {\\n\\n}\");\n```\n\nResults in:\n\n```js\n{ type: 'function expression',\n  name: 'app',\n  params: [ 'foo', 'bar' ],\n  string: 'app()',\n  original: 'var app = function(foo, bar) {\\n\\n}' }\n```\n\n### function expression params\n\n```js\nparse(\"var app=function(foo,bar) {\\n\\n}\");\n```\n\nResults in:\n\n```js\n{ type: 'function expression',\n  name: 'app',\n  params: [ 'foo', 'bar' ],\n  string: 'app()',\n  original: 'var app=function(foo,bar) {\\n\\n}' }\n```\n\n### prototype method params\n\n```js\nparse(\"Template.prototype.get = function(key, value, options) {}\");\n```\n\nResults in:\n\n```js\n{ type: 'prototype method',\n  class: 'Template',\n  name: 'get',\n  params: [ 'key', 'value', 'options' ],\n  string: 'Template.prototype.get()',\n  original: 'Template.prototype.get = function(key, value, options) {}' }\n```\n\n## Custom parsers\n\nInstantiate the `Parser` class to register custom parsers.\n\n```js\nconst { Parser} = require('parse-code-context');\nconst parser = new Parser();\n\nparser.capture(/foo\\(([^)]+)\\)/, match =\u003e {\n  return {\n    params: match[1].split(/[,\\s]+/)\n  };\n});\n\nconsole.log(parser.parse('foo(a, b, c)'));\n```\n\n## Credit\n\nRegex was originally sourced and modified from [https://github.com/visionmedia/dox](https://github.com/visionmedia/dox).\n\n## About\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eContributing\u003c/strong\u003e\u003c/summary\u003e\n\nPull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eRunning Tests\u003c/strong\u003e\u003c/summary\u003e\n\nRunning 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:\n\n```sh\n$ npm install \u0026\u0026 npm test\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eBuilding docs\u003c/strong\u003e\u003c/summary\u003e\n\n_(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.)_\n\nTo generate the readme, run the following command:\n\n```sh\n$ npm install -g verbose/verb#dev verb-generate-readme \u0026\u0026 verb\n```\n\n\u003c/details\u003e\n\n### Related projects\n\nYou might also be interested in these projects:\n\n* [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.\")\n* [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.\")\n* [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.\")\n\n### Author\n\n**Jon Schlinkert**\n\n* [GitHub Profile](https://github.com/jonschlinkert)\n* [Twitter Profile](https://twitter.com/jonschlinkert)\n* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)\n\n### License\n\nCopyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).\nReleased under the [MIT License](LICENSE).\n\n***\n\n_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on November 24, 2018._","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonschlinkert%2Fparse-code-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonschlinkert%2Fparse-code-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonschlinkert%2Fparse-code-context/lists"}