Skip to content

Commit d48e3b0

Browse files
committed
Build types refactoring
1 parent 070c719 commit d48e3b0

File tree

5 files changed

+47
-62
lines changed

5 files changed

+47
-62
lines changed

packages/core-js-types/src/base/core-js-types/core-js-types.pure.d.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

scripts/build-entries/index.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getModulesMetadata } from './get-dependencies.mjs';
22
import { features, proposals } from './entries-definitions.mjs';
3-
import { $proposal, $path, wrapEntry } from './templates.mjs';
3+
import { $proposal, $path, wrapEntryInStrict } from './templates.mjs';
44
import { modules as AllModules } from '@core-js/compat/src/data.mjs';
55
import { expandModules, modulesToStage } from './helpers.mjs';
66

@@ -77,7 +77,7 @@ async function buildEntry(entry, options) {
7777

7878
const filepath = `./packages/core-js/${ entry }.js`;
7979
await mkdir(dirname(filepath), { recursive: true });
80-
await writeFile(filepath, wrapEntry(tpl.entry));
80+
await writeFile(filepath, wrapEntryInStrict(tpl.entry));
8181

8282
built++;
8383

scripts/build-entries/templates.mjs

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const importModule = (module, level) => `require('${ level ? '../'.repeat(level)
77

88
const importModules = ({ modules, level }) => modules.map(module => importModule(module, level)).join('\n');
99

10-
const buildTypeName = (namespace, name) => `CoreJS.${ namespace }${ String(name).charAt(0).toUpperCase() + String(name).slice(1) }`;
10+
const buildCoreJSTypeName = (namespace, name) => `CoreJS.${ namespace }${ name.charAt(0).toUpperCase() + name.slice(1) }`;
1111

1212
function isAllowedFunctionName(name) {
1313
try {
@@ -36,13 +36,9 @@ const namespacesWithOneGeneric = [
3636
'AsyncIterator',
3737
];
3838

39-
function existsInES6(namespace) {
40-
const missingNamespacesInES6 = [
41-
'AsyncIterator',
42-
];
43-
44-
return !missingNamespacesInES6.includes(namespace);
45-
}
39+
const missingNamespacesInES6 = [
40+
'AsyncIterator',
41+
];
4642

4743
function getGenericsForNamespace(namespace) {
4844
if (namespace === 'WeakMap') {
@@ -72,13 +68,13 @@ function getCustomGenerics(count) {
7268
return `<${ names.slice(0, count).join(', ') }>`;
7369
}
7470

75-
export const wrapEntry = template => `'use strict';\n${ template }\n`;
71+
export const wrapEntryInStrict = template => `'use strict';\n${ template }\n`;
7672

7773
export const $justImport = p => ({
7874
entry: dedent`
7975
${ importModules(p) }
8076
`,
81-
dts: '',
77+
types: '',
8278
});
8379

8480
export const $prototype = p => ({
@@ -89,7 +85,7 @@ export const $prototype = p => ({
8985
9086
module.exports = getBuiltInPrototypeMethod('${ p.namespace }', '${ p.name }');
9187
`,
92-
dts: dedent`
88+
types: dedent`
9389
declare module '${ p.packageName }${ p.entry }' {
9490
type method${ getGenericsForNamespace(p.namespace) } = ${ p.prefix ?? '' }${ p.namespace }${ getCommonGenericsForNamespace(p.namespace) }['${ p.name }'];
9591
export = method;
@@ -105,7 +101,7 @@ export const $prototypeIterator = p => ({
105101
106102
module.exports = getIteratorMethod(${ p.source });
107103
`,
108-
dts: dedent`
104+
types: dedent`
109105
declare module '${ p.packageName }${ p.entry }' {
110106
const method: typeof ${ p.prefix ?? '' }${ p.namespace }.prototype[typeof Symbol.iterator];
111107
export = method;
@@ -121,10 +117,10 @@ export const $uncurried = p => ({
121117
122118
module.exports = entryUnbind('${ p.namespace }', '${ p.name }');
123119
`,
124-
dts: dedent`
120+
types: dedent`
125121
declare module '${ p.packageName }${ p.entry }' {
126122
type method${ getGenericsForNamespace(p.namespace) } = ${ p.prefix ?? '' }${ p.namespace }${ getCommonGenericsForNamespace(p.namespace) }['${ p.name }'];
127-
const resultMethod: ${ getGenericsForNamespace(p.namespace) }(self: ${ p.prefix && !existsInES6(p.namespace) ? p.prefix : '' }${ p.namespace }${ getCommonGenericsForNamespace(p.namespace) }, ...args: Parameters<method${ getCommonGenericsForNamespace(p.namespace) }>) => ReturnType<method${ getCommonGenericsForNamespace(p.namespace) }>;
123+
const resultMethod: ${ getGenericsForNamespace(p.namespace) }(self: ${ p.prefix && missingNamespacesInES6.includes(p.namespace) ? p.prefix : '' }${ p.namespace }${ getCommonGenericsForNamespace(p.namespace) }, ...args: Parameters<method${ getCommonGenericsForNamespace(p.namespace) }>) => ReturnType<method${ getCommonGenericsForNamespace(p.namespace) }>;
128124
export = resultMethod;
129125
}
130126
`,
@@ -138,9 +134,9 @@ export const $uncurriedWithCustomType = p => ({
138134
139135
module.exports = entryUnbind('${ p.namespace }', '${ p.name }');
140136
`,
141-
dts: dedent`
137+
types: dedent`
142138
declare module '${ p.packageName }${ p.entry }' {
143-
const resultMethod: ${ getCustomGenerics(p.genericsCount) }(self: ${ p.namespace }${ getCommonGenericsForNamespace(p.namespace) }, ...args: Parameters<${ buildTypeName(p.namespace, p.name) }${ getCustomGenerics(p.genericsCount) }>) => ReturnType<${ buildTypeName(p.namespace, p.name) }${ getCustomGenerics(p.genericsCount) }>;
139+
const resultMethod: ${ getCustomGenerics(p.genericsCount) }(self: ${ p.namespace }${ getCommonGenericsForNamespace(p.namespace) }, ...args: Parameters<${ buildCoreJSTypeName(p.namespace, p.name) }${ getCustomGenerics(p.genericsCount) }>) => ReturnType<${ buildCoreJSTypeName(p.namespace, p.name) }${ getCustomGenerics(p.genericsCount) }>;
144140
export = resultMethod;
145141
}
146142
`,
@@ -155,7 +151,7 @@ export const $uncurriedIterator = p => ({
155151
156152
module.exports = uncurryThis(getIteratorMethod(${ p.source }));
157153
`,
158-
dts: dedent`
154+
types: dedent`
159155
declare module '${ p.packageName }${ p.entry }' {
160156
type method${ getGenericsForNamespace(p.namespace) } = ${ p.namespace }${ getGenericsForNamespace(p.namespace) }[typeof Symbol.iterator];
161157
const resultMethod: ${ getGenericsForNamespace(p.namespace) }(self: ${ p.namespace }${ getCommonGenericsForNamespace(p.namespace) }, ...args: Parameters<method${ getCommonGenericsForNamespace(p.namespace) }>) => ReturnType<method${ getCommonGenericsForNamespace(p.namespace) }>;
@@ -172,7 +168,7 @@ export const $static = p => ({
172168
173169
module.exports = getBuiltInStaticMethod('${ p.namespace }', '${ p.name }');
174170
`,
175-
dts: dedent`
171+
types: dedent`
176172
declare module '${ p.packageName }${ p.entry }' {
177173
const method: typeof ${ p.prefix ?? '' }${ p.namespace }.${ p.name };
178174
export = method;
@@ -195,7 +191,7 @@ export const $staticWithContext = p => ({
195191
return apply(method, isCallable(this) ? this : getBuiltIn('${ p.namespace }'), arguments);
196192
};
197193
`,
198-
dts: dedent`
194+
types: dedent`
199195
declare module '${ p.packageName }${ p.entry }' {
200196
const method: typeof ${ p.prefix ?? '' }${ p.namespace }.${ p.name };
201197
export = method;
@@ -214,7 +210,7 @@ export const $patchableStatic = p => ({
214210
return apply(getBuiltInStaticMethod('${ p.namespace }', '${ p.name }'), this, arguments);
215211
};
216212
`,
217-
dts: dedent`
213+
types: dedent`
218214
declare module '${ p.packageName }${ p.entry }' {
219215
const method: typeof ${ p.prefix ?? '' }${ p.namespace }.${ p.name };
220216
export = method;
@@ -230,7 +226,7 @@ export const $namespace = p => ({
230226
231227
module.exports = path.${ p.name };
232228
`,
233-
dts: dedent`
229+
types: dedent`
234230
declare module '${ p.packageName }${ p.entry }' {
235231
const namespace: typeof ${ p.prefix ?? '' }${ p.name };
236232
export = namespace;
@@ -246,7 +242,7 @@ export const $helper = p => ({
246242
247243
module.exports = $export;
248244
`,
249-
dts: dedent`
245+
types: dedent`
250246
declare module '${ p.packageName }${ p.entry }' {
251247
const helper: (arg: NonNullable<any>) => any;
252248
export = helper;
@@ -262,7 +258,7 @@ export const $path = p => ({
262258
263259
module.exports = path;
264260
`,
265-
dts: dedent`
261+
types: dedent`
266262
declare module '${ p.packageName }${ p.entry }' {
267263
const path: typeof globalThis;
268264
export = path;
@@ -283,7 +279,7 @@ export const $instanceArray = p => ({
283279
return ownProperty;
284280
};
285281
`,
286-
dts: dedent`
282+
types: dedent`
287283
declare module '${ p.packageName }${ p.entry }' {
288284
const method: (arg: NonNullable<any>) => any;
289285
export = method;
@@ -305,7 +301,7 @@ export const $instanceNumber = p => ({
305301
return ownProperty;
306302
};
307303
`,
308-
dts: dedent`
304+
types: dedent`
309305
declare module '${ p.packageName }${ p.entry }' {
310306
const method: (arg: NonNullable<any>) => any;
311307
export = method;
@@ -327,7 +323,7 @@ export const $instanceString = p => ({
327323
return ownProperty;
328324
};
329325
`,
330-
dts: dedent`
326+
types: dedent`
331327
declare module '${ p.packageName }${ p.entry }' {
332328
const method: (arg: NonNullable<any>) => any;
333329
export = method;
@@ -349,7 +345,7 @@ export const $instanceFunction = p => ({
349345
} return ownProperty;
350346
};
351347
`,
352-
dts: dedent`
348+
types: dedent`
353349
declare module '${ p.packageName }${ p.entry }' {
354350
const method: (arg: NonNullable<any>) => any;
355351
export = method;
@@ -377,7 +373,7 @@ export const $instanceDOMIterables = p => ({
377373
return ownProperty;
378374
};
379375
`,
380-
dts: dedent`
376+
types: dedent`
381377
declare module '${ p.packageName }${ p.entry }' {
382378
const method: (arg: NonNullable<any>) => any;
383379
export = method;
@@ -402,7 +398,7 @@ export const $instanceArrayString = p => ({
402398
return ownProperty;
403399
};
404400
`,
405-
dts: dedent`
401+
types: dedent`
406402
declare module '${ p.packageName }${ p.entry }' {
407403
const method: (arg: NonNullable<any>) => any;
408404
export = method;
@@ -433,7 +429,7 @@ export const $instanceArrayDOMIterables = p => ({
433429
return ownProperty;
434430
};
435431
`,
436-
dts: dedent`
432+
types: dedent`
437433
declare module '${ p.packageName }${ p.entry }' {
438434
const method: (arg: NonNullable<any>) => any;
439435
export = method;
@@ -454,7 +450,7 @@ export const $instanceRegExpFlags = p => ({
454450
return (it === RegExpPrototype || isPrototypeOf(RegExpPrototype, it)) ? flags(it) : it.flags;
455451
};
456452
`,
457-
dts: dedent`
453+
types: dedent`
458454
declare module '${ p.packageName }${ p.entry }' {
459455
const method: (arg: NonNullable<any>) => any;
460456
export = method;
@@ -468,5 +464,5 @@ export const $proposal = p => ({
468464
// ${ p.link }
469465
${ importModules(p) }
470466
`,
471-
dts: '',
467+
types: '',
472468
});

scripts/build-types/index.mjs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,32 +82,33 @@ async function buildType(entry, options) {
8282
const indexPath = buildFilePath(tsVersion, 'index');
8383
const purePath = buildFilePath(tsVersion, 'pure');
8484

85-
const tplPure = template({ ...options, modules, rawModules, level, entry, types, packageName: PACKAGE_NAME_PURE, prefix: TYPE_PREFIX });
86-
const tpl = template({ ...options, modules, rawModules, level, entry, types, packageName: PACKAGE_NAME });
85+
const entryWithTypes = template({ ...options, modules, rawModules, level, entry, types, packageName: PACKAGE_NAME });
86+
const entryWithTypesPure = template({ ...options, modules, rawModules, level, entry, types, packageName: PACKAGE_NAME_PURE,
87+
prefix: TYPE_PREFIX });
8788

88-
await outputFile(indexPath, `${ tpl.dts }${ tpl.dts ? '\n\n' : '' }`, { flag: 'a' });
89-
await outputFile(purePath, `${ tplPure.dts }${ tplPure.dts ? '\n\n' : '' }`, { flag: 'a' });
89+
await outputFile(indexPath, `${ entryWithTypes.types }${ entryWithTypes.types ? '\n\n' : '' }`, { flag: 'a' });
90+
await outputFile(purePath, `${ entryWithTypesPure.types }${ entryWithTypesPure.types ? '\n\n' : '' }`, { flag: 'a' });
9091

9192
if (!entry.endsWith('/')) {
9293
const entryWithExt = `${ entry }.js`;
93-
const tplPureWithExt = template({ ...options, modules, rawModules, level, entry: entryWithExt,
94-
types, packageName: PACKAGE_NAME_PURE, prefix: TYPE_PREFIX });
95-
const tplWithExt = template({ ...options, modules, rawModules, level, types, entry: entryWithExt,
94+
const entryWithTypesWithExt = template({ ...options, modules, rawModules, level, types, entry: entryWithExt,
9695
packageName: PACKAGE_NAME });
96+
const entryWithTypesPureWithExt = template({ ...options, modules, rawModules, level, entry: entryWithExt,
97+
types, packageName: PACKAGE_NAME_PURE, prefix: TYPE_PREFIX });
9798

98-
await outputFile(indexPath, `${ tplWithExt.dts }${ tplWithExt.dts ? '\n\n' : '' }`, { flag: 'a' });
99-
await outputFile(purePath, `${ tplPureWithExt.dts }${ tplPureWithExt.dts ? '\n\n' : '' }`, { flag: 'a' });
99+
await outputFile(indexPath, `${ entryWithTypesWithExt.types }${ entryWithTypesWithExt.types ? '\n\n' : '' }`, { flag: 'a' });
100+
await outputFile(purePath, `${ entryWithTypesPureWithExt.types }${ entryWithTypesPureWithExt.types ? '\n\n' : '' }`, { flag: 'a' });
100101
}
101102

102103
if (entry.endsWith('/index')) {
103104
const entryWithoutIndex = entry.replace(/\/index$/, '');
104-
const tplPureWithoutIndex = template({ ...options, modules, rawModules, level, entry: entryWithoutIndex, types,
105-
packageName: PACKAGE_NAME_PURE, prefix: TYPE_PREFIX });
106-
const tplWithoutIndex = template({ ...options, modules, rawModules, level, types, entry: entryWithoutIndex,
105+
const entryWithTypesWithoutIndex = template({ ...options, modules, rawModules, level, types, entry: entryWithoutIndex,
107106
packageName: PACKAGE_NAME });
107+
const entryWithTypesPureWithoutIndex = template({ ...options, modules, rawModules, level, entry: entryWithoutIndex, types,
108+
packageName: PACKAGE_NAME_PURE, prefix: TYPE_PREFIX });
108109

109-
await outputFile(indexPath, `${ tplWithoutIndex.dts }${ tplWithoutIndex.dts ? '\n\n' : '' }`, { flag: 'a' });
110-
await outputFile(purePath, `${ tplPureWithoutIndex.dts }${ tplPureWithoutIndex.dts ? '\n\n' : '' }`, { flag: 'a' });
110+
await outputFile(indexPath, `${ entryWithTypesWithoutIndex.types }${ entryWithTypesWithoutIndex.types ? '\n\n' : '' }`, { flag: 'a' });
111+
await outputFile(purePath, `${ entryWithTypesPureWithoutIndex.types }${ entryWithTypesPureWithoutIndex.types ? '\n\n' : '' }`, { flag: 'a' });
111112
}
112113

113114
if (proposal) {

scripts/build-types/pure.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function processLines(lines, prefix) {
9797
.filter(line => line !== null);
9898
}
9999

100-
function wrapDTSInNamespace(content, namespace = 'CoreJS') {
100+
function wrapInNamespace(content, namespace = 'CoreJS') {
101101
const lines = content.split('\n');
102102
const preamble = [];
103103
let i = 0;
@@ -138,7 +138,7 @@ export async function preparePureTypes(typesPath, initialPath) {
138138
if (await pathExists(resultFilePath)) continue;
139139
const content = await fs.readFile(typePath, 'utf8');
140140
if (content.includes('declare namespace')) continue;
141-
const result = wrapDTSInNamespace(content);
141+
const result = wrapInNamespace(content);
142142
await outputFile(resultFilePath, result);
143143
}
144144
}

0 commit comments

Comments
 (0)