Skip to content

Commit f5501a8

Browse files
committed
tmp (tests for es5)
1 parent f92d91f commit f5501a8

File tree

11 files changed

+1988
-9
lines changed

11 files changed

+1988
-9
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"editor.codeActionsOnSave": {
3+
"source.organizeImports": true
4+
}
5+
}

build/index.ts

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { mkdir, readdir, readFile, writeFile } from "fs/promises";
1+
import { mkdir, readdir, writeFile } from "fs/promises";
22
import path from "path";
3+
import ts from "typescript";
4+
import { betterLibs, replacement } from "./replacement";
35

46
const projectDir = process.env.PROJECT || process.cwd();
57
const distDir = path.join(projectDir, "dist", "lib");
@@ -12,15 +14,60 @@ async function main() {
1214

1315
// copy TypeScript lib files
1416
const libs = await readdir(path.join(tsDir, "lib"));
15-
for (const libFile of libs) {
17+
const libFiles: string[] = libs.filter((libFile) =>
18+
/(?:^|\/|\\)lib\..+\.d\.ts$/.test(libFile)
19+
);
20+
21+
// modify each lib file
22+
for (const libFile of libFiles) {
23+
const tsLibFile = path.join(tsDir, "lib", libFile);
24+
const program = ts.createProgram([tsLibFile], {});
25+
const file = program.getSourceFile(tsLibFile);
26+
if (!file) {
27+
continue;
28+
}
29+
let result = betterLibs.has(libFile)
30+
? `/// <reference path="../../lib/${libFile}" />\n`
31+
: "";
32+
for (const statement of file.statements) {
33+
const res = checkStatement(statement);
34+
if (res) {
35+
result += res.getFullText(file);
36+
}
37+
}
38+
result += file.text.slice(file.endOfFileToken.pos);
39+
40+
await writeFile(path.join(distDir, libFile), result);
1641
console.log(libFile);
17-
if (/(?:^|\/|\\)lib\..+\.d\.ts$/.test(libFile)) {
18-
await writeFile(
19-
path.join(distDir, libFile),
20-
await readFile(path.join(path.join(tsDir, "lib", libFile)))
21-
);
42+
}
43+
}
44+
45+
function checkStatement(statement: ts.Statement): ts.Statement | undefined {
46+
// check for declrations
47+
if (ts.isVariableStatement(statement)) {
48+
for (const dec of statement.declarationList.declarations) {
49+
if (ts.isIdentifier(dec.name)) {
50+
if (replacement.has(dec.name.text)) {
51+
return undefined;
52+
}
53+
}
54+
}
55+
} else if (
56+
ts.isFunctionDeclaration(statement) ||
57+
ts.isInterfaceDeclaration(statement) ||
58+
ts.isTypeAliasDeclaration(statement)
59+
) {
60+
const repl = statement.name && replacement.has(statement.name.text);
61+
if (repl) {
62+
return undefined;
63+
}
64+
} else if (ts.isInterfaceDeclaration(statement)) {
65+
const repl = statement.name && replacement.has(statement.name.text);
66+
if (repl) {
67+
return undefined;
2268
}
2369
}
70+
return statement;
2471
}
2572

2673
main().catch((err) => {

build/replacement.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const replacement = new Set<string>([
2+
// lib.es5.d.ts
3+
"eval",
4+
"ObjectConstructor",
5+
]);
6+
7+
export const betterLibs = new Set<string>(["lib.es5.d.ts"]);

index.d.ts

Whitespace-only changes.

lib/lib.es5.d.ts

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/// <reference no-default-lib="true" />
2+
3+
/**
4+
* Evaluates JavaScript code and executes it.
5+
* @param x A String value that contains valid JavaScript code.
6+
*/
7+
declare function eval(x: string): unknown;
8+
9+
interface ObjectConstructor {
10+
new (value?: any): Object;
11+
(): {};
12+
<T>(value: T): T extends object ? T : {};
13+
14+
/** A reference to the prototype for a class of objects. */
15+
readonly prototype: Object;
16+
17+
/**
18+
* Returns the prototype of an object.
19+
* @param o The object that references the prototype.
20+
*/
21+
getPrototypeOf(o: any): unknown;
22+
23+
/**
24+
* Gets the own property descriptor of the specified object.
25+
* An own property descriptor is one that is defined directly on the object and is not inherited from the object's prototype.
26+
* @param o Object that contains the property.
27+
* @param p Name of the property.
28+
*/
29+
getOwnPropertyDescriptor(
30+
o: any,
31+
p: PropertyKey
32+
): PropertyDescriptor | undefined;
33+
34+
/**
35+
* Returns the names of the own properties of an object. The own properties of an object are those that are defined directly
36+
* on that object, and are not inherited from the object's prototype. The properties of an object include both fields (objects) and functions.
37+
* @param o Object that contains the own properties.
38+
*/
39+
getOwnPropertyNames(o: any): string[];
40+
41+
/**
42+
* Creates an object that has the specified prototype or that has null prototype.
43+
* @param o Object to use as a prototype. May be null.
44+
*/
45+
create(o: object | null): {};
46+
47+
/**
48+
* Creates an object that has the specified prototype, and that optionally contains specified properties.
49+
* @param o Object to use as a prototype. May be null
50+
* @param properties JavaScript object that contains one or more property descriptors.
51+
*/
52+
create<P extends Record<string, PropertyDescriptor>>(
53+
o: object | null,
54+
properties: P & ThisType<any>
55+
): {
56+
[K in keyof P]: P[K] extends { value: infer V }
57+
? V
58+
: P[K] extends { get: () => infer V }
59+
? V
60+
: unknown;
61+
};
62+
63+
/**
64+
* Adds a property to an object, or modifies attributes of an existing property.
65+
* @param o Object on which to add or modify the property. This can be a native JavaScript object (that is, a user-defined object or a built in object) or a DOM object.
66+
* @param p The property name.
67+
* @param attributes Descriptor for the property. It can be for a data property or an accessor property.
68+
*/
69+
defineProperty<O, K extends PropertyKey, D extends PropertyDescriptor>(
70+
o: O,
71+
p: K,
72+
attributes: D & ThisType<any>
73+
): O &
74+
(K extends PropertyKey
75+
? Record<
76+
K,
77+
D extends { value: infer V }
78+
? V
79+
: D extends { get: () => infer V }
80+
? V
81+
: unknown
82+
>
83+
: unknown);
84+
85+
/**
86+
* Adds one or more properties to an object, and/or modifies attributes of existing properties.
87+
* @param o Object on which to add or modify the properties. This can be a native JavaScript object or a DOM object.
88+
* @param properties JavaScript object that contains one or more descriptor objects. Each descriptor object describes a data property or an accessor property.
89+
*/
90+
defineProperties<O, P extends Record<PropertyKey, PropertyDescriptor>>(
91+
o: O,
92+
properties: P & ThisType<any>
93+
): {
94+
[K in keyof (O & P)]: P[K] extends { value: infer V }
95+
? V
96+
: P[K] extends { get: () => infer V }
97+
? V
98+
: K extends keyof O
99+
? O[K]
100+
: unknown;
101+
};
102+
103+
/**
104+
* Prevents the modification of attributes of existing properties, and prevents the addition of new properties.
105+
* @param o Object on which to lock the attributes.
106+
*/
107+
seal<T>(o: T): T;
108+
109+
/**
110+
* Prevents the modification of existing property attributes and values, and prevents the addition of new properties.
111+
* @param o Object on which to lock the attributes.
112+
*/
113+
freeze<T>(a: T[]): readonly T[];
114+
115+
/**
116+
* Prevents the modification of existing property attributes and values, and prevents the addition of new properties.
117+
* @param o Object on which to lock the attributes.
118+
*/
119+
freeze<T extends Function>(f: T): T;
120+
121+
/**
122+
* Prevents the modification of existing property attributes and values, and prevents the addition of new properties.
123+
* @param o Object on which to lock the attributes.
124+
*/
125+
freeze<T>(o: T): Readonly<T>;
126+
127+
/**
128+
* Prevents the addition of new properties to an object.
129+
* @param o Object to make non-extensible.
130+
*/
131+
preventExtensions<T>(o: T): T;
132+
133+
/**
134+
* Returns true if existing property attributes cannot be modified in an object and new properties cannot be added to the object.
135+
* @param o Object to test.
136+
*/
137+
isSealed(o: any): boolean;
138+
139+
/**
140+
* Returns true if existing property attributes and values cannot be modified in an object, and new properties cannot be added to the object.
141+
* @param o Object to test.
142+
*/
143+
isFrozen(o: any): boolean;
144+
145+
/**
146+
* Returns a value that indicates whether new properties can be added to an object.
147+
* @param o Object to test.
148+
*/
149+
isExtensible(o: any): boolean;
150+
151+
/**
152+
* Returns the names of the enumerable string properties and methods of an object.
153+
* @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.
154+
*/
155+
keys(o: object): string[];
156+
}

0 commit comments

Comments
 (0)