Skip to content

Commit 88b13c5

Browse files
committed
Build pure types script refactoring
1 parent e7a3166 commit 88b13c5

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

scripts/build-types/pure.mjs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function processLines(lines, prefix) {
103103
.filter(line => line !== null);
104104
}
105105

106-
function wrapInNamespace(content, namespace = 'CoreJS') {
106+
function wrapInNamespace(content) {
107107
const lines = content.split('\n');
108108
const headerLines = [];
109109

@@ -115,7 +115,7 @@ function wrapInNamespace(content, namespace = 'CoreJS') {
115115
// Update reference paths and add to header
116116
if (/\/\/\/\s*<reference types/.test(line)) {
117117
const match = line.match(/\/\/\/\s*<reference types="(?<path>[^"]+)"/);
118-
const typePath = match?.groups?.path ?? '';
118+
const typePath = match.groups.path;
119119
headerLines.push(line.replace(typePath, `../${ typePath }`));
120120
continue;
121121
}
@@ -135,12 +135,12 @@ function wrapInNamespace(content, namespace = 'CoreJS') {
135135
break;
136136
}
137137

138-
const namespaceBody = [...processLines(outside, namespace), ...sections.flatMap(s => processLines(s, namespace))]
138+
const namespaceBody = processLines(lines.slice(i), NAMESPACE)
139139
.reduce((res, line) => {
140-
if (line?.trim() !== '' || (res.at(-1) && res.at(-1).trim() !== '')) res.push(line);
140+
if (line.trim() !== '' || res.at(-1)?.trim() !== '') res.push(line);
141141
return res;
142142
}, [])
143-
.map(line => line ? ` ${ line }` : '')
143+
.map(line => line ? line.padStart(2) : '')
144144
.join('\n');
145145

146146
return `${ headerLines.length ? `${ headerLines.join('\n') }\n` : '' }declare namespace ${ NAMESPACE } {\n${ namespaceBody }\n}\n`;
@@ -154,14 +154,14 @@ export async function preparePureTypes(typesPath, initialPath) {
154154
if (entry.isDirectory()) {
155155
await preparePureTypes(path.join(typesPath, entry.name), initialPath);
156156
} else {
157-
if (entry.name.includes('core-js-types.d.ts')) continue;
157+
if (entry.name === 'core-js-types.d.ts') continue;
158158

159159
const typePath = path.join(typesPath, entry.name);
160160
const resultFilePath = typePath.replace(initialPath, `${ initialPath }/pure/`);
161161

162162
if (await pathExists(resultFilePath)) continue;
163163

164-
const content = await fs.readFile(typePath, 'utf8');
164+
const content = await fs.readFile(typePath, 'utf8'); // move to spread on top
165165

166166
const result = (content.includes('declare namespace')) ? content : wrapInNamespace(content);
167167

0 commit comments

Comments
 (0)