From 1c86f92a7648a928513b24cf47ae0a839dd659eb Mon Sep 17 00:00:00 2001 From: Jeroen Claassens Date: Thu, 11 Jul 2024 20:15:19 +0200 Subject: [PATCH 1/2] Fix TypeError when requiring discord.js --- package.json | 1 + src/lib/index.ts | 11 ++++++++--- tests/module.test.ts | 10 ++++++++++ tests/objects.test.ts | 5 +++++ vitest.config.ts | 5 +++-- 5 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 tests/module.test.ts diff --git a/package.json b/package.json index b8e4d66..8563998 100644 --- a/package.json +++ b/package.json @@ -66,6 +66,7 @@ "@typescript-eslint/parser": "^7.13.1", "@vitest/coverage-v8": "^1.6.0", "cz-conventional-changelog": "^3.3.0", + "discord.js": "^14.15.3", "eslint": "^8.57.0", "eslint-config-prettier": "^9.1.0", "eslint-plugin-prettier": "^5.1.3", diff --git a/src/lib/index.ts b/src/lib/index.ts index 0b1d556..3345d03 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -140,10 +140,15 @@ export class Type { public static resolve(value: any): string { const type = typeof value; switch (type) { - case 'object': - return value === null ? 'null' : value.constructor ? value.constructor.name : 'Object'; - case 'function': + case 'object': { + if (value === null) return 'null'; + if (!value.constructor?.name) return 'Object'; + return value.constructor.name; + } + case 'function': { + if (!value.constructor?.name) return 'Function'; return `${value.constructor.name}(${value.length}-arity)`; + } case 'undefined': return 'void'; default: diff --git a/tests/module.test.ts b/tests/module.test.ts new file mode 100644 index 0000000..371fc31 --- /dev/null +++ b/tests/module.test.ts @@ -0,0 +1,10 @@ +import discordjs from 'discord.js'; +import { Type } from '../src/lib/index.js'; + +describe('Modules', () => { + test('discord.js', () => { + expect(new Type(discordjs).toString()).toBe( + 'Record | AsyncFunction(1-arity) | AsyncFunction(7-arity) | Function | Function(0-arity) | Function(1-arity) | Function(2-arity) | Function(3-arity) | Function(4-arity) | Function(5-arity) | Function(6-arity) | GeneratorFunction(1-arity) | Record | Set | Snowflake | string>' + ); + }); +}); diff --git a/tests/objects.test.ts b/tests/objects.test.ts index 8b975bc..f585a60 100644 --- a/tests/objects.test.ts +++ b/tests/objects.test.ts @@ -26,4 +26,9 @@ describe('Objects', () => { test('object(recursive)', () => { expect(new Type({ foo: 'bar', hello: { baz: 'world' } }).toString()).toBe('Record | string>'); }); + + test('object(null prototype)', () => { + const obj = Object.create(null); + expect(new Type(obj).toString()).toBe('Record'); + }); }); diff --git a/vitest.config.ts b/vitest.config.ts index 291faa7..e5c4b84 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,4 +1,4 @@ -import { defineConfig } from 'vitest/config'; +import { configDefaults, defineConfig } from 'vitest/config'; export default defineConfig({ test: { @@ -11,7 +11,8 @@ export default defineConfig({ globals: true, coverage: { enabled: true, - reporter: ['text', 'lcov', 'clover'] + reporter: ['text', 'lcov', 'clover'], + exclude: [...(configDefaults.coverage.exclude ?? []), 'scripts/'] }, deps: { interopDefault: true From b3a189cc6cac88a45c0e9046ecac4ea03fdd7855 Mon Sep 17 00:00:00 2001 From: Jeroen Claassens Date: Thu, 11 Jul 2024 20:19:13 +0200 Subject: [PATCH 2/2] chore: whee. --- yarn.lock | 194 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 192 insertions(+), 2 deletions(-) diff --git a/yarn.lock b/yarn.lock index 48e1ed4..5d44343 100644 --- a/yarn.lock +++ b/yarn.lock @@ -330,6 +330,44 @@ __metadata: languageName: node linkType: hard +"@discordjs/builders@npm:^1.8.2": + version: 1.8.2 + resolution: "@discordjs/builders@npm:1.8.2" + dependencies: + "@discordjs/formatters": "npm:^0.4.0" + "@discordjs/util": "npm:^1.1.0" + "@sapphire/shapeshift": "npm:^3.9.7" + discord-api-types: "npm:0.37.83" + fast-deep-equal: "npm:^3.1.3" + ts-mixer: "npm:^6.0.4" + tslib: "npm:^2.6.2" + checksum: 10/64de3c72fdde202cc168ae5e42c05df44d3a4323cc1706fa667077b5bef1d60cf10d8e871bd4c758140432252f3c9c2747370f56fa0ff08c55669cb0a1e516d7 + languageName: node + linkType: hard + +"@discordjs/collection@npm:1.5.3": + version: 1.5.3 + resolution: "@discordjs/collection@npm:1.5.3" + checksum: 10/770d0576612555c848858ead2a6c6242252f51ae3a7a6fdcc4986ceeb330ed8cffb81bcd1819e1ef11cce946cb9e707791926e757985cc2e081e984012c08dad + languageName: node + linkType: hard + +"@discordjs/collection@npm:^2.1.0": + version: 2.1.0 + resolution: "@discordjs/collection@npm:2.1.0" + checksum: 10/386b508a0ed55614a8c32990a16eaaae4c9289172ce06fc7489b91932c593f7485fb62afd13cf71ad762ba927e73233ed63a8d6c8884de949cf1dfaa8ca391e8 + languageName: node + linkType: hard + +"@discordjs/formatters@npm:^0.4.0": + version: 0.4.0 + resolution: "@discordjs/formatters@npm:0.4.0" + dependencies: + discord-api-types: "npm:0.37.83" + checksum: 10/dbc75cf1048c928ddefb3d3f6268f8c153fce3030ef08afad06b7e2b1cd407451069eef1bcd903fc654d5e02fd1bf26372e12da41496182eb8fbee3dab87a83c + languageName: node + linkType: hard + "@discordjs/node-pre-gyp@npm:^0.4.5": version: 0.4.5 resolution: "@discordjs/node-pre-gyp@npm:0.4.5" @@ -349,6 +387,47 @@ __metadata: languageName: node linkType: hard +"@discordjs/rest@npm:^2.3.0": + version: 2.3.0 + resolution: "@discordjs/rest@npm:2.3.0" + dependencies: + "@discordjs/collection": "npm:^2.1.0" + "@discordjs/util": "npm:^1.1.0" + "@sapphire/async-queue": "npm:^1.5.2" + "@sapphire/snowflake": "npm:^3.5.3" + "@vladfrangu/async_event_emitter": "npm:^2.2.4" + discord-api-types: "npm:0.37.83" + magic-bytes.js: "npm:^1.10.0" + tslib: "npm:^2.6.2" + undici: "npm:6.13.0" + checksum: 10/55932ed31248f3b02e819fb2e197eb223862d29894f8b0271d8451c5d8226ae9f398d65234f299bdd5e8597ca8e287ad82867d9b85d54070b6ff172ad3e22ee9 + languageName: node + linkType: hard + +"@discordjs/util@npm:^1.1.0": + version: 1.1.0 + resolution: "@discordjs/util@npm:1.1.0" + checksum: 10/ce76daa238a4675e3fa081579645debb19df1028ed7c0a518ad81837ae5b17ee8eea8da22bb8d3671b5e5384f315a2972cc0f64d829531cc54fe2afecc0acb79 + languageName: node + linkType: hard + +"@discordjs/ws@npm:^1.1.1": + version: 1.1.1 + resolution: "@discordjs/ws@npm:1.1.1" + dependencies: + "@discordjs/collection": "npm:^2.1.0" + "@discordjs/rest": "npm:^2.3.0" + "@discordjs/util": "npm:^1.1.0" + "@sapphire/async-queue": "npm:^1.5.2" + "@types/ws": "npm:^8.5.10" + "@vladfrangu/async_event_emitter": "npm:^2.2.4" + discord-api-types: "npm:0.37.83" + tslib: "npm:^2.6.2" + ws: "npm:^8.16.0" + checksum: 10/d926fee9e6c6d9e01b574340bdec848baf620b08d34fa9d1547aadb806b5e25317707a82867b36ff6f75feefa373fa208648fac54e01038adb5dfc91c9e55349 + languageName: node + linkType: hard + "@esbuild/aix-ppc64@npm:0.21.5": version: 0.21.5 resolution: "@esbuild/aix-ppc64@npm:0.21.5" @@ -1081,6 +1160,13 @@ __metadata: languageName: node linkType: hard +"@sapphire/async-queue@npm:^1.5.2": + version: 1.5.2 + resolution: "@sapphire/async-queue@npm:1.5.2" + checksum: 10/55e0785997ec34479509c134a3ffc5e9dc0c183b56b1cb95b0505539972a538b0bff8922c1d051ebeee227b000f79f1f499f1897229a117644833375093aa7b3 + languageName: node + linkType: hard + "@sapphire/eslint-config@npm:^5.0.5": version: 5.0.5 resolution: "@sapphire/eslint-config@npm:5.0.5" @@ -1128,6 +1214,23 @@ __metadata: languageName: node linkType: hard +"@sapphire/shapeshift@npm:^3.9.7": + version: 3.9.7 + resolution: "@sapphire/shapeshift@npm:3.9.7" + dependencies: + fast-deep-equal: "npm:^3.1.3" + lodash: "npm:^4.17.21" + checksum: 10/f90f8e25920fe953a5231aa65f24829e1f2ecb26dcac7a09ff1a1f3cb988f174e27b3ac82fc4f6368891edff8ad6f90ee78cf5c0cab3038fd2714d02eba4eb0c + languageName: node + linkType: hard + +"@sapphire/snowflake@npm:3.5.3, @sapphire/snowflake@npm:^3.5.3": + version: 3.5.3 + resolution: "@sapphire/snowflake@npm:3.5.3" + checksum: 10/f306626f76a6e9bdc7de9130c1baf7ddcd8681d7d03b2ab6f2404081f71c94085d4001e8a62ae2c2372b3b54d2d52ec21d43695f2c73fb101caabc2d3bf524aa + languageName: node + linkType: hard + "@sapphire/ts-config@npm:^5.0.1": version: 5.0.1 resolution: "@sapphire/ts-config@npm:5.0.1" @@ -1158,6 +1261,7 @@ __metadata: "@typescript-eslint/parser": "npm:^7.13.1" "@vitest/coverage-v8": "npm:^1.6.0" cz-conventional-changelog: "npm:^3.3.0" + discord.js: "npm:^14.15.3" eslint: "npm:^8.57.0" eslint-config-prettier: "npm:^9.1.0" eslint-plugin-prettier: "npm:^5.1.3" @@ -1248,6 +1352,15 @@ __metadata: languageName: node linkType: hard +"@types/ws@npm:^8.5.10": + version: 8.5.10 + resolution: "@types/ws@npm:8.5.10" + dependencies: + "@types/node": "npm:*" + checksum: 10/9b414dc5e0b6c6f1ea4b1635b3568c58707357f68076df9e7cd33194747b7d1716d5189c0dbdd68c8d2521b148e88184cf881bac7429eb0e5c989b001539ed31 + languageName: node + linkType: hard + "@typescript-eslint/eslint-plugin@npm:^7.11.0, @typescript-eslint/eslint-plugin@npm:^7.13.1": version: 7.13.1 resolution: "@typescript-eslint/eslint-plugin@npm:7.13.1" @@ -1450,6 +1563,13 @@ __metadata: languageName: node linkType: hard +"@vladfrangu/async_event_emitter@npm:^2.2.4": + version: 2.4.3 + resolution: "@vladfrangu/async_event_emitter@npm:2.4.3" + checksum: 10/90b291955aa85390b3f3d58f362678ce0a04c327a8c8a1be61b94d81f9e6f22385d21e342f8d64988f11c1207f132d61210a1e45879f8a2faa29ab9a70cf5945 + languageName: node + linkType: hard + "JSONStream@npm:^1.3.5": version: 1.3.5 resolution: "JSONStream@npm:1.3.5" @@ -2385,6 +2505,33 @@ __metadata: languageName: node linkType: hard +"discord-api-types@npm:0.37.83": + version: 0.37.83 + resolution: "discord-api-types@npm:0.37.83" + checksum: 10/63aee8b4634f98e24921ba7ba82eab30105917d7bf40739b7fbe327a8c93134a78408d842bd7ad7cf3f45948d86db5ec0cb670d66e0fe0f8229cb15bfda5108f + languageName: node + linkType: hard + +"discord.js@npm:^14.15.3": + version: 14.15.3 + resolution: "discord.js@npm:14.15.3" + dependencies: + "@discordjs/builders": "npm:^1.8.2" + "@discordjs/collection": "npm:1.5.3" + "@discordjs/formatters": "npm:^0.4.0" + "@discordjs/rest": "npm:^2.3.0" + "@discordjs/util": "npm:^1.1.0" + "@discordjs/ws": "npm:^1.1.1" + "@sapphire/snowflake": "npm:3.5.3" + discord-api-types: "npm:0.37.83" + fast-deep-equal: "npm:3.1.3" + lodash.snakecase: "npm:4.1.1" + tslib: "npm:2.6.2" + undici: "npm:6.13.0" + checksum: 10/b18decbe8a0f1a3f569eb144be2a7039a08068131be2760190ac70fefdeb255cfed10ee0368e488f6c38d53d190c2988a6cc50c7db57a802cf54bb20cc47dc2e + languageName: node + linkType: hard + "doctrine@npm:^3.0.0": version: 3.0.0 resolution: "doctrine@npm:3.0.0" @@ -2807,7 +2954,7 @@ __metadata: languageName: node linkType: hard -"fast-deep-equal@npm:^3.1.1, fast-deep-equal@npm:^3.1.3": +"fast-deep-equal@npm:3.1.3, fast-deep-equal@npm:^3.1.1, fast-deep-equal@npm:^3.1.3": version: 3.1.3 resolution: "fast-deep-equal@npm:3.1.3" checksum: 10/e21a9d8d84f53493b6aa15efc9cfd53dd5b714a1f23f67fb5dc8f574af80df889b3bce25dc081887c6d25457cce704e636395333abad896ccdec03abaf1f3f9d @@ -4026,7 +4173,7 @@ __metadata: languageName: node linkType: hard -"lodash.snakecase@npm:^4.1.1": +"lodash.snakecase@npm:4.1.1, lodash.snakecase@npm:^4.1.1": version: 4.1.1 resolution: "lodash.snakecase@npm:4.1.1" checksum: 10/82ed40935d840477ef8fee64f9f263f75989c6cde36b84aae817246d95826228e1b5a7f6093c51de324084f86433634c7af244cb89496633cacfe443071450d0 @@ -4128,6 +4275,13 @@ __metadata: languageName: node linkType: hard +"magic-bytes.js@npm:^1.10.0": + version: 1.10.0 + resolution: "magic-bytes.js@npm:1.10.0" + checksum: 10/4b84d54b79914df3e9824ba6aba4eb01ea2555d14c7a4fa45be43ef8be3c286ad3a8dcdb536b328de2d7469dbbd3a21ee4b15d338cadc43eb08c0df249a19e7a + languageName: node + linkType: hard + "magic-string@npm:^0.30.5": version: 0.30.10 resolution: "magic-string@npm:0.30.10" @@ -5756,6 +5910,20 @@ __metadata: languageName: node linkType: hard +"ts-mixer@npm:^6.0.4": + version: 6.0.4 + resolution: "ts-mixer@npm:6.0.4" + checksum: 10/f20571a4a4ff7b5e1a2ff659208c1ea9d4180dda932b71d289edc99e25a2948c9048e2e676b930302ac0f8e88279e0da6022823183e67de3906a3f3a8b72ea80 + languageName: node + linkType: hard + +"tslib@npm:2.6.2": + version: 2.6.2 + resolution: "tslib@npm:2.6.2" + checksum: 10/bd26c22d36736513980091a1e356378e8b662ded04204453d353a7f34a4c21ed0afc59b5f90719d4ba756e581a162ecbf93118dc9c6be5acf70aa309188166ca + languageName: node + linkType: hard + "tslib@npm:^2.1.0, tslib@npm:^2.6.2, tslib@npm:^2.6.3": version: 2.6.3 resolution: "tslib@npm:2.6.3" @@ -5931,6 +6099,13 @@ __metadata: languageName: node linkType: hard +"undici@npm:6.13.0": + version: 6.13.0 + resolution: "undici@npm:6.13.0" + checksum: 10/4ec2038e95779d4f1114a5dcf5bc74ec59c7fc76f6287f8a6bea6d69113f0190e6d41cc6e14409b5d912b0a92ce910b33bfa05808f40b6bf2b802b58b427f2cf + languageName: node + linkType: hard + "undici@npm:^5.25.4": version: 5.28.4 resolution: "undici@npm:5.28.4" @@ -6282,6 +6457,21 @@ __metadata: languageName: node linkType: hard +"ws@npm:^8.16.0": + version: 8.18.0 + resolution: "ws@npm:8.18.0" + peerDependencies: + bufferutil: ^4.0.1 + utf-8-validate: ">=5.0.2" + peerDependenciesMeta: + bufferutil: + optional: true + utf-8-validate: + optional: true + checksum: 10/70dfe53f23ff4368d46e4c0b1d4ca734db2c4149c6f68bc62cb16fc21f753c47b35fcc6e582f3bdfba0eaeb1c488cddab3c2255755a5c3eecb251431e42b3ff6 + languageName: node + linkType: hard + "y18n@npm:^5.0.5": version: 5.0.8 resolution: "y18n@npm:5.0.8"