detype 0.5.1 → 0.6.1

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/README.md CHANGED
@@ -90,23 +90,26 @@ console.log("Hello from TypeScript");
90
90
  ## CLI Usage
91
91
 
92
92
  ```
93
- detype [-m | --remove-magic-comments] <INPUT> [OUTPUT]
93
+ detype [-m | --remove-magic-comments] <INPUT> [OUTPUT]
94
94
 
95
- INPUT Input file or directory
95
+ INPUT Input file or directory
96
96
 
97
- OUTPUT Output file or directory
98
- (optional if it can be inferred and it won't overwrite the source file)
97
+ OUTPUT Output file or directory
98
+ (optional if it can be inferred and it won't overwrite the source file)
99
99
 
100
- -m, --remove-magic-comments
101
- Remove magic comments only, don't perform ts > js transform
100
+ -t, --remove-ts-comments
101
+ Remove @ts-ignore and @ts-expect-error comments
102
102
 
103
- detype [-v | --version]
103
+ -m, --remove-magic-comments
104
+ Remove magic comments only, don't perform ts > js transform
104
105
 
105
- Print version and exit
106
+ detype [-v | --version]
106
107
 
107
- detype [-h | --help]
108
+ Print version and exit
108
109
 
109
- Print this help and exit
110
+ detype [-h | --help]
111
+
112
+ Print this help and exit
110
113
  ```
111
114
 
112
115
  ## Node API
@@ -147,6 +150,11 @@ export function removeMagicCommentsFromFile(
147
150
 
148
151
  ## Change log
149
152
 
153
+ ### 0.6
154
+
155
+ - feature: Option to remove @ts-ignore and @ts-expect-error comments
156
+ - fix: Preserve newline runs (especially in template literals!)
157
+
150
158
  ### 0.5
151
159
 
152
160
  - BREAKING CHANGE: Drop support for Node 12
package/dist/cli.js CHANGED
@@ -1,26 +1,10 @@
1
+ "use strict";
1
2
  var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
- var __defProps = Object.defineProperties;
4
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
6
  var __getProtoOf = Object.getPrototypeOf;
9
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
10
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
11
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
- var __spreadValues = (a, b) => {
13
- for (var prop in b || (b = {}))
14
- if (__hasOwnProp.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- if (__getOwnPropSymbols)
17
- for (var prop of __getOwnPropSymbols(b)) {
18
- if (__propIsEnum.call(b, prop))
19
- __defNormalProp(a, prop, b[prop]);
20
- }
21
- return a;
22
- };
23
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
24
8
  var __copyProps = (to, from, except, desc) => {
25
9
  if (from && typeof from === "object" || typeof from === "function") {
26
10
  for (let key of __getOwnPropNames(from))
@@ -29,7 +13,10 @@ var __copyProps = (to, from, except, desc) => {
29
13
  }
30
14
  return to;
31
15
  };
32
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
16
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
18
+ mod
19
+ ));
33
20
 
34
21
  // src/cli-lib.ts
35
22
  var import_fs2 = __toESM(require("fs"));
@@ -46,8 +33,9 @@ var import_template_ast_types = require("@vuedx/template-ast-types");
46
33
  var import_preset_typescript = __toESM(require("@babel/preset-typescript"));
47
34
  var import_string_prototype = require("string.prototype.replaceall");
48
35
  (0, import_string_prototype.shim)();
49
- async function transform(code, fileName, prettierOptions) {
36
+ async function transform(code, fileName, options = {}) {
50
37
  var _a, _b, _c, _d;
38
+ const { prettierOptions, ...removeTypeOptions } = options;
51
39
  const originalCode = code;
52
40
  const originalFileName = fileName;
53
41
  code = code.replaceAll("\r\n", "\n");
@@ -60,18 +48,36 @@ async function transform(code, fileName, prettierOptions) {
60
48
  if (script1 && script2 && script1.loc.start.offset < script2.loc.start.offset) {
61
49
  [script2, script1] = [script1, script2];
62
50
  }
63
- code = await removeTypesFromVueSfcScript(code, fileName, script1, (_c = parsedVue.descriptor.template) == null ? void 0 : _c.ast);
64
- code = await removeTypesFromVueSfcScript(code, fileName, script2, (_d = parsedVue.descriptor.template) == null ? void 0 : _d.ast);
51
+ code = await removeTypesFromVueSfcScript(
52
+ code,
53
+ fileName,
54
+ script1,
55
+ (_c = parsedVue.descriptor.template) == null ? void 0 : _c.ast,
56
+ removeTypeOptions
57
+ );
58
+ code = await removeTypesFromVueSfcScript(
59
+ code,
60
+ fileName,
61
+ script2,
62
+ (_d = parsedVue.descriptor.template) == null ? void 0 : _d.ast,
63
+ removeTypeOptions
64
+ );
65
65
  } else {
66
- code = await removeTypes(code, fileName);
66
+ code = await removeTypes(code, fileName, removeTypeOptions);
67
67
  }
68
- code = (0, import_prettier.format)(code, __spreadProps(__spreadValues({}, prettierOptions), {
68
+ code = (0, import_prettier.format)(code, {
69
+ ...prettierOptions,
69
70
  filepath: originalFileName
70
- }));
71
+ });
71
72
  return code;
72
73
  }
73
- async function removeTypes(code, fileName) {
74
- code = code.replaceAll(/\n\n+/g, "\n/* @detype: empty-line */\n");
74
+ async function removeTypes(code, fileName, options) {
75
+ code = code.replace(
76
+ /\n\n+/g,
77
+ (match) => `
78
+ /* @detype: empty-line=${match.length} */
79
+ `
80
+ );
75
81
  code = processMagicComments(code);
76
82
  const removeComments = {
77
83
  enter(p) {
@@ -103,15 +109,19 @@ async function removeTypes(code, fileName) {
103
109
  ].filter(Boolean),
104
110
  presets: [import_preset_typescript.default],
105
111
  generatorOpts: {
106
- shouldPrintComment: (comment) => comment !== "@detype: remove-me"
112
+ shouldPrintComment: (comment) => comment !== "@detype: remove-me" && (!options.removeTsComments || !comment.match(/^\s*(@ts-ignore|@ts-expect-error)/))
107
113
  }
108
114
  });
109
115
  if (!babelOutput || babelOutput.code === void 0 || babelOutput.code === null) {
110
116
  throw new Error("Babel error");
111
117
  }
112
- return babelOutput.code.replaceAll(/\n\n*/g, "\n").replaceAll("/* @detype: empty-line */", "\n\n");
118
+ return babelOutput.code.replaceAll(/\n\n*/g, "\n").replace(
119
+ /\/\* @detype: empty-line=([0-9]+) \*\//g,
120
+ (_match, p1) => `
121
+ `.repeat(p1 - 2)
122
+ );
113
123
  }
114
- async function removeTypesFromVueSfcScript(code, fileName, script, templateAst) {
124
+ async function removeTypesFromVueSfcScript(code, fileName, script, templateAst, options) {
115
125
  if (script === null || script.lang !== "ts")
116
126
  return code;
117
127
  if (script.setup && templateAst) {
@@ -127,8 +137,10 @@ async function removeTypesFromVueSfcScript(code, fileName, script, templateAst)
127
137
  });
128
138
  script.content += "/* @detype: remove-after-this */" + [...expressions].join(";");
129
139
  }
130
- let scriptCode = await removeTypes(script.content, fileName + ".ts");
131
- const removeAfterIndex = scriptCode.indexOf("/* @detype: remove-after-this */");
140
+ let scriptCode = await removeTypes(script.content, fileName + ".ts", options);
141
+ const removeAfterIndex = scriptCode.indexOf(
142
+ "/* @detype: remove-after-this */"
143
+ );
132
144
  if (removeAfterIndex >= 0) {
133
145
  scriptCode = scriptCode.slice(0, removeAfterIndex);
134
146
  }
@@ -186,19 +198,23 @@ function removeMagicComments(code, fileName, prettierOptions) {
186
198
  start = code.indexOf(REPLACE_COMMENT, before.length + keptText.length);
187
199
  startEnd = start + REPLACE_COMMENT.length;
188
200
  }
189
- code = (0, import_prettier.format)(code, __spreadProps(__spreadValues({}, prettierOptions), {
201
+ code = (0, import_prettier.format)(code, {
202
+ ...prettierOptions,
190
203
  filepath: fileName
191
- }));
204
+ });
192
205
  return code;
193
206
  }
194
207
 
195
208
  // src/transformFile.ts
196
209
  var import_prettier2 = require("prettier");
197
210
  var { readFile, writeFile } = import_fs.default.promises;
198
- async function transformFile(inputFileName, outputFileName) {
211
+ async function transformFile(inputFileName, outputFileName, options = {}) {
199
212
  const code = await readFile(inputFileName, "utf-8");
200
- const prettierConfig = await (0, import_prettier2.resolveConfig)(inputFileName);
201
- const output = await transform(code, inputFileName, prettierConfig);
213
+ const prettierOptions = await (0, import_prettier2.resolveConfig)(inputFileName);
214
+ const output = await transform(code, inputFileName, {
215
+ prettierOptions,
216
+ ...options
217
+ });
202
218
  await writeFile(outputFileName, output, "utf-8");
203
219
  }
204
220
  async function removeMagicCommentsFromFile(inputFileName, outputFileName) {
@@ -212,102 +228,111 @@ async function removeMagicCommentsFromFile(inputFileName, outputFileName) {
212
228
  var import_fast_glob = __toESM(require("fast-glob"));
213
229
 
214
230
  // package.json
215
- var name = "detype";
216
- var version = "0.5.1";
217
- var description = "Removes TypeScript type annotations but keeps the formatting";
218
- var main = "dist/index.js";
219
- var bin = "detype.js";
220
- var engines = {
221
- node: ">=14.19.3"
222
- };
223
- var scripts = {
224
- prepack: "rimraf dist && pnpm build",
225
- build: "tsup",
226
- dev: "tsup --watch",
227
- cq: "pnpm run test:typecheck && pnpm run test:lint && pnpm run test:prettier",
228
- test: "run-p 'test:*'",
229
- "test:unit": "vitest run",
230
- "test:typecheck": "tsc -p tsconfig.json --noEmit",
231
- "test:lint": "eslint src --max-warnings 0",
232
- "test:prettier": "prettier --check --ignore-path .gitignore --ignore-unknown . '!pnpm-lock.yaml'",
233
- format: "prettier . --write --ignore-path .gitignore --ignore-unknown . '!pnpm-lock.yaml'"
234
- };
235
- var files = [
236
- "dist/**/*",
237
- "index.d.ts"
238
- ];
239
- var dependencies = {
240
- "@babel/core": "^7.18.2",
241
- "@babel/preset-typescript": "^7.17.12",
242
- "@vuedx/compiler-sfc": "^0.7.1",
243
- "@vuedx/template-ast-types": "^0.7.3",
244
- "fast-glob": "^3.2.11",
245
- prettier: "^2.6.2",
246
- "string.prototype.replaceall": "^1.0.6"
247
- };
248
- var devDependencies = {
249
- "@babel/traverse": "^7.18.2",
250
- "@cyco130/eslint-config": "^1.1.0",
251
- "@types/babel__core": "^7.1.19",
252
- "@types/babel__traverse": "^7.17.1",
253
- "@types/node": "17.0.36",
254
- "@types/prettier": "^2.6.3",
255
- eslint: "^8.16.0",
256
- "npm-run-all": "^4.1.5",
257
- rimraf: "^3.0.2",
258
- tsup: "^6.0.1",
259
- typescript: "^4.7.2",
260
- vitest: "^0.13.0"
261
- };
262
- var repository = {
263
- type: "git",
264
- url: "git+https://github.com/cyco130/detype.git"
265
- };
266
- var keywords = [
267
- "typescript",
268
- "formatting",
269
- "vue",
270
- "sfc"
271
- ];
272
- var author = "Fatih Ayg\xFCn <cyco130@gmail.com>";
273
- var license = "MIT";
274
- var bugs = {
275
- url: "https://github.com/cyco130/detype/issues"
276
- };
277
- var homepage = "https://github.com/cyco130/detype#readme";
278
231
  var package_default = {
279
- name,
280
- version,
281
- description,
282
- main,
283
- bin,
284
- engines,
285
- scripts,
286
- files,
287
- dependencies,
288
- devDependencies,
289
- repository,
290
- keywords,
291
- author,
292
- license,
293
- bugs,
294
- homepage
232
+ name: "detype",
233
+ version: "0.6.1",
234
+ description: "Removes TypeScript type annotations but keeps the formatting",
235
+ main: "dist/index.js",
236
+ bin: "detype.js",
237
+ engines: {
238
+ node: ">=14.19.3"
239
+ },
240
+ scripts: {
241
+ prepack: "rimraf dist && pnpm build",
242
+ build: "tsup",
243
+ dev: "tsup --watch",
244
+ cq: "pnpm run test:typecheck && pnpm run test:lint && pnpm run test:prettier",
245
+ test: "run-p 'test:*'",
246
+ "test:unit": "vitest run",
247
+ "test:typecheck": "tsc -p tsconfig.json --noEmit",
248
+ "test:lint": "eslint src --max-warnings 0",
249
+ "test:prettier": "prettier --check --ignore-path .gitignore --ignore-unknown . '!pnpm-lock.yaml'",
250
+ format: "prettier . --write --ignore-path .gitignore --ignore-unknown . '!pnpm-lock.yaml'"
251
+ },
252
+ files: [
253
+ "dist/**/*",
254
+ "index.d.ts"
255
+ ],
256
+ dependencies: {
257
+ "@babel/core": "^7.19.6",
258
+ "@babel/preset-typescript": "^7.18.6",
259
+ "@vuedx/compiler-sfc": "^0.7.1",
260
+ "@vuedx/template-ast-types": "^0.7.3",
261
+ "fast-glob": "^3.2.12",
262
+ prettier: "^2.7.1",
263
+ "string.prototype.replaceall": "^1.0.6"
264
+ },
265
+ devDependencies: {
266
+ "@babel/traverse": "^7.19.6",
267
+ "@cyco130/eslint-config": "^2.1.2",
268
+ "@types/babel__core": "^7.1.19",
269
+ "@types/babel__traverse": "^7.18.2",
270
+ "@types/node": "18.11.4",
271
+ "@types/prettier": "^2.7.1",
272
+ eslint: "^8.26.0",
273
+ "npm-run-all": "^4.1.5",
274
+ rimraf: "^3.0.2",
275
+ tsup: "^6.3.0",
276
+ typescript: "^4.8.4",
277
+ vitest: "^0.24.3"
278
+ },
279
+ repository: {
280
+ type: "git",
281
+ url: "git+https://github.com/cyco130/detype.git"
282
+ },
283
+ keywords: [
284
+ "typescript",
285
+ "formatting",
286
+ "vue",
287
+ "sfc"
288
+ ],
289
+ author: "Fatih Ayg\xFCn <cyco130@gmail.com>",
290
+ license: "MIT",
291
+ bugs: {
292
+ url: "https://github.com/cyco130/detype/issues"
293
+ },
294
+ homepage: "https://github.com/cyco130/detype#readme"
295
295
  };
296
296
 
297
297
  // src/cli-lib.ts
298
298
  var { stat, mkdir } = import_fs2.default.promises;
299
299
  async function cli(...args2) {
300
- const [flag] = args2;
301
- let [, input, output] = args2;
302
- if (!flag || flag === "-h" || flag === "--help") {
300
+ let dashDash = false;
301
+ const params = [];
302
+ const flags = [];
303
+ for (const arg of args2) {
304
+ if (arg === "--") {
305
+ dashDash = true;
306
+ } else if (dashDash || !arg.startsWith("-")) {
307
+ params.push(arg);
308
+ } else {
309
+ flags.push(arg);
310
+ }
311
+ }
312
+ if (params.length > 2) {
313
+ console.error("Too many arguments");
314
+ return false;
315
+ }
316
+ let [input, output] = params;
317
+ if (params.length === 0 || flags.some((flag) => flag === "-h" || flag === "--help")) {
303
318
  printUsage();
304
- return !!flag;
319
+ return params.length > 0;
305
320
  }
306
- if (flag === "-v" || flag === "--version") {
321
+ if (flags.some((flag) => flag === "-v" || flag === "--version")) {
307
322
  console.log(VERSION);
308
323
  return true;
309
324
  }
310
- const removeMagic = flag === "-m" || flag === "--remove-magic-comments";
325
+ const removeMagic = flags.some(
326
+ (flag) => flag === "-m" || flag === "--remove-magic-comments"
327
+ );
328
+ const removeTsComments = flags.some(
329
+ (flag) => flag === "-t" || flag === "--remove-ts-comments"
330
+ );
331
+ if (removeMagic && removeTsComments) {
332
+ console.warn(
333
+ "--remove-ts-comments has no effect when --remove-magic-comments is used"
334
+ );
335
+ }
311
336
  if (!removeMagic) {
312
337
  [input, output] = args2;
313
338
  }
@@ -318,8 +343,10 @@ async function cli(...args2) {
318
343
  printUsage();
319
344
  return false;
320
345
  }
321
- const files2 = (await (0, import_fast_glob.default)(unixify(input + "/**/*.{ts,tsx,vue}"))).filter((file) => !file.endsWith(".d.ts"));
322
- const dirs = [...new Set(files2.map((file) => import_path.default.dirname(file)))].sort();
346
+ const files = (await (0, import_fast_glob.default)(unixify(input + "/**/*.{ts,tsx,vue}"))).filter(
347
+ (file) => !file.endsWith(".d.ts")
348
+ );
349
+ const dirs = [...new Set(files.map((file) => import_path.default.dirname(file)))].sort();
323
350
  await mkdir(import_path.default.normalize(output), { recursive: true });
324
351
  for (const dir of dirs) {
325
352
  const outDir = import_path.default.join(output, import_path.default.relative(input, dir));
@@ -327,10 +354,17 @@ async function cli(...args2) {
327
354
  continue;
328
355
  await mkdir(import_path.default.normalize(outDir), { recursive: true });
329
356
  }
330
- for (const file of files2) {
357
+ for (const file of files) {
331
358
  const inputDir = import_path.default.dirname(import_path.default.relative(input, file));
332
359
  const outputName = inferName(file, import_path.default.join(output, inputDir));
333
- removeMagic ? await removeMagicCommentsFromFile(import_path.default.normalize(file), import_path.default.normalize(outputName)) : await transformFile(import_path.default.normalize(file), import_path.default.normalize(outputName));
360
+ removeMagic ? await removeMagicCommentsFromFile(
361
+ import_path.default.normalize(file),
362
+ import_path.default.normalize(outputName)
363
+ ) : await transformFile(
364
+ import_path.default.normalize(file),
365
+ import_path.default.normalize(outputName),
366
+ { removeTsComments }
367
+ );
334
368
  }
335
369
  return true;
336
370
  }
@@ -346,7 +380,9 @@ async function cli(...args2) {
346
380
  }
347
381
  } else {
348
382
  if (removeMagic) {
349
- console.error("Output file name is required when removing magic comments");
383
+ console.error(
384
+ "Output file name is required when removing magic comments"
385
+ );
350
386
  return false;
351
387
  }
352
388
  if (input.endsWith(".vue")) {
@@ -359,19 +395,24 @@ async function cli(...args2) {
359
395
  if (outputDir) {
360
396
  await mkdir(import_path.default.normalize(outputDir), { recursive: true });
361
397
  }
362
- removeMagic ? await removeMagicCommentsFromFile(import_path.default.normalize(input), import_path.default.normalize(output)) : await transformFile(import_path.default.normalize(input), import_path.default.normalize(output));
398
+ removeMagic ? await removeMagicCommentsFromFile(
399
+ import_path.default.normalize(input),
400
+ import_path.default.normalize(output)
401
+ ) : await transformFile(import_path.default.normalize(input), import_path.default.normalize(output), {
402
+ removeTsComments
403
+ });
363
404
  return true;
364
405
  function inferName(input2, outputDir2) {
365
406
  let output2;
366
- const { dir, name: name2, ext } = import_path.default.parse(input2);
407
+ const { dir, name, ext } = import_path.default.parse(input2);
367
408
  if (removeMagic) {
368
- output2 = import_path.default.join(outputDir2 ?? dir, `${name2}${ext}`);
409
+ output2 = import_path.default.join(outputDir2 ?? dir, `${name}${ext}`);
369
410
  } else if (ext === ".ts") {
370
- output2 = import_path.default.join(outputDir2 ?? dir, name2 + ".js");
411
+ output2 = import_path.default.join(outputDir2 ?? dir, name + ".js");
371
412
  } else if (ext === ".tsx") {
372
- output2 = import_path.default.join(outputDir2 ?? dir, name2 + ".jsx");
413
+ output2 = import_path.default.join(outputDir2 ?? dir, name + ".jsx");
373
414
  } else if (ext === ".vue") {
374
- output2 = import_path.default.join(outputDir2 ?? dir, name2 + ".vue");
415
+ output2 = import_path.default.join(outputDir2 ?? dir, name + ".vue");
375
416
  } else {
376
417
  throw new Error(`Unknwon file extension ${input2}`);
377
418
  }
@@ -388,7 +429,10 @@ var USAGE = `Usage:
388
429
  INPUT Input file or directory
389
430
 
390
431
  OUTPUT Output file or directory
391
- (optional if it can be inferred and won't it overwrite the source file)
432
+ (optional if it can be inferred and it won't overwrite the source file)
433
+
434
+ -t, --remove-ts-comments
435
+ Remove @ts-ignore and @ts-expect-error comments
392
436
 
393
437
  -m, --remove-magic-comments
394
438
  Remove magic comments only, don't perform ts > js transform
@@ -401,8 +445,8 @@ var USAGE = `Usage:
401
445
 
402
446
  Print this help and exit`;
403
447
  var VERSION = package_default.version;
404
- function unixify(name2) {
405
- return name2.replaceAll(import_path.default.sep, "/");
448
+ function unixify(name) {
449
+ return name.replaceAll(import_path.default.sep, "/");
406
450
  }
407
451
 
408
452
  // src/cli.ts
package/dist/index.d.ts CHANGED
@@ -1,13 +1,21 @@
1
1
  import { Options } from 'prettier';
2
2
  export { Options as PrettierOptions } from 'prettier';
3
3
 
4
+ interface RemoveTypeOptions {
5
+ /** Whether to remove ts-ignore and ts-expect-error comments */
6
+ removeTsComments?: boolean;
7
+ }
8
+ interface TransformOptions extends RemoveTypeOptions {
9
+ /** Prettier options */
10
+ prettierOptions?: Options | null;
11
+ }
4
12
  /**
5
13
  * Transform TypeScript code into vanilla JavaScript without affecting the formatting
6
14
  * @param code Source coude
7
15
  * @param fileName File name for the source
8
- * @param prettierOptions Options to pass to prettier
16
+ * @param options Options
9
17
  */
10
- declare function transform(code: string, fileName: string, prettierOptions?: Options | null): Promise<string>;
18
+ declare function transform(code: string, fileName: string, options?: TransformOptions): Promise<string>;
11
19
  /**
12
20
  * Removes magic comments without performing the TS to JS transform
13
21
  * @param code Source coude
@@ -21,7 +29,7 @@ declare function removeMagicComments(code: string, fileName: string, prettierOpt
21
29
  * @param inputFileName
22
30
  * @param outputFileName
23
31
  */
24
- declare function transformFile(inputFileName: string, outputFileName: string): Promise<void>;
32
+ declare function transformFile(inputFileName: string, outputFileName: string, options?: RemoveTypeOptions): Promise<void>;
25
33
  /**
26
34
  * Remove magic comments from the input file and write the output to another file
27
35
  * @param inputFileName
package/dist/index.js CHANGED
@@ -1,26 +1,10 @@
1
+ "use strict";
1
2
  var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
- var __defProps = Object.defineProperties;
4
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
6
  var __getProtoOf = Object.getPrototypeOf;
9
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
10
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
11
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
- var __spreadValues = (a, b) => {
13
- for (var prop in b || (b = {}))
14
- if (__hasOwnProp.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- if (__getOwnPropSymbols)
17
- for (var prop of __getOwnPropSymbols(b)) {
18
- if (__propIsEnum.call(b, prop))
19
- __defNormalProp(a, prop, b[prop]);
20
- }
21
- return a;
22
- };
23
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
24
8
  var __export = (target, all) => {
25
9
  for (var name in all)
26
10
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -33,7 +17,10 @@ var __copyProps = (to, from, except, desc) => {
33
17
  }
34
18
  return to;
35
19
  };
36
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
37
24
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
38
25
 
39
26
  // src/index.ts
@@ -54,8 +41,9 @@ var import_template_ast_types = require("@vuedx/template-ast-types");
54
41
  var import_preset_typescript = __toESM(require("@babel/preset-typescript"));
55
42
  var import_string_prototype = require("string.prototype.replaceall");
56
43
  (0, import_string_prototype.shim)();
57
- async function transform(code, fileName, prettierOptions) {
44
+ async function transform(code, fileName, options = {}) {
58
45
  var _a, _b, _c, _d;
46
+ const { prettierOptions, ...removeTypeOptions } = options;
59
47
  const originalCode = code;
60
48
  const originalFileName = fileName;
61
49
  code = code.replaceAll("\r\n", "\n");
@@ -68,18 +56,36 @@ async function transform(code, fileName, prettierOptions) {
68
56
  if (script1 && script2 && script1.loc.start.offset < script2.loc.start.offset) {
69
57
  [script2, script1] = [script1, script2];
70
58
  }
71
- code = await removeTypesFromVueSfcScript(code, fileName, script1, (_c = parsedVue.descriptor.template) == null ? void 0 : _c.ast);
72
- code = await removeTypesFromVueSfcScript(code, fileName, script2, (_d = parsedVue.descriptor.template) == null ? void 0 : _d.ast);
59
+ code = await removeTypesFromVueSfcScript(
60
+ code,
61
+ fileName,
62
+ script1,
63
+ (_c = parsedVue.descriptor.template) == null ? void 0 : _c.ast,
64
+ removeTypeOptions
65
+ );
66
+ code = await removeTypesFromVueSfcScript(
67
+ code,
68
+ fileName,
69
+ script2,
70
+ (_d = parsedVue.descriptor.template) == null ? void 0 : _d.ast,
71
+ removeTypeOptions
72
+ );
73
73
  } else {
74
- code = await removeTypes(code, fileName);
74
+ code = await removeTypes(code, fileName, removeTypeOptions);
75
75
  }
76
- code = (0, import_prettier.format)(code, __spreadProps(__spreadValues({}, prettierOptions), {
76
+ code = (0, import_prettier.format)(code, {
77
+ ...prettierOptions,
77
78
  filepath: originalFileName
78
- }));
79
+ });
79
80
  return code;
80
81
  }
81
- async function removeTypes(code, fileName) {
82
- code = code.replaceAll(/\n\n+/g, "\n/* @detype: empty-line */\n");
82
+ async function removeTypes(code, fileName, options) {
83
+ code = code.replace(
84
+ /\n\n+/g,
85
+ (match) => `
86
+ /* @detype: empty-line=${match.length} */
87
+ `
88
+ );
83
89
  code = processMagicComments(code);
84
90
  const removeComments = {
85
91
  enter(p) {
@@ -111,15 +117,19 @@ async function removeTypes(code, fileName) {
111
117
  ].filter(Boolean),
112
118
  presets: [import_preset_typescript.default],
113
119
  generatorOpts: {
114
- shouldPrintComment: (comment) => comment !== "@detype: remove-me"
120
+ shouldPrintComment: (comment) => comment !== "@detype: remove-me" && (!options.removeTsComments || !comment.match(/^\s*(@ts-ignore|@ts-expect-error)/))
115
121
  }
116
122
  });
117
123
  if (!babelOutput || babelOutput.code === void 0 || babelOutput.code === null) {
118
124
  throw new Error("Babel error");
119
125
  }
120
- return babelOutput.code.replaceAll(/\n\n*/g, "\n").replaceAll("/* @detype: empty-line */", "\n\n");
126
+ return babelOutput.code.replaceAll(/\n\n*/g, "\n").replace(
127
+ /\/\* @detype: empty-line=([0-9]+) \*\//g,
128
+ (_match, p1) => `
129
+ `.repeat(p1 - 2)
130
+ );
121
131
  }
122
- async function removeTypesFromVueSfcScript(code, fileName, script, templateAst) {
132
+ async function removeTypesFromVueSfcScript(code, fileName, script, templateAst, options) {
123
133
  if (script === null || script.lang !== "ts")
124
134
  return code;
125
135
  if (script.setup && templateAst) {
@@ -135,8 +145,10 @@ async function removeTypesFromVueSfcScript(code, fileName, script, templateAst)
135
145
  });
136
146
  script.content += "/* @detype: remove-after-this */" + [...expressions].join(";");
137
147
  }
138
- let scriptCode = await removeTypes(script.content, fileName + ".ts");
139
- const removeAfterIndex = scriptCode.indexOf("/* @detype: remove-after-this */");
148
+ let scriptCode = await removeTypes(script.content, fileName + ".ts", options);
149
+ const removeAfterIndex = scriptCode.indexOf(
150
+ "/* @detype: remove-after-this */"
151
+ );
140
152
  if (removeAfterIndex >= 0) {
141
153
  scriptCode = scriptCode.slice(0, removeAfterIndex);
142
154
  }
@@ -194,9 +206,10 @@ function removeMagicComments(code, fileName, prettierOptions) {
194
206
  start = code.indexOf(REPLACE_COMMENT, before.length + keptText.length);
195
207
  startEnd = start + REPLACE_COMMENT.length;
196
208
  }
197
- code = (0, import_prettier.format)(code, __spreadProps(__spreadValues({}, prettierOptions), {
209
+ code = (0, import_prettier.format)(code, {
210
+ ...prettierOptions,
198
211
  filepath: fileName
199
- }));
212
+ });
200
213
  return code;
201
214
  }
202
215
 
@@ -204,10 +217,13 @@ function removeMagicComments(code, fileName, prettierOptions) {
204
217
  var import_fs = __toESM(require("fs"));
205
218
  var import_prettier2 = require("prettier");
206
219
  var { readFile, writeFile } = import_fs.default.promises;
207
- async function transformFile(inputFileName, outputFileName) {
220
+ async function transformFile(inputFileName, outputFileName, options = {}) {
208
221
  const code = await readFile(inputFileName, "utf-8");
209
- const prettierConfig = await (0, import_prettier2.resolveConfig)(inputFileName);
210
- const output = await transform(code, inputFileName, prettierConfig);
222
+ const prettierOptions = await (0, import_prettier2.resolveConfig)(inputFileName);
223
+ const output = await transform(code, inputFileName, {
224
+ prettierOptions,
225
+ ...options
226
+ });
211
227
  await writeFile(outputFileName, output, "utf-8");
212
228
  }
213
229
  async function removeMagicCommentsFromFile(inputFileName, outputFileName) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "detype",
3
- "version": "0.5.1",
3
+ "version": "0.6.1",
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,27 @@
12
12
  "index.d.ts"
13
13
  ],
14
14
  "dependencies": {
15
- "@babel/core": "^7.18.2",
16
- "@babel/preset-typescript": "^7.17.12",
15
+ "@babel/core": "^7.19.6",
16
+ "@babel/preset-typescript": "^7.18.6",
17
17
  "@vuedx/compiler-sfc": "^0.7.1",
18
18
  "@vuedx/template-ast-types": "^0.7.3",
19
- "fast-glob": "^3.2.11",
20
- "prettier": "^2.6.2",
19
+ "fast-glob": "^3.2.12",
20
+ "prettier": "^2.7.1",
21
21
  "string.prototype.replaceall": "^1.0.6"
22
22
  },
23
23
  "devDependencies": {
24
- "@babel/traverse": "^7.18.2",
25
- "@cyco130/eslint-config": "^1.1.0",
24
+ "@babel/traverse": "^7.19.6",
25
+ "@cyco130/eslint-config": "^2.1.2",
26
26
  "@types/babel__core": "^7.1.19",
27
- "@types/babel__traverse": "^7.17.1",
28
- "@types/node": "17.0.36",
29
- "@types/prettier": "^2.6.3",
30
- "eslint": "^8.16.0",
27
+ "@types/babel__traverse": "^7.18.2",
28
+ "@types/node": "18.11.4",
29
+ "@types/prettier": "^2.7.1",
30
+ "eslint": "^8.26.0",
31
31
  "npm-run-all": "^4.1.5",
32
32
  "rimraf": "^3.0.2",
33
- "tsup": "^6.0.1",
34
- "typescript": "^4.7.2",
35
- "vitest": "^0.13.0"
33
+ "tsup": "^6.3.0",
34
+ "typescript": "^4.8.4",
35
+ "vitest": "^0.24.3"
36
36
  },
37
37
  "repository": {
38
38
  "type": "git",