Skip to content

Commit

Permalink
refactor: small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Aug 1, 2019
1 parent e273e6b commit 8740fd6
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ const COMMENT_STYLES = {
none: null,
};

/**
* Compute the comment style to use for given text:
* - If text starts with a block comment, nothing is done (i.e use `none`).
* - Otherwise, use the `regular` style.
*
* @param {string} text The text to comment.
* @return {string} The comment style name.
*/
function computeDefaultCommentStyle(text) {
const trimmedText = text.trim();
const start = trimmedText.slice(0, 3);
const startWithComment = start === '/**' || start === '/*!';
return startWithComment ? 'none' : 'regular';
}

/**
* The plugin name.
* @type {string}
Expand Down Expand Up @@ -403,23 +418,16 @@ module.exports = class LicensePlugin {
data,
});

// Make a block comment if needed
const trimmedBanner = text.trim();
const start = trimmedBanner.slice(0, 3);
const startWithComment = start === '/**' || start === '/*!';
// Compute comment style to use.
const style = _.has(banner, 'commentStyle') ? banner.commentStyle : computeDefaultCommentStyle(text);

if (!startWithComment) {
const style = _.has(banner, 'commentStyle') ? banner.commentStyle : 'regular';
if (!_.has(COMMENT_STYLES, style)) {
throw new Error(`Unknown comment style ${style}, please use one of: ${_.keys(COMMENT_STYLES)}`);
}

const commentStyle = COMMENT_STYLES[style];
if (commentStyle) {
return generateBlockComment(text, commentStyle);
}
// Ensure given style name is valid.
if (!_.has(COMMENT_STYLES, style)) {
throw new Error(`Unknown comment style ${style}, please use one of: ${_.keys(COMMENT_STYLES)}`);
}

return text;
this.debug(`generate banner using comment style: ${style}`);

return COMMENT_STYLES[style] ? generateBlockComment(text, COMMENT_STYLES[style]) : text;
}
};

0 comments on commit 8740fd6

Please sign in to comment.