@jitar/reflection
TypeScript icon, indicating that this package has built-in type declarations

0.7.1 • Public • Published

Jitar Reflection

This package provides static and dynamic reflection for JavaScript applications. It's used in the Jitar project for analyzing and splitting applications, but can also be used as standalone library in any project.

To add this package to your project run:

npm install @jitar/reflection

For more information about Jitar:

Usage

Static reflection

For static reflection the reflector provides functions for parsing and analyzing JavaScript code:

  • parse(code: string) - Parses JS code into a ReflectionModule model
  • parseClass(code: string) - Parses a class declaration statement into a ReflectionClass model
  • parseFunction(code: string) - Parses a function declaration statement into a ReflectionFunction model
  • parseField(code: string) - Parses a const / let /var declaration statement into a ReflectionField model
  • parseImport(code: string) - Parses an import statement into a ReflectionImport model
  • parseExport(code: string) - Parses an export statement into a ReflectionExport model

Dynamic reflection

For dynamic reflection the reflector provides function for analyzing JavaScript classes and objects:

  • fromModule(module: object) - Parses a module (its fields, functions and classes) into a ReflectionModule model
  • fromClass(clazz: Function) - Parses a class into a ReflectionClass model
  • fromObject(object: object) - Parses an object (class instance) into a ReflectionClass model
  • fromFunction(funktion: Function) - Parses a function into a ReflectionFunction model

Helper functions

The reflector also provides other helper functions:

  • createInstance(clazz: Function, args: unknown[]) - Creates a new class instance
  • isClassObject(object: object) - Checks if the object is a class instance
  • getClass(object: object) - Gets the class from an instance object
  • getParentClass(clazz: Function) - Gets the parent class from a class

Known limitations

  1. Declaration of multiple values is not supported
// Supported
const a = 1;
export { a }

// Unsupported (will be supported)
const b = 2, c = 3;
export { b, c }
  1. Generator as object properties are not supported
// Supported
function* myGenerator() { /* ... */ }

// Supported
class Foo =
{
    *generator1() { /* ... */ }

    async *generator2() { /* ... */ }

    static *generator3() { /* ... */ }
};

// Unsupported (dynamic properties won't be supported)
class Bar
{
  *[Symbol.iterator]() { /* ... */ }
}
  1. Destructuring not fully supported
// Supported
const [ a, b = 42, ...others ] = myArray;
const { a, b = 42, ...others } = myObject;
// Aliases are not supported (will be supported)
const [ a, b: c, ...others ] = myArray;
const { a, b: c, ...others } = myObject;
// Nested destructuring is not supported (will be supported)
const [ a, [ b = 42, c, d ] ] = myArray;
const { a: { c, d = true }, b = 42 } = myObject;
// Dynamic property destructuring is not supported (won't be supported)
const [ [a]: b ] = myArray;
const { [a]: b } = myObject;

Package Sidebar

Install

npm i @jitar/reflection

Homepage

jitar.dev

Weekly Downloads

8

Version

0.7.1

License

MIT

Unpacked Size

85 kB

Total Files

100

Last publish

Collaborators

  • basmasking
  • petermasking
  • maskingtechnology