1.2.1 • Published 8 months ago

babel-plugin-event-on-class-extend v1.2.1

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

Babel Plugin For Event On Class Extend

Demo

Run Example To See Compiled Output

Stack Overflow Answer

Changelog

NPM Package

Install

npm i -D babel-plugin-event-on-class-extend

Add this plugin to your babel config file or add it from the cli

Babel Config

{
	"plugins": ["babel-plugin-event-on-class-extend"]
}

CLI

npx babel ... --plugins=babel-plugin-event-on-class-extend

Usage

class X {
	static name = '"I\'m X"'

	public instanceMethod() {}

	// new (...args: any[]) => X Prevent calling instance method
	static onExtend(targetClass: new (...args: any[]) => X) {
		console.log(`Class X is extended by ${targetClass.name}`)
		// targetClass.instanceMethod() This will throw error
	}
}

class Y extends X {
	static name = '"I\'m Y"'
}

// X?.onExtend(Y) will be called here

This will output Class X is extended by "I'm Y" when ran.

Options

How To Use Plugin Options

Change the plugin usage to this in babel config file

{
	"plugins": [
		[
			"babel-plugin-event-on-class-extend",
			{ "staticCallbackName": "onExtend" }
		]
	]
}

List

OptionDescriptionDefaultRequired
staticCallbackNameChange the default static method callback nameonExtendfalse
classOptionalChainEnable class optional operator. X?. optional chainfalsefalse
passDerivedClassAsParameterIf disable derived class will not be passed to onExtend as parameter falsetruefalse
1.2.1

8 months ago

1.2.0

8 months ago

1.1.5

8 months ago

1.1.4

8 months ago

1.1.3

8 months ago

1.1.2

8 months ago

1.1.1

8 months ago

1.1.0

8 months ago

1.0.0

8 months ago

0.0.2

8 months ago

0.0.1

8 months ago