Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

compatfactory

wessberg253kMIT4.0.4TypeScript support: included

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

typescript, NodeFactory, Compiler API, compat, normalize

readme

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

TypeScript's Compiler APIs are constantly evolving. With the release of TypeScript 4.0, the TypeScript 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. With the release of TypeScript 5.0, the old factory functions were removed entirely.

Nowadays, if you maintain a library or a tool that needs to work across multiple versions of TypeScript and you use any of TypeScript's Compiler APIs, you're going to have a really tough time. It will be error prone, difficult to read, and hard to maintain. There are many differences between the signatures of these methods across all versions of TypeScript, and many may not even exist.

This library exists to fix this problem. It simply provides a 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 the most recent TypeScript Compiler API for every TypeScript version without having to worry about inconsistensies
  • Tiny

Backers

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)

changelog

4.0.4 (2024-10-31)

4.0.3 (2024-10-31)

Bug Fixes

  • bugs related to createImportDeclaration on older TypeScript versions (8ef960e)

4.0.2 (2024-09-25)

Bug Fixes

  • export- and import declaration bugs on TS 5.2 (b5b9223)

4.0.1 (2024-09-24)

Bug Fixes

  • handle export declarations on TS v4.8 and 4.9 (b05744c)

4.0.0 (2024-09-24)

Features

  • add TypeScript v5.2.2 compatibility (8090a3d)
  • add TypeScript v5.3 support (4542219)
  • add TypeScript v5.5 support (feefd1b)
  • add TypeScript v5.6 support (e941e67)

3.0.0 (2023-08-01)

Bug Fixes

  • allow TypeScript@5 and newer as peer dependency (68cf1e7)
  • update docs (865d43a)

Features

  • add support for TypeScript v5.1 (1b1111c)

2.0.9 (2023-01-09)

Bug Fixes

  • set undefined instead of empty array as decorators for all TS versions (905175e)

2.0.8 (2023-01-09)

Bug Fixes

  • fix issue with updateImportEqualsDeclaration on TypeScript 3.x (c5a67b8)

2.0.7 (2023-01-09)

Bug Fixes

  • implement compat handling for createTypeParameterDeclaration with or without modifiers as first argument (d87f910)

2.0.6 (2023-01-09)

Bug Fixes

  • fix issues with overloads (acc70eb)

2.0.5 (2023-01-09)

Bug Fixes

  • add missing overloaded signatures (95a6eee)

2.0.4 (2023-01-09)

Bug Fixes

  • fix issue with overloaded arguments (45a7456)

2.0.3 (2023-01-09)

Bug Fixes

  • fix issue with constructing ClassStaticDeclarations (4370bfd)

2.0.2 (2023-01-09)

Bug Fixes

  • correctly split decorators and modifiers (5cb9b93)

2.0.1 (2023-01-09)

Bug Fixes

  • resolve issues with import declarations (6efb703)

2.0.0 (2023-01-09)

Bug Fixes

Features

  • add support for TypeScript v4.8 (05020b8)
  • add support for TypeScript v4.9 (4d157b3)

1.0.1 (2022-05-30)

Features

  • add support for passing in 5 arguments to creatImportTypeNode on all versions of TypeScript (9ae49d6)

1.0.0 (2022-05-29)

0.0.13 (2022-04-12)

0.0.12 (2021-11-17)

Bug Fixes

  • ensure that createExportSpecifier can be called with three arguments across all TypeScript versions (aad253d)

0.0.11 (2021-11-17)

Bug Fixes

  • ensure that createImportSpecifier can be called with three arguments across all TypeScript versions (cd840b0)

0.0.10 (2021-11-17)

Features

  • add TypeScript v4.5 support (2d33062)

0.0.9 (2021-08-30)

Features

  • add support for TypeScript v4.4 and add more compatibility fixes (285ae96)

0.0.8 (2021-06-11)

Features

  • property-access-chain: add compatibility for TypeScript < 3.6 when generating PropertyAccessChains (909ab22)

0.0.7 (2021-05-29)

Bug Fixes

  • use provided modifiers when creating/updating MethodSignatures (cfac75f)

0.0.6 (2021-05-29)

Bug Fixes

  • allow passing an already wrapped TypeScript object to ensureNodeFactory without performing any additional wrapping (913abb8)

0.0.5 (2021-05-29)

Bug Fixes

  • fix signature for createVariableDeclaration (a311bc1)

0.0.4 (2021-05-28)

Features

  • add additional JSDoc factory functions for older TypeScript versions (99ced96)

0.0.3 (2021-05-28)

Features

  • unify signatures for createImportEqualsDeclaration and createMappedTypeNode with latest TypeScript versions for TypeScript <4 (ed7e4e0)

0.0.2 (2021-05-28)

Features

  • unify signatures for createImportEqualsDeclaration and createMappedTypeNode with latest TypeScript versions for v4.0 and v4.1 (a4e3f5f)

0.0.1 (2021-05-28)