Clean up docgen code and some reaction stuff

This commit is contained in:
Schuyler Cebulskie
2016-11-12 22:07:17 -05:00
parent acdf2d14c2
commit 99b8d8f031
10 changed files with 76 additions and 103 deletions

View File

@@ -21,7 +21,6 @@ const DocumentedEvent = require('./DocumentedEvent');
*/ */
class DocumentedClass extends DocumentedItem { class DocumentedClass extends DocumentedItem {
constructor(docParent, data) { constructor(docParent, data) {
super(docParent, data); super(docParent, data);
this.props = new Map(); this.props = new Map();
@@ -70,9 +69,7 @@ class DocumentedClass extends DocumentedItem {
extends: augments, extends: augments,
access, access,
}; };
if (this.classConstructor) { if (this.classConstructor) serialized.classConstructor = this.classConstructor.serialize();
serialized.classConstructor = this.classConstructor.serialize();
}
serialized.methods = Array.from(this.methods.values()).map(m => m.serialize()); serialized.methods = Array.from(this.methods.values()).map(m => m.serialize());
serialized.properties = Array.from(this.props.values()).map(p => p.serialize()); serialized.properties = Array.from(this.props.values()).map(p => p.serialize());
serialized.events = Array.from(this.events.values()).map(e => e.serialize()); serialized.events = Array.from(this.events.values()).map(e => e.serialize());

View File

@@ -17,14 +17,11 @@ const DocumentedParam = require('./DocumentedParam');
*/ */
class DocumentedConstructor extends DocumentedItem { class DocumentedConstructor extends DocumentedItem {
registerMetaInfo(data) { registerMetaInfo(data) {
super.registerMetaInfo(data); super.registerMetaInfo(data);
this.directData = data; this.directData = data;
const newParams = []; const newParams = [];
for (const param of data.params) { for (const param of data.params) newParams.push(new DocumentedParam(this, param));
newParams.push(new DocumentedParam(this, param));
}
this.directData.params = newParams; this.directData.params = newParams;
} }
@@ -40,7 +37,6 @@ class DocumentedConstructor extends DocumentedItem {
params: params.map(p => p.serialize()), params: params.map(p => p.serialize()),
}; };
} }
} }
module.exports = DocumentedConstructor; module.exports = DocumentedConstructor;

View File

@@ -50,15 +50,12 @@ const DocumentedParam = require('./DocumentedParam');
*/ */
class DocumentedEvent extends DocumentedItem { class DocumentedEvent extends DocumentedItem {
registerMetaInfo(data) { registerMetaInfo(data) {
this.directData = data; this.directData = data;
this.directData.meta = new DocumentedItemMeta(this, data.meta); this.directData.meta = new DocumentedItemMeta(this, data.meta);
const newParams = []; const newParams = [];
data.params = data.params || []; data.params = data.params || [];
for (const param of data.params) { for (const param of data.params) newParams.push(new DocumentedParam(this, param));
newParams.push(new DocumentedParam(this, param));
}
this.directData.params = newParams; this.directData.params = newParams;
} }
@@ -74,7 +71,6 @@ class DocumentedEvent extends DocumentedItem {
params: params.map(p => p.serialize()), params: params.map(p => p.serialize()),
}; };
} }
} }
module.exports = DocumentedEvent; module.exports = DocumentedEvent;

View File

@@ -50,7 +50,6 @@ const DocumentedParam = require('./DocumentedParam');
*/ */
class DocumentedFunction extends DocumentedItem { class DocumentedFunction extends DocumentedItem {
registerMetaInfo(data) { registerMetaInfo(data) {
super.registerMetaInfo(data); super.registerMetaInfo(data);
this.directData = data; this.directData = data;
@@ -59,9 +58,7 @@ class DocumentedFunction extends DocumentedItem {
names: ['void'], names: ['void'],
}); });
const newParams = []; const newParams = [];
for (const param of data.params) { for (const param of data.params) newParams.push(new DocumentedParam(this, param));
newParams.push(new DocumentedParam(this, param));
}
this.directData.params = newParams; this.directData.params = newParams;
} }

View File

@@ -1,8 +1,8 @@
const cwd = (`${process.cwd()}\\`).replace(/\\/g, '/');
const backToForward = /\\/g;
const DocumentedItem = require('./DocumentedItem'); const DocumentedItem = require('./DocumentedItem');
const cwd = `${process.cwd()}\\`.replace(/\\/g, '/');
const backToForward = /\\/g;
/* /*
{ lineno: 7, { lineno: 7,
filename: 'VoiceChannel.js', filename: 'VoiceChannel.js',
@@ -10,7 +10,6 @@ const DocumentedItem = require('./DocumentedItem');
*/ */
class DocumentedItemMeta extends DocumentedItem { class DocumentedItemMeta extends DocumentedItem {
registerMetaInfo(data) { registerMetaInfo(data) {
super.registerMetaInfo(data); super.registerMetaInfo(data);
this.directData.line = data.lineno; this.directData.line = data.lineno;
@@ -23,7 +22,6 @@ class DocumentedItemMeta extends DocumentedItem {
const { line, file, path } = this.directData; const { line, file, path } = this.directData;
return { line, file, path }; return { line, file, path };
} }
} }
module.exports = DocumentedItemMeta; module.exports = DocumentedItemMeta;

View File

@@ -21,7 +21,6 @@ const DocumentedParam = require('./DocumentedParam');
*/ */
class DocumentedMember extends DocumentedItem { class DocumentedMember extends DocumentedItem {
registerMetaInfo(data) { registerMetaInfo(data) {
super.registerMetaInfo(data); super.registerMetaInfo(data);
this.directData = data; this.directData = data;
@@ -52,7 +51,6 @@ class DocumentedMember extends DocumentedItem {
props: properties.map(p => p.serialize()), props: properties.map(p => p.serialize()),
}; };
} }
} }
module.exports = DocumentedMember; module.exports = DocumentedMember;

View File

@@ -14,7 +14,6 @@ const DocumentedVarType = require('./DocumentedVarType');
*/ */
class DocumentedParam extends DocumentedItem { class DocumentedParam extends DocumentedItem {
registerMetaInfo(data) { registerMetaInfo(data) {
super.registerMetaInfo(data); super.registerMetaInfo(data);
this.directData = data; this.directData = data;

View File

@@ -19,11 +19,6 @@ const DocumentedParam = require('./DocumentedParam');
*/ */
class DocumentedTypeDef extends DocumentedItem { class DocumentedTypeDef extends DocumentedItem {
constructor(...args) {
super(...args);
}
registerMetaInfo(data) { registerMetaInfo(data) {
super.registerMetaInfo(data); super.registerMetaInfo(data);
this.props = new Map(); this.props = new Map();
@@ -31,9 +26,7 @@ class DocumentedTypeDef extends DocumentedItem {
this.directData.meta = new DocumentedItemMeta(this, data.meta); this.directData.meta = new DocumentedItemMeta(this, data.meta);
this.directData.type = new DocumentedVarType(this, data.type); this.directData.type = new DocumentedVarType(this, data.type);
data.properties = data.properties || []; data.properties = data.properties || [];
for (const prop of data.properties) { for (const prop of data.properties) this.props.set(prop.name, new DocumentedParam(this, prop));
this.props.set(prop.name, new DocumentedParam(this, prop));
}
} }
serialize() { serialize() {
@@ -50,7 +43,6 @@ class DocumentedTypeDef extends DocumentedItem {
serialized.properties = Array.from(this.props.values()).map(p => p.serialize()); serialized.properties = Array.from(this.props.values()).map(p => p.serialize());
return serialized; return serialized;
} }
} }
module.exports = DocumentedTypeDef; module.exports = DocumentedTypeDef;

View File

@@ -11,10 +11,24 @@ const DocumentedItem = require('./DocumentedItem');
const regex = /([\w]+)([^\w]+)/; const regex = /([\w]+)([^\w]+)/;
const regexG = /([\w]+)([^\w]+)/g; const regexG = /([\w]+)([^\w]+)/g;
function splitVarName(str) { class DocumentedVarType extends DocumentedItem {
if (str === '*') { registerMetaInfo(data) {
return ['*', '']; super.registerMetaInfo(data);
this.directData = data;
} }
serialize() {
super.serialize();
const names = [];
for (const name of this.directData.names) names.push(splitVarName(name));
return {
types: names,
};
}
}
function splitVarName(str) {
if (str === '*') return ['*', ''];
const matches = str.match(regexG); const matches = str.match(regexG);
const output = []; const output = [];
if (matches) { if (matches) {
@@ -28,23 +42,4 @@ function splitVarName(str) {
return output; return output;
} }
class DocumentedVarType extends DocumentedItem {
registerMetaInfo(data) {
super.registerMetaInfo(data);
this.directData = data;
}
serialize() {
super.serialize();
const names = [];
for (const name of this.directData.names) {
names.push(splitVarName(name));
}
return {
types: names,
};
}
}
module.exports = DocumentedVarType; module.exports = DocumentedVarType;

View File

@@ -164,40 +164,6 @@ class Message {
} }
} }
_addReaction(emoji, user) {
const emojiID = emoji.id ? `${emoji.name}:${emoji.id}` : emoji.name;
let reaction;
if (this.reactions.has(emojiID)) {
reaction = this.reactions.get(emojiID);
if (!reaction.me) reaction.me = user.id === this.client.user.id;
} else {
reaction = new MessageReaction(this, emoji, 0, user.id === this.client.user.id);
this.reactions.set(emojiID, reaction);
}
if (!reaction.users.has(user.id)) {
reaction.users.set(user.id, user);
reaction.count++;
return reaction;
}
return null;
}
_removeReaction(emoji, user) {
const emojiID = emoji.id || emoji;
if (this.reactions.has(emojiID)) {
const reaction = this.reactions.get(emojiID);
if (reaction.users.has(user.id)) {
reaction.users.delete(user.id);
reaction.count--;
if (user.id === this.client.user.id) {
reaction.me = false;
}
return reaction;
}
}
return null;
}
patch(data) { // eslint-disable-line complexity patch(data) { // eslint-disable-line complexity
if (data.author) { if (data.author) {
this.author = this.client.users.get(data.author.id); this.author = this.client.users.get(data.author.id);
@@ -323,18 +289,6 @@ class Message {
}); });
} }
addReaction(emoji) {
if (emoji.identifier) {
emoji = emoji.identifier;
} else if (typeof emoji === 'string') {
if (!emoji.includes('%')) emoji = encodeURIComponent(emoji);
} else {
return Promise.reject(`Emoji must be a string or an Emoji/ReactionEmoji`);
}
return this.client.rest.methods.addMessageReaction(this.channel.id, this.id, emoji);
}
/** /**
* An array of cached versions of the message, including the current version. * An array of cached versions of the message, including the current version.
* Sorted from latest (first) to oldest (last). * Sorted from latest (first) to oldest (last).
@@ -427,6 +381,23 @@ class Message {
return this.client.rest.methods.unpinMessage(this); return this.client.rest.methods.unpinMessage(this);
} }
/**
* Adds a reaction to the message
* @param {Emoji|ReactionEmoji|string} emoji Emoji to react with
* @returns {Promise<MessageReaction>}
*/
addReaction(emoji) {
if (emoji.identifier) {
emoji = emoji.identifier;
} else if (typeof emoji === 'string') {
if (!emoji.includes('%')) emoji = encodeURIComponent(emoji);
} else {
return Promise.reject('The emoji must be a string or an Emoji/ReactionEmoji.');
}
return this.client.rest.methods.addMessageReaction(this.channel.id, this.id, emoji);
}
/** /**
* Deletes the message * Deletes the message
* @param {number} [timeout=0] How long to wait to delete the message in milliseconds * @param {number} [timeout=0] How long to wait to delete the message in milliseconds
@@ -513,6 +484,40 @@ class Message {
toString() { toString() {
return this.content; return this.content;
} }
_addReaction(emoji, user) {
const emojiID = emoji.id ? `${emoji.name}:${emoji.id}` : emoji.name;
let reaction;
if (this.reactions.has(emojiID)) {
reaction = this.reactions.get(emojiID);
if (!reaction.me) reaction.me = user.id === this.client.user.id;
} else {
reaction = new MessageReaction(this, emoji, 0, user.id === this.client.user.id);
this.reactions.set(emojiID, reaction);
}
if (!reaction.users.has(user.id)) {
reaction.users.set(user.id, user);
reaction.count++;
return reaction;
}
return null;
}
_removeReaction(emoji, user) {
const emojiID = emoji.id || emoji;
if (this.reactions.has(emojiID)) {
const reaction = this.reactions.get(emojiID);
if (reaction.users.has(user.id)) {
reaction.users.delete(user.id);
reaction.count--;
if (user.id === this.client.user.id) {
reaction.me = false;
}
return reaction;
}
}
return null;
}
} }
module.exports = Message; module.exports = Message;