Skip to content

Commit f40c665

Browse files
committed
Types refactoring
1 parent 785c136 commit f40c665

File tree

69 files changed

+142
-108
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+142
-108
lines changed

packages/core-js-types/package.json

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,40 @@
2525
"email": "zloirock@zloirock.ru",
2626
"url": "http://zloirock.ru"
2727
},
28-
"types": "dist/core-js-types.56.d.ts",
29-
"files": [
30-
"dist/core-js-types.56.d.ts"
31-
],
28+
"types": "./actual.d.ts",
3229
"typesVersions": {
3330
">=5.6": {
34-
"*": ["dist/core-js-types.56.d.ts"]
31+
"*": ["dist/56/*"]
3532
},
3633
">=5.2": {
37-
"*": ["dist/core-js-types.52.d.ts"]
34+
"*": ["dist/52/*"]
35+
}
36+
},
37+
"exports": {
38+
".": {
39+
"types": "./actual.d.ts",
40+
"default": "./actual.d.ts"
41+
},
42+
"./actual": {
43+
"types": "./actual.d.ts",
44+
"default": "./actual.d.ts"
45+
},
46+
"./es": {
47+
"types": "./es.d.ts",
48+
"default": "./es.d.ts"
49+
},
50+
"./full": {
51+
"types": "./full.d.ts",
52+
"default": "./full.d.ts"
53+
},
54+
"./pure": {
55+
"types": "./pure.d.ts",
56+
"default": "./pure.d.ts"
57+
58+
},
59+
"./stable": {
60+
"types": "./stable.d.ts",
61+
"default": "./stable.d.ts"
3862
}
3963
},
4064
"sideEffects": false

packages/core-js-types/src/56/proposals/async-iteration.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@ interface SymbolConstructor {
1212
*/
1313
readonly asyncIterator: unique symbol;
1414
}
15+
16+
interface AsyncIteratorConstructor {
17+
18+
}
19+
20+
declare var AsyncIterator: AsyncIteratorConstructor;

packages/core-js-types/src/56/proposals/error-cause.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ declare global {
5555

5656
(message?: string, options?: ErrorOptions): URIError;
5757
}
58+
59+
interface AggregateError extends Error {
60+
errors: any[];
61+
}
62+
63+
interface AggregateErrorConstructor {
64+
new (errors: Iterable<any>, message?: string): AggregateError;
65+
(errors: Iterable<any>, message?: string): AggregateError;
66+
readonly prototype: AggregateError;
67+
}
68+
69+
var AggregateError: AggregateErrorConstructor;
5870
}
5971

6072
export {};

scripts/build-types/config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ export default {
22
buildDir: 'packages/core-js-types/dist/',
33
bundleName: 'core-js-types',
44
latestTsVersion: 59,
5-
packageName: '@core-js/pure/',
5+
packageName: '@core-js/',
66
srcDir: 'packages/core-js-types/src/',
77
};

scripts/build-types/index.mjs

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@ const { copy, outputFile, pathExists, readdir, remove } = fs;
1010

1111
const versionArg = argv._.find(item => item.startsWith('version='));
1212
const VERSION = versionArg ? versionArg.slice('version='.length) : undefined;
13-
14-
async function buildType(typeFilePath, entry, options) {
13+
const imports = {
14+
es: new Set(),
15+
stable: new Set(),
16+
actual: new Set(),
17+
full: new Set(),
18+
pure: new Set(),
19+
};
20+
21+
async function buildType(entry, options) {
1522
let {
1623
entryFromNamespace,
1724
subset = entryFromNamespace ?? 'full',
1825
template, templateStable, templateActual, templateFull, filter, modules, enforceEntryCreation,
26+
customType, tsVersion,
1927
} = options;
2028

2129
switch (subset) {
@@ -50,9 +58,23 @@ async function buildType(typeFilePath, entry, options) {
5058

5159
if (filter) modules = modules.filter(it => filter.has(it));
5260

61+
const tplPure = template({ ...options, modules, rawModules, level, entry, types, packageName: `${ config.packageName }pure/` });
5362
const tpl = template({ ...options, modules, rawModules, level, entry, types, packageName: config.packageName });
5463

55-
await outputFile(typeFilePath, `${ tpl.dts }${ tpl.dts ? '\n\n' : '' }`, { flag: 'a' });
64+
types.forEach(type => {
65+
imports[subset].add(type);
66+
imports.pure.add(type);
67+
});
68+
if (customType) {
69+
imports[subset].add(customType);
70+
imports.pure.add(customType);
71+
}
72+
73+
const filePath = buildFilePath(tsVersion, subset);
74+
const filePathPure = buildFilePath(tsVersion, 'pure');
75+
76+
await outputFile(filePath, `${ tpl.dts }${ tpl.dts ? '\n\n' : '' }`, { flag: 'a' });
77+
await outputFile(filePathPure, `${ tplPure.dts }${ tplPure.dts ? '\n\n' : '' }`, { flag: 'a' });
5678
}
5779

5880
async function getVersions() {
@@ -70,19 +92,7 @@ async function buildTypesDirForTSVersion(version) {
7092
const versions = await getVersions();
7193
for (const v of versions) {
7294
if (v > version) break;
73-
await copy(path.join(config.srcDir, v.toString()), path.join(config.buildDir, version.toString()));
74-
}
75-
}
76-
77-
async function addImports(filePath, fromPath) {
78-
const entries = await readdir(fromPath, { withFileTypes: true });
79-
for (const entry of entries) {
80-
if (entry.isDirectory()) {
81-
await addImports(filePath, path.join(fromPath, entry.name));
82-
} else {
83-
const typePath = path.join(fromPath, entry.name).replace(config.buildDir, '');
84-
await outputFile(filePath, `/// <reference path="./${ typePath }" />\n`, { flag: 'a' });
85-
}
95+
await copy(path.join(config.srcDir, v.toString()), path.join(config.buildDir, version.toString(), 'types'));
8696
}
8797
}
8898

@@ -96,38 +106,43 @@ const ESWithProposalsSet = new Set(ESWithProposalsModules);
96106
const StableSet = new Set(StableModules);
97107
const ActualSet = new Set(ActualModules);
98108

109+
function buildFilePath(tsVersion, subset) {
110+
return path.join(config.buildDir, tsVersion.toString(), `${ subset }.d.ts`);
111+
}
112+
113+
async function prependImports(version) {
114+
for (const subset of Object.keys(imports)) {
115+
const filePath = buildFilePath(version, subset);
116+
const importLines = Array.from(imports[subset], it => `/// <reference types="./types/${ it }.d.ts" />`).join('\n');
117+
const originalContent = await fs.readFile(filePath, 'utf8');
118+
await outputFile(filePath, `${ importLines }\n\n${ originalContent }`);
119+
}
120+
}
121+
99122
async function buildTypesForTSVersion(tsVersion) {
100123
tsVersion = tsVersion.toString().replace('.', '');
101-
const bundleName = `${ config.bundleName }.${ tsVersion === config.latestTsVersion ? 'latest' : tsVersion }.d.ts`;
102-
const bundlePath = path.join(config.buildDir, bundleName);
103-
const typesPath = path.join(config.buildDir, tsVersion.toString());
124+
const bundlePath = path.join(config.buildDir, tsVersion);
104125
if (await pathExists(bundlePath)) await remove(bundlePath);
105-
if (await pathExists(typesPath)) await remove(typesPath);
106126
await buildTypesDirForTSVersion(tsVersion);
107-
await addImports(bundlePath, typesPath);
108127

109128
for (const [entry, definition] of Object.entries(features)) {
110-
await buildType(bundlePath, `es/${ entry }`, { ...definition, entryFromNamespace: 'es' });
111-
await buildType(bundlePath, `stable/${ entry }`, { ...definition, entryFromNamespace: 'stable' });
112-
await buildType(bundlePath, `actual/${ entry }`, { ...definition, entryFromNamespace: 'actual' });
113-
await buildType(bundlePath, `full/${ entry }`, { ...definition, entryFromNamespace: 'full' });
129+
await buildType(`es/${ entry }`, { ...definition, entryFromNamespace: 'es', tsVersion });
130+
await buildType(`stable/${ entry }`, { ...definition, entryFromNamespace: 'stable', tsVersion });
131+
await buildType(`actual/${ entry }`, { ...definition, entryFromNamespace: 'actual', tsVersion });
132+
await buildType(`full/${ entry }`, { ...definition, entryFromNamespace: 'full', tsVersion });
114133
}
115134

116135
for (const [name, definition] of Object.entries(proposals)) {
117-
await buildType(bundlePath, `proposals/${ name }`, { ...definition, template: $proposal });
136+
await buildType(`proposals/${ name }`, { ...definition, template: $proposal, tsVersion });
118137
}
119138

120-
await buildType(bundlePath, 'stage/3', { template: $path, modules: ActualModules, subset: 'es-stage' });
121-
await buildType(bundlePath, 'stage/2.7', { template: $path, modules: modulesToStage(StableModules, 2.7), subset: 'es-stage' });
122-
await buildType(bundlePath, 'stage/2', { template: $path, modules: modulesToStage(StableModules, 2), subset: 'es-stage' });
123-
await buildType(bundlePath, 'stage/1', { template: $path, modules: modulesToStage(StableModules, 1), subset: 'es-stage' });
124-
await buildType(bundlePath, 'stage/0', { template: $path, modules: AllModules, subset: 'es-stage' });
125-
126-
await buildType(bundlePath, 'es/index', { template: $path, modules: ESModules, subset: 'es' });
127-
await buildType(bundlePath, 'stable/index', { template: $path, modules: StableModules, subset: 'stable' });
128-
await buildType(bundlePath, 'actual/index', { template: $path, modules: ActualModules });
129-
await buildType(bundlePath, 'full/index', { template: $path, modules: AllModules });
130-
await buildType(bundlePath, 'index', { template: $path, modules: ActualModules });
139+
await buildType('es/index', { template: $path, modules: ESModules, subset: 'es', tsVersion });
140+
await buildType('stable/index', { template: $path, modules: StableModules, subset: 'stable', tsVersion });
141+
await buildType('actual/index', { template: $path, modules: ActualModules, tsVersion });
142+
await buildType('full/index', { template: $path, modules: AllModules, tsVersion });
143+
await buildType('index', { template: $path, modules: ActualModules, tsVersion });
144+
145+
await prependImports(tsVersion);
131146
}
132147

133148
if (VERSION) {

tests/type-definitions/global/proposals/accessible-object-hasownproperty.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'core-js/full';
2-
import '@core-js/types';
32

43
Object.hasOwn({a: 1}, 'a');
54
Object.hasOwn([], 0);

tests/type-definitions/global/proposals/array-buffer-base64.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'core-js/full';
2-
import '@core-js/types';
32

43
function acceptUint8Array(v: Uint8Array) {}
54
function acceptProcessMetadata(v: { read: number; written: number }) {}

tests/type-definitions/global/proposals/array-buffer-transfer.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'core-js/full';
2-
import '@core-js/types';
32

43
const ab = new ArrayBuffer(16);
54
// todo uncomment when fixed

tests/type-definitions/global/proposals/array-filtering.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'core-js/full';
2-
import '@core-js/types';
32

43
[1, 2, 3].filterReject((v, i, arr) => v > 1);
54
['a', 'b'].filterReject((v, i, arr) => v === 'a');

tests/type-definitions/global/proposals/array-find-from-last.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'core-js/full';
2-
import '@core-js/types';
32

43
const res: number | undefined = [1, 2, 3].findLast(v => v > 1);
54
[1, 2, 3].findLast((v): v is 2 => v === 2);

0 commit comments

Comments
 (0)