downlevel-dts
Advanced tools
Comparing version 0.5.0 to 0.6.0
@@ -11,2 +11,8 @@ /// <reference path="./src/test.d.ts" /> | ||
abstract class D { | ||
/* | ||
* @readonly | ||
* @memberof BlobLeaseClient | ||
* @type {number} | ||
preserve this too */ | ||
p: number; | ||
@@ -17,4 +23,6 @@ readonly q: any; | ||
} | ||
/** is this a single-line comment? */ | ||
import { C as CD } from "./src/test"; | ||
import * as rex_1 from "./src/test"; | ||
//another comment | ||
export { rex_1 as rex } from "./src/test"; | ||
@@ -25,2 +33,3 @@ export interface E { | ||
} | ||
/// is this a single-line comment? | ||
export type F = Pick<E, Exclude<keyof E, 'a'>>; | ||
@@ -33,4 +42,13 @@ export class G { | ||
} | ||
export interface I extends Pick<E, Exclude<keyof E, 'a'>> { | ||
version: number; | ||
} | ||
declare function guardIsString(val: any): val is string; | ||
/** side-effects! */ | ||
declare function assertIsString(val: any, msg?: string): void; | ||
declare function assert(val: any, msg?: string): void; | ||
type J = [ | ||
/*foo*/ string, | ||
/*bar*/ number, | ||
/*arr*/ ...boolean[] | ||
]; |
109
index.js
@@ -44,2 +44,3 @@ #!/usr/bin/env node | ||
} | ||
/** | ||
@@ -71,13 +72,17 @@ * @param {import("typescript").TypeChecker} checker | ||
let flags = ts.getCombinedModifierFlags(n); | ||
if (!getMatchingAccessor(n, "get")) { | ||
const other = getMatchingAccessor(n, "get"); | ||
if (!other) { | ||
flags |= ts.ModifierFlags.Readonly; | ||
} | ||
const modifiers = ts.createModifiersFromModifierFlags(flags); | ||
return ts.createProperty( | ||
n.decorators, | ||
modifiers, | ||
n.name, | ||
/*?! token*/ undefined, | ||
defaultAny(n.type), | ||
/*initialiser*/ undefined | ||
return copyComment( | ||
other ? [n, other] : [n], | ||
ts.createProperty( | ||
n.decorators, | ||
modifiers, | ||
n.name, | ||
/*?! token*/ undefined, | ||
defaultAny(n.type), | ||
/*initialiser*/ undefined | ||
) | ||
); | ||
@@ -91,9 +96,12 @@ } else if (ts.isSetAccessor(n)) { | ||
assert(n.parameters && n.parameters.length); | ||
return ts.createProperty( | ||
n.decorators, | ||
n.modifiers, | ||
n.name, | ||
/*?! token*/ undefined, | ||
defaultAny(n.parameters[0].type), | ||
/*initialiser*/ undefined | ||
return copyComment( | ||
[n], | ||
ts.createProperty( | ||
n.decorators, | ||
n.modifiers, | ||
n.name, | ||
/*?! token*/ undefined, | ||
defaultAny(n.parameters[0].type), | ||
/*initialiser*/ undefined | ||
) | ||
); | ||
@@ -131,7 +139,10 @@ } | ||
), | ||
ts.createExportDeclaration( | ||
undefined, | ||
undefined, | ||
ts.createNamedExports([ts.createExportSpecifier(tempName, n.exportClause.name)]), | ||
n.moduleSpecifier | ||
copyComment( | ||
[n], | ||
ts.createExportDeclaration( | ||
undefined, | ||
undefined, | ||
ts.createNamedExports([ts.createExportSpecifier(tempName, n.exportClause.name)]), | ||
n.moduleSpecifier | ||
) | ||
) | ||
@@ -143,4 +154,7 @@ ]; | ||
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); | ||
} else if ( | ||
(ts.isTypeReferenceNode(n) && ts.isIdentifier(n.typeName) && n.typeName.escapedText === "Omit") || | ||
(ts.isExpressionWithTypeArguments(n) && ts.isIdentifier(n.expression) && n.expression.escapedText === "Omit") | ||
) { | ||
const symbol = checker.getSymbolAtLocation(ts.isTypeReferenceNode(n) ? n.typeName : n.expression); | ||
const typeArguments = n.typeArguments; | ||
@@ -162,2 +176,10 @@ | ||
} | ||
} else if (n.kind === ts.SyntaxKind.NamedTupleMember) { | ||
const member = /** @type {import("typescript").NamedTupleMember} */ (n); | ||
return ts.addSyntheticLeadingComment( | ||
member.dotDotDotToken ? ts.createRestTypeNode(member.type) : member.type, | ||
ts.SyntaxKind.MultiLineCommentTrivia, | ||
ts.unescapeLeadingUnderscores(member.name.escapedText), | ||
/*hasTrailingNewline*/ false | ||
); | ||
} | ||
@@ -168,3 +190,2 @@ return ts.visitEachChild(n, transform, k); | ||
} | ||
/** @param {import("typescript").TypeNode | undefined} t */ | ||
@@ -182,5 +203,29 @@ function defaultAny(t) { | ||
const isOther = getset === "get" ? ts.isSetAccessor : ts.isGetAccessor; | ||
return n.parent.members.some(m => isOther(m) && m.name.getText() === n.name.getText()); | ||
return n.parent.members.find(m => isOther(m) && m.name.getText() === n.name.getText()); | ||
} | ||
/** | ||
* @param {Node[]} originals | ||
* @param {Node} rewrite | ||
*/ | ||
function copyComment(originals, rewrite) { | ||
const file = originals[0].getSourceFile().getFullText(); | ||
const ranges = flatMap(originals, o => { | ||
const comments = ts.getLeadingCommentRanges(file, o.getFullStart()); | ||
return comments ? comments : []; | ||
}); | ||
if (!ranges.length) return rewrite; | ||
let kind = ts.SyntaxKind.SingleLineCommentTrivia; | ||
let hasTrailingNewline = false; | ||
const commentText = flatMap(ranges, r => { | ||
if (r.kind === ts.SyntaxKind.MultiLineCommentTrivia) kind = ts.SyntaxKind.MultiLineCommentTrivia; | ||
hasTrailingNewline = hasTrailingNewline || !!r.hasTrailingNewLine; | ||
const comment = file.slice(r.pos, r.end); | ||
const text = comment.startsWith("//") ? comment.slice(2) : comment.slice(3, comment.length - 2); | ||
return text.split("\n").map(line => line.trimStart()); | ||
}).join("\n"); | ||
return ts.addSyntheticLeadingComment(rewrite, kind, commentText, hasTrailingNewline); | ||
} | ||
/** @param {string} s */ | ||
@@ -207,1 +252,17 @@ function dedupeTripleSlash(s) { | ||
} | ||
/** | ||
* @template T,U | ||
* @param {readonly T[]} l | ||
* @param {(t: T) => U[]} f | ||
* @return {U[]} | ||
*/ | ||
function flatMap(l, f) { | ||
if (l.flatMap) return l.flatMap(f); | ||
const acc = []; | ||
for (const x of l) { | ||
const ys = f(x); | ||
acc.push(...ys); | ||
} | ||
return acc; | ||
} |
{ | ||
"name": "downlevel-dts", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Convert d.ts to be compatible with older typescript compilers", | ||
@@ -16,3 +16,3 @@ "homepage": "https://github.com/sandersn/downlevel-dts", | ||
"shelljs": "^0.8.3", | ||
"typescript": "^3.8.0-dev.20200111" | ||
"typescript": "^4.1.0-dev.20200804" | ||
}, | ||
@@ -19,0 +19,0 @@ "devDependencies": { |
@@ -21,2 +21,9 @@ downlevel-dts rewrites .d.ts files created by any version of Typescript so | ||
Note that not all features can be downlevelled. For example, | ||
Typescript 4.0 allows spreading multiple tuple type variables, at any | ||
position in a tuple. This is not allowed in previous versions, but has | ||
no obvious downlevel emit, so downlevel-dts doesn't attempt to do | ||
anything. Be sure to test the output of downlevel-dts with the | ||
appropriate version of Typescript. | ||
## Features | ||
@@ -213,2 +220,21 @@ | ||
### `[named: number, tuple: string, ...members: boolean[]]` (4.0) | ||
Typescript 4.0 supports naming tuple members: | ||
```ts | ||
type T = [foo: number, bar: string]; | ||
``` | ||
becomes | ||
```ts | ||
type T = [/** foo */ number, /** bar */ string]; | ||
``` | ||
#### Semantics | ||
The downlevel semantics are exactly the same as the original, but | ||
the Typescript language service won't be able to show the member names. | ||
## Target | ||
@@ -233,8 +259,8 @@ | ||
"typesVersions": { | ||
"<3.8": { "*": ["ts3.4/*"] } | ||
"<3.9": { "*": ["ts3.4/*"] } | ||
} | ||
``` | ||
4. `$ cp tsconfig.json ts3.4/tsconfig.json` | ||
4. `$ cp tsconfig.json ts3.9/tsconfig.json` | ||
These instructions are modified and simplified from the Definitely Typed. | ||
These instructions are modified and simplified from the Definitely Typed ones. |
@@ -12,3 +12,9 @@ /// <reference types="node" /> | ||
abstract class D { | ||
/** | ||
* @readonly | ||
* @memberof BlobLeaseClient | ||
* @type {number} | ||
*/ | ||
get p(): number; | ||
/** preserve this too */ | ||
set p(value: number); | ||
@@ -19,4 +25,6 @@ get q(); | ||
} | ||
/** is this a single-line comment? */ | ||
import type { C as CD } from "./src/test"; | ||
// another comment | ||
export * as rex from "./src/test"; | ||
@@ -29,3 +37,4 @@ | ||
export type F = Omit<E, 'a'>; | ||
/// is this a single-line comment? | ||
export type F = Omit<E, 'a'> | ||
@@ -38,4 +47,10 @@ export class G { | ||
} | ||
export interface I extends Omit<E, 'a'> { | ||
version: number; | ||
} | ||
declare function guardIsString(val: any): val is string; | ||
/** side-effects! */ | ||
declare function assertIsString(val: any, msg?: string): asserts val is string; | ||
declare function assert(val: any, msg?: string): asserts val; | ||
type J = [foo: string, bar: number, ...arr:boolean[]] |
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
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
21328
440
264
+ Addedtypescript@4.9.5(transitive)
- Removedtypescript@3.9.10(transitive)