2.2.1 • Published 3 months ago

reflection-js v2.2.1

Weekly downloads
109
License
ISC
Repository
github
Last release
3 months ago

Reflection / parsefunc is a reflection API for nodejs

Build Status Coverage Status NPM version License

Motivation

This is a very simple reflection API for javascript functions build on acorn. reflection-js is mostly designed to fit 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

2.2.1

3 months ago

2.2.0

3 months ago

2.1.0

8 months ago

2.0.7

3 years ago

2.0.6

3 years ago

2.0.5

3 years ago

2.0.3

3 years ago

2.0.4

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.5.0

3 years ago

1.4.3

5 years ago

1.4.2

6 years ago

1.4.0

7 years ago

1.3.1

7 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago

0.2.0

9 years ago

0.1.6

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago