compatfactory
A library that unifies the TypeScript Compiler API factory functions across all versions of TypeScript and makes them conform with the Node Factory API
Last updated 3 years ago by wessberg .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install compatfactory 
SYNC missed versions from official npm registry.
Logo

A library that unifies the TypeScript Compiler API factory functions across all versions of TypeScript and makes them conform with the Node Factory API

Downloads per month NPM version Dependencies Contributors code style: prettier License: MIT Support on Patreon

Description

With the release of TypeScript 4.0, the team announced that they would move away from the old set of factory functions for creating and updating nodes, and over to a new Node Factory API.

While this is great, a lot of tooling around TypeScript works across multiple versions of it and either have to stick with the old, deprecated factory functions, stop supporting older versions of TypeScript, or interchangeably use the old or new factory functions depending on the TypeScript version being used. This is cumbersome, error prone, difficult to read, and hard to maintain.

This library provides a little helper function, ensureNodeFactory, which takes a NodeFactory or a typescript object, and then returns an object conforming to the NodeFactory interface. In case a NodeFactory is passed to it, or if one could be found via the typescript.factory property, it will patch any inconsistencies there may be between the signatures of the factory functions across TypeScript versions and most often simply return the existing one with no further edits. For older TypeScript versions, it will wrap its factory functions with the new API such that you can simply use one API for all your operations.

Features

  • A simple wrapper that enables you to use one API for every TypeScript version
  • Simple and tiny

Backers

Bubbles Christopher Blanchard Ideal Postcodes Xerox Trent Raymond scrubtheweb
Bubbles
Twitter: @usebubbles
Christopher Blanchard Ideal Postcodes Xerox Trent Raymond scrubtheweb

Patreon

Patrons on Patreon

Table of Contents

Install

npm

$ npm install compatfactory

Yarn

$ yarn add compatfactory

pnpm

$ pnpm add compatfactory

Peer Dependencies

compatfactory depends on typescript, so you need to manually install this as well.

Usage

Simply import ensureNodeFactory and use it in place of the Node Factory you would otherwise be working with.

One very basic example could be:

import {ensureNodeFactory} from "compatfactory";

// Will use typescript.factory if available, and otherwise return an object that wraps typescript's helper functions
// but makes them conform with the Node Factory API
const factory = ensureNodeFactory(typescript);
factory.createClassDeclaration(/* ... */);

A more realistic example would be inside a Custom Transformer context:

import {ensureNodeFactory} from "compatfactory";
import type TS from "typescript";

function getCustomTransformers(typescript: typeof TS): TS.CustomTransformers {
	return {
		before: [
			context => {
				const factory = ensureNodeFactory(context.factory ?? typescript);

				return sourceFile => {
					return factory.updateSourceFile(
						sourceFile
						// ...
					);
				};
			}
		]
	};
}

Contributing

Do you want to contribute? Awesome! Please follow these recommendations.

Maintainers

Frederik Wessberg
Frederik Wessberg
Twitter: @FredWessberg
Github: @wessberg
Lead Developer

License

MIT © Frederik Wessberg (@FredWessberg) (Website)

Current Tags

  • 1.0.1                                ...           latest (3 years ago)

15 Versions

  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
  • 0.0.13                                ...           3 years ago
  • 0.0.12                                ...           3 years ago
  • 0.0.11                                ...           3 years ago
  • 0.0.10                                ...           3 years ago
  • 0.0.9                                ...           3 years ago
  • 0.0.8                                ...           3 years ago
  • 0.0.7                                ...           4 years ago
  • 0.0.6                                ...           4 years ago
  • 0.0.5                                ...           4 years ago
  • 0.0.4                                ...           4 years ago
  • 0.0.3                                ...           4 years ago
  • 0.0.2                                ...           4 years ago
  • 0.0.1                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (42)

Copyright 2013 - present © cnpmjs.org | Home |