downlevel-dts
Advanced tools
Comparing version 0.6.0 to 0.7.0
49
index.js
@@ -7,2 +7,3 @@ #!/usr/bin/env node | ||
const assert = require("assert"); | ||
const semver = require("semver"); | ||
@@ -13,6 +14,7 @@ /** @typedef {import("typescript").Node} Node */ | ||
* @param {string} target | ||
* @param {import("semver").SemVer} targetVersion | ||
*/ | ||
function main(src, target) { | ||
function main(src, target, targetVersion) { | ||
if (!src || !target) { | ||
console.log("Usage: node index.js test test/ts3.4"); | ||
console.log("Usage: node index.js test test/ts3.4 [--to=3.4]"); | ||
process.exit(1); | ||
@@ -32,3 +34,3 @@ } | ||
}); | ||
for (const t of ts.transform(files, [doTransform.bind(null, checker)]).transformed) { | ||
for (const t of ts.transform(files, [doTransform.bind(null, checker, targetVersion)]).transformed) { | ||
const f = /** @type {import("typescript").SourceFile} */ (t); | ||
@@ -45,3 +47,9 @@ const targetPath = path.join(target, path.resolve(f.fileName).slice(path.resolve(src).length)); | ||
const target = process.argv[3]; | ||
main(src, target); | ||
const to = process.argv.find(arg => arg.startsWith("--to")); | ||
/** @type {*} */ let targetVersion = semver.minVersion("3.4.0"); | ||
if (to) { | ||
const userInput = semver.coerce(to.split("=")[1]); | ||
if (userInput) targetVersion = userInput; | ||
} | ||
main(src, target, targetVersion); | ||
} | ||
@@ -51,5 +59,6 @@ | ||
* @param {import("typescript").TypeChecker} checker | ||
* @param {import("semver").SemVer} targetVersion | ||
* @param {import("typescript").TransformationContext} k | ||
*/ | ||
function doTransform(checker, k) { | ||
function doTransform(checker, targetVersion, k) { | ||
/** | ||
@@ -60,3 +69,9 @@ * @param {Node} n | ||
const transform = function(n) { | ||
if (ts.isFunctionDeclaration(n) && n.type && ts.isTypePredicateNode(n.type) && n.type.assertsModifier) { | ||
if ( | ||
semver.lt(targetVersion, "3.7.0") && | ||
ts.isFunctionDeclaration(n) && | ||
n.type && | ||
ts.isTypePredicateNode(n.type) && | ||
n.type.assertsModifier | ||
) { | ||
return ts.createFunctionDeclaration( | ||
@@ -74,3 +89,3 @@ n.decorators, | ||
if (ts.isGetAccessor(n)) { | ||
if (semver.lt(targetVersion, "3.6.0") && ts.isGetAccessor(n)) { | ||
// get x(): number => x: number | ||
@@ -94,3 +109,3 @@ let flags = ts.getCombinedModifierFlags(n); | ||
); | ||
} else if (ts.isSetAccessor(n)) { | ||
} else if (semver.lt(targetVersion, "3.6.0") && ts.isSetAccessor(n)) { | ||
// set x(value: number) => x: number | ||
@@ -114,3 +129,8 @@ let flags = ts.getCombinedModifierFlags(n); | ||
} | ||
} else if (ts.isPropertyDeclaration(n) && ts.isPrivateIdentifier(n.name) && n.name.escapedText === "#private") { | ||
} else if ( | ||
semver.lt(targetVersion, "3.8.0") && | ||
ts.isPropertyDeclaration(n) && | ||
ts.isPrivateIdentifier(n.name) && | ||
n.name.escapedText === "#private" | ||
) { | ||
// #private => private "#private" | ||
@@ -128,2 +148,3 @@ const modifiers = ts.createModifiersFromModifierFlags(ts.ModifierFlags.Private); | ||
} else if ( | ||
semver.lt(targetVersion, "3.8.0") && | ||
ts.isExportDeclaration(n) && | ||
@@ -151,10 +172,9 @@ n.exportClause && | ||
undefined, | ||
ts.createNamedExports([ts.createExportSpecifier(tempName, n.exportClause.name)]), | ||
n.moduleSpecifier | ||
ts.createNamedExports([ts.createExportSpecifier(tempName, n.exportClause.name)]) | ||
) | ||
) | ||
]; | ||
} else if (ts.isExportDeclaration(n) && n.isTypeOnly) { | ||
} else if (semver.lt(targetVersion, "3.8.0") && ts.isExportDeclaration(n) && n.isTypeOnly) { | ||
return ts.createExportDeclaration(n.decorators, n.modifiers, n.exportClause, n.moduleSpecifier); | ||
} else if (ts.isImportClause(n) && n.isTypeOnly) { | ||
} else if (semver.lt(targetVersion, "3.8.0") && ts.isImportClause(n) && n.isTypeOnly) { | ||
return ts.createImportClause(n.name, n.namedBindings); | ||
@@ -169,2 +189,3 @@ } else if ( | ||
if ( | ||
semver.lt(targetVersion, "3.5.0") && | ||
symbol && | ||
@@ -183,3 +204,3 @@ symbol.declarations.length && | ||
} | ||
} else if (n.kind === ts.SyntaxKind.NamedTupleMember) { | ||
} else if (semver.lt(targetVersion, "4.0.0") && n.kind === ts.SyntaxKind.NamedTupleMember) { | ||
const member = /** @type {import("typescript").NamedTupleMember} */ (n); | ||
@@ -186,0 +207,0 @@ return ts.addSyntheticLeadingComment( |
const { main } = require("./index"); | ||
const sh = require("shelljs"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
/** | ||
* @param {string} description | ||
* @param {{ [s: string]: () => void }} tests | ||
*/ | ||
function suite(description, tests) { | ||
describe(description, () => { | ||
for (const k in tests) { | ||
test(k, tests[k], 10 * 1000); | ||
} | ||
}); | ||
} | ||
suite("main", { | ||
works() { | ||
if (fs.existsSync("test/ts3.4")) { | ||
sh.rm("-r", "test/ts3.4"); | ||
} | ||
main("test", "test/ts3.4"); | ||
expect(fs.readFileSync("test/ts3.4/test.d.ts", "utf8")).toEqual( | ||
fs.readFileSync("baselines/ts3.4/test.d.ts", "utf8") | ||
const semver = require("semver"); | ||
describe("main", () => { | ||
const tsVersions = ["3.4", "3.5", "3.6", "3.7", "3.8", "3.9", "4.0"]; | ||
if (fs.existsSync(`baselines/local`)) { | ||
sh.rm("-r", `baselines/local`); | ||
} | ||
for (const tsVersion of tsVersions) { | ||
test( | ||
"downlevel TS to " + tsVersion, | ||
() => { | ||
main("test", `baselines/local/ts${tsVersion}`, semver.coerce(tsVersion)); | ||
expect(fs.readFileSync(`baselines/local/ts${tsVersion}/test.d.ts`, "utf8")).toEqual( | ||
fs.readFileSync(`baselines/reference/ts${tsVersion}/test.d.ts`, "utf8") | ||
); | ||
expect(fs.readFileSync(`baselines/local/ts${tsVersion}/src/test.d.ts`, "utf8")).toEqual( | ||
fs.readFileSync(`baselines/reference/ts${tsVersion}/src/test.d.ts`, "utf8") | ||
); | ||
}, | ||
10 * 1000 | ||
); | ||
expect(fs.readFileSync("test/ts3.4/src/test.d.ts", "utf8")).toEqual( | ||
fs.readFileSync("baselines/ts3.4/src/test.d.ts", "utf8") | ||
); | ||
} | ||
}); |
{ | ||
"name": "downlevel-dts", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "Convert d.ts to be compatible with older typescript compilers", | ||
@@ -10,3 +10,3 @@ "homepage": "https://github.com/sandersn/downlevel-dts", | ||
"test": "jest", | ||
"baseline-accept": "cp -r test/ts3.4 baselines" | ||
"baseline-accept": "cp -r baselines/local/ts* baselines/reference" | ||
}, | ||
@@ -17,3 +17,4 @@ "author": "Nathan Shively-Sanders", | ||
"shelljs": "^0.8.3", | ||
"typescript": "^4.1.0-dev.20200804" | ||
"semver": "^7.3.2", | ||
"typescript": "^4.1.0-dev.20201026" | ||
}, | ||
@@ -23,2 +24,3 @@ "devDependencies": { | ||
"@types/shelljs": "^0.8.6", | ||
"@types/semver": "^7.3.4", | ||
"husky": "^4.0.0", | ||
@@ -25,0 +27,0 @@ "jest": "^24.9.0", |
@@ -241,4 +241,4 @@ downlevel-dts rewrites .d.ts files created by any version of Typescript so | ||
Since the earliest downlevel feature is from Typescript 3.5, | ||
downlevel-dts targets Typescript 3.4. In the future the downlevel | ||
target may be configurable as Typescript 3.4 becomes less used. | ||
downlevel-dts targets Typescript 3.4 by default. The downlevel target is | ||
configurable with `--to` argument. | ||
@@ -253,3 +253,3 @@ Currently, Typescript 3.0 features like `unknown` are not | ||
1. `$ npm install downlevel-dts` | ||
2. `$ npx downlevel-dts . ts3.4` | ||
2. `$ npx downlevel-dts . ts3.4 [--to=3.4]` | ||
3. To your package.json, add | ||
@@ -256,0 +256,0 @@ |
@@ -0,3 +1,4 @@ | ||
/// <reference path="./src/test.d.ts" /> | ||
/// <reference types="node" /> | ||
/// <reference path="./src/test.d.ts" /> | ||
export class C { | ||
@@ -53,1 +54,3 @@ protected get p(): number; | ||
type J = [foo: string, bar: number, ...arr:boolean[]] | ||
export * as default from "./src/test"; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33614
29
911
3
7
1
+ Addedsemver@^7.3.2
+ Addedsemver@7.6.3(transitive)