New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

reflection-js

Package Overview
Dependencies
Maintainers
2
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

reflection-js

Module reflection API for nodejs

latest
Source
npmnpm
Version
2.2.2
Version published
Weekly downloads
228
-4.2%
Maintainers
2
Weekly downloads
 
Created
Source

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

  • 131

Keywords

reflection

FAQs

Package last updated on 31 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts