# reflection-js

> Module reflection API for nodejs

Latest version **2.2.2** (published 2024-05-31) · ISC license · 0 weekly downloads

## Install

```sh
npm install reflection-js
pnpm add reflection-js
yarn add reflection-js
bun add reflection-js
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.2 |
| Published | 2024-05-31 |
| First published | 2015-10-18 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 9.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Francois Leurent |
| Maintainers | 131, frodon |
| Keywords | reflection |

## Links

- npm: https://www.npmjs.com/package/reflection-js
- Repository: https://github.com/131/reflection
- Homepage: https://github.com/131/reflection#readme
- Issues: https://github.com/131/reflection/issues
- npm.io page: https://npm.io/package/reflection-js

## Dependencies (4)

- [mout](https://npm.io/package/mout.md) ^1.0.0
- [nyks](https://npm.io/package/nyks.md) ^6.1.5
- [acorn](https://npm.io/package/acorn.md) ^8.11.3
- [acorn-loose](https://npm.io/package/acorn-loose.md) ^8.4.0

## Recent versions

- 2.2.2 (latest) — 2024-05-31
- 2.2.1 — 2024-02-02
- 2.2.0 — 2024-01-25
- 2.1.0 — 2023-09-15
- 2.0.7 — 2021-10-15
- 2.0.6 — 2021-08-16
- 2.0.5 — 2021-02-19
- 2.0.4 — 2021-02-17
- 2.0.3 — 2021-02-17
- 2.0.2 — 2021-02-07
- 2.0.1 — 2021-01-29
- 2.0.0 — 2021-01-29
- 1.5.0 — 2021-01-28
- 1.4.3 — 2019-04-29
- 1.4.2 — 2018-02-13
- … 7 more at https://npm.io/package/reflection-js/versions

## README

# Reflection / parsefunc is a reflection API for nodejs

[![Build Status](https://github.com/131/reflection/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/131/reflection/actions/workflows/tests.yml)
[![Coverage Status](https://coveralls.io/repos/github/131/reflection/badge.svg?branch=master)](https://coveralls.io/github/131/reflection?branch=master)
[![NPM version](https://img.shields.io/npm/v/reflection-js.svg)](https://www.npmjs.com/package/reflection-js)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](http://opensource.org/licenses/MIT)

# Motivation
This is a very simple reflection API for javascript functions build on [acorn](https://www.npmjs.com/package/acorn).  [reflection-js](https://github.com/131/reflection) is mostly designed to fit [cnyks](https://github.com/131/cnyks)'s needs.




# PHPdoc/JSdoc
PHPdoc is part of PHP reflection API, that is, function described with this syntax can access their own comment.
There is no standard way to attach a comment to a function in javascript.
Esprima provide a way to parse the AST (and to retrieve comment) but we need a little more.

... enter the JSdoc syntax !


# Attach a JSdoc to a javascript function
## JSdoc pattern

```
foo.prototype.bar = function() /**
* This comment is valid and can describe the function behavior
* This syntax allow reflection API to work, as the comment will be serialized in the function body
*/ {
  return 43;
}


class Bar {
  async static bar() /**
  * This comment is valid and can describe the function behavior
  * This syntax allow reflection API to work, as the comment will be serialized in the function body
  */ {
    return 43;
  }

}
```
# Usage
```
const parsefunc = require('reflection-js/parsefunc');
var heavyComputation = function (a, b = 1) /**
* This function computer bar
* @param {string} a Initial rotation speed
* @param {string} [b=1] this is foo
*/ {
  return a + b;
};


console.log(parsefunc(heavyComputation));

{
  "name": "heavyComputation",
  "params": {
    "a": {
      "type": "string",
      "descr": "Initial rotation speed",
      "optional": false
    },
    "b": {
      "type": "string",
      "descr": "this is foo",
      "value": "1",
      "optional": true
    }
  },
  "blocs": // all parsed @sections
  "doc": [
    "This function computer bar"
  ],
  "jsdoc": // raw comment string
}
```


# Credits 
* [131](https://github.com/131)

---
_Source: https://npm.io/package/reflection-js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
