detype 0.6.2 → 0.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +52 -42
- package/dist/index.d.ts +3 -0
- package/dist/index.js +13 -4
- package/package.json +15 -14
package/dist/cli.js
CHANGED
|
@@ -14,16 +14,20 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
14
14
|
return to;
|
|
15
15
|
};
|
|
16
16
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
17
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
18
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
19
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
20
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
17
21
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
18
22
|
mod
|
|
19
23
|
));
|
|
20
24
|
|
|
21
25
|
// src/cli-lib.ts
|
|
22
|
-
var
|
|
23
|
-
var
|
|
26
|
+
var import_node_fs2 = __toESM(require("fs"));
|
|
27
|
+
var import_node_path = __toESM(require("path"));
|
|
24
28
|
|
|
25
29
|
// src/transformFile.ts
|
|
26
|
-
var
|
|
30
|
+
var import_node_fs = __toESM(require("fs"));
|
|
27
31
|
|
|
28
32
|
// src/transform.ts
|
|
29
33
|
var import_core = require("@babel/core");
|
|
@@ -92,10 +96,11 @@ async function removeTypes(code, fileName, options) {
|
|
|
92
96
|
}
|
|
93
97
|
}
|
|
94
98
|
};
|
|
95
|
-
const
|
|
99
|
+
const babelConfig = {
|
|
96
100
|
filename: fileName,
|
|
97
101
|
retainLines: true,
|
|
98
102
|
plugins: [
|
|
103
|
+
// Plugin to remove leading comments attached to TypeScript-only constructs
|
|
99
104
|
{
|
|
100
105
|
name: "detype-comment-remover",
|
|
101
106
|
visitor: {
|
|
@@ -111,7 +116,11 @@ async function removeTypes(code, fileName, options) {
|
|
|
111
116
|
generatorOpts: {
|
|
112
117
|
shouldPrintComment: (comment) => comment !== "@detype: remove-me" && (!options.removeTsComments || !comment.match(/^\s*(@ts-ignore|@ts-expect-error)/))
|
|
113
118
|
}
|
|
114
|
-
}
|
|
119
|
+
};
|
|
120
|
+
if (options.customizeBabelConfig) {
|
|
121
|
+
options.customizeBabelConfig(babelConfig);
|
|
122
|
+
}
|
|
123
|
+
const babelOutput = await (0, import_core.transformAsync)(code, babelConfig);
|
|
115
124
|
if (!babelOutput || babelOutput.code === void 0 || babelOutput.code === null) {
|
|
116
125
|
throw new Error("Babel error");
|
|
117
126
|
}
|
|
@@ -207,7 +216,7 @@ function removeMagicComments(code, fileName, prettierOptions) {
|
|
|
207
216
|
|
|
208
217
|
// src/transformFile.ts
|
|
209
218
|
var import_prettier2 = require("prettier");
|
|
210
|
-
var { readFile, writeFile } =
|
|
219
|
+
var { readFile, writeFile } = import_node_fs.default.promises;
|
|
211
220
|
async function transformFile(inputFileName, outputFileName, options = {}) {
|
|
212
221
|
const code = await readFile(inputFileName, "utf-8");
|
|
213
222
|
const prettierOptions = await (0, import_prettier2.resolveConfig)(inputFileName);
|
|
@@ -230,7 +239,7 @@ var import_fast_glob = __toESM(require("fast-glob"));
|
|
|
230
239
|
// package.json
|
|
231
240
|
var package_default = {
|
|
232
241
|
name: "detype",
|
|
233
|
-
version: "0.6.
|
|
242
|
+
version: "0.6.3",
|
|
234
243
|
description: "Removes TypeScript type annotations but keeps the formatting",
|
|
235
244
|
main: "dist/index.js",
|
|
236
245
|
bin: "detype.js",
|
|
@@ -254,27 +263,28 @@ var package_default = {
|
|
|
254
263
|
"index.d.ts"
|
|
255
264
|
],
|
|
256
265
|
dependencies: {
|
|
257
|
-
"@babel/core": "^7.
|
|
258
|
-
"@babel/preset-typescript": "^7.
|
|
266
|
+
"@babel/core": "^7.21.4",
|
|
267
|
+
"@babel/preset-typescript": "^7.21.4",
|
|
268
|
+
"@vue/compiler-dom": "^3.2.47",
|
|
259
269
|
"@vuedx/compiler-sfc": "0.7.1",
|
|
260
270
|
"@vuedx/template-ast-types": "0.7.1",
|
|
261
271
|
"fast-glob": "^3.2.12",
|
|
262
|
-
prettier: "^2.8.
|
|
272
|
+
prettier: "^2.8.7",
|
|
263
273
|
"string.prototype.replaceall": "^1.0.7"
|
|
264
274
|
},
|
|
265
275
|
devDependencies: {
|
|
266
|
-
"@babel/traverse": "^7.
|
|
267
|
-
"@cyco130/eslint-config": "^
|
|
268
|
-
"@types/babel__core": "^7.
|
|
276
|
+
"@babel/traverse": "^7.21.4",
|
|
277
|
+
"@cyco130/eslint-config": "^3.0.2",
|
|
278
|
+
"@types/babel__core": "^7.20.0",
|
|
269
279
|
"@types/babel__traverse": "^7.18.3",
|
|
270
|
-
"@types/node": "18.
|
|
271
|
-
"@types/prettier": "^2.7.
|
|
272
|
-
eslint: "^8.
|
|
280
|
+
"@types/node": "18.15.11",
|
|
281
|
+
"@types/prettier": "^2.7.2",
|
|
282
|
+
eslint: "^8.37.0",
|
|
273
283
|
"npm-run-all": "^4.1.5",
|
|
274
|
-
rimraf: "^
|
|
275
|
-
tsup: "^6.
|
|
276
|
-
typescript: "^
|
|
277
|
-
vitest: "0.
|
|
284
|
+
rimraf: "^4.4.1",
|
|
285
|
+
tsup: "^6.7.0",
|
|
286
|
+
typescript: "^5.0.3",
|
|
287
|
+
vitest: "0.29.8"
|
|
278
288
|
},
|
|
279
289
|
repository: {
|
|
280
290
|
type: "git",
|
|
@@ -295,7 +305,7 @@ var package_default = {
|
|
|
295
305
|
};
|
|
296
306
|
|
|
297
307
|
// src/cli-lib.ts
|
|
298
|
-
var { stat, mkdir } =
|
|
308
|
+
var { stat, mkdir } = import_node_fs2.default.promises;
|
|
299
309
|
async function cli(...args2) {
|
|
300
310
|
let dashDash = false;
|
|
301
311
|
const params = [];
|
|
@@ -346,23 +356,23 @@ async function cli(...args2) {
|
|
|
346
356
|
const files = (await (0, import_fast_glob.default)(unixify(input + "/**/*.{ts,tsx,vue}"))).filter(
|
|
347
357
|
(file) => !file.endsWith(".d.ts")
|
|
348
358
|
);
|
|
349
|
-
const dirs = [...new Set(files.map((file) =>
|
|
350
|
-
await mkdir(
|
|
359
|
+
const dirs = [...new Set(files.map((file) => import_node_path.default.dirname(file)))].sort();
|
|
360
|
+
await mkdir(import_node_path.default.normalize(output), { recursive: true });
|
|
351
361
|
for (const dir of dirs) {
|
|
352
|
-
const outDir =
|
|
362
|
+
const outDir = import_node_path.default.join(output, import_node_path.default.relative(input, dir));
|
|
353
363
|
if (outDir === output)
|
|
354
364
|
continue;
|
|
355
|
-
await mkdir(
|
|
365
|
+
await mkdir(import_node_path.default.normalize(outDir), { recursive: true });
|
|
356
366
|
}
|
|
357
367
|
for (const file of files) {
|
|
358
|
-
const inputDir =
|
|
359
|
-
const outputName = inferName(file,
|
|
368
|
+
const inputDir = import_node_path.default.dirname(import_node_path.default.relative(input, file));
|
|
369
|
+
const outputName = inferName(file, import_node_path.default.join(output, inputDir));
|
|
360
370
|
removeMagic ? await removeMagicCommentsFromFile(
|
|
361
|
-
|
|
362
|
-
|
|
371
|
+
import_node_path.default.normalize(file),
|
|
372
|
+
import_node_path.default.normalize(outputName)
|
|
363
373
|
) : await transformFile(
|
|
364
|
-
|
|
365
|
-
|
|
374
|
+
import_node_path.default.normalize(file),
|
|
375
|
+
import_node_path.default.normalize(outputName),
|
|
366
376
|
{ removeTsComments }
|
|
367
377
|
);
|
|
368
378
|
}
|
|
@@ -391,28 +401,28 @@ async function cli(...args2) {
|
|
|
391
401
|
}
|
|
392
402
|
output = inferName(input);
|
|
393
403
|
}
|
|
394
|
-
const outputDir =
|
|
404
|
+
const outputDir = import_node_path.default.dirname(output);
|
|
395
405
|
if (outputDir) {
|
|
396
|
-
await mkdir(
|
|
406
|
+
await mkdir(import_node_path.default.normalize(outputDir), { recursive: true });
|
|
397
407
|
}
|
|
398
408
|
removeMagic ? await removeMagicCommentsFromFile(
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
) : await transformFile(
|
|
409
|
+
import_node_path.default.normalize(input),
|
|
410
|
+
import_node_path.default.normalize(output)
|
|
411
|
+
) : await transformFile(import_node_path.default.normalize(input), import_node_path.default.normalize(output), {
|
|
402
412
|
removeTsComments
|
|
403
413
|
});
|
|
404
414
|
return true;
|
|
405
415
|
function inferName(input2, outputDir2) {
|
|
406
416
|
let output2;
|
|
407
|
-
const { dir, name, ext } =
|
|
417
|
+
const { dir, name, ext } = import_node_path.default.parse(input2);
|
|
408
418
|
if (removeMagic) {
|
|
409
|
-
output2 =
|
|
419
|
+
output2 = import_node_path.default.join(outputDir2 ?? dir, `${name}${ext}`);
|
|
410
420
|
} else if (ext === ".ts") {
|
|
411
|
-
output2 =
|
|
421
|
+
output2 = import_node_path.default.join(outputDir2 ?? dir, name + ".js");
|
|
412
422
|
} else if (ext === ".tsx") {
|
|
413
|
-
output2 =
|
|
423
|
+
output2 = import_node_path.default.join(outputDir2 ?? dir, name + ".jsx");
|
|
414
424
|
} else if (ext === ".vue") {
|
|
415
|
-
output2 =
|
|
425
|
+
output2 = import_node_path.default.join(outputDir2 ?? dir, name + ".vue");
|
|
416
426
|
} else {
|
|
417
427
|
throw new Error(`Unknwon file extension ${input2}`);
|
|
418
428
|
}
|
|
@@ -446,7 +456,7 @@ var USAGE = `Usage:
|
|
|
446
456
|
Print this help and exit`;
|
|
447
457
|
var VERSION = package_default.version;
|
|
448
458
|
function unixify(name) {
|
|
449
|
-
return name.replaceAll(
|
|
459
|
+
return name.replaceAll(import_node_path.default.sep, "/");
|
|
450
460
|
}
|
|
451
461
|
|
|
452
462
|
// src/cli.ts
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { Options } from 'prettier';
|
|
2
2
|
export { Options as PrettierOptions } from 'prettier';
|
|
3
|
+
import { TransformOptions as TransformOptions$1 } from '@babel/core';
|
|
3
4
|
|
|
4
5
|
interface RemoveTypeOptions {
|
|
5
6
|
/** Whether to remove ts-ignore and ts-expect-error comments */
|
|
6
7
|
removeTsComments?: boolean;
|
|
8
|
+
/** Escape hatch for customizing Babel configuration */
|
|
9
|
+
customizeBabelConfig?(config: TransformOptions$1): void;
|
|
7
10
|
}
|
|
8
11
|
interface TransformOptions extends RemoveTypeOptions {
|
|
9
12
|
/** Prettier options */
|
package/dist/index.js
CHANGED
|
@@ -18,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
return to;
|
|
19
19
|
};
|
|
20
20
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
25
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
26
|
mod
|
|
23
27
|
));
|
|
@@ -100,10 +104,11 @@ async function removeTypes(code, fileName, options) {
|
|
|
100
104
|
}
|
|
101
105
|
}
|
|
102
106
|
};
|
|
103
|
-
const
|
|
107
|
+
const babelConfig = {
|
|
104
108
|
filename: fileName,
|
|
105
109
|
retainLines: true,
|
|
106
110
|
plugins: [
|
|
111
|
+
// Plugin to remove leading comments attached to TypeScript-only constructs
|
|
107
112
|
{
|
|
108
113
|
name: "detype-comment-remover",
|
|
109
114
|
visitor: {
|
|
@@ -119,7 +124,11 @@ async function removeTypes(code, fileName, options) {
|
|
|
119
124
|
generatorOpts: {
|
|
120
125
|
shouldPrintComment: (comment) => comment !== "@detype: remove-me" && (!options.removeTsComments || !comment.match(/^\s*(@ts-ignore|@ts-expect-error)/))
|
|
121
126
|
}
|
|
122
|
-
}
|
|
127
|
+
};
|
|
128
|
+
if (options.customizeBabelConfig) {
|
|
129
|
+
options.customizeBabelConfig(babelConfig);
|
|
130
|
+
}
|
|
131
|
+
const babelOutput = await (0, import_core.transformAsync)(code, babelConfig);
|
|
123
132
|
if (!babelOutput || babelOutput.code === void 0 || babelOutput.code === null) {
|
|
124
133
|
throw new Error("Babel error");
|
|
125
134
|
}
|
|
@@ -214,9 +223,9 @@ function removeMagicComments(code, fileName, prettierOptions) {
|
|
|
214
223
|
}
|
|
215
224
|
|
|
216
225
|
// src/transformFile.ts
|
|
217
|
-
var
|
|
226
|
+
var import_node_fs = __toESM(require("fs"));
|
|
218
227
|
var import_prettier2 = require("prettier");
|
|
219
|
-
var { readFile, writeFile } =
|
|
228
|
+
var { readFile, writeFile } = import_node_fs.default.promises;
|
|
220
229
|
async function transformFile(inputFileName, outputFileName, options = {}) {
|
|
221
230
|
const code = await readFile(inputFileName, "utf-8");
|
|
222
231
|
const prettierOptions = await (0, import_prettier2.resolveConfig)(inputFileName);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "detype",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.3",
|
|
4
4
|
"description": "Removes TypeScript type annotations but keeps the formatting",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"bin": "detype.js",
|
|
@@ -12,27 +12,28 @@
|
|
|
12
12
|
"index.d.ts"
|
|
13
13
|
],
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@babel/core": "^7.
|
|
16
|
-
"@babel/preset-typescript": "^7.
|
|
15
|
+
"@babel/core": "^7.21.4",
|
|
16
|
+
"@babel/preset-typescript": "^7.21.4",
|
|
17
|
+
"@vue/compiler-dom": "^3.2.47",
|
|
17
18
|
"@vuedx/compiler-sfc": "0.7.1",
|
|
18
19
|
"@vuedx/template-ast-types": "0.7.1",
|
|
19
20
|
"fast-glob": "^3.2.12",
|
|
20
|
-
"prettier": "^2.8.
|
|
21
|
+
"prettier": "^2.8.7",
|
|
21
22
|
"string.prototype.replaceall": "^1.0.7"
|
|
22
23
|
},
|
|
23
24
|
"devDependencies": {
|
|
24
|
-
"@babel/traverse": "^7.
|
|
25
|
-
"@cyco130/eslint-config": "^
|
|
26
|
-
"@types/babel__core": "^7.
|
|
25
|
+
"@babel/traverse": "^7.21.4",
|
|
26
|
+
"@cyco130/eslint-config": "^3.0.2",
|
|
27
|
+
"@types/babel__core": "^7.20.0",
|
|
27
28
|
"@types/babel__traverse": "^7.18.3",
|
|
28
|
-
"@types/node": "18.
|
|
29
|
-
"@types/prettier": "^2.7.
|
|
30
|
-
"eslint": "^8.
|
|
29
|
+
"@types/node": "18.15.11",
|
|
30
|
+
"@types/prettier": "^2.7.2",
|
|
31
|
+
"eslint": "^8.37.0",
|
|
31
32
|
"npm-run-all": "^4.1.5",
|
|
32
|
-
"rimraf": "^
|
|
33
|
-
"tsup": "^6.
|
|
34
|
-
"typescript": "^
|
|
35
|
-
"vitest": "0.
|
|
33
|
+
"rimraf": "^4.4.1",
|
|
34
|
+
"tsup": "^6.7.0",
|
|
35
|
+
"typescript": "^5.0.3",
|
|
36
|
+
"vitest": "0.29.8"
|
|
36
37
|
},
|
|
37
38
|
"repository": {
|
|
38
39
|
"type": "git",
|