# rbxts-transformer-macros

> A roblox-ts transformer that allows you to add custom macros to (almost) any type.

Latest version **0.0.11** (published 2021-02-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install rbxts-transformer-macros
pnpm add rbxts-transformer-macros
yarn add rbxts-transformer-macros
bun add rbxts-transformer-macros
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.11 |
| Published | 2021-02-06 |
| First published | 2021-02-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 20.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Fireboltofdeath |
| Maintainers | fireboltofdeath |

## Links

- npm: https://www.npmjs.com/package/rbxts-transformer-macros
- Repository: https://github.com/FireTS/rbxts-transformer-wrapper
- Homepage: https://github.com/FireTS/rbxts-transformer-wrapper#readme
- Issues: https://github.com/FireTS/rbxts-transformer-wrapper/issues
- npm.io page: https://npm.io/package/rbxts-transformer-macros

## Dependencies (2)

- [byots](https://npm.io/package/byots.md) ^4.1.0-dev.20201102.16.32
- [typescript](https://npm.io/package/typescript.md) ^4.1.3

## Recent versions

- 0.0.11 (latest) — 2021-02-06
- 0.0.10 — 2021-02-06
- 0.0.9 — 2021-02-06
- 0.0.8 — 2021-02-06
- 0.0.7 — 2021-02-06
- 0.0.6 — 2021-02-06
- 0.0.5 — 2021-02-06
- 0.0.4 — 2021-02-06
- 0.0.3 — 2021-02-06
- 0.0.2 — 2021-02-06
- 0.0.1 — 2021-02-06

## README

# rbxts-transformer-macros
A roblox-ts transformer that allows you to add macros to instances, strings, etc at compile-time. This was specifically designed to act as a compile-time "wrapper" for instances, but works on almost any interface.

## What is the purpose of wrapping instances?
Wrapping instances is a useful way to extend functionality of Roblox's instances, as they do not provide the ability to augment them natively.

## Why use compile-time wrapping (macros), rather than runtime wrappers?
Runtime wrappers do not work well in practice, have performance overhead and require considerable thought when using them with C functions.
For example, the following code would not work if `instance` was a runtime wrapped instance, since the value you act on is not an actual instance.
```ts
const objectValue: ObjectValue = ...;
const instance = ...;

objectValue.Value = instance; // runtime error, but no compile-time error
```

To use them with native C functions, or really anything expecting an actual instance, you'd have to convert your code to something like this.
```ts
const objectValue: ObjectValue = ...;
const instance = ...;

objectValue.Value = instance.inner;
```

Compile-time macros have zero overhead simply for existing, are easier to use, and generally more convenient.

# Documentation
## How to use the macros?
Even if you define the macros using $defineCallMacros or $definePropMacros, you can't access them quite yet.
You'll have to add them to your type definitions, and there's two ways to do this.

You can declare an ambient interface to override the interface you're augmenting, like below.
```ts
// ambient.d.ts
interface Instance {
  GetComponent(name: string): Component;
  Components: Array<Component>;
}
```
Alternatively, you can make a wrapper interface, however this does require you to cast your value to the wrapper interface to work.
This method is convenient if you have macros that only work in specific contexts, e.g client/server. It is far less convenient to have to cast values, so use ambient declarations when possible.
```ts
// anywhere.d?.ts
interface WrappedInstance extends Instance {
  GetComponent(name: string): Component;
  Components: Array<Component>;
}

const wrappedInstance = instance as WrappedInstance;
const wrappedPlayer = player as Player & WrappedInstance;
```

## How to define a call macro?
Call macros must be in a ModuleScript (script.ts), and exported.
```ts
import { $defineCallMacros } from "rbxts-transformer-macros";

export const CALL_MACROS = $defineCallMacros<Instance>({
  GetComponent(name: string) {
    return findComponent(this, name);
  }
})
```

## How to define a property macro?
Property macros must be in a ModuleScript (script.ts), and exported.
A notable difference between property macros in runtime wrappers vs compile-time, compile-time property macros *can* yield if the macro yields. This can be a good thing, but it can also cause issues if you aren't aware of it.
```ts
import { $definePropMacros } from "rbxts-transformer-macros";

export const PROP_MACROS = $definePropMacros<Instance>({
  Components() {
    return getComponents(this);
  }
})
```

---
_Source: https://npm.io/package/rbxts-transformer-macros · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
