Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downlevel readonly array & readonly tuple #28

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ appropriate version of Typescript.

Here is the list of features that are downlevelled:

### ReadonlyArray and readonly tuples (3.4)

```ts
export declare let readonlyArr: readonly string[];
export declare let readonlyTuple: readonly [string, number];
```

becomes

```ts
export declare let readonlyArr: ReadonlyArray<string>;
export declare let readonlyTuple: Readonly<[string, number]>;
```

### `Omit` (3.5)

```ts
Expand Down
24 changes: 24 additions & 0 deletions baselines/reference/ts3.3/src/test.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export class C {
p: number;
readonly q: string;
r: boolean;
}
export namespace N {
class D {
p: number;
readonly q: string;
r: boolean;
}
}
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'>;
export declare let readonlyArr: ReadonlyArray<string>;
export declare let readonlyTuple: Readonly<[
string,
number
]>;
52 changes: 52 additions & 0 deletions baselines/reference/ts3.3/test.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/// <reference path="./src/test.d.ts" />
/// <reference types="node" />
export class C {
protected p: number;
public readonly q: string;
private r: boolean;
}
// hi, this should still be there
export namespace N {
abstract class D {
/*
* @readonly
* @memberof BlobLeaseClient
* @type {number}

preserve this too */
p: number;
readonly q: any;
abstract r: boolean;
}
}
/** 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 };
export interface E {
a: number;
b: number;
}
/// is this a single-line comment?
export type F = Pick<E, Exclude<keyof E, 'a'>>;
export class G {
private "G.#private";
}
export class H extends G {
private "H.#private";
}
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[]
];
import * as default_1 from "./src/test";
export { default_1 as default };
5 changes: 5 additions & 0 deletions baselines/reference/ts3.4/src/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ export interface E {
b: number;
}
export type F = Omit<E, 'a'>;
export declare let readonlyArr: readonly string[];
export declare let readonlyTuple: readonly [
string,
number
];
5 changes: 5 additions & 0 deletions baselines/reference/ts3.5/src/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ export interface E {
b: number;
}
export type F = Omit<E, 'a'>;
export declare let readonlyArr: readonly string[];
export declare let readonlyTuple: readonly [
string,
number
];
5 changes: 5 additions & 0 deletions baselines/reference/ts3.6/src/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ export interface E {
b: number;
}
export type F = Omit<E, 'a'>;
export declare let readonlyArr: readonly string[];
export declare let readonlyTuple: readonly [
string,
number
];
5 changes: 5 additions & 0 deletions baselines/reference/ts3.7/src/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ export interface E {
b: number;
}
export type F = Omit<E, 'a'>;
export declare let readonlyArr: readonly string[];
export declare let readonlyTuple: readonly [
string,
number
];
5 changes: 5 additions & 0 deletions baselines/reference/ts3.8/src/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ export interface E {
b: number;
}
export type F = Omit<E, 'a'>;
export declare let readonlyArr: readonly string[];
export declare let readonlyTuple: readonly [
string,
number
];
5 changes: 5 additions & 0 deletions baselines/reference/ts3.9/src/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ export interface E {
b: number;
}
export type F = Omit<E, 'a'>;
export declare let readonlyArr: readonly string[];
export declare let readonlyTuple: readonly [
string,
number
];
5 changes: 5 additions & 0 deletions baselines/reference/ts4.0/src/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ export interface E {
b: number;
}
export type F = Omit<E, 'a'>;
export declare let readonlyArr: readonly string[];
export declare let readonlyTuple: readonly [
string,
number
];
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,22 @@ function doTransform(checker, targetVersion, k) {
ts.unescapeLeadingUnderscores(member.name.escapedText),
/*hasTrailingNewline*/ false
);
} else if (
semver.lt(targetVersion, "3.4.0") &&
ts.isTypeOperatorNode(n) &&
n.operator === ts.SyntaxKind.ReadonlyKeyword
) {
if (ts.isArrayTypeNode(n.type)) {
// let arr: readonly number[];
// =>
// let arr: ReadonlyArray<number>;
return ts.createTypeReferenceNode("ReadonlyArray", [transform(n.type.elementType)]);
} else if (ts.isTupleTypeNode(n.type)) {
// let tup: readonly [string, number];
// =>
// let tup: Readonly<[string, number]>;
return ts.createTypeReferenceNode("Readonly", [transform(n.type)]);
}
}
return ts.visitEachChild(n, transform, k);
};
Expand Down
2 changes: 1 addition & 1 deletion index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fs = require("fs");
const semver = require("semver");

describe("main", () => {
const tsVersions = ["3.4", "3.5", "3.6", "3.7", "3.8", "3.9", "4.0"];
const tsVersions = ["3.3", "3.4", "3.5", "3.6", "3.7", "3.8", "3.9", "4.0"];

if (fs.existsSync(`baselines/local`)) {
sh.rm("-r", `baselines/local`);
Expand Down
5 changes: 4 additions & 1 deletion test/src/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ export interface E {
b: number;
}

export type F = Omit<E, 'a'>;
export type F = Omit<E, 'a'>;

export declare let readonlyArr: readonly string[];
export declare let readonlyTuple: readonly [string, number];