Skip to content

Commit

Permalink
feat(responsibility): modify interface
Browse files Browse the repository at this point in the history
- Remove static method `assign`
- Remove private function `setDescription`
- Add two properties:
   * descriptions
   * primary

BREAKING CHANGE: `CrcResponsibility's` interface now has three properties:

1. `comments` - An array of doctrine comments.
2. `descriptions` - A string array of all property and method descriptions.
3. `primary` - The description that pertains to a CrcClass.

#44,#187
  • Loading branch information
gregswindle committed Feb 18, 2018
1 parent 3964030 commit d6c982f
Showing 1 changed file with 49 additions and 43 deletions.
92 changes: 49 additions & 43 deletions lib/crc/crc-responsibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,38 @@ const {
first,
get,
isEmpty,
pull,
replace
} = require("lodash");

const getComments = (crcClass) => {
const comments = get(crcClass, "code.ast.comments", []);
return comments.map((comment) => doctrine.parse(
comment.value,
const cleanText = (text) => {
const SINGLE_SPACE = " ";

return replace(text, /\s+/gim, SINGLE_SPACE).trim();
};

const getComments = (crcClass) => get(crcClass, "code.ast.comments", [])
.map((comment) => doctrine.parse(
get(comment, "value"),
doctrineOpts.parse
));
};

const getPrimaryDescription =
(comments) => get(
comments.find((comment) => !isEmpty(comment.description)),
"description"
comments.find((comment) => !isEmpty(get(comment, "description"))),
"description",
""
);

const getDescriptionsFromTags = (comments) => {
const comment = comments.find((cmt) => {
const tags = get(cmt, "tags", []);
return tags.find((tag) => doctrineOpts.descriptions.includes(tag.title));
});
const tags = get(comment, "tags", []);
return pull(tags.map((tag) => get(tag, "description", null)), null);
};

const getDescriptionFromTags = (comments) => {
const comment = comments.find((cmt) => {
const tags = get(cmt, "tags", []);
Expand All @@ -39,32 +54,21 @@ const descriptionFromContext = (context) => {
"unwrap": true
}));

const description =
getPrimaryDescription(comments) || getDescriptionFromTags(comments);

const SINGLE_SPACE = " ";

return replace(description, /\s+/gim, SINGLE_SPACE).trim();
return cleanText(getPrimaryDescription(comments) ||
getDescriptionFromTags(comments));
};

const getDescription =
(crcClass) => descriptionFromContext(crcClass.meta.context);

/**
* Assigns a value to CrcClass#description, if one exists in JSDoc comments.
*
* @param {CrcClass} crcClass - The object/class we want to describe.
*
* @returns {void} Void.
*
* @example
* setDescription(crcClass);
* @ignore
* @private
*/

const setDescription = (crcClass) => {
crcClass.meta.description = getDescription(crcClass);
(crcClass) => cleanText(descriptionFromContext(crcClass.meta.context));

const getAllDescriptions = (comments) => {
const descriptions =
comments
.filter((comment) => !isEmpty(get(comment, "description")))
.map((comment) => get(comment, "description"))
.filter((description) => !description.startsWith("eslint-disable"));
const tagDescriptions = getDescriptionsFromTags(comments);
return descriptions.concat(tagDescriptions);
};

/**
Expand All @@ -76,6 +80,21 @@ const setDescription = (crcClass) => {
class CrcResponsibility {
constructor (crcClass) {
this.comments = getComments(crcClass);
this.descriptions = getAllDescriptions(this.comments);
this.primary = getDescription(crcClass);

// CrcClass.meta.description = this.primary;
}

/**
* Get all responsibilities.
*
* @returns {array.<string>} An arrary of resposibilities.
* @memberof CrcResponsibility
*/

valueOf () {
return this.descriptions;
}

/**
Expand Down Expand Up @@ -106,19 +125,6 @@ class CrcResponsibility {
static descriptionFromContext (context) {
return descriptionFromContext(context);
}

/**
* @static assign - Set CrcClass property values.
*
* @param {CrcClass} crcClass - The CrcClass whose `meta.description` property
* you want to assign.
*
* @returns {void}
*/

static assign (crcClass) {
setDescription(crcClass);
}
}

module.exports = CrcResponsibility;

0 comments on commit d6c982f

Please sign in to comment.