POC. Simple plugin-based meta-programming platform on top of Typescript

Overview

comp-plugins POC

Running:

  1. yarn to install dependencies
  2. yarn dev to run the script

The what

The script creates a new typescript compiler instance (program, checker and language service) and loads the project at ./sample. It then proceeds to transform the sample with the configured plugins (it creates a transformer factory for each plugin). The plugins then proceed to modify the AST and produce changes where needed. The output is written to disk and a node process starts running index.js.

The output of the script is split into the compile time logs (including full dumps of the output files) and the runtime logs. (look after the attempting to run log)

The line plugin

This is the most basic plugin. It brings c++'s __line into typescript :) It basically replaces any uses of the __line identifier with a number literal representing the line it is located on.

NOT VALID ANYMORE Now it wraps the number literal in an invocation to __compute_line function generated in a fake source file and imported when the plugin finds any usage of __line.

The macro plugin

This plugin implements macros on a very basic level. A macro is just a call expression that contains a double non-null expression before it's identifier test!!(...). This is just one way to do this. You could also completely ignore this and just look for a specific set of identifiers. There are 2 macros implemented:

  • magic!!(): it gets replaced with the number literal 42
  • log!!(...any[]): it gets replaced with a call to console.log. this example shows that you could have 0 cost abstractions like using different log providers depending on configuration options. you could also completely remove calls depending on the LOG_LEVEL config.

The callsite plugin

This plugins looks for usages of the CallArgumentText<T> and CallPosition types in the parameters of functions. It then binds references to those parameters and replaces the call expression with a modified version that provides the proper values for the random. A bit hard to grasp, so here's an example:

// if you have this signature
function assert(condition: unknown, argText?: CallArgumentText<typeof condition>, position?: CallPosition): asserts condition;

// and you invoke it like so
assert(a === 42);

// it will be converted to the following call
assert(a == 42, "a == 42", {line: 42, col: 69, file: "some random absolute path"});

// so the runtime can make sense of it and use it for stuff
function assert(condition: unknown, argText?: CallArgumentText<typeof condition>, position?: CallPosition): asserts condition {
    if (!argText || !position) throw new Error("Invalid call to assert.");

    if (!condition) {
        throw new AssertionError(argText, position.line, position.col, position.file);
    }
}

The auto register plugin

This plugin looks for exported class declarations that end with Service ex RandomService and automatically creates a registry that contains references to all of them. Those references can then be used to create instances of the services. The output of this plugin is a generated file that must be imported.

Language service plugins

DOCS WIP

The why

Provide a solid meta-programming platform for Typescript. Help people to remove the runtime bloat and allow for easy to implement zero-cost abstractions. For details see my suggestion in the Deepkit Discord server here.

To do:

  • before and after hooks for plugins
  • allowing plugins to append statements to the source file without replacing existing statements
  • allowing plugins to create fake source files to generate arbitrary code in it and refer to it from the real source files (should also be able to emit them)
  • hack the language service and provide cool editor features to plugins
  • add watch mode
  • NOT IN SCOPE ANYMORE write a few more example plugins (any suggestions appreciated) and simplify the plugins API along the way

Making sense of this codebase

Well... there are no docs, this is just a POC :) So go exploring! Start with src/bin.ts and sample/index.ts.

You might also like...

It redirects the website request from facebook to any blog while keeping the meta data for the each link.

Vercel Redirect It redirects the website request from facebook to any blog while keeping the meta data for the each link. This app uses Next.js and th

Dec 4, 2022

A POC of a Discord.js bot that sends 3D rendering instructions to a Go server through gRPC which responds with the image bytes which are then sent back on Discord.

A POC of a Discord.js bot that sends 3D rendering instructions to a Go server through gRPC which responds with the image bytes which are then sent back on Discord.

Jan 8, 2022

This repository aims to create a POC about authentication and authorization using NestJS, Prisma and JWT.

A progressive Node.js framework for building efficient and scalable server-side applications. Description Nest framework TypeScript starter repository

Nov 2, 2022

Embeddable 3D Rendering Engine with JS, a POC project.

Embeddable 3D Rendering Engine with JS, a POC project.

Three.V8 Three.V8 is a proof of concept (POC) of an embedabble 3D rendering engine using JavaScript as user script. Currently, the whole project requi

Nov 29, 2022

Minimal framework for SSG (WIP, PoC)

Frostleaf https://zenn.dev/0918nobita/scraps/64a268583b8463 Development Install tools asdf plugin-add nodejs asdf plugin-add pnpm asdf install Install

Jun 4, 2022

[OUTDATED] [PoC] Magnit bonus card numbers & QR code gen

[OUTDATED] [PoC] Magnit bonus card numbers & QR code gen

magnitqr [OUTDATED] [PoC] Magnit bonus card numbers & QR code generator and saver https://rdavydov.github.io/magnitqr/ SPA that was used "in the field

Oct 25, 2022

관세청 개인통관고유부호(PCCC)를 가져오는 PoC

pccc-poc 관세청 개인통관고유부호(PCCC)를 가져오는 PoC Installation git clone https://github.com/stevejkang/pccc-poc.git && cd pccc-poc npm install # edit index.ts (fi

Oct 5, 2022

POC OF CVE-2022-21970

POC OF CVE-2022-21970

CVE-2022-21970 Description Microsoft Edge (Chromium-based) Elevation of Privilege Vulnerability. This vulnerability allows an attacker to execute java

Dec 9, 2022

Movehat is a TypeScript SDK for Move on Sui built on top of Sui's TypeScript SDK and our fork of Ian Macalinao's `move-ts`.

Movehat Movehat is a TypeScript SDK for Move on Sui built on top of Sui's TypeScript SDK and our fork of Ian Macalinao's move-ts. Movehat aspires to b

Sep 30, 2022
Owner
Ciobanu Laurentiu
Just a plain old full-stack dev. Obsessed with code generation. Also currently obsessed with Kubernetes :)
Ciobanu Laurentiu
A lightweight, performant, and simple-to-use wrapper component to stick section headers to the top when scrolling brings them to top

A lightweight, performant, and simple-to-use wrapper component to stick section headers to the top when scrolling brings them to top

Mayank 7 Jun 27, 2022
🧭 Full Stack TypeScript Application Developed as POC

Tractian Challenge POC Full Stack Development Project Browse Back End code» - Browse Front End code» Built With Table of Contents Deployed Instances I

Nivaldo Farias 1 Nov 20, 2022
POC implementation of liveblocks.io obsidian plugin

Obsidian Liveblocks by shabegom A POC implementation of liveblocks.io inside an obsidian plugin. Install Create an account at https://liveblocks.io Gr

Sam 9 Oct 7, 2022
When a person that doesn't know how to create a programming language tries to create a programming language

Kochanowski Online Spróbuj Kochanowskiego bez konfiguracji projektu! https://mmusielik.xyz/projects/kochanowski Instalacja Stwórz nowy projekt przez n

Maciej Musielik 18 Dec 4, 2022
Cookbook Method is the process of learning a programming language by building up a repository of small programs that implement specific programming concepts.

CookBook - Hacktoberfest Find the book you want to read next! PRESENTED BY What is CookBook? A cookbook in the programming context is collection of ti

GDSC-NITH 16 Nov 17, 2022
Dojo - meta information for the project.

dojo-meta This repository contains information regarding Dojo (versions 2 and newer) that crosses package boundaries. Visit us at dojo.io for document

Dojo 227 Nov 22, 2022
Tool made to easily merge multiple GTA 5 vehicle meta files.

mmVehiclesMetaMerger Tool made to easily merge multiple GTA5 vehicle meta files. Showcase Go to Youtube video. Download Click here to go to the releas

Mateusz Mleczek 11 Jan 2, 2023
Bruh. Generalized Meta transaction dispatcher.

metuh A generalized meta-transaction library for Ethereum-flavour chains. Not overengineered like all of OpenZeppelin. Inspired heavily by the Lens Di

Liam Zebedee 2 Mar 22, 2022
Utilities for meta-level interactions with the Prisma toolkit in Node.js.

@prisma-spectrum/reflector Utilities for meta-level interactions with the Prisma toolkit in Node.js. ⚠️ Warning: This is not a formal Prisma product l

Prisma 12 Dec 16, 2022
Functions and objects that make it easier to add fields to Portable Text editors for accessibility meta information, like language changes or abbreviations.

Porta11y Porta11y is a collection of accessibility-focused annotations, decorators and validators for Sanity’s Portable Text editor. Portable Text is

Hidde de Vries 21 Aug 25, 2022