https://github.com/131/reflection
https://github.com/131/reflection
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/131/reflection
- Owner: 131
- License: mit
- Created: 2015-10-18T09:21:11.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2024-05-31T14:35:46.000Z (about 2 years ago)
- Last Synced: 2025-05-10T12:08:36.587Z (about 1 year ago)
- Language: JavaScript
- Size: 39.1 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Reflection / parsefunc is a reflection API for nodejs
[](https://github.com/131/reflection/actions/workflows/tests.yml)
[](https://coveralls.io/github/131/reflection?branch=master)
[](https://www.npmjs.com/package/reflection-js)
[](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)