npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

macro2

v0.1.0

Published

TypeScript compiler wrapper with macro support

Downloads

4

Readme

macro2

Macro2 is an experimental TypeScript compiler wrapper that adds function-like macros.

Installation

npm i macro2

Example usage

Let's say you want to access an interface's keys at runtime, like this:

// index.ts
import { keys } from './keys'

interface Thing {
  foo: number
  bar: number
}

let k = keys<Thing>()
console.log(k) // ['foo', 'bar']
// typeof k is ('foo' | 'bar')[]

In keys.ts, you'd define the macro like this:

import { Macro } from 'macro2'

export const keys = Macro(function ({ callExpression }) {
  let typeNames = callExpression
    .getTypeArguments()[0]
    .getType()
    .getProperties()
    .map((p) => p.getName())
  return JSON.stringify(typeNames) as any
}) as <T>() => Array<keyof T>

After you run macro2, the compiled output in index.js will look something like:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
let k = ["foo", "bar"];
console.log(k); // ['foo', 'bar']
// typeof k is ('foo' | 'bar')[]

A macro definition function is passed to Macro() to be evaluated at compile-time, and our keys() call expression is replaced with the string this function returns. The macro definition function can access the ts-morph-wrapped AST node to read call signature information (eg. getting property names from the type argument).

How it works

macro2 replaces the tsc command you'd typically use, and uses the same tsconfig.json.

Before delegating to the TypeScript compiler's usual behavior, macro2 scans your project for variable assignments to call expressions referencing its exported Macro function, eg:

import { Macro } from 'macro2' 
//                    variable assignment
//     |--------------------------------------------------|
//     v                                                  v
export const myMacro = Macro(function() { return '"foo"' })
//           ^     ^   ^                                  ^
//           |-----|   |----------------------------------|
//         identifier             call expression

Then macro2 scans for all call expressions referencing a defined macro's identifier, and replaces them with the macro's expanded form:

import {myMacro} from './my-macro.ts'
//      call expression
//        |-------|
//        v       v
let bar = myMacro()
//        ^     ^
//        |-----|
//       identifier

// compiles to:
let bar = "foo"

Using third-party macros

Currently, you'll have to redefine third-party macros in your project for macro2 to find them.

For example, to use macro2-keys:

npm install macro2-keys
// my-macros.ts
import { Macro } from 'macro2'
import { keys as _keys } from 'macro2-keys'

export const keys: typeof _keys = Macro(_keys as any)
// app.ts
import { keys } from './my-macros'
let k = keys<{foo: string, bar: string}>() // ['foo', 'bar']