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

inline-codegen

v0.0.8

Published

Insert code inline next to the statement that generates it

Downloads

13

Readme

inline-codegen

(perhaps a better name would be living-snippets?)

Generates code and places the result directly inline in the source code file.

  1. Scans a typescript file for lines starting with INSERT_SNIPPET_ABOVE(fn, vars)
  2. Executes the source code file and sets an environmental variable that causes INSERT_SNIPPET_ABOVE to run which in turn executes fn(vars) and saves the result
  3. interpolates those results directly above the INSERT_SNIPPET_ABOVE statements as necessary

under normal conditions INSERT_SNIPPET_ABOVE just noops

Why?

I am prototyping an idea I have. I have to generate a lot of boilerplate code to get typescript to not explode due to https://github.com/Microsoft/TypeScript/issues/25023

I was able to encode a solution as a snippet but it's about a dozen+ plus lines of code per use. So it starts to get pretty hard to maintain all that boilerplate.

So instead I wrote this module to see if:

per h-mst:

export const MSTModelSnippet = (name: string) => {
  return `
type ${name}ModelTypeOf = typeof ${name}Model
type ${name}TypeTypeOf = typeof ${name}Model.Type
export interface ${name}Type extends ${name}TypeTypeOf {}
type ${name}SnapshotTypeTypeOf = typeof ${name}Model.SnapshotType
export interface ${name}SnapshotType extends ${name}SnapshotTypeTypeOf {}
type ${name}CreationTypeTypeOf = typeof ${name}Model.CreationType
export interface ${name}CreationType extends ${name}CreationTypeTypeOf {}
export interface ${name} extends ${name}ModelTypeOf {
  Type: ${name}Type
  CreationType: ${name}CreationType
  SnapshotType: ${name}SnapshotType
}
export const ${name}: ${name} = ${name}Model
`
}

source code where the expansion occurs:

import {types} from "mobx-state-tree"
const M1Model = types.model("M1", {})
INSERT_SNIPPET_ABOVE(import("h-mst").MSTModelSnippet, M1Model.name)

which transforms into

const M1Model = types.model("M1", {})
//#region ISA_START Automatically Generated Code (Do not edit)
type M1ModelTypeOf = typeof M1Model
type M1TypeTypeOf = typeof M1Model.Type
export interface M1Type extends M1TypeTypeOf {}
type M1SnapshotTypeTypeOf = typeof M1Model.SnapshotType
export interface M1SnapshotType extends M1SnapshotTypeTypeOf {}
type M1CreationTypeTypeOf = typeof M1Model.CreationType
export interface M1CreationType extends M1CreationTypeTypeOf {}
export interface M1 extends M1ModelTypeOf {
  Type: M1Type
  CreationType: M1CreationType
  SnapshotType: M1SnapshotType
}
export const M1: M1 = M1Model
//#endregion ISA_END Automatically Generated Code (Do not edit)
INSERT_SNIPPET_ABOVE(MSTModelSnippet, M1Model.name)

Would work... and so far it looks promising (if obviously a bit less than ideal)

(Also notice the use of a #region this allows vcode to collapse and hide the codegen fairly seamlessly)

I believe this approach may have applications in other areas and be a good general compromise to waiting for macros to be added to typescript.

I have several additional ideas I would like to try out to further enhance the power of this tool.

Current shortcomings

Imagine the simplest possible implementation and that's what's done here, no fancy parsing (probably not a good idea anyhow in the long run since the snippets may need to be able to run for the rest of the file to compile).

Currently the way it works is to actually execute the file with the codegen in it, then capture the result and then re-insert it. This allows for the trick of pulling the M1Model.name. However if there are multiple codegens that depend on each other then it can cause a catch-22.