detype 0.4.1 → 0.4.2
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 +38 -41
- package/dist/index.js +20 -21
- package/package.json +21 -20
package/dist/cli.js
CHANGED
|
@@ -21,33 +21,30 @@ var __spreadValues = (a, b) => {
|
|
|
21
21
|
return a;
|
|
22
22
|
};
|
|
23
23
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
|
-
var
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
|
|
24
|
+
var __copyProps = (to, from, except, desc) => {
|
|
25
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
26
|
+
for (let key of __getOwnPropNames(from))
|
|
27
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
28
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
30
29
|
}
|
|
31
|
-
return
|
|
32
|
-
};
|
|
33
|
-
var __toModule = (module2) => {
|
|
34
|
-
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
|
|
30
|
+
return to;
|
|
35
31
|
};
|
|
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));
|
|
36
33
|
|
|
37
34
|
// src/cli-lib.ts
|
|
38
|
-
var import_fs2 =
|
|
39
|
-
var import_path =
|
|
35
|
+
var import_fs2 = __toESM(require("fs"));
|
|
36
|
+
var import_path = __toESM(require("path"));
|
|
40
37
|
|
|
41
38
|
// src/transformFile.ts
|
|
42
|
-
var import_fs =
|
|
39
|
+
var import_fs = __toESM(require("fs"));
|
|
43
40
|
|
|
44
41
|
// src/transform.ts
|
|
45
|
-
var import_core =
|
|
46
|
-
var import_prettier =
|
|
47
|
-
var import_compiler_sfc =
|
|
48
|
-
var import_template_ast_types =
|
|
49
|
-
var import_preset_typescript =
|
|
50
|
-
var import_string_prototype =
|
|
42
|
+
var import_core = require("@babel/core");
|
|
43
|
+
var import_prettier = require("prettier");
|
|
44
|
+
var import_compiler_sfc = require("@vuedx/compiler-sfc");
|
|
45
|
+
var import_template_ast_types = require("@vuedx/template-ast-types");
|
|
46
|
+
var import_preset_typescript = __toESM(require("@babel/preset-typescript"));
|
|
47
|
+
var import_string_prototype = require("string.prototype.replaceall");
|
|
51
48
|
(0, import_string_prototype.shim)();
|
|
52
49
|
async function transform(code, fileName, prettierOptions) {
|
|
53
50
|
var _a, _b, _c, _d;
|
|
@@ -117,7 +114,7 @@ async function removeTypesFromVueSfcScript(code, fileName, script, templateAst)
|
|
|
117
114
|
if (script === null || script.lang !== "ts")
|
|
118
115
|
return code;
|
|
119
116
|
if (script.setup && templateAst) {
|
|
120
|
-
const expressions = new Set();
|
|
117
|
+
const expressions = /* @__PURE__ */ new Set();
|
|
121
118
|
(0, import_template_ast_types.traverse)(templateAst, {
|
|
122
119
|
enter(node) {
|
|
123
120
|
if ((0, import_template_ast_types.isSimpleExpressionNode)(node) && !node.isStatic) {
|
|
@@ -195,7 +192,7 @@ function removeMagicComments(code, fileName, prettierOptions) {
|
|
|
195
192
|
}
|
|
196
193
|
|
|
197
194
|
// src/transformFile.ts
|
|
198
|
-
var import_prettier2 =
|
|
195
|
+
var import_prettier2 = require("prettier");
|
|
199
196
|
var { readFile, writeFile } = import_fs.default.promises;
|
|
200
197
|
async function transformFile(inputFileName, outputFileName) {
|
|
201
198
|
const code = await readFile(inputFileName, "utf-8");
|
|
@@ -211,11 +208,11 @@ async function removeMagicCommentsFromFile(inputFileName, outputFileName) {
|
|
|
211
208
|
}
|
|
212
209
|
|
|
213
210
|
// src/cli-lib.ts
|
|
214
|
-
var import_fast_glob =
|
|
211
|
+
var import_fast_glob = __toESM(require("fast-glob"));
|
|
215
212
|
|
|
216
213
|
// package.json
|
|
217
214
|
var name = "detype";
|
|
218
|
-
var version = "0.4.
|
|
215
|
+
var version = "0.4.2";
|
|
219
216
|
var description = "Removes TypeScript type annotations but keeps the formatting";
|
|
220
217
|
var main = "dist/index.js";
|
|
221
218
|
var bin = "detype.js";
|
|
@@ -237,34 +234,34 @@ var files = [
|
|
|
237
234
|
"index.d.ts"
|
|
238
235
|
];
|
|
239
236
|
var dependencies = {
|
|
240
|
-
"@babel/core": "^7.
|
|
241
|
-
"@babel/preset-typescript": "^7.
|
|
237
|
+
"@babel/core": "^7.17.8",
|
|
238
|
+
"@babel/preset-typescript": "^7.16.7",
|
|
242
239
|
"@vuedx/compiler-sfc": "^0.7.1",
|
|
243
240
|
"@vuedx/template-ast-types": "^0.7.2",
|
|
244
|
-
"fast-glob": "^3.2.
|
|
245
|
-
prettier: "^2.
|
|
241
|
+
"fast-glob": "^3.2.11",
|
|
242
|
+
prettier: "^2.6.2",
|
|
246
243
|
"string.prototype.replaceall": "^1.0.6"
|
|
247
244
|
};
|
|
248
245
|
var devDependencies = {
|
|
249
|
-
"@babel/traverse": "^7.
|
|
250
|
-
"@types/jest": "^27.
|
|
251
|
-
"@types/node": "
|
|
252
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
253
|
-
"@typescript-eslint/parser": "^
|
|
254
|
-
esbuild: "^0.
|
|
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",
|
|
255
252
|
"esbuild-jest": "^0.5.0",
|
|
256
|
-
"esbuild-node-externals": "^1.
|
|
257
|
-
eslint: "^
|
|
258
|
-
"eslint-config-prettier": "^8.
|
|
259
|
-
"eslint-import-resolver-typescript": "^2.
|
|
260
|
-
"eslint-plugin-import": "^2.
|
|
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",
|
|
261
258
|
"eslint-plugin-no-only-tests": "^2.6.0",
|
|
262
259
|
"eslint-plugin-only-warn": "^1.0.3",
|
|
263
|
-
"eslint-plugin-ssr-friendly": "^1.0.
|
|
264
|
-
jest: "^27.
|
|
260
|
+
"eslint-plugin-ssr-friendly": "^1.0.6",
|
|
261
|
+
jest: "^27.5.1",
|
|
265
262
|
"npm-run-all": "^4.1.5",
|
|
266
263
|
rimraf: "^3.0.2",
|
|
267
|
-
typescript: "^4.
|
|
264
|
+
typescript: "^4.6.3"
|
|
268
265
|
};
|
|
269
266
|
var repository = {
|
|
270
267
|
type: "git",
|
package/dist/index.js
CHANGED
|
@@ -21,39 +21,38 @@ var __spreadValues = (a, b) => {
|
|
|
21
21
|
return a;
|
|
22
22
|
};
|
|
23
23
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
|
-
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
|
|
25
24
|
var __export = (target, all) => {
|
|
26
|
-
__markAsModule(target);
|
|
27
25
|
for (var name in all)
|
|
28
26
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
29
27
|
};
|
|
30
|
-
var
|
|
31
|
-
if (
|
|
32
|
-
for (let key of __getOwnPropNames(
|
|
33
|
-
if (!__hasOwnProp.call(
|
|
34
|
-
__defProp(
|
|
28
|
+
var __copyProps = (to, from, except, desc) => {
|
|
29
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
30
|
+
for (let key of __getOwnPropNames(from))
|
|
31
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
32
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
35
33
|
}
|
|
36
|
-
return
|
|
37
|
-
};
|
|
38
|
-
var __toModule = (module2) => {
|
|
39
|
-
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
|
|
34
|
+
return to;
|
|
40
35
|
};
|
|
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));
|
|
37
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
41
38
|
|
|
42
39
|
// src/index.ts
|
|
43
|
-
|
|
40
|
+
var src_exports = {};
|
|
41
|
+
__export(src_exports, {
|
|
44
42
|
removeMagicComments: () => removeMagicComments,
|
|
45
43
|
removeMagicCommentsFromFile: () => removeMagicCommentsFromFile,
|
|
46
44
|
transform: () => transform,
|
|
47
45
|
transformFile: () => transformFile
|
|
48
46
|
});
|
|
47
|
+
module.exports = __toCommonJS(src_exports);
|
|
49
48
|
|
|
50
49
|
// src/transform.ts
|
|
51
|
-
var import_core =
|
|
52
|
-
var import_prettier =
|
|
53
|
-
var import_compiler_sfc =
|
|
54
|
-
var import_template_ast_types =
|
|
55
|
-
var import_preset_typescript =
|
|
56
|
-
var import_string_prototype =
|
|
50
|
+
var import_core = require("@babel/core");
|
|
51
|
+
var import_prettier = require("prettier");
|
|
52
|
+
var import_compiler_sfc = require("@vuedx/compiler-sfc");
|
|
53
|
+
var import_template_ast_types = require("@vuedx/template-ast-types");
|
|
54
|
+
var import_preset_typescript = __toESM(require("@babel/preset-typescript"));
|
|
55
|
+
var import_string_prototype = require("string.prototype.replaceall");
|
|
57
56
|
(0, import_string_prototype.shim)();
|
|
58
57
|
async function transform(code, fileName, prettierOptions) {
|
|
59
58
|
var _a, _b, _c, _d;
|
|
@@ -123,7 +122,7 @@ async function removeTypesFromVueSfcScript(code, fileName, script, templateAst)
|
|
|
123
122
|
if (script === null || script.lang !== "ts")
|
|
124
123
|
return code;
|
|
125
124
|
if (script.setup && templateAst) {
|
|
126
|
-
const expressions = new Set();
|
|
125
|
+
const expressions = /* @__PURE__ */ new Set();
|
|
127
126
|
(0, import_template_ast_types.traverse)(templateAst, {
|
|
128
127
|
enter(node) {
|
|
129
128
|
if ((0, import_template_ast_types.isSimpleExpressionNode)(node) && !node.isStatic) {
|
|
@@ -201,8 +200,8 @@ function removeMagicComments(code, fileName, prettierOptions) {
|
|
|
201
200
|
}
|
|
202
201
|
|
|
203
202
|
// src/transformFile.ts
|
|
204
|
-
var import_fs =
|
|
205
|
-
var import_prettier2 =
|
|
203
|
+
var import_fs = __toESM(require("fs"));
|
|
204
|
+
var import_prettier2 = require("prettier");
|
|
206
205
|
var { readFile, writeFile } = import_fs.default.promises;
|
|
207
206
|
async function transformFile(inputFileName, outputFileName) {
|
|
208
207
|
const code = await readFile(inputFileName, "utf-8");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "detype",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"description": "Removes TypeScript type annotations but keeps the formatting",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"bin": "detype.js",
|
|
@@ -12,34 +12,34 @@
|
|
|
12
12
|
"index.d.ts"
|
|
13
13
|
],
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@babel/core": "^7.
|
|
16
|
-
"@babel/preset-typescript": "^7.
|
|
15
|
+
"@babel/core": "^7.17.8",
|
|
16
|
+
"@babel/preset-typescript": "^7.16.7",
|
|
17
17
|
"@vuedx/compiler-sfc": "^0.7.1",
|
|
18
18
|
"@vuedx/template-ast-types": "^0.7.2",
|
|
19
|
-
"fast-glob": "^3.2.
|
|
20
|
-
"prettier": "^2.
|
|
19
|
+
"fast-glob": "^3.2.11",
|
|
20
|
+
"prettier": "^2.6.2",
|
|
21
21
|
"string.prototype.replaceall": "^1.0.6"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@babel/traverse": "^7.
|
|
25
|
-
"@types/jest": "^27.
|
|
26
|
-
"@types/node": "
|
|
27
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
28
|
-
"@typescript-eslint/parser": "^
|
|
29
|
-
"esbuild": "^0.
|
|
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
30
|
"esbuild-jest": "^0.5.0",
|
|
31
|
-
"esbuild-node-externals": "^1.
|
|
32
|
-
"eslint": "^
|
|
33
|
-
"eslint-config-prettier": "^8.
|
|
34
|
-
"eslint-import-resolver-typescript": "^2.
|
|
35
|
-
"eslint-plugin-import": "^2.
|
|
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
36
|
"eslint-plugin-no-only-tests": "^2.6.0",
|
|
37
37
|
"eslint-plugin-only-warn": "^1.0.3",
|
|
38
|
-
"eslint-plugin-ssr-friendly": "^1.0.
|
|
39
|
-
"jest": "^27.
|
|
38
|
+
"eslint-plugin-ssr-friendly": "^1.0.6",
|
|
39
|
+
"jest": "^27.5.1",
|
|
40
40
|
"npm-run-all": "^4.1.5",
|
|
41
41
|
"rimraf": "^3.0.2",
|
|
42
|
-
"typescript": "^4.
|
|
42
|
+
"typescript": "^4.6.3"
|
|
43
43
|
},
|
|
44
44
|
"repository": {
|
|
45
45
|
"type": "git",
|
|
@@ -65,5 +65,6 @@
|
|
|
65
65
|
"test:typecheck": "tsc -p tsconfig.json --noEmit",
|
|
66
66
|
"test:lint": "eslint src --max-warnings 0",
|
|
67
67
|
"format": "prettier . --write"
|
|
68
|
-
}
|
|
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"
|
|
69
70
|
}
|