Socket
Socket
Sign inDemoInstall

downlevel-dts

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

downlevel-dts - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

7

baselines/ts3.4/src/test.d.ts

@@ -13,1 +13,8 @@ export class C {

}
export { C as DetectiveComics };
export type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
export interface E {
a: number;
b: number;
}
export type F = Omit<E, 'a'>;

13

baselines/ts3.4/test.d.ts

@@ -0,1 +1,3 @@

/// <reference path="./src/test.d.ts" />
/// <reference types="node" />
export class C {

@@ -6,2 +8,3 @@ protected p: number;

}
// hi, this should still be there
export namespace N {

@@ -14,3 +17,9 @@ abstract class D {

}
import * as rex_1 from "src/test";
export { rex_1 as rex } from "src/test";
import { C as CD } from "./src/test";
import * as rex_1 from "./src/test";
export { rex_1 as rex } from "./src/test";
export interface E {
a: number;
b: number;
}
export type F = Pick<E, Exclude<keyof E, 'a'>>;

121

index.js

@@ -9,5 +9,49 @@ #!/usr/bin/env node

/** @typedef {import("typescript").Node} Node */
/**
* @param {string} src
* @param {string} target
*/
function main(src, target) {
if (!src || !target) {
console.log("Usage: node index.js test test/ts3.4");
process.exit(1);
}
/** @param {import("typescript").TransformationContext} k */
function doTransform(k) {
// TODO: target path is probably wrong for absolute src (or target?)
// TODO: Probably will want to alter package.json if discovered in the right place.
const program = ts.createProgram(
sh
.find(path.join(src))
.filter(f => f.endsWith(".d.ts") && !/node_modules/.test(f)),
{}
);
const checker = program.getTypeChecker(); // just used for setting parent pointers right now
const files = mapDefined(program.getRootFileNames(), program.getSourceFile);
const printer = ts.createPrinter({
newLine: ts.NewLineKind.CarriageReturnLineFeed
});
for (const t of ts.transform(files, [doTransform.bind(null, checker)])
.transformed) {
const f = /** @type {import("typescript").SourceFile} */ (t);
const targetPath = path.join(
target,
path.resolve(f.fileName).slice(path.resolve(src).length)
);
sh.mkdir("-p", path.dirname(targetPath));
fs.writeFileSync(targetPath, dedupeTripleSlash(printer.printFile(f)));
}
}
module.exports.main = main;
if (!(/** @type {*} */ (module.parent))) {
const src = process.argv[2];
const target = process.argv[3];
main(src, target);
}
/**
* @param {import("typescript").TypeChecker} checker
* @param {import("typescript").TransformationContext} k
*/
function doTransform(checker, k) {
/**

@@ -79,2 +123,35 @@ * @param {Node} n

];
} else if (ts.isExportDeclaration(n) && n.isTypeOnly) {
return ts.createExportDeclaration(
n.decorators,
n.modifiers,
n.exportClause,
n.moduleSpecifier
);
} else if (ts.isImportClause(n) && n.isTypeOnly) {
return ts.createImportClause(n.name, n.namedBindings);
} else if (
ts.isTypeReferenceNode(n) &&
ts.isIdentifier(n.typeName) &&
n.typeName.escapedText === "Omit"
) {
const symbol = checker.getSymbolAtLocation(n.typeName);
const typeArguments = n.typeArguments;
if (
symbol &&
symbol.declarations.length &&
symbol.declarations[0]
.getSourceFile()
.fileName.includes("node_modules/typescript/lib/lib") &&
typeArguments
) {
return ts.createTypeReferenceNode(ts.createIdentifier("Pick"), [
typeArguments[0],
ts.createTypeReferenceNode(ts.createIdentifier("Exclude"), [
ts.createTypeOperatorNode(typeArguments[0]),
typeArguments[1]
])
]);
}
}

@@ -105,40 +182,10 @@ return ts.visitEachChild(n, transform, k);

}
/**
* @param {string} src
* @param {string} target
*/
function main(src, target) {
if (!src || !target) {
console.log("Usage: node index.js test test/ts3.4");
process.exit(1);
}
// TODO: target path is probably wrong for absolute src (or target?)
// TODO: Probably will want to alter package.json if discovered in the right place.
const program = ts.createProgram(
sh
.find(path.join(src))
.filter(f => f.endsWith(".d.ts") && !/node_modules/.test(f)),
{}
);
const checker = program.getTypeChecker(); // just used for setting parent pointers right now
const files = mapDefined(program.getRootFileNames(), program.getSourceFile);
const printer = ts.createPrinter({
newLine: ts.NewLineKind.CarriageReturnLineFeed
});
for (const t of ts.transform(files, [doTransform]).transformed) {
const f = /** @type {import("typescript").SourceFile} */ (t);
const targetPath = path.join(target, f.fileName.slice(src.length));
sh.mkdir("-p", path.dirname(targetPath));
fs.writeFileSync(targetPath, printer.printFile(f));
}
/** @param {string} s */
function dedupeTripleSlash(s) {
const lines = s.split("\n");
const i = lines.findIndex(line => !line.startsWith("/// <reference "));
return [...new Set(lines.slice(0, i)), ...lines.slice(i)].join("\n");
}
module.exports.main = main;
if (!(/** @type {*} */ (module.parent))) {
const src = process.argv[2];
const target = process.argv[3];
main(src, target);
}
/**

@@ -145,0 +192,0 @@ * @template T,U

{
"name": "downlevel-dts",
"version": "0.2.0",
"version": "0.3.0",
"description": "Convert d.ts to be compatible with older typescript compilers",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -15,1 +15,12 @@ export class C {

}
export type { C as DetectiveComics };
export type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
export interface E {
a: number;
b: number;
}
export type F = Omit<E, 'a'>;

@@ -0,1 +1,3 @@

/// <reference types="node" />
/// <reference path="./src/test.d.ts" />
export class C {

@@ -7,2 +9,3 @@ protected get p(): number;

}
// hi, this should still be there
export namespace N {

@@ -16,3 +19,11 @@ abstract class D {

}
import type { C as CD } from "./src/test";
export * as rex from "src/test";
export * as rex from "./src/test";
export interface E {
a: number;
b: number;
}
export type F = Omit<E, 'a'>;
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