downlevel-dts
Advanced tools
Comparing version 0.1.0 to 0.2.0
181
index.js
@@ -1,33 +0,154 @@ | ||
const { Project, ts } = require("ts-morph"); | ||
const { cwd } = require('process') | ||
const path = require('path') | ||
const src = process.argv[2] | ||
const target = process.argv[3] | ||
if (!src || !target) { | ||
console.log("Usage: node ../index.js . ts3.4") | ||
process.exit(1) | ||
} | ||
const project = new Project({ | ||
tsConfigFilePath: path.join(src, "tsconfig.json") | ||
}) | ||
for (const f of project.getSourceFiles("**/*.d.ts")) { | ||
const gs = f.getDescendantsOfKind(ts.SyntaxKind.GetAccessor) | ||
for (const g of gs) { | ||
const s = g.getParent().getChildrenOfKind(ts.SyntaxKind.SetAccessor).find(s => s.getName() === g.getName()) | ||
g.replaceWithText(`${s ? "" : "readonly "}${g.getName()}: ${g.getType().getText()}`) | ||
if (s) { | ||
s.remove() | ||
} | ||
#!/usr/bin/env node | ||
const sh = require("shelljs"); | ||
const fs = require("fs"); | ||
const ts = require("typescript"); | ||
const path = require("path"); | ||
const assert = require("assert"); | ||
/** @typedef {import("typescript").Node} Node */ | ||
/** @param {import("typescript").TransformationContext} k */ | ||
function doTransform(k) { | ||
/** | ||
* @param {Node} n | ||
* @return {import("typescript").VisitResult<Node>} | ||
*/ | ||
const transform = function(n) { | ||
if (ts.isGetAccessor(n)) { | ||
// get x(): number => x: number | ||
let flags = ts.getCombinedModifierFlags(n); | ||
if (!getMatchingAccessor(n, "get")) { | ||
flags |= ts.ModifierFlags.Readonly; | ||
} | ||
const modifiers = ts.createModifiersFromModifierFlags(flags); | ||
return ts.createProperty( | ||
n.decorators, | ||
modifiers, | ||
n.name, | ||
/*?! token*/ undefined, | ||
defaultAny(n.type), | ||
/*initialiser*/ undefined | ||
); | ||
} else if (ts.isSetAccessor(n)) { | ||
// set x(value: number) => x: number | ||
let flags = ts.getCombinedModifierFlags(n); | ||
if (getMatchingAccessor(n, "set")) { | ||
return undefined; | ||
} else { | ||
assert(n.parameters && n.parameters.length); | ||
return ts.createProperty( | ||
n.decorators, | ||
n.modifiers, | ||
n.name, | ||
/*?! token*/ undefined, | ||
defaultAny(n.parameters[0].type), | ||
/*initialiser*/ undefined | ||
); | ||
} | ||
} else if ( | ||
ts.isExportDeclaration(n) && | ||
n.exportClause && | ||
n.moduleSpecifier && | ||
ts.isNamespaceExport(n.exportClause) | ||
) { | ||
// export * as ns from 'x' | ||
// => | ||
// import * as ns_1 from 'x' | ||
// export { ns_1 as ns } | ||
const tempName = ts.createUniqueName(n.exportClause.name.getText()); | ||
return [ | ||
ts.createImportDeclaration( | ||
n.decorators, | ||
n.modifiers, | ||
ts.createImportClause( | ||
/*name*/ undefined, | ||
ts.createNamespaceImport(tempName) | ||
), | ||
n.moduleSpecifier | ||
), | ||
ts.createExportDeclaration( | ||
undefined, | ||
undefined, | ||
ts.createNamedExports([ | ||
ts.createExportSpecifier(tempName, n.exportClause.name) | ||
]), | ||
n.moduleSpecifier | ||
) | ||
]; | ||
} | ||
const ss = f.getDescendantsOfKind(ts.SyntaxKind.SetAccessor) | ||
for (const s of ss) { | ||
const g = s.getParent().getChildrenOfKind(ts.SyntaxKind.GetAccessor).find(g => s.getName() === g.getName()) | ||
if (!g) { | ||
s.replaceWithText(`${s.getName()}: ${s.getType().getText()}`) | ||
} | ||
} | ||
f.copy(path.join(cwd(), target, path.relative(cwd(), f.getFilePath())), { overwrite: true }) | ||
f.refreshFromFileSystemSync() | ||
return ts.visitEachChild(n, transform, k); | ||
}; | ||
return transform; | ||
} | ||
project.save() | ||
/** @param {import("typescript").TypeNode | undefined} t */ | ||
function defaultAny(t) { | ||
return t || ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword); | ||
} | ||
/** | ||
* @param {import("typescript").AccessorDeclaration} n | ||
* @param {'get' | 'set'} getset | ||
*/ | ||
function getMatchingAccessor(n, getset) { | ||
if (!ts.isClassDeclaration(n.parent)) | ||
throw new Error( | ||
"Bad AST -- accessor parent should be a class declaration." | ||
); | ||
const isOther = getset === "get" ? ts.isSetAccessor : ts.isGetAccessor; | ||
return n.parent.members.some( | ||
m => isOther(m) && m.name.getText() === n.name.getText() | ||
); | ||
} | ||
/** | ||
* @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)); | ||
} | ||
} | ||
module.exports.main = main; | ||
if (!(/** @type {*} */ (module.parent))) { | ||
const src = process.argv[2]; | ||
const target = process.argv[3]; | ||
main(src, target); | ||
} | ||
/** | ||
* @template T,U | ||
* @param {readonly T[]} l | ||
* @param {(t: T) => U | false | undefined} f | ||
* @return {U[]} | ||
*/ | ||
function mapDefined(l, f) { | ||
const acc = []; | ||
for (const x of l) { | ||
const y = f(x); | ||
if (y) acc.push(y); | ||
} | ||
return acc; | ||
} |
{ | ||
"name": "downlevel-dts", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Convert d.ts to be compatible with older typescript compilers", | ||
"main": "index.js", | ||
"bin": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "jest", | ||
"baseline-accept": "cp -r test/ts3.4 baselines" | ||
}, | ||
@@ -12,4 +14,18 @@ "author": "Nathan Shively-Sanders", | ||
"dependencies": { | ||
"ts-morph": "^4.2.0" | ||
"shelljs": "^0.8.3", | ||
"typescript": "^3.8.0-dev.20200111" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^13.1.6", | ||
"@types/shelljs": "^0.8.6", | ||
"husky": "^4.0.0", | ||
"jest": "^24.9.0", | ||
"prettier": "^1.19.1", | ||
"pretty-quick": "^2.0.1" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "pretty-quick --staged" | ||
} | ||
} | ||
} |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
9458
15
265
2
2
6
3
1
+ Addedshelljs@^0.8.3
+ Addedfunction-bind@1.1.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedinterpret@1.4.0(transitive)
+ Addedis-core-module@2.15.1(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedrechoir@0.6.2(transitive)
+ Addedresolve@1.22.8(transitive)
+ Addedshelljs@0.8.5(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
+ Addedtypescript@3.9.10(transitive)
- Removedts-morph@^4.2.0
- Removed@dsherret/to-absolute-glob@2.0.2(transitive)
- Removed@nodelib/fs.scandir@2.1.5(transitive)
- Removed@nodelib/fs.stat@2.0.5(transitive)
- Removed@nodelib/fs.walk@1.2.8(transitive)
- Removed@types/glob@7.2.0(transitive)
- Removed@types/minimatch@3.0.55.1.2(transitive)
- Removed@types/node@22.9.0(transitive)
- Removedansi-styles@3.2.1(transitive)
- Removedarray-differ@3.0.0(transitive)
- Removedarray-union@2.1.0(transitive)
- Removedarrify@2.0.1(transitive)
- Removedbraces@3.0.3(transitive)
- Removedchalk@2.4.2(transitive)
- Removedcode-block-writer@10.1.1(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removeddir-glob@3.0.1(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedfast-glob@3.3.2(transitive)
- Removedfastq@1.17.1(transitive)
- Removedfill-range@7.1.1(transitive)
- Removedfs-extra@8.1.0(transitive)
- Removedglob-parent@5.1.2(transitive)
- Removedglobby@10.0.2(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removedignore@5.3.2(transitive)
- Removedis-absolute@1.0.0(transitive)
- Removedis-extglob@2.1.1(transitive)
- Removedis-glob@4.0.3(transitive)
- Removedis-negated-glob@1.0.0(transitive)
- Removedis-number@7.0.0(transitive)
- Removedis-relative@1.0.0(transitive)
- Removedis-unc-path@1.0.0(transitive)
- Removedis-windows@1.0.2(transitive)
- Removedjsonfile@4.0.0(transitive)
- Removedmerge2@1.4.1(transitive)
- Removedmicromatch@4.0.8(transitive)
- Removedmultimatch@4.0.0(transitive)
- Removedpath-type@4.0.0(transitive)
- Removedpicomatch@2.3.1(transitive)
- Removedqueue-microtask@1.2.3(transitive)
- Removedreusify@1.0.4(transitive)
- Removedrun-parallel@1.2.0(transitive)
- Removedslash@3.0.0(transitive)
- Removedsupports-color@5.5.0(transitive)
- Removedto-regex-range@5.0.1(transitive)
- Removedts-morph@4.3.3(transitive)
- Removedtypescript@3.6.4(transitive)
- Removedunc-path-regex@0.1.2(transitive)
- Removedundici-types@6.19.8(transitive)
- Removeduniversalify@0.1.2(transitive)