Skip to content

Commit

Permalink
refactor: use only single return when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Nov 21, 2016
1 parent 8652b07 commit 0ba7a47
Showing 1 changed file with 35 additions and 37 deletions.
72 changes: 35 additions & 37 deletions src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,52 +119,50 @@ class LicensePlugin {
const filePath = path.resolve(file);

const exists = fs.existsSync(filePath);
if (!exists) {
return {code};
}
const result = {code};

const content = fs.readFileSync(filePath, 'utf-8');
if (exists) {
const content = fs.readFileSync(filePath, 'utf-8');

// Create the template function with lodash.
const tmpl = _.template(content);
// Create the template function with lodash.
const tmpl = _.template(content);

// Generate the banner.
let banner = tmpl({
_,
moment,
pkg: this._pkg,
dependencies: _.values(this._dependencies),
});
// Generate the banner.
let banner = tmpl({
_,
moment,
pkg: this._pkg,
dependencies: _.values(this._dependencies),
});

// Make a block comment if needed
const trimmedBanner = banner.trim();
const start = trimmedBanner.slice(0, 3);
if (start !== '/**') {
const bannerContent = trimmedBanner
.split(`${EOL}`)
.map((line) => _.trimEnd(` * ${line}`))
.join(`${EOL}`);
// Make a block comment if needed
const trimmedBanner = banner.trim();
const start = trimmedBanner.slice(0, 3);
if (start !== '/**') {
const bannerContent = trimmedBanner
.split(`${EOL}`)
.map((line) => _.trimEnd(` * ${line}`))
.join(`${EOL}`);

banner = `/**${EOL}${bannerContent}${EOL} */${EOL}`;
}
banner = `/**${EOL}${bannerContent}${EOL} */${EOL}`;
}

// Create a magicString: do not manipulate the string directly since it
// will be used to generate the sourcemap.
const magicString = new MagicString(code);
// Create a magicString: do not manipulate the string directly since it
// will be used to generate the sourcemap.
const magicString = new MagicString(code);

// Prepend the banner.
magicString.prepend(`${banner}${EOL}`);
// Prepend the banner.
magicString.prepend(`${banner}${EOL}`);

// Create the result object.
const result = {
code: magicString.toString(),
};
// Create the result object.
result.code = magicString.toString();

// Add sourceMap information if it is enabled.
if (this._options.sourceMap !== false) {
result.map = magicString.generateMap({
hires: true,
});
// Add sourceMap information if it is enabled.
if (this._options.sourceMap !== false) {
result.map = magicString.generateMap({
hires: true,
});
}
}

return result;
Expand Down

0 comments on commit 0ba7a47

Please sign in to comment.