From 71cb319a7be0c026bf29c7ee615937dbfec76908 Mon Sep 17 00:00:00 2001 From: Thiago Santos Date: Tue, 24 Sep 2024 07:58:36 -0300 Subject: [PATCH] feat: adding modifiers metadata Modifiers metadata will allow you to have access to class, methods and properties detailed informations, giving the opportunity to create more complex decorating automations --- .husky/pre-commit | 1 + .lintstagedrc | 4 + README.md | 7 +- index.js | 2 - package-lock.json | 740 ++++++++++++++++++++++++++ package.json | 8 +- plugin.js | 2 + src/index.ts | 8 +- src/internal/index.ts | 1 + src/internal/meta-storage.ts | 13 + src/meta-info.ts | 79 +-- src/meta-type.ts | 42 ++ src/plugin/decorators.ts | 64 +++ src/{ => plugin}/emitter.ts | 70 ++- src/{ => plugin}/factory.ts | 0 src/plugin/index.ts | 3 + src/{ => plugin}/module-exists.ts | 0 src/{ => plugin}/ts-loader.ts | 0 src/{ => plugin}/tsconfig-provider.ts | 0 src/{ => plugin}/typescript-loader.ts | 0 20 files changed, 948 insertions(+), 96 deletions(-) create mode 100644 .husky/pre-commit create mode 100644 .lintstagedrc delete mode 100644 index.js create mode 100644 plugin.js create mode 100644 src/internal/index.ts create mode 100644 src/internal/meta-storage.ts create mode 100644 src/meta-type.ts create mode 100644 src/plugin/decorators.ts rename src/{ => plugin}/emitter.ts (66%) rename src/{ => plugin}/factory.ts (100%) create mode 100644 src/plugin/index.ts rename src/{ => plugin}/module-exists.ts (100%) rename src/{ => plugin}/ts-loader.ts (100%) rename src/{ => plugin}/tsconfig-provider.ts (100%) rename src/{ => plugin}/typescript-loader.ts (100%) diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100644 index 00000000..2312dc58 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1 @@ +npx lint-staged diff --git a/.lintstagedrc b/.lintstagedrc new file mode 100644 index 00000000..0946b6e7 --- /dev/null +++ b/.lintstagedrc @@ -0,0 +1,4 @@ +{ + "*.ts": "npm run lint:fix", + "*.js": "npm run lint:fix" +} diff --git a/README.md b/README.md index 54af06db..b0760df2 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Now, to use this plugin, do the following steps: ```json "compilerOptions": { "plugins": [ - "nestjs-auto-reflect-metadata-emitter" + "nestjs-auto-reflect-metadata-emitter/plugin" ] } ``` @@ -46,9 +46,8 @@ Finally, metadata may be a sensitive data of your application, so, you can erase ## What we're not doing yet. * We're not generating metadata of get and set accessors; -* We're not discriminating between private and public properties; -Those are points of evolution of this library and we'll address them as soon as possible. If you have any suggestions or contributions to do, feel free to contact us! +This is a point of evolution of this library and we'll address them as soon as possible. If you have any suggestions or contributions to do, feel free to contact us! ## How to use it with Jest? @@ -60,7 +59,7 @@ You can set the transformer of this library to run with jest following the examp "ts-jest", { "astTransformers": { - "before": ["node_modules/nestjs-auto-reflect-metadata-emitter"] + "before": ["node_modules/nestjs-auto-reflect-metadata-emitter/plugin"] } } ] diff --git a/index.js b/index.js deleted file mode 100644 index d29c86c0..00000000 --- a/index.js +++ /dev/null @@ -1,2 +0,0 @@ -module.exports = require('./dist'); -module.exports.default = module.exports; diff --git a/package-lock.json b/package-lock.json index a2237fab..bcb92511 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,15 +10,21 @@ "license": "ISC", "devDependencies": { "@nestjs/cli": "^10.4.5", + "@types/lint-staged": "^13.3.0", "@typescript-eslint/eslint-plugin": "^6.0.0", "@typescript-eslint/parser": "^6.0.0", "eslint": "^8.0.0", "eslint-plugin-unused-imports": "^4.1.4", + "husky": "^9.1.6", + "lint-staged": "^15.2.10", "prettier": "^3.3.3", "reflect-metadata": "^0.1.0", "tsconfig-paths": "^4.2.0", "typescript": "^5.3.3" }, + "engines": { + "node": ">=20" + }, "peerDependencies": { "@nestjs/cli": "*", "reflect-metadata": "*", @@ -734,6 +740,13 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/lint-staged": { + "version": "13.3.0", + "resolved": "https://registry.npmjs.org/@types/lint-staged/-/lint-staged-13.3.0.tgz", + "integrity": "sha512-WxGjVP+rA4OJlEdbZdT9MS9PFKQ7kVPhLn26gC+2tnBWBEFEj/KW+IbFfz6sxdxY5U6V7BvyF+3BzCGsAMHhNg==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/node": { "version": "22.5.5", "resolved": "https://registry.npmjs.org/@types/node/-/node-22.5.5.tgz", @@ -1622,6 +1635,77 @@ "@colors/colors": "1.5.0" } }, + "node_modules/cli-truncate": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/cli-truncate/-/cli-truncate-4.0.0.tgz", + "integrity": "sha512-nPdaFdQ0h/GEigbPClz11D0v/ZJEwxmeVZGeMo3Z5StPtUTkA9o1lD6QwoirYiSDzbcwn2XcjwmCp68W1IS4TA==", + "dev": true, + "license": "MIT", + "dependencies": { + "slice-ansi": "^5.0.0", + "string-width": "^7.0.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/cli-truncate/node_modules/ansi-regex": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-6.1.0.tgz", + "integrity": "sha512-7HSX4QQb4CspciLpVFwyRe79O3xsIZDDLER21kERQ71oaPodF8jL725AgJMFAYbooIqolJoRLuM81SpeUkpkvA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/chalk/ansi-regex?sponsor=1" + } + }, + "node_modules/cli-truncate/node_modules/emoji-regex": { + "version": "10.4.0", + "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-10.4.0.tgz", + "integrity": "sha512-EC+0oUMY1Rqm4O6LLrgjtYDvcVYTy7chDnM4Q7030tP4Kwj3u/pR6gP9ygnp2CJMK5Gq+9Q2oqmrFJAz01DXjw==", + "dev": true, + "license": "MIT" + }, + "node_modules/cli-truncate/node_modules/string-width": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-7.2.0.tgz", + "integrity": "sha512-tsaTIkKW9b4N+AEj+SVA+WhJzV7/zMhcSu78mLKWSk7cXMOSHsBKFWUs0fWwq8QyK3MgJBQRX6Gbi4kYbdvGkQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "emoji-regex": "^10.3.0", + "get-east-asian-width": "^1.0.0", + "strip-ansi": "^7.1.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/cli-truncate/node_modules/strip-ansi": { + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-7.1.0.tgz", + "integrity": "sha512-iq6eVVI64nQQTRYq2KtEg2d2uU7LElhTJwsH4YzIHZshxlgZms/wIc4VoDQTlG/IvVIrBKG06CrZnp0qv7hkcQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-regex": "^6.0.1" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/chalk/strip-ansi?sponsor=1" + } + }, "node_modules/cli-width": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/cli-width/-/cli-width-3.0.0.tgz", @@ -1662,6 +1746,13 @@ "dev": true, "license": "MIT" }, + "node_modules/colorette": { + "version": "2.0.20", + "resolved": "https://registry.npmjs.org/colorette/-/colorette-2.0.20.tgz", + "integrity": "sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w==", + "dev": true, + "license": "MIT" + }, "node_modules/commander": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/commander/-/commander-4.1.1.tgz", @@ -1872,6 +1963,19 @@ "node": ">=10.13.0" } }, + "node_modules/environment": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/environment/-/environment-1.1.0.tgz", + "integrity": "sha512-xUtoPkMggbz0MPyPiIWr1Kp4aeWJjDZ6SMvURhimjdZgsRuDplF5/s9hcgGhyXMhs+6vpnuoiZ2kFiu3FMnS8Q==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/error-ex": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz", @@ -2206,6 +2310,13 @@ "node": ">=0.10.0" } }, + "node_modules/eventemitter3": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-5.0.1.tgz", + "integrity": "sha512-GWkBvjiSZK87ELrYOSESUYeVIc9mvLLf/nXalMOS5dYrgZq9o5OVkbZAVM06CVxYsCwH9BDZFPlQTlPA1j4ahA==", + "dev": true, + "license": "MIT" + }, "node_modules/events": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/events/-/events-3.3.0.tgz", @@ -2216,6 +2327,59 @@ "node": ">=0.8.x" } }, + "node_modules/execa": { + "version": "8.0.1", + "resolved": "https://registry.npmjs.org/execa/-/execa-8.0.1.tgz", + "integrity": "sha512-VyhnebXciFV2DESc+p6B+y0LjSm0krU4OgJN44qFAhBY0TJ+1V61tYD2+wHusZ6F9n5K+vl8k0sTy7PEfV4qpg==", + "dev": true, + "license": "MIT", + "dependencies": { + "cross-spawn": "^7.0.3", + "get-stream": "^8.0.1", + "human-signals": "^5.0.0", + "is-stream": "^3.0.0", + "merge-stream": "^2.0.0", + "npm-run-path": "^5.1.0", + "onetime": "^6.0.0", + "signal-exit": "^4.1.0", + "strip-final-newline": "^3.0.0" + }, + "engines": { + "node": ">=16.17" + }, + "funding": { + "url": "https://github.com/sindresorhus/execa?sponsor=1" + } + }, + "node_modules/execa/node_modules/mimic-fn": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-4.0.0.tgz", + "integrity": "sha512-vqiC06CuhBTUdZH+RYl8sFrL096vA45Ok5ISO6sE/Mr1jRbGH4Csnhi8f3wKVl7x8mO4Au7Ir9D3Oyv1VYMFJw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/execa/node_modules/onetime": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/onetime/-/onetime-6.0.0.tgz", + "integrity": "sha512-1FlR+gjXK7X+AsAHso35MnyN5KqGwJRi/31ft6x0M194ht7S+rWAvd7PHss9xSKMzE0asv1pyIHaJYq+BbacAQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "mimic-fn": "^4.0.0" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/external-editor": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/external-editor/-/external-editor-3.1.0.tgz", @@ -2460,6 +2624,19 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/get-east-asian-width": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/get-east-asian-width/-/get-east-asian-width-1.2.0.tgz", + "integrity": "sha512-2nk+7SIVb14QrgXFHcm84tD4bKQz0RxPuMT8Ag5KPOq7J5fEmAg0UbXdTOSHqNuHSU28k55qnceesxXRZGzKWA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/get-intrinsic": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.4.tgz", @@ -2480,6 +2657,19 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/get-stream": { + "version": "8.0.1", + "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-8.0.1.tgz", + "integrity": "sha512-VaUJspBffn/LMCJVoMvSAdmscJyS1auj5Zulnn5UoYcY531UWmdwhRWkcGKnGU93m5HSXP9LP2usOryrBtQowA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=16" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/glob": { "version": "10.4.2", "resolved": "https://registry.npmjs.org/glob/-/glob-10.4.2.tgz", @@ -2699,6 +2889,32 @@ "node": ">= 0.4" } }, + "node_modules/human-signals": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/human-signals/-/human-signals-5.0.0.tgz", + "integrity": "sha512-AXcZb6vzzrFAUE61HnN4mpLqd/cSIwNQjtNWR0euPm6y0iqx3G4gOXaIDdtdDwZmhwe82LA6+zinmW4UBWVePQ==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=16.17.0" + } + }, + "node_modules/husky": { + "version": "9.1.6", + "resolved": "https://registry.npmjs.org/husky/-/husky-9.1.6.tgz", + "integrity": "sha512-sqbjZKK7kf44hfdE94EoX8MZNk0n7HeW37O4YrVGCF4wzgQjp+akPAkfUK5LZ6KuR/6sqeAVuXHji+RzQgOn5A==", + "dev": true, + "license": "MIT", + "bin": { + "husky": "bin.js" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/typicode" + } + }, "node_modules/iconv-lite": { "version": "0.4.24", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", @@ -2899,6 +3115,19 @@ "node": ">=8" } }, + "node_modules/is-stream": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-3.0.0.tgz", + "integrity": "sha512-LnQR4bZ9IADDRSkvpqMGvt/tEJWclzklNgSw48V5EAaAeDd6qGvN8ei6k5p0tvxSR171VmGyHuTiAOfxAbr8kA==", + "dev": true, + "license": "MIT", + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/is-unicode-supported": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz", @@ -3071,6 +3300,19 @@ "node": ">= 0.8.0" } }, + "node_modules/lilconfig": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/lilconfig/-/lilconfig-3.1.2.tgz", + "integrity": "sha512-eop+wDAvpItUys0FWkHIKeC9ybYrTGbU41U5K7+bttZZeohvnY7M9dZ5kB21GNWiFT2q1OoPTvncPCgSOVO5ow==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/sponsors/antonk52" + } + }, "node_modules/lines-and-columns": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.2.4.tgz", @@ -3078,6 +3320,160 @@ "dev": true, "license": "MIT" }, + "node_modules/lint-staged": { + "version": "15.2.10", + "resolved": "https://registry.npmjs.org/lint-staged/-/lint-staged-15.2.10.tgz", + "integrity": "sha512-5dY5t743e1byO19P9I4b3x8HJwalIznL5E1FWYnU6OWw33KxNBSLAc6Cy7F2PsFEO8FKnLwjwm5hx7aMF0jzZg==", + "dev": true, + "license": "MIT", + "dependencies": { + "chalk": "~5.3.0", + "commander": "~12.1.0", + "debug": "~4.3.6", + "execa": "~8.0.1", + "lilconfig": "~3.1.2", + "listr2": "~8.2.4", + "micromatch": "~4.0.8", + "pidtree": "~0.6.0", + "string-argv": "~0.3.2", + "yaml": "~2.5.0" + }, + "bin": { + "lint-staged": "bin/lint-staged.js" + }, + "engines": { + "node": ">=18.12.0" + }, + "funding": { + "url": "https://opencollective.com/lint-staged" + } + }, + "node_modules/lint-staged/node_modules/chalk": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-5.3.0.tgz", + "integrity": "sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w==", + "dev": true, + "license": "MIT", + "engines": { + "node": "^12.17.0 || ^14.13 || >=16.0.0" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/lint-staged/node_modules/commander": { + "version": "12.1.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-12.1.0.tgz", + "integrity": "sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/listr2": { + "version": "8.2.4", + "resolved": "https://registry.npmjs.org/listr2/-/listr2-8.2.4.tgz", + "integrity": "sha512-opevsywziHd3zHCVQGAj8zu+Z3yHNkkoYhWIGnq54RrCVwLz0MozotJEDnKsIBLvkfLGN6BLOyAeRrYI0pKA4g==", + "dev": true, + "license": "MIT", + "dependencies": { + "cli-truncate": "^4.0.0", + "colorette": "^2.0.20", + "eventemitter3": "^5.0.1", + "log-update": "^6.1.0", + "rfdc": "^1.4.1", + "wrap-ansi": "^9.0.0" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/listr2/node_modules/ansi-regex": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-6.1.0.tgz", + "integrity": "sha512-7HSX4QQb4CspciLpVFwyRe79O3xsIZDDLER21kERQ71oaPodF8jL725AgJMFAYbooIqolJoRLuM81SpeUkpkvA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/chalk/ansi-regex?sponsor=1" + } + }, + "node_modules/listr2/node_modules/ansi-styles": { + "version": "6.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-6.2.1.tgz", + "integrity": "sha512-bN798gFfQX+viw3R7yrGWRqnrN2oRkEkUjjl4JNn4E8GxxbjtG3FbrEIIY3l8/hrwUwIeCZvi4QuOTP4MErVug==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/listr2/node_modules/emoji-regex": { + "version": "10.4.0", + "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-10.4.0.tgz", + "integrity": "sha512-EC+0oUMY1Rqm4O6LLrgjtYDvcVYTy7chDnM4Q7030tP4Kwj3u/pR6gP9ygnp2CJMK5Gq+9Q2oqmrFJAz01DXjw==", + "dev": true, + "license": "MIT" + }, + "node_modules/listr2/node_modules/string-width": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-7.2.0.tgz", + "integrity": "sha512-tsaTIkKW9b4N+AEj+SVA+WhJzV7/zMhcSu78mLKWSk7cXMOSHsBKFWUs0fWwq8QyK3MgJBQRX6Gbi4kYbdvGkQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "emoji-regex": "^10.3.0", + "get-east-asian-width": "^1.0.0", + "strip-ansi": "^7.1.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/listr2/node_modules/strip-ansi": { + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-7.1.0.tgz", + "integrity": "sha512-iq6eVVI64nQQTRYq2KtEg2d2uU7LElhTJwsH4YzIHZshxlgZms/wIc4VoDQTlG/IvVIrBKG06CrZnp0qv7hkcQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-regex": "^6.0.1" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/chalk/strip-ansi?sponsor=1" + } + }, + "node_modules/listr2/node_modules/wrap-ansi": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-9.0.0.tgz", + "integrity": "sha512-G8ura3S+3Z2G+mkgNRq8dqaFZAuxfsxpBB8OCTGRTCtp+l/v9nbFNmCUP1BZMts3G1142MsZfn6eeUKrr4PD1Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-styles": "^6.2.1", + "string-width": "^7.0.0", + "strip-ansi": "^7.1.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/chalk/wrap-ansi?sponsor=1" + } + }, "node_modules/loader-runner": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/loader-runner/-/loader-runner-4.3.0.tgz", @@ -3135,6 +3531,209 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/log-update": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/log-update/-/log-update-6.1.0.tgz", + "integrity": "sha512-9ie8ItPR6tjY5uYJh8K/Zrv/RMZ5VOlOWvtZdEHYSTFKZfIBPQa9tOAEeAWhd+AnIneLJ22w5fjOYtoutpWq5w==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-escapes": "^7.0.0", + "cli-cursor": "^5.0.0", + "slice-ansi": "^7.1.0", + "strip-ansi": "^7.1.0", + "wrap-ansi": "^9.0.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/log-update/node_modules/ansi-escapes": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-7.0.0.tgz", + "integrity": "sha512-GdYO7a61mR0fOlAsvC9/rIHf7L96sBc6dEWzeOu+KAea5bZyQRPIpojrVoI4AXGJS/ycu/fBTdLrUkA4ODrvjw==", + "dev": true, + "license": "MIT", + "dependencies": { + "environment": "^1.0.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/log-update/node_modules/ansi-regex": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-6.1.0.tgz", + "integrity": "sha512-7HSX4QQb4CspciLpVFwyRe79O3xsIZDDLER21kERQ71oaPodF8jL725AgJMFAYbooIqolJoRLuM81SpeUkpkvA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/chalk/ansi-regex?sponsor=1" + } + }, + "node_modules/log-update/node_modules/ansi-styles": { + "version": "6.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-6.2.1.tgz", + "integrity": "sha512-bN798gFfQX+viw3R7yrGWRqnrN2oRkEkUjjl4JNn4E8GxxbjtG3FbrEIIY3l8/hrwUwIeCZvi4QuOTP4MErVug==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/log-update/node_modules/cli-cursor": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/cli-cursor/-/cli-cursor-5.0.0.tgz", + "integrity": "sha512-aCj4O5wKyszjMmDT4tZj93kxyydN/K5zPWSCe6/0AV/AA1pqe5ZBIw0a2ZfPQV7lL5/yb5HsUreJ6UFAF1tEQw==", + "dev": true, + "license": "MIT", + "dependencies": { + "restore-cursor": "^5.0.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/log-update/node_modules/emoji-regex": { + "version": "10.4.0", + "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-10.4.0.tgz", + "integrity": "sha512-EC+0oUMY1Rqm4O6LLrgjtYDvcVYTy7chDnM4Q7030tP4Kwj3u/pR6gP9ygnp2CJMK5Gq+9Q2oqmrFJAz01DXjw==", + "dev": true, + "license": "MIT" + }, + "node_modules/log-update/node_modules/is-fullwidth-code-point": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-5.0.0.tgz", + "integrity": "sha512-OVa3u9kkBbw7b8Xw5F9P+D/T9X+Z4+JruYVNapTjPYZYUznQ5YfWeFkOj606XYYW8yugTfC8Pj0hYqvi4ryAhA==", + "dev": true, + "license": "MIT", + "dependencies": { + "get-east-asian-width": "^1.0.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/log-update/node_modules/onetime": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/onetime/-/onetime-7.0.0.tgz", + "integrity": "sha512-VXJjc87FScF88uafS3JllDgvAm+c/Slfz06lorj2uAY34rlUu0Nt+v8wreiImcrgAjjIHp1rXpTDlLOGw29WwQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "mimic-function": "^5.0.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/log-update/node_modules/restore-cursor": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/restore-cursor/-/restore-cursor-5.1.0.tgz", + "integrity": "sha512-oMA2dcrw6u0YfxJQXm342bFKX/E4sG9rbTzO9ptUcR/e8A33cHuvStiYOwH7fszkZlZ1z/ta9AAoPk2F4qIOHA==", + "dev": true, + "license": "MIT", + "dependencies": { + "onetime": "^7.0.0", + "signal-exit": "^4.1.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/log-update/node_modules/slice-ansi": { + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/slice-ansi/-/slice-ansi-7.1.0.tgz", + "integrity": "sha512-bSiSngZ/jWeX93BqeIAbImyTbEihizcwNjFoRUIY/T1wWQsfsm2Vw1agPKylXvQTU7iASGdHhyqRlqQzfz+Htg==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-styles": "^6.2.1", + "is-fullwidth-code-point": "^5.0.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/chalk/slice-ansi?sponsor=1" + } + }, + "node_modules/log-update/node_modules/string-width": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-7.2.0.tgz", + "integrity": "sha512-tsaTIkKW9b4N+AEj+SVA+WhJzV7/zMhcSu78mLKWSk7cXMOSHsBKFWUs0fWwq8QyK3MgJBQRX6Gbi4kYbdvGkQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "emoji-regex": "^10.3.0", + "get-east-asian-width": "^1.0.0", + "strip-ansi": "^7.1.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/log-update/node_modules/strip-ansi": { + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-7.1.0.tgz", + "integrity": "sha512-iq6eVVI64nQQTRYq2KtEg2d2uU7LElhTJwsH4YzIHZshxlgZms/wIc4VoDQTlG/IvVIrBKG06CrZnp0qv7hkcQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-regex": "^6.0.1" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/chalk/strip-ansi?sponsor=1" + } + }, + "node_modules/log-update/node_modules/wrap-ansi": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-9.0.0.tgz", + "integrity": "sha512-G8ura3S+3Z2G+mkgNRq8dqaFZAuxfsxpBB8OCTGRTCtp+l/v9nbFNmCUP1BZMts3G1142MsZfn6eeUKrr4PD1Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-styles": "^6.2.1", + "string-width": "^7.0.0", + "strip-ansi": "^7.1.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/chalk/wrap-ansi?sponsor=1" + } + }, "node_modules/lru-cache": { "version": "10.4.3", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-10.4.3.tgz", @@ -3245,6 +3844,19 @@ "node": ">=6" } }, + "node_modules/mimic-function": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/mimic-function/-/mimic-function-5.0.1.tgz", + "integrity": "sha512-VP79XUPxV2CigYP3jWwAUFSku2aKqBH7uTAapFWCBqutsbmDo96KY5o8uh6U+/YSIn5OxJnXp73beVkpqMIGhA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/minimatch": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", @@ -3340,6 +3952,35 @@ "node": ">=0.10.0" } }, + "node_modules/npm-run-path": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-5.3.0.tgz", + "integrity": "sha512-ppwTtiJZq0O/ai0z7yfudtBpWIoxM8yE6nHi1X47eFR2EWORqfbu6CnPlNsjeN683eT0qG6H/Pyf9fCcvjnnnQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "path-key": "^4.0.0" + }, + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/npm-run-path/node_modules/path-key": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/path-key/-/path-key-4.0.0.tgz", + "integrity": "sha512-haREypq7xkM7ErfgIyA0z+Bj4AGKlMSdlQE2jvJo6huWD1EdkKYV+G/T4nq0YEF2vgTT8kqMFKo1uHn950r4SQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -3566,6 +4207,19 @@ "url": "https://github.com/sponsors/jonschlinkert" } }, + "node_modules/pidtree": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/pidtree/-/pidtree-0.6.0.tgz", + "integrity": "sha512-eG2dWTVw5bzqGRztnHExczNxt5VGsE6OwTeCG3fdUf9KBsZzO3R5OIIIzWR+iZA0NtZ+RDVdaoE2dK1cn6jH4g==", + "dev": true, + "license": "MIT", + "bin": { + "pidtree": "bin/pidtree.js" + }, + "engines": { + "node": ">=0.10" + } + }, "node_modules/pluralize": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/pluralize/-/pluralize-8.0.0.tgz", @@ -3753,6 +4407,13 @@ "node": ">=0.10.0" } }, + "node_modules/rfdc": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/rfdc/-/rfdc-1.4.1.tgz", + "integrity": "sha512-q1b3N5QkRUWUl7iyylaaj3kOpIT0N2i9MqIEQXP73GVsN9cw3fdx8X63cEmWhJGi2PPCF23Ijp7ktmd39rawIA==", + "dev": true, + "license": "MIT" + }, "node_modules/rimraf": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", @@ -4004,6 +4665,49 @@ "node": ">=8" } }, + "node_modules/slice-ansi": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/slice-ansi/-/slice-ansi-5.0.0.tgz", + "integrity": "sha512-FC+lgizVPfie0kkhqUScwRu1O/lF6NOgJmlCgK+/LYxDCTk8sGelYaHDhFcDN+Sn3Cv+3VSa4Byeo+IMCzpMgQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "ansi-styles": "^6.0.0", + "is-fullwidth-code-point": "^4.0.0" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/chalk/slice-ansi?sponsor=1" + } + }, + "node_modules/slice-ansi/node_modules/ansi-styles": { + "version": "6.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-6.2.1.tgz", + "integrity": "sha512-bN798gFfQX+viw3R7yrGWRqnrN2oRkEkUjjl4JNn4E8GxxbjtG3FbrEIIY3l8/hrwUwIeCZvi4QuOTP4MErVug==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/slice-ansi/node_modules/is-fullwidth-code-point": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-4.0.0.tgz", + "integrity": "sha512-O4L094N2/dZ7xqVdrXhh9r1KODPJpFms8B5sGdJLPy664AgvXsreZUyCQQNItZRDlYug4xStLjNp/sz3HvBowQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/source-map": { "version": "0.7.4", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.7.4.tgz", @@ -4045,6 +4749,16 @@ "safe-buffer": "~5.2.0" } }, + "node_modules/string-argv": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/string-argv/-/string-argv-0.3.2.tgz", + "integrity": "sha512-aqD2Q0144Z+/RqG52NeHEkZauTAUWJO8c6yTftGJKO3Tja5tUgIfmIl6kExvhtxSDP7fXB6DvzkfMpCd/F3G+Q==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=0.6.19" + } + }, "node_modules/string-width": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", @@ -4113,6 +4827,19 @@ "node": ">=4" } }, + "node_modules/strip-final-newline": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/strip-final-newline/-/strip-final-newline-3.0.0.tgz", + "integrity": "sha512-dOESqjYr96iWYylGObzd39EuNTa5VJxyvVAEm5Jnh7KGo75V43Hk1odPQkNDyXNmUR6k+gEiDVXnjB8HJ3crXw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/strip-json-comments": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz", @@ -4583,6 +5310,19 @@ "dev": true, "license": "ISC" }, + "node_modules/yaml": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.5.1.tgz", + "integrity": "sha512-bLQOjaX/ADgQ20isPJRvF0iRUHIxVhYvr53Of7wGcWlO2jvtUlH5m87DsmulFVxRpNLOnI4tB6p/oh8D7kpn9Q==", + "dev": true, + "license": "ISC", + "bin": { + "yaml": "bin.mjs" + }, + "engines": { + "node": ">= 14" + } + }, "node_modules/yargs-parser": { "version": "21.1.1", "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-21.1.1.tgz", diff --git a/package.json b/package.json index 8acb51db..ffda1d7f 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "main": "dist/index.js", "files": [ "dist", - "index.js" + "plugin.js" ], "scripts": { "lint": "npm run lint:format && npm run lint:style", @@ -13,17 +13,21 @@ "lint:format:fix": "prettier --write '{src,test}/**/*.ts'", "lint:style": "eslint 'src/**'", "lint:style:fix": "eslint 'src/**' --fix", - "build": "nest build" + "build": "nest build", + "prepare": "husky" }, "author": "", "license": "ISC", "description": "", "devDependencies": { "@nestjs/cli": "^10.4.5", + "@types/lint-staged": "^13.3.0", "@typescript-eslint/eslint-plugin": "^6.0.0", "@typescript-eslint/parser": "^6.0.0", "eslint": "^8.0.0", "eslint-plugin-unused-imports": "^4.1.4", + "husky": "^9.1.6", + "lint-staged": "^15.2.10", "prettier": "^3.3.3", "reflect-metadata": "^0.1.0", "tsconfig-paths": "^4.2.0", diff --git a/plugin.js b/plugin.js new file mode 100644 index 00000000..f4e1d62c --- /dev/null +++ b/plugin.js @@ -0,0 +1,2 @@ +module.exports = require('./dist/plugin'); +module.exports.default = module.exports; diff --git a/src/index.ts b/src/index.ts index 5ea6d44f..75629df7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,7 @@ -export * from './emitter'; -export * from './factory'; export * from './meta-info'; +export * from './meta-type'; + +import './plugin/decorators'; +import { blockAccess } from './internal'; + +blockAccess(); diff --git a/src/internal/index.ts b/src/internal/index.ts new file mode 100644 index 00000000..f559db00 --- /dev/null +++ b/src/internal/index.ts @@ -0,0 +1 @@ +export * from './meta-storage'; diff --git a/src/internal/meta-storage.ts b/src/internal/meta-storage.ts new file mode 100644 index 00000000..79b69439 --- /dev/null +++ b/src/internal/meta-storage.ts @@ -0,0 +1,13 @@ +import { ClassMetadata } from '../meta-type'; + +let blocked = false; +const metadata = new Map(); + +export function getMetadataStorage() { + if (!blocked) return metadata; + throw new Error('Invalid Operation'); +} + +export function blockAccess() { + blocked = true; +} diff --git a/src/meta-info.ts b/src/meta-info.ts index 8a5acb25..9ec04468 100644 --- a/src/meta-info.ts +++ b/src/meta-info.ts @@ -1,81 +1,14 @@ import 'reflect-metadata'; -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export type ClassType = abstract new (...args: any[]) => unknown; -export type Key = string | symbol; +import { ClassType, ClassMetadata } from './meta-type'; +import { getMetadataStorage } from './internal'; -export interface ConstructorMetadata { - cls: ClassType; - args: unknown[]; -} -export interface MethodMetadata { - name: Key; - args: unknown[]; - returnType: unknown; - propertyDescriptor: PropertyDescriptor; -} -export interface PropertyMetadata { - name: Key; - type: unknown; -} - -interface ClassMetadata { - ctor: ConstructorMetadata; - properties: Map; - methods: Map; -} -const metadata = new Map(); - -function getMeta(prototype: object) { - let ref = metadata.get(prototype); - if (!ref) { - ref = { - ctor: undefined as unknown as ConstructorMetadata, - properties: new Map(), - methods: new Map(), - }; - metadata.set(prototype, ref); - } - return ref; -} - -export function registerClassMetadata(cls: ClassType) { - const { prototype } = cls; - const ref = getMeta(prototype); - ref.ctor = { - cls, - args: Reflect.getMetadata('design:paramtypes', cls), - }; -} - -export function registerPropertyMetadata(prototype: object, key: Key) { - const ref = getMeta(prototype); - const type = Reflect.getMetadata('design:type', prototype, key); - ref.properties.set(key, { - name: key, - type, - }); -} - -export function registerMethodMetadata( - prototype: object, - key: Key, - propertyDescriptor: PropertyDescriptor, -) { - const ref = getMeta(prototype); - const returnType = Reflect.getMetadata('design:returntype', prototype, key); - ref.methods.set(key, { - name: key, - args: Reflect.getMetadata('design:paramtypes', prototype, key), - returnType, - propertyDescriptor, - }); -} +const metadata = getMetadataStorage(); /** * Return metadata of the class informed, or undefined if there is none * @param cls The Class to get metadata from */ -export function getClassMetadata(cls: ClassType) { +export function getClassMetadata(cls: ClassType): ClassMetadata | undefined { return metadata.get(cls.prototype); } @@ -83,7 +16,7 @@ export function getClassMetadata(cls: ClassType) { * Returns an iterable that allows you to iterate over all the metadata * collected. You must filter it as you need */ -export function iterateMetadata() { +export function iterateMetadata(): Iterable { return metadata.values(); } @@ -91,6 +24,6 @@ export function iterateMetadata() { * Clear all the metadata collected. We recommend you to call this function * at the and of the metadata consumption */ -export function clearAllMetadata() { +export function clearAllMetadata(): void { metadata.clear(); } diff --git a/src/meta-type.ts b/src/meta-type.ts new file mode 100644 index 00000000..e95b3435 --- /dev/null +++ b/src/meta-type.ts @@ -0,0 +1,42 @@ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export type ClassType = abstract new (...args: any[]) => unknown; +export type Key = string | symbol; + +export interface ModifiersMetadata { + public: boolean; + readonly: boolean; + static: boolean; + abstract: boolean; + accessor: boolean; + async: boolean; + const: boolean; + override: boolean; + private: boolean; + protected: boolean; + exported: boolean; +} + +export interface BaseMetadata { + modifiers: ModifiersMetadata; +} + +export interface ConstructorMetadata extends BaseMetadata { + cls: ClassType; + args: unknown[]; +} +export interface MethodMetadata extends BaseMetadata { + name: Key; + args: unknown[]; + returnType: unknown; + propertyDescriptor: PropertyDescriptor; +} +export interface PropertyMetadata extends BaseMetadata { + name: Key; + type: unknown; +} + +export interface ClassMetadata { + ctor: ConstructorMetadata; + properties: Map; + methods: Map; +} diff --git a/src/plugin/decorators.ts b/src/plugin/decorators.ts new file mode 100644 index 00000000..4bec7ddf --- /dev/null +++ b/src/plugin/decorators.ts @@ -0,0 +1,64 @@ +import { getMetadataStorage } from '../internal'; +import { + ClassType, + ConstructorMetadata, + Key, + ModifiersMetadata, +} from '../meta-type'; + +const metadata = getMetadataStorage(); + +function getMeta(prototype: object) { + let ref = metadata.get(prototype); + if (!ref) { + ref = { + ctor: undefined as unknown as ConstructorMetadata, + properties: new Map(), + methods: new Map(), + }; + metadata.set(prototype, ref); + } + return ref; +} + +export function registerClassMetadata(modifiers: ModifiersMetadata) { + return (cls: ClassType) => { + const { prototype } = cls; + const ref = getMeta(prototype); + ref.ctor = { + cls, + args: Reflect.getMetadata('design:paramtypes', cls), + modifiers, + }; + }; +} + +export function registerPropertyMetadata(modifiers: ModifiersMetadata) { + return (prototype: object, key: Key) => { + const ref = getMeta(prototype); + const type = Reflect.getMetadata('design:type', prototype, key); + ref.properties.set(key, { + name: key, + type, + modifiers, + }); + }; +} + +export function registerMethodMetadata(modifiers: ModifiersMetadata) { + return ( + prototype: object, + key: Key, + propertyDescriptor: PropertyDescriptor, + ) => { + const ref = getMeta(prototype); + const returnType = Reflect.getMetadata('design:returntype', prototype, key); + ref.methods.set(key, { + name: key, + args: Reflect.getMetadata('design:paramtypes', prototype, key), + returnType, + propertyDescriptor, + modifiers, + }); + }; +} diff --git a/src/emitter.ts b/src/plugin/emitter.ts similarity index 66% rename from src/emitter.ts rename to src/plugin/emitter.ts index dc9ea700..387aa90a 100644 --- a/src/emitter.ts +++ b/src/plugin/emitter.ts @@ -3,6 +3,7 @@ import * as ts from 'typescript'; import { moduleExists } from './module-exists'; import { tsBinary } from './ts-loader'; +import { Key, ModifiersMetadata } from '../meta-type'; function isStatic( node: ts.MethodDeclaration | ts.ClassDeclaration | ts.PropertyDeclaration, @@ -25,6 +26,43 @@ function addRef( } } +function* emitPropertyAssignments(obj: Record) { + for (const k in obj) { + if (k in obj) { + yield tsBinary.factory.createPropertyAssignment( + k, + obj[k] ? tsBinary.factory.createTrue() : tsBinary.factory.createFalse(), + ); + } + } +} + +function getProperties(node: ts.HasModifiers): ts.ObjectLiteralElementLike[] { + const modifiers = new Set(); + for (const modifier of tsBinary.getModifiers(node) ?? []) { + modifiers.add(modifier.kind); + } + const access = { + private: modifiers.has(ts.SyntaxKind.PrivateKeyword), + protected: modifiers.has(ts.SyntaxKind.ProtectedKeyword), + }; + const properties: ModifiersMetadata = { + exported: modifiers.has(ts.SyntaxKind.ExportKeyword), + ...access, + public: !access.private && !access.protected, + readonly: modifiers.has(ts.SyntaxKind.ReadonlyKeyword), + static: modifiers.has(ts.SyntaxKind.StaticKeyword), + abstract: modifiers.has(ts.SyntaxKind.AbstractKeyword), + accessor: modifiers.has(ts.SyntaxKind.AccessorKeyword), + async: modifiers.has(ts.SyntaxKind.AsyncKeyword), + const: modifiers.has(ts.SyntaxKind.ConstKeyword), + override: modifiers.has(ts.SyntaxKind.OverrideKeyword), + }; + return [ + ...emitPropertyAssignments(properties as unknown as Record), + ]; +} + function addParameterRefs( node: NodeWithParameters, imports: Map, @@ -58,6 +96,7 @@ export function before() { !isStatic(node) ) { let identifier: string; + const modifiers = getProperties(node); if (!tsBinary.isClassDeclaration(node)) { if (node.type) addRef(node, imports, mustImport); if (tsBinary.isMethodDeclaration(node)) { @@ -75,21 +114,26 @@ export function before() { } } } - const decorator = tsBinary.factory.createDecorator( - tsBinary.factory.createPropertyAccessChain( - tsBinary.factory.createCallExpression( - tsBinary.factory.createIdentifier('require'), - undefined, - [ - tsBinary.factory.createStringLiteral( - 'nestjs-auto-reflect-metadata-emitter', - ), - ], + const requireCall = tsBinary.factory.createCallExpression( + tsBinary.factory.createIdentifier('require'), + undefined, + [ + tsBinary.factory.createStringLiteral( + 'nestjs-auto-reflect-metadata-emitter/plugin', ), - undefined, - tsBinary.factory.createIdentifier(identifier), - ), + ], + ); + const decoratorAccess = tsBinary.factory.createPropertyAccessChain( + requireCall, + undefined, + tsBinary.factory.createIdentifier(identifier), + ); + const decoratorCall = tsBinary.factory.createCallExpression( + decoratorAccess, + undefined, + [tsBinary.factory.createObjectLiteralExpression(modifiers)], ); + const decorator = tsBinary.factory.createDecorator(decoratorCall); node = tsBinary.factory.replaceDecoratorsAndModifiers(node, [ ...(node.modifiers ?? []), decorator, diff --git a/src/factory.ts b/src/plugin/factory.ts similarity index 100% rename from src/factory.ts rename to src/plugin/factory.ts diff --git a/src/plugin/index.ts b/src/plugin/index.ts new file mode 100644 index 00000000..ccdcd801 --- /dev/null +++ b/src/plugin/index.ts @@ -0,0 +1,3 @@ +export * from './decorators'; +export * from './emitter'; +export * from './factory'; diff --git a/src/module-exists.ts b/src/plugin/module-exists.ts similarity index 100% rename from src/module-exists.ts rename to src/plugin/module-exists.ts diff --git a/src/ts-loader.ts b/src/plugin/ts-loader.ts similarity index 100% rename from src/ts-loader.ts rename to src/plugin/ts-loader.ts diff --git a/src/tsconfig-provider.ts b/src/plugin/tsconfig-provider.ts similarity index 100% rename from src/tsconfig-provider.ts rename to src/plugin/tsconfig-provider.ts diff --git a/src/typescript-loader.ts b/src/plugin/typescript-loader.ts similarity index 100% rename from src/typescript-loader.ts rename to src/plugin/typescript-loader.ts