JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 43447
  • Score
    100M100P100Q161120F
  • License MIT

Function parameter decorator transform plugin for babel v7, just like typescript.

Package Exports

  • babel-plugin-parameter-decorator

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (babel-plugin-parameter-decorator) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Babel Plugin Parameter Decorator

Function parameter decorator transform plugin for babel v7, just like typescript parameter decorator

function validate(target, property, descriptor) {
  const fn = descriptor.value;

  descriptor.value = function (...args) {
    const metadata = `meta_${property}`;
    target[metadata].forEach(function (metadata) {
      if (args[metadata.index] === undefined) {
        throw new Error(`${metadata.key} is required`);
      }
    });

    return fn.apply(this, args);
  };

  return descriptor;
}

function required(key) {
  return function (target, propertyKey, parameterIndex) {
    const metadata = `meta_${propertyKey}`;
    target[metadata] = [
      ...(target[metadata] || []),
      {
        index: parameterIndex,
        key
      }
    ]
  };
}

class Greeter {
    constructor(message) {
        this.greeting = message;
    }

    @validate
    greet(@required('name') name) {
        return "Hello " + name + ", " + this.greeting;
    }
}

NOTE:

This package depends on @babel/plugin-proposal-decorators.

Installation & Usage

npm install @babel/plugin-proposal-decorators babel-plugin-parameter-decorator -D

And the .babelrc looks like:

    {
        "plugins": [
            ["@babel/plugin-proposal-decorators", { "legacy": true }],
            "babel-plugin-parameter-decorator"
        ]
    }