Registry / babel-plugin-parameter-decorator

babel-plugin-parameter-decorator

JSON →
library1.0.16jsnpmunverified

A Babel plugin that transforms function parameter decorators to behave like TypeScript parameter decorators. Current stable version is 1.0.16, with infrequent releases. It enables parameter decorators on constructor parameters and method parameters when using Babel with legacy decorator support. Unlike TypeScript's built-in support, this plugin requires the separate `@babel/plugin-proposal-decorators` with `legacy: true` and optionally `@babel/plugin-proposal-class-properties`. It is the only dedicated Babel plugin for parameter decorator transformation as of 2024, filling a gap left by other decorator plugins. Works with both JavaScript and TypeScript via Babel presets.

npm install babel-plugin-parameter-decorator
INSTALL
IMPORT
SIG · BABEL-PLUGIN-PARAM
B
babel-plugin-parameter-decorator
javascriptv1.0.16
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

babel-plugin-parameter-decorator
// in .babelrc or babel.config.js { "plugins": [ ["@babel/plugin-proposal-decorators", { "legacy": true }], "babel-plugin-parameter-decorator" ] }
{ "plugins": ["babel-plugin-parameter-decorator"] // missing @babel/plugin-proposal-decorators }
This plugin is a Babel plugin, not a JavaScript module import. It is used only in Babel configuration. Must be placed after the decorators plugin.

Shows how to install, configure Babel with plugin-proposal-decorators (legacy mode) and this plugin, and then transpile a class with parameter decorators.

// Install dependencies // npm install --save-dev @babel/core @babel/cli @babel/plugin-proposal-decorators babel-plugin-parameter-decorator // .babelrc { "plugins": [ ["@babel/plugin-proposal-decorators", { "legacy": true }], "babel-plugin-parameter-decorator" ] } // input.js function required(key) { return function (target, propertyKey, parameterIndex) { const metadata = `meta_${propertyKey}`; target[metadata] = [ ...(target[metadata] || []), { index: parameterIndex, key } ]; }; } function validate(target, property, descriptor) { const fn = descriptor.value; descriptor.value = function(...args) { const metadata = `meta_${property}`; target[metadata].forEach(function(meta) { if (args[meta.index] === undefined) { throw new Error(`${meta.key} is required`); } }); return fn.apply(this, args); }; return descriptor; } class Greeter { constructor(message) { this.greeting = message; } @validate greet(@required('name') name) { return "Hello " + name + ", " + this.greeting; } } // Run: npx babel input.js --out-file output.js
Debug
Known issues
gotchaPlugin must be placed after @babel/plugin-proposal-decorators in plugins array.
fix
Ensure order: [['@babel/plugin-proposal-decorators', {legacy: true}], 'babel-plugin-parameter-decorator']
affects: *
gotchaIf using @babel/preset-typescript, TypeScript-only imports used in decorators will be removed by default, breaking references.
fix
Set 'onlyRemoveTypeImports' to true in preset options: ['@babel/preset-typescript', { onlyRemoveTypeImports: true }]
affects: *
gotchaRequires @babel/plugin-proposal-decorators with legacy: true; the newer '2023-01' or '2023-11' decorator proposal is not supported.
fix
Use legacy: true (the Stage 1 decorators proposal).
affects: *
Errors
Common errors & fixes
Error: [BABEL] unknown: Decorators are not enabled.
Missing @babel/plugin-proposal-decorators plugin in Babel config.
fix
Add ["@babel/plugin-proposal-decorators", { "legacy": true }] before babel-plugin-parameter-decorator.
TypeError: Cannot read properties of undefined (reading 'forEach')
Parameter decorator used on a method without the companion method decorator (e.g., @validate) writing metadata.
fix
Ensure a method decorator writes the metadata that the parameter decorator expects; example uses @validate.
Error: Parameter decorator is not a function
Incorrect import or invalid decorator function; parameter decorator must return a function that takes (target, propertyKey, parameterIndex).
fix
Define parameter decorator correctly: function decorator(key) { return function(target, propKey, paramIndex) { ... }; }
Upgrade
Version history
1.0.16latest on npm
Audit
Dependencies
@babel/plugin-proposal-decoratorsrequiredRequired as a peer/ runtime dependency; must be configured with legacy: true for this plugin to work.
Agent activity
14 hits · last 30 days
node
12
Resources
babel-plugin-parameter-decorator — npm install babel-plugin-parameter-decorator · libregistry