0.7.1 • Published 3 months ago
@jitar/reflection v0.7.1
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 modelparseClass(code: string)
- Parses a class declaration statement into a ReflectionClass modelparseFunction(code: string)
- Parses a function declaration statement into a ReflectionFunction modelparseField(code: string)
- Parses a const / let /var declaration statement into a ReflectionField modelparseImport(code: string)
- Parses an import statement into a ReflectionImport modelparseExport(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 modelfromClass(clazz: Function)
- Parses a class into a ReflectionClass modelfromObject(object: object)
- Parses an object (class instance) into a ReflectionClass modelfromFunction(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 instanceisClassObject(object: object)
- Checks if the object is a class instancegetClass(object: object)
- Gets the class from an instance objectgetParentClass(clazz: Function)
- Gets the parent class from a class
Known limitations
- 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 }
- 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]() { /* ... */ }
}
- 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;