detype 0.4.2 → 0.5.0

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
@@ -22,13 +22,13 @@ let x: string;
22
22
  // This comment should be deleted
23
23
  // Ditto for this
24
24
  interface Foo {
25
- // This should go too
26
- bar: number;
25
+ // This should go too
26
+ bar: number;
27
27
  }
28
28
 
29
29
  // This comment should also be kept
30
30
  export function bar(foo: Foo): Date {
31
- return new Date();
31
+ return new Date();
32
32
  }
33
33
  ```
34
34
 
@@ -41,7 +41,7 @@ let x;
41
41
 
42
42
  // This comment should also be kept
43
43
  export function bar(foo) {
44
- return new Date();
44
+ return new Date();
45
45
  }
46
46
  ```
47
47
 
@@ -85,7 +85,7 @@ console.log("Hello from TypeScript");
85
85
 
86
86
  ## System requirements
87
87
 
88
- `detype` requires Node version 12.22.7 or later.
88
+ `detype` requires Node version 14.19.3 or later.
89
89
 
90
90
  ## CLI Usage
91
91
 
@@ -114,52 +114,63 @@ detype [-h | --help]
114
114
  ```ts
115
115
  // Transform TypeScript code into vanilla JavaScript without affecting the formatting
116
116
  function transform(
117
- // Source code
118
- code: string,
119
- // File name for the source
120
- fileName: string,
121
- // Options to pass to prettier
122
- prettierOptions?: PrettierOptions | null,
117
+ // Source code
118
+ code: string,
119
+ // File name for the source
120
+ fileName: string,
121
+ // Options to pass to prettier
122
+ prettierOptions?: PrettierOptions | null,
123
123
  ): Promise<string>;
124
124
 
125
125
  // Transform the input file and write the output to another file
126
126
  function transformFile(
127
- inputFileName: string,
128
- outputFileName: string,
127
+ inputFileName: string,
128
+ outputFileName: string,
129
129
  ): Promise<void>;
130
130
 
131
131
  // Remove magic comments without performing the TS to JS transform
132
132
  export function removeMagicComments(
133
- // Source code
134
- code: string,
135
- // File name for the source
136
- fileName: string,
137
- // Options to pass to prettier
138
- prettierOptions?: PrettierOptions | null,
133
+ // Source code
134
+ code: string,
135
+ // File name for the source
136
+ fileName: string,
137
+ // Options to pass to prettier
138
+ prettierOptions?: PrettierOptions | null,
139
139
  ): string;
140
140
 
141
141
  // Remove magic comments from the input file and write the output to another file
142
142
  export function removeMagicCommentsFromFile(
143
- inputFileName: string,
144
- outputFileName: string,
143
+ inputFileName: string,
144
+ outputFileName: string,
145
145
  ): Promise<void>;
146
146
  ```
147
147
 
148
148
  ## Change log
149
+
150
+ ### 0.5
151
+
152
+ - BREAKING CHANGE: Drop support for Node 12
153
+ - chore: Set up CI workflows
154
+
149
155
  ### 0.4
156
+
150
157
  - feature: CLI support for removing magic comments
151
158
  - chore: Improve documentation
152
159
 
153
160
  ### 0.3
161
+
154
162
  - feature: Magic comments
155
163
  - feature: Expose type declarations
156
164
  - fix: Better empty line handling
157
165
 
158
166
  ### 0.2
167
+
159
168
  - feature: for Vue single file components
160
169
 
161
170
  ### 0.1
171
+
162
172
  - Initial release
163
173
 
164
174
  ## Credits
165
- Fatih Aygün, under MIT License
175
+
176
+ Fatih Aygün, under MIT License
package/dist/cli.js CHANGED
@@ -212,56 +212,51 @@ var import_fast_glob = __toESM(require("fast-glob"));
212
212
 
213
213
  // package.json
214
214
  var name = "detype";
215
- var version = "0.4.2";
215
+ var version = "0.5.0";
216
216
  var description = "Removes TypeScript type annotations but keeps the formatting";
217
217
  var main = "dist/index.js";
218
218
  var bin = "detype.js";
219
219
  var engines = {
220
- node: ">=12.22.7"
220
+ node: ">=14.19.3"
221
221
  };
222
222
  var scripts = {
223
- prepublishOnly: "pnpm build",
224
- build: "node build.mjs",
225
- watch: "node build.mjs --watch",
223
+ prepack: "rimraf dist && pnpm build",
224
+ build: "tsup",
225
+ dev: "tsup --watch",
226
+ cq: "pnpm run test:typecheck && pnpm run test:lint && pnpm run test:prettier",
226
227
  test: "run-p 'test:*'",
227
- "test:unit": "jest",
228
+ "test:unit": "vitest run",
228
229
  "test:typecheck": "tsc -p tsconfig.json --noEmit",
229
230
  "test:lint": "eslint src --max-warnings 0",
230
- format: "prettier . --write"
231
+ "test:prettier": "prettier --check --ignore-path .gitignore --ignore-unknown . '!pnpm-lock.yaml'",
232
+ format: "prettier . --write --ignore-path .gitignore --ignore-unknown . '!pnpm-lock.yaml'"
231
233
  };
232
234
  var files = [
233
235
  "dist/**/*",
234
236
  "index.d.ts"
235
237
  ];
236
238
  var dependencies = {
237
- "@babel/core": "^7.17.8",
238
- "@babel/preset-typescript": "^7.16.7",
239
+ "@babel/core": "^7.18.2",
240
+ "@babel/preset-typescript": "^7.17.12",
239
241
  "@vuedx/compiler-sfc": "^0.7.1",
240
- "@vuedx/template-ast-types": "^0.7.2",
242
+ "@vuedx/template-ast-types": "^0.7.3",
241
243
  "fast-glob": "^3.2.11",
242
244
  prettier: "^2.6.2",
243
245
  "string.prototype.replaceall": "^1.0.6"
244
246
  };
245
247
  var devDependencies = {
246
- "@babel/traverse": "^7.17.3",
247
- "@types/jest": "^27.4.1",
248
- "@types/node": "17.0.23",
249
- "@typescript-eslint/eslint-plugin": "^5.17.0",
250
- "@typescript-eslint/parser": "^5.17.0",
251
- esbuild: "^0.14.31",
252
- "esbuild-jest": "^0.5.0",
253
- "esbuild-node-externals": "^1.4.1",
254
- eslint: "^8.12.0",
255
- "eslint-config-prettier": "^8.5.0",
256
- "eslint-import-resolver-typescript": "^2.7.1",
257
- "eslint-plugin-import": "^2.25.4",
258
- "eslint-plugin-no-only-tests": "^2.6.0",
259
- "eslint-plugin-only-warn": "^1.0.3",
260
- "eslint-plugin-ssr-friendly": "^1.0.6",
261
- jest: "^27.5.1",
248
+ "@babel/traverse": "^7.18.2",
249
+ "@cyco130/eslint-config": "^1.1.0",
250
+ "@types/babel__core": "^7.1.19",
251
+ "@types/babel__traverse": "^7.17.1",
252
+ "@types/node": "17.0.36",
253
+ "@types/prettier": "^2.6.3",
254
+ eslint: "^8.16.0",
262
255
  "npm-run-all": "^4.1.5",
263
256
  rimraf: "^3.0.2",
264
- typescript: "^4.6.3"
257
+ tsup: "^6.0.1",
258
+ typescript: "^4.7.2",
259
+ vitest: "^0.13.0"
265
260
  };
266
261
  var repository = {
267
262
  type: "git",
@@ -301,7 +296,8 @@ var package_default = {
301
296
  // src/cli-lib.ts
302
297
  var { stat, mkdir } = import_fs2.default.promises;
303
298
  async function cli(...args2) {
304
- let [flag, input, output] = args2;
299
+ const [flag] = args2;
300
+ let [, input, output] = args2;
305
301
  if (!flag || flag === "-h" || flag === "--help") {
306
302
  printUsage();
307
303
  return !!flag;
@@ -321,19 +317,19 @@ async function cli(...args2) {
321
317
  printUsage();
322
318
  return false;
323
319
  }
324
- const files2 = (await (0, import_fast_glob.default)(import_path.default.join(input, "**/*.{ts,tsx,vue}"))).filter((file) => !file.endsWith(".d.ts"));
320
+ const files2 = (await (0, import_fast_glob.default)(unixify(input + "/**/*.{ts,tsx,vue}"))).filter((file) => !file.endsWith(".d.ts"));
325
321
  const dirs = [...new Set(files2.map((file) => import_path.default.dirname(file)))].sort();
326
- await mkdir(output, { recursive: true });
322
+ await mkdir(import_path.default.normalize(output), { recursive: true });
327
323
  for (const dir of dirs) {
328
324
  const outDir = import_path.default.join(output, import_path.default.relative(input, dir));
329
325
  if (outDir === output)
330
326
  continue;
331
- await mkdir(outDir, { recursive: true });
327
+ await mkdir(import_path.default.normalize(outDir), { recursive: true });
332
328
  }
333
329
  for (const file of files2) {
334
330
  const inputDir = import_path.default.dirname(import_path.default.relative(input, file));
335
331
  const outputName = inferName(file, import_path.default.join(output, inputDir));
336
- removeMagic ? await removeMagicCommentsFromFile(file, outputName) : await transformFile(file, outputName);
332
+ 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));
337
333
  }
338
334
  return true;
339
335
  }
@@ -360,21 +356,21 @@ async function cli(...args2) {
360
356
  }
361
357
  const outputDir = import_path.default.dirname(output);
362
358
  if (outputDir) {
363
- await mkdir(outputDir, { recursive: true });
359
+ await mkdir(import_path.default.normalize(outputDir), { recursive: true });
364
360
  }
365
- removeMagic ? await removeMagicCommentsFromFile(input, output) : await transformFile(input, output);
361
+ 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));
366
362
  return true;
367
363
  function inferName(input2, outputDir2) {
368
364
  let output2;
369
365
  const { dir, name: name2, ext } = import_path.default.parse(input2);
370
366
  if (removeMagic) {
371
- output2 = import_path.default.join(outputDir2 != null ? outputDir2 : dir, `${name2}${ext}`);
367
+ output2 = import_path.default.join(outputDir2 ?? dir, `${name2}${ext}`);
372
368
  } else if (ext === ".ts") {
373
- output2 = import_path.default.join(outputDir2 != null ? outputDir2 : dir, name2 + ".js");
369
+ output2 = import_path.default.join(outputDir2 ?? dir, name2 + ".js");
374
370
  } else if (ext === ".tsx") {
375
- output2 = import_path.default.join(outputDir2 != null ? outputDir2 : dir, name2 + ".jsx");
371
+ output2 = import_path.default.join(outputDir2 ?? dir, name2 + ".jsx");
376
372
  } else if (ext === ".vue") {
377
- output2 = import_path.default.join(outputDir2 != null ? outputDir2 : dir, name2 + ".vue");
373
+ output2 = import_path.default.join(outputDir2 ?? dir, name2 + ".vue");
378
374
  } else {
379
375
  throw new Error(`Unknwon file extension ${input2}`);
380
376
  }
@@ -404,6 +400,9 @@ var USAGE = `Usage:
404
400
 
405
401
  Print this help and exit`;
406
402
  var VERSION = package_default.version;
403
+ function unixify(name2) {
404
+ return name2.replaceAll(import_path.default.sep, "/");
405
+ }
407
406
 
408
407
  // src/cli.ts
409
408
  var args = process.argv.slice(2);
@@ -0,0 +1,32 @@
1
+ import { Options } from 'prettier';
2
+ export { Options as PrettierOptions } from 'prettier';
3
+
4
+ /**
5
+ * Transform TypeScript code into vanilla JavaScript without affecting the formatting
6
+ * @param code Source coude
7
+ * @param fileName File name for the source
8
+ * @param prettierOptions Options to pass to prettier
9
+ */
10
+ declare function transform(code: string, fileName: string, prettierOptions?: Options | null): Promise<string>;
11
+ /**
12
+ * Removes magic comments without performing the TS to JS transform
13
+ * @param code Source coude
14
+ * @param fileName File name for the source
15
+ * @param prettierOptions Options to pass to prettier
16
+ */
17
+ declare function removeMagicComments(code: string, fileName: string, prettierOptions?: Options | null): string;
18
+
19
+ /**
20
+ * Transform the input file and write the output to another file
21
+ * @param inputFileName
22
+ * @param outputFileName
23
+ */
24
+ declare function transformFile(inputFileName: string, outputFileName: string): Promise<void>;
25
+ /**
26
+ * Remove magic comments from the input file and write the output to another file
27
+ * @param inputFileName
28
+ * @param outputFileName
29
+ */
30
+ declare function removeMagicCommentsFromFile(inputFileName: string, outputFileName: string): Promise<void>;
31
+
32
+ export { removeMagicComments, removeMagicCommentsFromFile, transform, transformFile };
package/index.d.ts CHANGED
@@ -1,47 +1 @@
1
- import { Options as PrettierOptions } from "prettier";
2
-
3
- export { PrettierOptions };
4
-
5
- /**
6
- * Transform TypeScript code into vanilla JavaScript without affecting the formatting
7
- * @param code Source coude
8
- * @param fileName File name for the source
9
- * @param prettierOptions Options to pass to prettier
10
- */
11
- export function transform(
12
- code: string,
13
- fileName: string,
14
- prettierOptions?: PrettierOptions | null | undefined,
15
- ): Promise<string>;
16
-
17
- /**
18
- * Transform the input file and write the output to another file
19
- * @param inputFileName
20
- * @param outputFileName
21
- */
22
- export function transformFile(
23
- inputFileName: string,
24
- outputFileName: string,
25
- ): Promise<void>;
26
-
27
- /**
28
- * Removes magic comments without performing the TS to JS transform
29
- * @param code Source coude
30
- * @param fileName File name for the source
31
- * @param prettierOptions Options to pass to prettier
32
- */
33
- export function removeMagicComments(
34
- code: string,
35
- fileName: string,
36
- prettierOptions?: PrettierOptions | null,
37
- ): string;
38
-
39
- /**
40
- * Remove magic comments from the input file and write the output to another file
41
- * @param inputFileName
42
- * @param outputFileName
43
- */
44
- export function removeMagicCommentsFromFile(
45
- inputFileName: string,
46
- outputFileName: string,
47
- ): Promise<void>;
1
+ export * from "./dist";
package/package.json CHANGED
@@ -1,45 +1,38 @@
1
1
  {
2
2
  "name": "detype",
3
- "version": "0.4.2",
3
+ "version": "0.5.0",
4
4
  "description": "Removes TypeScript type annotations but keeps the formatting",
5
5
  "main": "dist/index.js",
6
6
  "bin": "detype.js",
7
7
  "engines": {
8
- "node": ">=12.22.7"
8
+ "node": ">=14.19.3"
9
9
  },
10
10
  "files": [
11
11
  "dist/**/*",
12
12
  "index.d.ts"
13
13
  ],
14
14
  "dependencies": {
15
- "@babel/core": "^7.17.8",
16
- "@babel/preset-typescript": "^7.16.7",
15
+ "@babel/core": "^7.18.2",
16
+ "@babel/preset-typescript": "^7.17.12",
17
17
  "@vuedx/compiler-sfc": "^0.7.1",
18
- "@vuedx/template-ast-types": "^0.7.2",
18
+ "@vuedx/template-ast-types": "^0.7.3",
19
19
  "fast-glob": "^3.2.11",
20
20
  "prettier": "^2.6.2",
21
21
  "string.prototype.replaceall": "^1.0.6"
22
22
  },
23
23
  "devDependencies": {
24
- "@babel/traverse": "^7.17.3",
25
- "@types/jest": "^27.4.1",
26
- "@types/node": "17.0.23",
27
- "@typescript-eslint/eslint-plugin": "^5.17.0",
28
- "@typescript-eslint/parser": "^5.17.0",
29
- "esbuild": "^0.14.31",
30
- "esbuild-jest": "^0.5.0",
31
- "esbuild-node-externals": "^1.4.1",
32
- "eslint": "^8.12.0",
33
- "eslint-config-prettier": "^8.5.0",
34
- "eslint-import-resolver-typescript": "^2.7.1",
35
- "eslint-plugin-import": "^2.25.4",
36
- "eslint-plugin-no-only-tests": "^2.6.0",
37
- "eslint-plugin-only-warn": "^1.0.3",
38
- "eslint-plugin-ssr-friendly": "^1.0.6",
39
- "jest": "^27.5.1",
24
+ "@babel/traverse": "^7.18.2",
25
+ "@cyco130/eslint-config": "^1.1.0",
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",
40
31
  "npm-run-all": "^4.1.5",
41
32
  "rimraf": "^3.0.2",
42
- "typescript": "^4.6.3"
33
+ "tsup": "^6.0.1",
34
+ "typescript": "^4.7.2",
35
+ "vitest": "^0.13.0"
43
36
  },
44
37
  "repository": {
45
38
  "type": "git",
@@ -58,13 +51,14 @@
58
51
  },
59
52
  "homepage": "https://github.com/cyco130/detype#readme",
60
53
  "scripts": {
61
- "build": "node build.mjs",
62
- "watch": "node build.mjs --watch",
54
+ "build": "tsup",
55
+ "dev": "tsup --watch",
56
+ "cq": "pnpm run test:typecheck && pnpm run test:lint && pnpm run test:prettier",
63
57
  "test": "run-p 'test:*'",
64
- "test:unit": "jest",
58
+ "test:unit": "vitest run",
65
59
  "test:typecheck": "tsc -p tsconfig.json --noEmit",
66
60
  "test:lint": "eslint src --max-warnings 0",
67
- "format": "prettier . --write"
68
- },
69
- "readme": "# detype\n\n> Remove the types, keep the formatting\n\n```sh\nnpm i -g detype\n```\n\nSuppose you have a library that you want to provide usage examples for. **detype** can help you generate vanilla JavaScript samples from TypeScript samples automatically and remove the burden of maintaining two separate versions of what is essentially the same code.\n\nIt is a command line tool and a library that removes type annotations and other TypeScript specific syntax constructs and outputs vanilla JavaScript **without altering the source formatting** too much. It supports `.ts`, `.tsx`, as well as `.vue` files.\n\nIn other words, it turns this:\n\n```ts\nimport type { ParsedPath } from \"path\";\n\nlet x: string;\n\n// This comment should be kept\n\n// This comment should be deleted\n// Ditto for this\ninterface Foo {\n // This should go too\n bar: number;\n}\n\n// This comment should also be kept\nexport function bar(foo: Foo): Date {\n return new Date();\n}\n```\n\ninto this:\n\n```js\nlet x;\n\n// This comment should be kept\n\n// This comment should also be kept\nexport function bar(foo) {\n return new Date();\n}\n```\n\nThe output is very close to hand-written JavaScript, especially if you were already using Prettier for formatting.\n\n## Doesn't `tsc` already do that?\n\nThere are lots of tools for transpiling TypeScript into plain JavaScript (`tsc`, `babel`, `swc`, `esbuild`, `sucrase` etc.) but none of them is perfectly suitable for this specific use case. Most of them don't preserve the formatting at all. `sucrase` comes close, but it doesn't remove comments attached to TypeScript-only constructs.\n\n`detype` uses [Babel](https://babeljs.io/), a small Babel plugin to remove comments attached to TypeScript-only constructs, and [Prettier](https://prettier.io/) under the hood. For Vue files, it also uses the tools from the [VueDX project](https://github.com/vuedx/languagetools).\n\n## Magic comments\n\nSometimes you want the generated JavaScript to be slightly different than the TypeScript original. You can use the magic comments feature to achieve this:\n\nInput:\n\n```ts\n// @detype: replace\n// These two lines will be removed\nconsole.log(\"Hello from TypeScript\");\n// @detype: with\n// // Notice the double comments!\n// console.log(\"Hello from JavaScript\");\n// @detype: end\n```\n\nOutput:\n\n```js\n// Notice the double comments!\nconsole.log(\"Hello from JavaScript\");\n```\n\nIf you just want to remove the magic comments, you can use the `-m` CLI flag or the `removeMagicComments` function to generate uncluttered TypeScript like this:\n\n```ts\n// These two lines will be removed\nconsole.log(\"Hello from TypeScript\");\n```\n\n## System requirements\n\n`detype` requires Node version 12.22.7 or later.\n\n## CLI Usage\n\n```\ndetype [-m | --remove-magic-comments] <INPUT> [OUTPUT]\n\n INPUT Input file or directory\n\n OUTPUT Output file or directory\n (optional if it can be inferred and it won't overwrite the source file)\n\n -m, --remove-magic-comments\n Remove magic comments only, don't perform ts > js transform\n\ndetype [-v | --version]\n\n Print version and exit\n\ndetype [-h | --help]\n\n Print this help and exit\n```\n\n## Node API\n\n```ts\n// Transform TypeScript code into vanilla JavaScript without affecting the formatting\nfunction transform(\n // Source code\n code: string,\n // File name for the source\n fileName: string,\n // Options to pass to prettier\n prettierOptions?: PrettierOptions | null,\n): Promise<string>;\n\n// Transform the input file and write the output to another file\nfunction transformFile(\n inputFileName: string,\n outputFileName: string,\n): Promise<void>;\n\n// Remove magic comments without performing the TS to JS transform\nexport function removeMagicComments(\n // Source code\n code: string,\n // File name for the source\n fileName: string,\n // Options to pass to prettier\n prettierOptions?: PrettierOptions | null,\n): string;\n\n// Remove magic comments from the input file and write the output to another file\nexport function removeMagicCommentsFromFile(\n inputFileName: string,\n outputFileName: string,\n): Promise<void>;\n```\n\n## Change log\n### 0.4\n- feature: CLI support for removing magic comments\n- chore: Improve documentation\n\n### 0.3\n- feature: Magic comments\n- feature: Expose type declarations\n- fix: Better empty line handling\n\n### 0.2\n- feature: for Vue single file components\n\n### 0.1\n- Initial release\n\n## Credits\nFatih Aygün, under MIT License"
61
+ "test:prettier": "prettier --check --ignore-path .gitignore --ignore-unknown . '!pnpm-lock.yaml'",
62
+ "format": "prettier . --write --ignore-path .gitignore --ignore-unknown . '!pnpm-lock.yaml'"
63
+ }
70
64
  }