Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ts-patch

Package Overview
Dependencies
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-patch - npm Package Compare versions

Comparing version 2.1.0 to 3.0.0-beta1

actions/check.d.ts

6

index.d.ts

@@ -1,4 +0,4 @@

export { getTSPackage, getTSModule, TSPackage, TSModule } from './lib/file-utils';
export { TSPOptions } from './lib/system/options';
export { install, uninstall, patch, setOptions } from './lib/actions';
export { InstallerOptions, getInstallerOptions } from './options';
export { install, uninstall, patch, check } from './actions';
export { getLiveModule } from './module';
export * from './plugin-types';

@@ -17,12 +17,14 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.setOptions = exports.patch = exports.uninstall = exports.install = exports.getTSModule = exports.getTSPackage = void 0;
var file_utils_1 = require("./lib/file-utils");
Object.defineProperty(exports, "getTSPackage", { enumerable: true, get: function () { return file_utils_1.getTSPackage; } });
Object.defineProperty(exports, "getTSModule", { enumerable: true, get: function () { return file_utils_1.getTSModule; } });
var actions_1 = require("./lib/actions");
exports.getLiveModule = exports.check = exports.patch = exports.uninstall = exports.install = exports.getInstallerOptions = exports.InstallerOptions = void 0;
var options_1 = require("./options");
Object.defineProperty(exports, "InstallerOptions", { enumerable: true, get: function () { return options_1.InstallerOptions; } });
Object.defineProperty(exports, "getInstallerOptions", { enumerable: true, get: function () { return options_1.getInstallerOptions; } });
var actions_1 = require("./actions");
Object.defineProperty(exports, "install", { enumerable: true, get: function () { return actions_1.install; } });
Object.defineProperty(exports, "uninstall", { enumerable: true, get: function () { return actions_1.uninstall; } });
Object.defineProperty(exports, "patch", { enumerable: true, get: function () { return actions_1.patch; } });
Object.defineProperty(exports, "setOptions", { enumerable: true, get: function () { return actions_1.setOptions; } });
Object.defineProperty(exports, "check", { enumerable: true, get: function () { return actions_1.check; } });
var module_1 = require("./module");
Object.defineProperty(exports, "getLiveModule", { enumerable: true, get: function () { return module_1.getLiveModule; } });
__exportStar(require("./plugin-types"), exports);
//# sourceMappingURL=index.js.map
{
"name": "ts-patch",
"version": "2.1.0",
"version": "3.0.0-beta1",
"description": "Patch typescript to support custom transformers in tsconfig.json",

@@ -30,14 +30,14 @@ "main": "./index.js",

"chalk": "^4.1.2",
"glob": "^8.0.3",
"global-prefix": "^3.0.0",
"minimist": "^1.2.6",
"resolve": "^1.22.1",
"shelljs": "^0.8.5",
"minimist": "^1.2.8",
"resolve": "^1.22.2",
"semver": "^7.3.8",
"strip-ansi": "^6.0.1"
},
"bin": {
"ts-patch": "./bin/cli.js"
"ts-patch": "./bin/ts-patch.js",
"tspc": "./bin/tspc.js"
},
"peerDependencies": {
"typescript": ">=4.0.0"
"typescript": ">=5.0.0"
},

@@ -50,34 +50,2 @@ "directories": {

{
"type": "feat",
"section": "Features"
},
{
"type": "fix",
"section": "Bug Fixes"
},
{
"type": "chore",
"hidden": true
},
{
"type": "docs",
"hidden": true
},
{
"type": "style",
"hidden": true
},
{
"type": "refactor",
"hidden": true
},
{
"type": "perf",
"hidden": true
},
{
"type": "test",
"hidden": true
},
{
"type": "change",

@@ -84,0 +52,0 @@ "section": "Changes"

@@ -0,1 +1,4 @@

/**
* NOTE: This file is used during the build process for patch as well
*/
import type ts from 'typescript';

@@ -43,5 +46,5 @@ export interface PluginConfig {

}
export declare type TransformerList = Required<ts.CustomTransformers>;
export declare type TransformerPlugin = TransformerBasePlugin | ts.TransformerFactory<ts.SourceFile>;
export declare type PluginFactory = LSPattern | ProgramPattern | ConfigPattern | CompilerOptionsPattern | TypeCheckerPattern | RawPattern;
export type TransformerList = Required<ts.CustomTransformers>;
export type TransformerPlugin = TransformerBasePlugin | ts.TransformerFactory<ts.SourceFile>;
export type PluginFactory = LSPattern | ProgramPattern | ConfigPattern | CompilerOptionsPattern | TypeCheckerPattern | RawPattern;
export interface TransformerBasePlugin {

@@ -52,4 +55,4 @@ before?: ts.TransformerFactory<ts.SourceFile>;

}
export declare type DiagnosticMap = WeakMap<ts.Program, ts.Diagnostic[]>;
export declare type TransformerExtras = {
export type DiagnosticMap = WeakMap<ts.Program, ts.Diagnostic[]>;
export type TransformerExtras = {
/**

@@ -67,3 +70,3 @@ * Originating TypeScript instance

};
export declare type ProgramTransformerExtras = {
export type ProgramTransformerExtras = {
/**

@@ -74,8 +77,8 @@ * Originating TypeScript instance

};
export declare type ProgramTransformer = (program: ts.Program, host: ts.CompilerHost | undefined, config: PluginConfig, extras: ProgramTransformerExtras) => ts.Program;
export declare type LSPattern = (ls: ts.LanguageService, config: {}) => TransformerPlugin;
export declare type CompilerOptionsPattern = (compilerOpts: ts.CompilerOptions, config: {}) => TransformerPlugin;
export declare type ConfigPattern = (config: {}) => TransformerPlugin;
export declare type TypeCheckerPattern = (checker: ts.TypeChecker, config: {}) => TransformerPlugin;
export declare type ProgramPattern = (program: ts.Program, config: {}, extras: TransformerExtras) => TransformerPlugin;
export declare type RawPattern = (context: ts.TransformationContext, program: ts.Program, config: {}) => ts.Transformer<ts.SourceFile>;
export type ProgramTransformer = (program: ts.Program, host: ts.CompilerHost | undefined, config: PluginConfig, extras: ProgramTransformerExtras) => ts.Program;
export type LSPattern = (ls: ts.LanguageService, config: {}) => TransformerPlugin;
export type CompilerOptionsPattern = (compilerOpts: ts.CompilerOptions, config: {}) => TransformerPlugin;
export type ConfigPattern = (config: {}) => TransformerPlugin;
export type TypeCheckerPattern = (checker: ts.TypeChecker, config: {}) => TransformerPlugin;
export type ProgramPattern = (program: ts.Program, config: {}, extras: TransformerExtras) => TransformerPlugin;
export type RawPattern = (context: ts.TransformationContext, program: ts.Program, config: {}) => ts.Transformer<ts.SourceFile>;

@@ -12,4 +12,12 @@ [![npm version](https://badge.fury.io/js/ts-patch.svg)](https://badge.fury.io/js/ts-patch)

## TypeScript v5 Note
TS v5 has made some fundamental changes which affect the current process. As a result, it is not yet supported.
We're working on adding support. More notes on that here:
- [Issue #93 — Not working with TypeScript v5 (author's note)](https://github.com/nonara/ts-patch/issues/93)
## Features
* Patch / unpatch any version of typescript (4.0+)
* Patch / unpatch any version of typescript (4.*)
* Advanced options for patching individual libraries, specific locations, etc. (see `ts-patch /?`)

@@ -44,14 +52,2 @@ * _(New)_ Supports 'transforming' the `Program` instance during creation. (see: [Transforming Program](#transforming-program))

## Rewrite Coming Soon...
With a couple years of hindsight, it's time for a much needed redesign to make a more complete plugin ecosystem! The new
design will also be friendlier for the various package management apps.
Development is already underway, but my time to work on it is limited! To follow progress, see [this discussion](https://github.com/nonara/ts-patch/discussions/40).
Notes —
- The new version will be a major release
- It will still support all legacy packages and config
- Previous major version will be maintained and patched for any issues for at least one year
## Table of Contents

@@ -206,2 +202,4 @@ - [Configuring](#configuring)

[`{ transform: "typia/lib/transform" }`](https://github.com/samchon/typia)
### Transforming Program

@@ -208,0 +206,0 @@

@@ -42,5 +42,3 @@ declare namespace ts {

declare namespace ts {
const tspVersion: string;
const originalCreateProgram: typeof ts.createProgram;
const diagnosticMap: WeakMap<ts.Program, ts.Diagnostic[]>;
}

@@ -1,488 +0,489 @@

"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function (t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s)
if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
var tsp = (function () {
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function (t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s)
if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m)
return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
ar.push(r.value);
}
catch (error) {
e = { error: error };
}
finally {
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m)
return o;
var i = m.call(o), r, ar = [], e;
try {
if (r && !r.done && (m = i["return"]))
m.call(i);
while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
ar.push(r.value);
}
catch (error) {
e = { error: error };
}
finally {
if (e)
throw e.error;
}
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2)
for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar)
ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
try {
if (r && !r.done && (m = i["return"]))
m.call(i);
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __values = (this && this.__values) || function (o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m)
return m.call(o);
if (o && typeof o.length === "number")
return {
next: function () {
if (o && i >= o.length)
o = void 0;
return { value: o && o[i++], done: !o };
finally {
if (e)
throw e.error;
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s)
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var tsp;
(function (tsp) {
var activeProgramTransformers = new Set();
var dirname = require("path").dirname;
function getProjectDir(compilerOptions) {
return compilerOptions.configFilePath && dirname(compilerOptions.configFilePath);
}
function getProjectConfig(compilerOptions, rootFileNames) {
var configFilePath = compilerOptions.configFilePath;
var projectDir = getProjectDir(compilerOptions);
if (configFilePath === undefined) {
var baseDir = (rootFileNames.length > 0) ? dirname(rootFileNames[0]) : projectDir !== null && projectDir !== void 0 ? projectDir : process.cwd();
configFilePath = ts.findConfigFile(baseDir, ts.sys.fileExists);
if (configFilePath) {
var config = readConfig(configFilePath);
compilerOptions = __assign(__assign({}, config.options), compilerOptions);
projectDir = getProjectDir(compilerOptions);
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2)
for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar)
ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __values = (this && this.__values) || function (o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m)
return m.call(o);
if (o && typeof o.length === "number")
return {
next: function () {
if (o && i >= o.length)
o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s)
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var tsp;
(function (tsp) {
var activeProgramTransformers = new Set();
var dirname = require("path").dirname;
function getProjectDir(compilerOptions) {
return compilerOptions.configFilePath && dirname(compilerOptions.configFilePath);
}
return ({ projectDir: projectDir, compilerOptions: compilerOptions });
}
function readConfig(configFileNamePath) {
var projectDir = dirname(configFileNamePath);
var result = ts.readConfigFile(configFileNamePath, ts.sys.readFile);
if (result.error)
throw new Error("Error in tsconfig.json: " + result.error.messageText);
return ts.parseJsonConfigFileContent(result.config, ts.sys, projectDir, undefined, configFileNamePath);
}
function preparePluginsFromCompilerOptions(plugins) {
if (!plugins)
return [];
if ((plugins.length === 1) && plugins[0].customTransformers) {
var _a = plugins[0].customTransformers, _b = _a.before, before = _b === void 0 ? [] : _b, _c = _a.after, after = _c === void 0 ? [] : _c;
return __spreadArray(__spreadArray([], __read(before.map(function (item) { return ({ transform: item }); })), false), __read(after.map(function (item) { return ({ transform: item, after: true }); })), false);
}
return plugins;
}
function createProgram(rootNamesOrOptions, options, host, oldProgram, configFileParsingDiagnostics) {
var e_1, _a, e_2, _b;
var _c;
var rootNames;
var createOpts = !Array.isArray(rootNamesOrOptions) ? rootNamesOrOptions : void 0;
if (createOpts) {
rootNames = createOpts.rootNames;
options = createOpts.options;
host = createOpts.host;
oldProgram = createOpts.oldProgram;
configFileParsingDiagnostics = createOpts.configFileParsingDiagnostics;
}
else {
options = options;
rootNames = rootNamesOrOptions;
}
var projectConfig = getProjectConfig(options, rootNames);
if (tsp.isTSC) {
options = projectConfig.compilerOptions;
if (createOpts)
createOpts.options = options;
}
var program = createOpts ?
ts.originalCreateProgram(createOpts) :
ts.originalCreateProgram(rootNames, options, host, oldProgram, configFileParsingDiagnostics);
var plugins = preparePluginsFromCompilerOptions(options.plugins);
var pluginCreator = new tsp.PluginCreator(plugins, (_c = projectConfig.projectDir) !== null && _c !== void 0 ? _c : process.cwd());
var programTransformers = new Map(pluginCreator.getProgramTransformers());
try {
for (var _d = __values(pluginCreator.getProgramTransformers()), _e = _d.next(); !_e.done; _e = _d.next()) {
var _f = __read(_e.value, 1), transformer = _f[0];
if (activeProgramTransformers.has(transformer))
programTransformers.delete(transformer);
else
activeProgramTransformers.add(transformer);
function getProjectConfig(compilerOptions, rootFileNames) {
var configFilePath = compilerOptions.configFilePath;
var projectDir = getProjectDir(compilerOptions);
if (configFilePath === undefined) {
var baseDir = (rootFileNames.length > 0) ? dirname(rootFileNames[0]) : projectDir !== null && projectDir !== void 0 ? projectDir : process.cwd();
configFilePath = tsp.shim.findConfigFile(baseDir, tsp.shim.sys.fileExists);
if (configFilePath) {
var config = readConfig(configFilePath);
compilerOptions = __assign(__assign({}, config.options), compilerOptions);
projectDir = getProjectDir(compilerOptions);
}
}
return ({ projectDir: projectDir, compilerOptions: compilerOptions });
}
catch (e_1_1) {
e_1 = { error: e_1_1 };
function readConfig(configFileNamePath) {
var projectDir = dirname(configFileNamePath);
var result = tsp.shim.readConfigFile(configFileNamePath, tsp.shim.sys.readFile);
if (result.error)
throw new Error("Error in tsconfig.json: " + result.error.messageText);
return tsp.shim.parseJsonConfigFileContent(result.config, tsp.shim.sys, projectDir, undefined, configFileNamePath);
}
finally {
try {
if (_e && !_e.done && (_a = _d.return))
_a.call(_d);
function preparePluginsFromCompilerOptions(plugins) {
if (!plugins)
return [];
if ((plugins.length === 1) && plugins[0].customTransformers) {
var _a = plugins[0].customTransformers, _b = _a.before, before = _b === void 0 ? [] : _b, _c = _a.after, after = _c === void 0 ? [] : _c;
return __spreadArray(__spreadArray([], __read(before.map(function (item) { return ({ transform: item }); })), false), __read(after.map(function (item) { return ({ transform: item, after: true }); })), false);
}
finally {
if (e_1)
throw e_1.error;
}
return plugins;
}
try {
for (var programTransformers_1 = __values(programTransformers), programTransformers_1_1 = programTransformers_1.next(); !programTransformers_1_1.done; programTransformers_1_1 = programTransformers_1.next()) {
var _g = __read(programTransformers_1_1.value, 2), programTransformer = _g[0], config = _g[1];
var newProgram = programTransformer(program, host, config, { ts: ts });
if (typeof (newProgram === null || newProgram === void 0 ? void 0 : newProgram["emit"]) === "function")
program = newProgram;
function createProgram(rootNamesOrOptions, options, host, oldProgram, configFileParsingDiagnostics) {
var e_1, _a, e_2, _b;
var _c;
var rootNames;
var createOpts = !Array.isArray(rootNamesOrOptions) ? rootNamesOrOptions : void 0;
if (createOpts) {
rootNames = createOpts.rootNames;
options = createOpts.options;
host = createOpts.host;
oldProgram = createOpts.oldProgram;
configFileParsingDiagnostics = createOpts.configFileParsingDiagnostics;
}
}
catch (e_2_1) {
e_2 = { error: e_2_1 };
}
finally {
try {
if (programTransformers_1_1 && !programTransformers_1_1.done && (_b = programTransformers_1.return))
_b.call(programTransformers_1);
else {
options = options;
rootNames = rootNamesOrOptions;
}
finally {
if (e_2)
throw e_2.error;
var projectConfig = getProjectConfig(options, rootNames);
if (["tsc", "tsserver", "tsserverlibrary"].includes(tsp.currentLibrary)) {
options = projectConfig.compilerOptions;
if (createOpts)
createOpts.options = options;
}
}
programTransformers.forEach(function (c, transformer) { return activeProgramTransformers.delete(transformer); });
if (!program.originalEmit) {
program.originalEmit = program.emit;
program.emit = newEmit;
}
function newEmit(targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers) {
var e_3, _a;
var additionalArgs = [];
for (var _i = 5; _i < arguments.length; _i++) {
additionalArgs[_i - 5] = arguments[_i];
}
var transformers = pluginCreator.createTransformers({ program: program }, customTransformers);
var result = program.originalEmit.apply(program, __spreadArray([targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, transformers], __read(additionalArgs), false));
var program = createOpts ?
tsp.shim.originalCreateProgram(createOpts) :
tsp.shim.originalCreateProgram(rootNames, options, host, oldProgram, configFileParsingDiagnostics);
var plugins = preparePluginsFromCompilerOptions(options.plugins);
var pluginCreator = new tsp.PluginCreator(plugins, (_c = projectConfig.projectDir) !== null && _c !== void 0 ? _c : process.cwd());
var programTransformers = new Map(pluginCreator.getProgramTransformers());
try {
for (var _b = __values(tsp.diagnosticMap.get(program) || []), _c = _b.next(); !_c.done; _c = _b.next()) {
var diagnostic = _c.value;
if (!result.diagnostics.includes(diagnostic))
result.diagnostics.push(diagnostic);
for (var _d = __values(pluginCreator.getProgramTransformers()), _e = _d.next(); !_e.done; _e = _d.next()) {
var _f = __read(_e.value, 1), transformer = _f[0];
if (activeProgramTransformers.has(transformer))
programTransformers.delete(transformer);
else
activeProgramTransformers.add(transformer);
}
}
catch (e_3_1) {
e_3 = { error: e_3_1 };
catch (e_1_1) {
e_1 = { error: e_1_1 };
}
finally {
try {
if (_c && !_c.done && (_a = _b.return))
_a.call(_b);
if (_e && !_e.done && (_a = _d.return))
_a.call(_d);
}
finally {
if (e_3)
throw e_3.error;
if (e_1)
throw e_1.error;
}
}
return result;
}
return program;
}
tsp.createProgram = createProgram;
})(tsp || (tsp = {}));
var tsp;
(function (tsp) {
Object.defineProperties(ts, {
isTSC: { get: function () { return tsp.isTSC; }, enumerable: true },
tspVersion: { get: function () { return tsp.tspVersion; }, enumerable: true },
PluginCreator: { get: function () { return tsp.PluginCreator; }, enumerable: true },
originalCreateProgram: { value: ts.createProgram, enumerable: true },
createProgram: { value: tsp.createProgram },
diagnosticMap: { get: function () { return tsp.diagnosticMap; }, enumerable: true }
});
})(tsp || (tsp = {}));
var tsp;
(function (tsp) {
var path = require("path");
var fs = require("fs");
var tsNodeIncluded = false;
var requireStack = [];
var PluginCreator = (function () {
function PluginCreator(configs, resolveBaseDir) {
var e_4, _a;
if (resolveBaseDir === void 0) {
resolveBaseDir = process.cwd();
}
this.configs = configs;
this.resolveBaseDir = resolveBaseDir;
PluginCreator.validateConfigs(configs);
try {
for (var configs_1 = __values(configs), configs_1_1 = configs_1.next(); !configs_1_1.done; configs_1_1 = configs_1.next()) {
var config = configs_1_1.value;
if (config["beforeEmit"])
config.transformProgram = true;
for (var programTransformers_1 = __values(programTransformers), programTransformers_1_1 = programTransformers_1.next(); !programTransformers_1_1.done; programTransformers_1_1 = programTransformers_1.next()) {
var _g = __read(programTransformers_1_1.value, 2), programTransformer = _g[0], config = _g[1];
var newProgram = programTransformer(program, host, config, { ts: ts });
if (typeof (newProgram === null || newProgram === void 0 ? void 0 : newProgram["emit"]) === "function")
program = newProgram;
}
}
catch (e_4_1) {
e_4 = { error: e_4_1 };
catch (e_2_1) {
e_2 = { error: e_2_1 };
}
finally {
try {
if (configs_1_1 && !configs_1_1.done && (_a = configs_1.return))
_a.call(configs_1);
if (programTransformers_1_1 && !programTransformers_1_1.done && (_b = programTransformers_1.return))
_b.call(programTransformers_1);
}
finally {
if (e_4)
throw e_4.error;
if (e_2)
throw e_2.error;
}
}
}
PluginCreator.prototype.mergeTransformers = function (into, source) {
var _a, _b, _c;
var slice = function (input) { return (Array.isArray(input) ? input.slice() : [input]); };
if (source.before)
(_a = into.before).push.apply(_a, __spreadArray([], __read(slice(source.before)), false));
if (source.after)
(_b = into.after).push.apply(_b, __spreadArray([], __read(slice(source.after)), false));
if (source.afterDeclarations)
(_c = into.afterDeclarations).push.apply(_c, __spreadArray([], __read(slice(source.afterDeclarations)), false));
return this;
};
PluginCreator.prototype.createTransformers = function (params, customTransformers) {
var e_5, _a;
var transformers = { before: [], after: [], afterDeclarations: [] };
var _b = __read(("ls" in params) ? [params.ls, params.ls.getProgram()] : [void 0, params.program], 2), ls = _b[0], program = _b[1];
try {
for (var _c = __values(this.configs), _d = _c.next(); !_d.done; _d = _c.next()) {
var config = _d.value;
if (!config.transform || config.transformProgram)
continue;
var factory = this.resolveFactory(config);
if (factory === undefined)
continue;
this.mergeTransformers(transformers, PluginCreator.createTransformerFromPattern({ factory: factory, config: config, program: program, ls: ls }));
programTransformers.forEach(function (c, transformer) { return activeProgramTransformers.delete(transformer); });
if (!program.originalEmit) {
program.originalEmit = program.emit;
program.emit = newEmit;
}
function newEmit(targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers) {
var e_3, _a;
var additionalArgs = [];
for (var _i = 5; _i < arguments.length; _i++) {
additionalArgs[_i - 5] = arguments[_i];
}
}
catch (e_5_1) {
e_5 = { error: e_5_1 };
}
finally {
var transformers = pluginCreator.createTransformers({ program: program }, customTransformers);
var result = program.originalEmit.apply(program, __spreadArray([targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, transformers], __read(additionalArgs), false));
try {
if (_d && !_d.done && (_a = _c.return))
_a.call(_c);
for (var _b = __values(tsp.diagnosticMap.get(program) || []), _c = _b.next(); !_c.done; _c = _b.next()) {
var diagnostic = _c.value;
if (!result.diagnostics.includes(diagnostic))
result.diagnostics.push(diagnostic);
}
}
catch (e_3_1) {
e_3 = { error: e_3_1 };
}
finally {
if (e_5)
throw e_5.error;
try {
if (_c && !_c.done && (_a = _b.return))
_a.call(_b);
}
finally {
if (e_3)
throw e_3.error;
}
}
return result;
}
if (customTransformers)
this.mergeTransformers(transformers, customTransformers);
return transformers;
};
PluginCreator.prototype.getProgramTransformers = function () {
var e_6, _a;
var res = [];
try {
for (var _b = __values(this.configs), _c = _b.next(); !_c.done; _c = _b.next()) {
var config = _c.value;
if (!config.transform || !config.transformProgram)
continue;
var factory = this.resolveFactory(config);
if (factory === undefined)
continue;
res.push([factory, config]);
return program;
}
tsp.createProgram = createProgram;
})(tsp || (tsp = {}));
var tsp;
(function (tsp) {
var path = require("path");
var tsNodeIncluded = false;
var requireStack = [];
var PluginCreator = (function () {
function PluginCreator(configs, resolveBaseDir) {
var e_4, _a;
if (resolveBaseDir === void 0) {
resolveBaseDir = process.cwd();
}
}
catch (e_6_1) {
e_6 = { error: e_6_1 };
}
finally {
this.configs = configs;
this.resolveBaseDir = resolveBaseDir;
PluginCreator.validateConfigs(configs);
try {
if (_c && !_c.done && (_a = _b.return))
_a.call(_b);
for (var configs_1 = __values(configs), configs_1_1 = configs_1.next(); !configs_1_1.done; configs_1_1 = configs_1.next()) {
var config = configs_1_1.value;
if (config["beforeEmit"])
config.transformProgram = true;
}
}
catch (e_4_1) {
e_4 = { error: e_4_1 };
}
finally {
if (e_6)
throw e_6.error;
try {
if (configs_1_1 && !configs_1_1.done && (_a = configs_1.return))
_a.call(configs_1);
}
finally {
if (e_4)
throw e_4.error;
}
}
}
return res;
};
PluginCreator.prototype.resolveFactory = function (config) {
var _a;
var tsConfig = config.tsConfig && path.resolve(this.resolveBaseDir, config.tsConfig);
var transform = config.transform;
var importKey = config.import || "default";
var tsConfigCleanup;
if (transform.match(/\.ts$/)) {
if (tsNodeIncluded && (tsConfig !== this.currentProject))
tsNodeIncluded = false;
this.currentProject = tsConfig;
if (!tsNodeIncluded) {
PluginCreator.prototype.mergeTransformers = function (into, source) {
var _a, _b, _c;
var slice = function (input) { return (Array.isArray(input) ? input.slice() : [input]); };
if (source.before)
(_a = into.before).push.apply(_a, __spreadArray([], __read(slice(source.before)), false));
if (source.after)
(_b = into.after).push.apply(_b, __spreadArray([], __read(slice(source.after)), false));
if (source.afterDeclarations)
(_c = into.afterDeclarations).push.apply(_c, __spreadArray([], __read(slice(source.afterDeclarations)), false));
return this;
};
PluginCreator.prototype.createTransformers = function (params, customTransformers) {
var e_5, _a;
var transformers = { before: [], after: [], afterDeclarations: [] };
var _b = __read(("ls" in params) ? [params.ls, params.ls.getProgram()] : [void 0, params.program], 2), ls = _b[0], program = _b[1];
try {
for (var _c = __values(this.configs), _d = _c.next(); !_d.done; _d = _c.next()) {
var config = _d.value;
if (!config.transform || config.transformProgram)
continue;
var factory = this.resolveFactory(config);
if (factory === undefined)
continue;
this.mergeTransformers(transformers, PluginCreator.createTransformerFromPattern({ factory: factory, config: config, program: program, ls: ls }));
}
}
catch (e_5_1) {
e_5 = { error: e_5_1 };
}
finally {
try {
require("ts-node").register(__assign(__assign({ transpileOnly: true }, (tsConfig ? { project: tsConfig } : { skipProject: true })), { compilerOptions: {
target: "ES2018",
jsx: "react",
esModuleInterop: true,
module: "commonjs",
} }));
tsNodeIncluded = true;
if (_d && !_d.done && (_a = _c.return))
_a.call(_c);
}
catch (e) {
if (e.code === "MODULE_NOT_FOUND")
throw new Error("Cannot use a typescript-based transformer without ts-node installed. " +
"Either add ts-node as a (dev)-dependency or install globally.");
else
throw e;
finally {
if (e_5)
throw e_5.error;
}
}
var tsConfigData = tsConfig && ts.readConfigFile(tsConfig, ts.sys.readFile).config;
if ((_a = tsConfigData === null || tsConfigData === void 0 ? void 0 : tsConfigData.compilerOptions) === null || _a === void 0 ? void 0 : _a.paths) {
if (customTransformers)
this.mergeTransformers(transformers, customTransformers);
return transformers;
};
PluginCreator.prototype.getProgramTransformers = function () {
var e_6, _a;
var res = [];
try {
for (var _b = __values(this.configs), _c = _b.next(); !_c.done; _c = _b.next()) {
var config = _c.value;
if (!config.transform || !config.transformProgram)
continue;
var factory = this.resolveFactory(config);
if (factory === undefined)
continue;
res.push([factory, config]);
}
}
catch (e_6_1) {
e_6 = { error: e_6_1 };
}
finally {
try {
var tsConfigPaths = require("tsconfig-paths");
var absoluteBaseUrl = tsConfigPaths.loadConfig(tsConfig).absoluteBaseUrl;
tsConfigCleanup = tsConfigPaths.register({
baseUrl: absoluteBaseUrl,
paths: tsConfigData.compilerOptions.paths
});
if (_c && !_c.done && (_a = _b.return))
_a.call(_b);
}
catch (e) {
if (e.code === "MODULE_NOT_FOUND")
console.warn("Paths specified in transformer tsconfig.json, but they may not be able to be resolved. " +
"If not, try adding 'tsconfig-paths' as a dev dependency");
else
throw e;
finally {
if (e_6)
throw e_6.error;
}
}
}
var modulePath = require.resolve(transform, { paths: [this.resolveBaseDir] });
if (requireStack.indexOf(modulePath) > -1)
return;
requireStack.push(modulePath);
var commonjsModule = require(modulePath);
requireStack.pop();
tsConfigCleanup === null || tsConfigCleanup === void 0 ? void 0 : tsConfigCleanup();
var factoryModule = (typeof commonjsModule === "function") ? { default: commonjsModule } : commonjsModule;
var factory = factoryModule[importKey];
if (!factory)
throw new Error("tsconfig.json > plugins: \"".concat(transform, "\" does not have an export \"").concat(importKey, "\": ") +
require("util").inspect(factoryModule));
if (typeof factory !== "function") {
throw new Error("tsconfig.json > plugins: \"".concat(transform, "\" export \"").concat(importKey, "\" is not a plugin: ") +
require("util").inspect(factory));
}
return factory;
};
PluginCreator.validateConfigs = function (configs) {
var e_7, _a;
try {
for (var configs_2 = __values(configs), configs_2_1 = configs_2.next(); !configs_2_1.done; configs_2_1 = configs_2.next()) {
var config = configs_2_1.value;
if (!config.name && !config.transform)
throw new Error("tsconfig.json plugins error: transform must be present");
return res;
};
PluginCreator.prototype.resolveFactory = function (config) {
var _a;
var tsConfig = config.tsConfig && path.resolve(this.resolveBaseDir, config.tsConfig);
var transform = config.transform;
var importKey = config.import || "default";
var tsConfigCleanup;
if (transform.match(/\.ts$/)) {
if (tsNodeIncluded && (tsConfig !== this.currentProject))
tsNodeIncluded = false;
this.currentProject = tsConfig;
if (!tsNodeIncluded) {
try {
require("ts-node").register(__assign(__assign({ transpileOnly: true }, (tsConfig ? { project: tsConfig } : { skipProject: true })), { compilerOptions: {
target: "ES2018",
jsx: "react",
esModuleInterop: true,
module: "commonjs",
} }));
tsNodeIncluded = true;
}
catch (e) {
if (e.code === "MODULE_NOT_FOUND")
throw new Error("Cannot use a typescript-based transformer without ts-node installed. " +
"Either add ts-node as a (dev)-dependency or install globally.");
else
throw e;
}
}
var tsConfigData = tsConfig && tsp.shim.readConfigFile(tsConfig, tsp.shim.sys.readFile).config;
if ((_a = tsConfigData === null || tsConfigData === void 0 ? void 0 : tsConfigData.compilerOptions) === null || _a === void 0 ? void 0 : _a.paths) {
try {
var tsConfigPaths = require("tsconfig-paths");
var absoluteBaseUrl = tsConfigPaths.loadConfig(tsConfig).absoluteBaseUrl;
tsConfigCleanup = tsConfigPaths.register({
baseUrl: absoluteBaseUrl,
paths: tsConfigData.compilerOptions.paths
});
}
catch (e) {
if (e.code === "MODULE_NOT_FOUND")
console.warn("Paths specified in transformer tsconfig.json, but they may not be able to be resolved. " +
"If not, try adding 'tsconfig-paths' as a dev dependency");
else
throw e;
}
}
}
}
catch (e_7_1) {
e_7 = { error: e_7_1 };
}
finally {
var modulePath = require.resolve(transform, { paths: [this.resolveBaseDir] });
if (requireStack.indexOf(modulePath) > -1)
return;
requireStack.push(modulePath);
var commonjsModule = require(modulePath);
requireStack.pop();
tsConfigCleanup === null || tsConfigCleanup === void 0 ? void 0 : tsConfigCleanup();
var factoryModule = (typeof commonjsModule === "function") ? { default: commonjsModule } : commonjsModule;
var factory = factoryModule[importKey];
if (!factory)
throw new Error("tsconfig.json > plugins: \"".concat(transform, "\" does not have an export \"").concat(importKey, "\": ") +
require("util").inspect(factoryModule));
if (typeof factory !== "function") {
throw new Error("tsconfig.json > plugins: \"".concat(transform, "\" export \"").concat(importKey, "\" is not a plugin: ") +
require("util").inspect(factory));
}
return factory;
};
PluginCreator.validateConfigs = function (configs) {
var e_7, _a;
try {
if (configs_2_1 && !configs_2_1.done && (_a = configs_2.return))
_a.call(configs_2);
for (var configs_2 = __values(configs), configs_2_1 = configs_2.next(); !configs_2_1.done; configs_2_1 = configs_2.next()) {
var config = configs_2_1.value;
if (!config.name && !config.transform)
throw new Error("tsconfig.json plugins error: transform must be present");
}
}
catch (e_7_1) {
e_7 = { error: e_7_1 };
}
finally {
if (e_7)
throw e_7.error;
try {
if (configs_2_1 && !configs_2_1.done && (_a = configs_2.return))
_a.call(configs_2);
}
finally {
if (e_7)
throw e_7.error;
}
}
}
};
PluginCreator.createTransformerFromPattern = function (_a) {
var factory = _a.factory, config = _a.config, program = _a.program, ls = _a.ls;
var transform = config.transform, after = config.after, afterDeclarations = config.afterDeclarations, name = config.name, type = config.type, transformProgram = config.transformProgram, cleanConfig = __rest(config, ["transform", "after", "afterDeclarations", "name", "type", "transformProgram"]);
if (!transform)
throw new Error("Not a valid config entry: \"transform\" key not found");
var ret;
switch (config.type) {
case "ls":
if (!ls)
throw new Error("Plugin ".concat(transform, " needs a LanguageService"));
ret = factory(ls, cleanConfig);
break;
case "config":
ret = factory(cleanConfig);
break;
case "compilerOptions":
ret = factory(program.getCompilerOptions(), cleanConfig);
break;
case "checker":
ret = factory(program.getTypeChecker(), cleanConfig);
break;
case undefined:
case "program":
var _b = tsp.diagnosticExtrasFactory(program), addDiagnostic = _b.addDiagnostic, removeDiagnostic = _b.removeDiagnostic, diagnostics = _b.diagnostics;
ret = factory(program, cleanConfig, {
ts: ts,
addDiagnostic: addDiagnostic,
removeDiagnostic: removeDiagnostic,
diagnostics: diagnostics,
library: tsp.currentLibrary
});
break;
case "raw":
ret = function (ctx) { return factory(ctx, program, cleanConfig); };
break;
default:
throw new Error("Invalid plugin type found in tsconfig.json: '".concat(config.type, "'"));
}
if (typeof ret === "function")
return after ? ({ after: ret }) :
afterDeclarations ? ({ afterDeclarations: ret }) :
{ before: ret };
return ret;
};
return PluginCreator;
}());
tsp.PluginCreator = PluginCreator;
})(tsp || (tsp = {}));
var tsp;
(function (tsp) {
tsp.diagnosticMap = new WeakMap();
function diagnosticExtrasFactory(program) {
var diagnostics = tsp.diagnosticMap.get(program) || tsp.diagnosticMap.set(program, []).get(program);
var addDiagnostic = function (diag) { return diagnostics.push(diag); };
var removeDiagnostic = function (index) { diagnostics.splice(index, 1); };
return { addDiagnostic: addDiagnostic, removeDiagnostic: removeDiagnostic, diagnostics: diagnostics };
}
tsp.diagnosticExtrasFactory = diagnosticExtrasFactory;
})(tsp || (tsp = {}));
var tsp;
(function (tsp) {
tsp.shim = {
get createProgram() { var _a; return (_a = ts.createProgram) !== null && _a !== void 0 ? _a : createProgram; },
get originalCreateProgram() { var _a; return (_a = ts.originalCreateProgram) !== null && _a !== void 0 ? _a : originalCreateProgram; },
get findConfigFile() { var _a; return (_a = ts.findConfigFile) !== null && _a !== void 0 ? _a : findConfigFile; },
get readConfigFile() { var _a; return (_a = ts.readConfigFile) !== null && _a !== void 0 ? _a : readConfigFile; },
get parseJsonConfigFileContent() { var _a; return (_a = ts.parseJsonConfigFileContent) !== null && _a !== void 0 ? _a : parseJsonConfigFileContent; },
get sys() { var _a; return (_a = ts.sys) !== null && _a !== void 0 ? _a : sys; },
};
PluginCreator.createTransformerFromPattern = function (_a) {
var factory = _a.factory, config = _a.config, program = _a.program, ls = _a.ls;
var transform = config.transform, after = config.after, afterDeclarations = config.afterDeclarations, name = config.name, type = config.type, transformProgram = config.transformProgram, cleanConfig = __rest(config, ["transform", "after", "afterDeclarations", "name", "type", "transformProgram"]);
if (!transform)
throw new Error("Not a valid config entry: \"transform\" key not found");
var ret;
switch (config.type) {
case "ls":
if (!ls)
throw new Error("Plugin ".concat(transform, " needs a LanguageService"));
ret = factory(ls, cleanConfig);
break;
case "config":
ret = factory(cleanConfig);
break;
case "compilerOptions":
ret = factory(program.getCompilerOptions(), cleanConfig);
break;
case "checker":
ret = factory(program.getTypeChecker(), cleanConfig);
break;
case undefined:
case "program":
var _b = tsp.diagnosticExtrasFactory(program), addDiagnostic = _b.addDiagnostic, removeDiagnostic = _b.removeDiagnostic, diagnostics = _b.diagnostics;
ret = factory(program, cleanConfig, {
ts: ts,
addDiagnostic: addDiagnostic,
removeDiagnostic: removeDiagnostic,
diagnostics: diagnostics,
library: tsp.getCurrentLibrary()
});
break;
case "raw":
ret = function (ctx) { return factory(ctx, program, cleanConfig); };
break;
default:
throw new Error("Invalid plugin type found in tsconfig.json: '".concat(config.type, "'"));
}
if (typeof ret === "function")
return after ? ({ after: ret }) :
afterDeclarations ? ({ afterDeclarations: ret }) :
{ before: ret };
return ret;
};
return PluginCreator;
}());
tsp.PluginCreator = PluginCreator;
})(tsp || (tsp = {}));
var tsp;
(function (tsp) {
tsp.diagnosticMap = new WeakMap();
function diagnosticExtrasFactory(program) {
var diagnostics = tsp.diagnosticMap.get(program) || tsp.diagnosticMap.set(program, []).get(program);
var addDiagnostic = function (diag) { return diagnostics.push(diag); };
var removeDiagnostic = function (index) { diagnostics.splice(index, 1); };
return { addDiagnostic: addDiagnostic, removeDiagnostic: removeDiagnostic, diagnostics: diagnostics };
}
tsp.diagnosticExtrasFactory = diagnosticExtrasFactory;
tsp.getCurrentLibrary = function () { return require("path").basename(__filename, require("path").extname(__filename)); };
})(tsp || (tsp = {}));
})(tsp || (tsp = {}));
return tsp;
})();

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc