Skip to content

Commit

Permalink
refactor: standardize export style to named exports (#10630)
Browse files Browse the repository at this point in the history
* refactor: standardize export style to named exports

* refactor: export enums directly

* fix: update importing in generateRequires script

* fix: missed places

* feat: add eslint rule

* fix: ci errors

* fix: leftovers

* fix: remove accidentally readded interaction response

* fix: remove interaction response export

* fix: correct collection export

* chore: add another eslint rule

---------

Co-authored-by: almeidx <github@almeidx.dev>
Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Jan 18, 2025
1 parent c6e16c3 commit ad6b006
Show file tree
Hide file tree
Showing 287 changed files with 1,191 additions and 1,145 deletions.
24 changes: 24 additions & 0 deletions packages/discord.js/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,30 @@
"name": "clearInterval",
"message": "Import clearInterval from `node:timers` instead"
}
],

"no-restricted-syntax": [
"error",
{
"selector": "AssignmentExpression[left.object.name='module'][left.property.name='exports']",
"message": "Use named exports instead of module.exports"
},
{
"selector": "VariableDeclarator[init.callee.name='require'][init.arguments.0.value=/^\\./]:not([id.type='ObjectPattern'])",
"message": "Use object destructuring when requiring local modules"
}
]
}
},
{
"files": ["src/client/websocket/handlers/*.js"],
"rules": {
"no-restricted-syntax": [
"error",
{
"selector": "VariableDeclarator[init.callee.name='require'][init.arguments.0.value=/^\\./]:not([id.type='ObjectPattern'])",
"message": "Use object destructuring when requiring local modules"
}
]
}
},
Expand Down
12 changes: 7 additions & 5 deletions packages/discord.js/scripts/generateRequires.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { readdir, writeFile } from 'node:fs/promises';

async function writeWebsocketHandlerImports() {
const lines = ["'use strict';\n", 'const handlers = Object.fromEntries(['];
const lines = ["'use strict';\n", 'const PacketHandlers = Object.fromEntries(['];

const handlersDirectory = new URL('../src/client/websocket/handlers', import.meta.url);

for (const file of await (await readdir(handlersDirectory)).sort()) {
for (const file of (await readdir(handlersDirectory)).sort()) {
if (file === 'index.js') continue;

lines.push(` ['${file.slice(0, -3)}', require('./${file.slice(0, -3)}')],`);
}

lines.push(']);\n\nmodule.exports = handlers;\n');
lines.push(']);\n\nexports.PacketHandlers = PacketHandlers;\n');

const outputFile = new URL('../src/client/websocket/handlers/index.js', import.meta.url);

Expand All @@ -36,15 +36,17 @@ async function writeClientActionImports() {
for (const file of (await readdir(actionsDirectory)).sort()) {
if (file === 'Action.js' || file === 'ActionsManager.js') continue;

lines.push(` this.register(require('./${file.slice(0, -3)}'));`);
const actionName = file.slice(0, -3);

lines.push(` this.register(require('./${actionName}').${actionName}Action);`);
}

lines.push(' }\n');
lines.push(' register(Action) {');
lines.push(" this[Action.name.replace(/Action$/, '')] = new Action(this.client);");
lines.push(' }');
lines.push('}\n');
lines.push('module.exports = ActionsManager;\n');
lines.push('exports.ActionsManager = ActionsManager;\n');

const outputFile = new URL('../src/client/actions/ActionsManager.js', import.meta.url);

Expand Down
4 changes: 2 additions & 2 deletions packages/discord.js/src/client/BaseClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const EventEmitter = require('node:events');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v10');
const { DiscordjsTypeError, ErrorCodes } = require('../errors');
const Options = require('../util/Options');
const { Options } = require('../util/Options');
const { flatten } = require('../util/Util');

/**
Expand Down Expand Up @@ -112,7 +112,7 @@ class BaseClient extends EventEmitter {
}
}

module.exports = BaseClient;
exports.BaseClient = BaseClient;

/**
* @external REST
Expand Down
48 changes: 24 additions & 24 deletions packages/discord.js/src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@ const { Collection } = require('@discordjs/collection');
const { makeURLSearchParams } = require('@discordjs/rest');
const { WebSocketManager, WebSocketShardEvents, WebSocketShardStatus } = require('@discordjs/ws');
const { GatewayDispatchEvents, GatewayIntentBits, OAuth2Scopes, Routes } = require('discord-api-types/v10');
const BaseClient = require('./BaseClient');
const ActionsManager = require('./actions/ActionsManager');
const ClientVoiceManager = require('./voice/ClientVoiceManager');
const PacketHandlers = require('./websocket/handlers');
const { BaseClient } = require('./BaseClient');
const { ActionsManager } = require('./actions/ActionsManager');
const { ClientVoiceManager } = require('./voice/ClientVoiceManager');
const { PacketHandlers } = require('./websocket/handlers');
const { DiscordjsError, DiscordjsTypeError, ErrorCodes } = require('../errors');
const BaseGuildEmojiManager = require('../managers/BaseGuildEmojiManager');
const ChannelManager = require('../managers/ChannelManager');
const GuildManager = require('../managers/GuildManager');
const UserManager = require('../managers/UserManager');
const ShardClientUtil = require('../sharding/ShardClientUtil');
const ClientPresence = require('../structures/ClientPresence');
const GuildPreview = require('../structures/GuildPreview');
const GuildTemplate = require('../structures/GuildTemplate');
const Invite = require('../structures/Invite');
const { BaseGuildEmojiManager } = require('../managers/BaseGuildEmojiManager');
const { ChannelManager } = require('../managers/ChannelManager');
const { GuildManager } = require('../managers/GuildManager');
const { UserManager } = require('../managers/UserManager');
const { ShardClientUtil } = require('../sharding/ShardClientUtil');
const { ClientPresence } = require('../structures/ClientPresence');
const { GuildPreview } = require('../structures/GuildPreview');
const { GuildTemplate } = require('../structures/GuildTemplate');
const { Invite } = require('../structures/Invite');
const { Sticker } = require('../structures/Sticker');
const StickerPack = require('../structures/StickerPack');
const VoiceRegion = require('../structures/VoiceRegion');
const Webhook = require('../structures/Webhook');
const Widget = require('../structures/Widget');
const { StickerPack } = require('../structures/StickerPack');
const { VoiceRegion } = require('../structures/VoiceRegion');
const { Webhook } = require('../structures/Webhook');
const { Widget } = require('../structures/Widget');
const { resolveInviteCode, resolveGuildTemplateCode } = require('../util/DataResolver');
const Events = require('../util/Events');
const IntentsBitField = require('../util/IntentsBitField');
const Options = require('../util/Options');
const PermissionsBitField = require('../util/PermissionsBitField');
const Status = require('../util/Status');
const Sweepers = require('../util/Sweepers');
const { Events } = require('../util/Events');
const { IntentsBitField } = require('../util/IntentsBitField');
const { Options } = require('../util/Options');
const { PermissionsBitField } = require('../util/PermissionsBitField');
const { Status } = require('../util/Status');
const { Sweepers } = require('../util/Sweepers');

const WaitingForGuildEvents = [GatewayDispatchEvents.GuildCreate, GatewayDispatchEvents.GuildDelete];
const BeforeReadyWhitelist = [
Expand Down Expand Up @@ -732,7 +732,7 @@ class Client extends BaseClient {
}
}

module.exports = Client;
exports.Client = Client;

/**
* @class SnowflakeUtil
Expand Down
6 changes: 3 additions & 3 deletions packages/discord.js/src/client/WebhookClient.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const BaseClient = require('./BaseClient');
const { BaseClient } = require('./BaseClient');
const { DiscordjsError, ErrorCodes } = require('../errors');
const Webhook = require('../structures/Webhook');
const { Webhook } = require('../structures/Webhook');
const { parseWebhookURL } = require('../util/Util');

/**
Expand Down Expand Up @@ -100,4 +100,4 @@ class WebhookClient extends BaseClient {

Webhook.applyToClass(WebhookClient);

module.exports = WebhookClient;
exports.WebhookClient = WebhookClient;
6 changes: 3 additions & 3 deletions packages/discord.js/src/client/actions/Action.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Partials = require('../../util/Partials');
const { Partials } = require('../../util/Partials');

/*
Expand All @@ -14,7 +14,7 @@ that WebSocket events don't clash with REST methods.
*/

class GenericAction {
class Action {
constructor(client) {
this.client = client;
}
Expand Down Expand Up @@ -117,4 +117,4 @@ class GenericAction {
}
}

module.exports = GenericAction;
exports.Action = Action;
82 changes: 42 additions & 40 deletions packages/discord.js/src/client/actions/ActionsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,52 @@ class ActionsManager {
constructor(client) {
this.client = client;

this.register(require('./ChannelCreate'));
this.register(require('./ChannelDelete'));
this.register(require('./ChannelUpdate'));
this.register(require('./GuildChannelsPositionUpdate'));
this.register(require('./GuildEmojiCreate'));
this.register(require('./GuildEmojiDelete'));
this.register(require('./GuildEmojiUpdate'));
this.register(require('./GuildEmojisUpdate'));
this.register(require('./GuildMemberRemove'));
this.register(require('./GuildMemberUpdate'));
this.register(require('./GuildRoleCreate'));
this.register(require('./GuildRoleDelete'));
this.register(require('./GuildRolesPositionUpdate'));
this.register(require('./GuildScheduledEventDelete'));
this.register(require('./GuildScheduledEventUserAdd'));
this.register(require('./GuildScheduledEventUserRemove'));
this.register(require('./GuildStickerCreate'));
this.register(require('./GuildStickerDelete'));
this.register(require('./GuildStickerUpdate'));
this.register(require('./GuildStickersUpdate'));
this.register(require('./GuildUpdate'));
this.register(require('./InteractionCreate'));
this.register(require('./MessageCreate'));
this.register(require('./MessageDelete'));
this.register(require('./MessageDeleteBulk'));
this.register(require('./MessagePollVoteAdd'));
this.register(require('./MessagePollVoteRemove'));
this.register(require('./MessageReactionAdd'));
this.register(require('./MessageReactionRemove'));
this.register(require('./MessageReactionRemoveAll'));
this.register(require('./MessageReactionRemoveEmoji'));
this.register(require('./MessageUpdate'));
this.register(require('./StageInstanceCreate'));
this.register(require('./StageInstanceDelete'));
this.register(require('./StageInstanceUpdate'));
this.register(require('./ThreadCreate'));
this.register(require('./ThreadMembersUpdate'));
this.register(require('./TypingStart'));
this.register(require('./UserUpdate'));
this.register(require('./ChannelCreate').ChannelCreateAction);
this.register(require('./ChannelDelete').ChannelDeleteAction);
this.register(require('./ChannelUpdate').ChannelUpdateAction);
this.register(require('./GuildChannelsPositionUpdate').GuildChannelsPositionUpdateAction);
this.register(require('./GuildEmojiCreate').GuildEmojiCreateAction);
this.register(require('./GuildEmojiDelete').GuildEmojiDeleteAction);
this.register(require('./GuildEmojiUpdate').GuildEmojiUpdateAction);
this.register(require('./GuildEmojiUpdate').GuildEmojiUpdateAction);
this.register(require('./GuildEmojisUpdate').GuildEmojisUpdateAction);
this.register(require('./GuildMemberRemove').GuildMemberRemoveAction);
this.register(require('./GuildMemberUpdate').GuildMemberUpdateAction);
this.register(require('./GuildRoleCreate').GuildRoleCreateAction);
this.register(require('./GuildRoleDelete').GuildRoleDeleteAction);
this.register(require('./GuildRolesPositionUpdate').GuildRolesPositionUpdateAction);
this.register(require('./GuildScheduledEventDelete').GuildScheduledEventDeleteAction);
this.register(require('./GuildScheduledEventUserAdd').GuildScheduledEventUserAddAction);
this.register(require('./GuildScheduledEventUserRemove').GuildScheduledEventUserRemoveAction);
this.register(require('./GuildStickerCreate').GuildStickerCreateAction);
this.register(require('./GuildStickerDelete').GuildStickerDeleteAction);
this.register(require('./GuildStickerUpdate').GuildStickerUpdateAction);
this.register(require('./GuildStickerUpdate').GuildStickerUpdateAction);
this.register(require('./GuildStickersUpdate').GuildStickersUpdateAction);
this.register(require('./GuildUpdate').GuildUpdateAction);
this.register(require('./InteractionCreate').InteractionCreateAction);
this.register(require('./MessageCreate').MessageCreateAction);
this.register(require('./MessageDelete').MessageDeleteAction);
this.register(require('./MessageDeleteBulk').MessageDeleteBulkAction);
this.register(require('./MessagePollVoteAdd').MessagePollVoteAddAction);
this.register(require('./MessagePollVoteRemove').MessagePollVoteRemoveAction);
this.register(require('./MessageReactionAdd').MessageReactionAddAction);
this.register(require('./MessageReactionRemove').MessageReactionRemoveAction);
this.register(require('./MessageReactionRemoveAll').MessageReactionRemoveAllAction);
this.register(require('./MessageReactionRemoveEmoji').MessageReactionRemoveEmojiAction);
this.register(require('./MessageUpdate').MessageUpdateAction);
this.register(require('./StageInstanceCreate').StageInstanceCreateAction);
this.register(require('./StageInstanceDelete').StageInstanceDeleteAction);
this.register(require('./StageInstanceUpdate').StageInstanceUpdateAction);
this.register(require('./ThreadCreate').ThreadCreateAction);
this.register(require('./ThreadMembersUpdate').ThreadMembersUpdateAction);
this.register(require('./TypingStart').TypingStartAction);
this.register(require('./UserUpdate').UserUpdateAction);
}

register(Action) {
this[Action.name.replace(/Action$/, '')] = new Action(this.client);
}
}

module.exports = ActionsManager;
exports.ActionsManager = ActionsManager;
6 changes: 3 additions & 3 deletions packages/discord.js/src/client/actions/ChannelCreate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const Action = require('./Action');
const Events = require('../../util/Events');
const { Action } = require('./Action');
const { Events } = require('../../util/Events');

class ChannelCreateAction extends Action {
handle(data) {
Expand All @@ -20,4 +20,4 @@ class ChannelCreateAction extends Action {
}
}

module.exports = ChannelCreateAction;
exports.ChannelCreateAction = ChannelCreateAction;
6 changes: 3 additions & 3 deletions packages/discord.js/src/client/actions/ChannelDelete.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const Action = require('./Action');
const Events = require('../../util/Events');
const { Action } = require('./Action');
const { Events } = require('../../util/Events');

class ChannelDeleteAction extends Action {
handle(data) {
Expand All @@ -20,4 +20,4 @@ class ChannelDeleteAction extends Action {
}
}

module.exports = ChannelDeleteAction;
exports.ChannelDeleteAction = ChannelDeleteAction;
4 changes: 2 additions & 2 deletions packages/discord.js/src/client/actions/ChannelUpdate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Action = require('./Action');
const { Action } = require('./Action');
const { createChannel } = require('../../util/Channels');

class ChannelUpdateAction extends Action {
Expand Down Expand Up @@ -39,4 +39,4 @@ class ChannelUpdateAction extends Action {
}
}

module.exports = ChannelUpdateAction;
exports.ChannelUpdateAction = ChannelUpdateAction;
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const Action = require('./Action');
const { Action } = require('./Action');

class GuildChannelsPositionUpdate extends Action {
class GuildChannelsPositionUpdateAction extends Action {
handle(data) {
const client = this.client;

Expand All @@ -18,4 +18,4 @@ class GuildChannelsPositionUpdate extends Action {
}
}

module.exports = GuildChannelsPositionUpdate;
exports.GuildChannelsPositionUpdateAction = GuildChannelsPositionUpdateAction;
6 changes: 3 additions & 3 deletions packages/discord.js/src/client/actions/GuildEmojiCreate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const Action = require('./Action');
const Events = require('../../util/Events');
const { Action } = require('./Action');
const { Events } = require('../../util/Events');

class GuildEmojiCreateAction extends Action {
handle(guild, createdEmoji) {
Expand All @@ -17,4 +17,4 @@ class GuildEmojiCreateAction extends Action {
}
}

module.exports = GuildEmojiCreateAction;
exports.GuildEmojiCreateAction = GuildEmojiCreateAction;
6 changes: 3 additions & 3 deletions packages/discord.js/src/client/actions/GuildEmojiDelete.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const Action = require('./Action');
const Events = require('../../util/Events');
const { Action } = require('./Action');
const { Events } = require('../../util/Events');

class GuildEmojiDeleteAction extends Action {
handle(emoji) {
Expand All @@ -16,4 +16,4 @@ class GuildEmojiDeleteAction extends Action {
}
}

module.exports = GuildEmojiDeleteAction;
exports.GuildEmojiDeleteAction = GuildEmojiDeleteAction;
6 changes: 3 additions & 3 deletions packages/discord.js/src/client/actions/GuildEmojiUpdate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const Action = require('./Action');
const Events = require('../../util/Events');
const { Action } = require('./Action');
const { Events } = require('../../util/Events');

class GuildEmojiUpdateAction extends Action {
handle(current, data) {
Expand All @@ -17,4 +17,4 @@ class GuildEmojiUpdateAction extends Action {
}
}

module.exports = GuildEmojiUpdateAction;
exports.GuildEmojiUpdateAction = GuildEmojiUpdateAction;
Loading

0 comments on commit ad6b006

Please sign in to comment.