Skip to content

Utility module for method parameter name extraction usually can be used in decorators.

License

Notifications You must be signed in to change notification settings

sekko27/param_names

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

param_names ci codecov deno doc

Custom badge Custom badge Custom badge

PARAM-NAMES

Introduction

Parse method (constructors, methods, static methods, setters) parameter names mostly because typescript compiler does not emit this kind of information.

It also caches the result so subsequent calls can be served from it.

The initial implementation uses the acorn and the acorn-class-fields modules and expose utility methods based on this implementation.

Supports

Tested on

  • constructor / method / static method / setter parameters
  • rest parameters
  • generics
  • default parameters

Usage

The utilities prepared to easy use in decorators.

import {
    parseConstructorParamNames,
    parseMethodParamNames,
    parseSetterParamNames,
    parseStaticMethodParamNames
} from **https://deno.land/x/param_names/mod.ts**;

const PrintConstructorParamNames = (cls: any) => console.log(parseConstructorParamNames(cls));
const PrintMethodParamNames = (proto: any, method: string) => console.log(parseMethodParamNames(proto, method));
const PrintSetterParamNames = (proto: any, property: string) => console.log(parseSetterParamNames(proto, property));
const PrintStaticMethodParamNames = (cls: any, method: string) => console.log(parseStaticMethodParamNames(cls, method));

// Will print [**p1**, **rest**]
@PrintConstructorParamNames
class Test {
    constructor(private readonly p1: number, ...rest: boolean[]) {
    }
    
    // Will print [**input**, **limit**]
    @PrintMethodParamNames
    public async method(input: ReadableStream, limit: number = 1024): Promise<WritableStream> {
        // ...
    }

    // Will print [**value**]
    @PrintSetterParamNames
    set name(value: string) {}

    // Will print [**arr**, **mapper**]
    @PrintStaticMethodParamNames    
    protected static async staticMethod<T, R = any>(arr: T[], mapper: (value: T) => R): Promise<R[]> {
        // ...
    }
}

Testing

We exclude lint checks for no-explicit-any, ban-types, require-await, no-unused-vars, no-inferrable-types, because we test parameter names not the implementations. We also add --no-check flag for ignore ts errors in tests.

deno test -A --unstable --coverage=./.cov --no-check --config deno.json

About

Utility module for method parameter name extraction usually can be used in decorators.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published