Skip to content

Commit

Permalink
the same name and values for comments option (cli and compress function)
Browse files Browse the repository at this point in the history
- specialComments -> comments
- values: exclamation, first-exclamation, none and boolean
(true=exclamation, false=none)
  • Loading branch information
lahmatiy committed May 8, 2016
1 parent a46e072 commit 34745de
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
16 changes: 6 additions & 10 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,15 @@ function resolveSourceMap(source, inputMap, map, inputFile, outputFile) {
};
}

function processCommentsOption(specialComments) {
switch (specialComments) {
function processCommentsOption(value) {
switch (value) {
case 'exclamation':
return 'all';

case 'first-exclamation':
return 'first-only';

case 'none':
return false;
return value;
}

console.error('Wrong value for `comments` option: %s', specialComments);
console.error('Wrong value for `comments` option: %s', value);
process.exit(2);
}

Expand All @@ -209,7 +205,7 @@ var command = cli.create('csso', '[input] [output]')
var map = options.map;
var inputMap = options.inputMap;
var structureOptimisationOff = options.restructureOff;
var specialComments = processCommentsOption(options.comments);
var comments = processCommentsOption(options.comments);
var debug = options.debug;
var statistics = options.stat;
var inputStream;
Expand Down Expand Up @@ -261,7 +257,7 @@ var command = cli.create('csso', '[input] [output]')
sourceMap: sourceMap.output,
usage: usageData,
restructure: !structureOptimisationOff,
specialComments: specialComments,
comments: comments,
debug: debug
});

Expand Down
22 changes: 9 additions & 13 deletions lib/compressor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@ function compressBlock(ast, usageData, num, logger) {
return ast;
}

function getSpecialCommentsOption(options) {
var specialComments = 'specialComments' in options ? options.specialComments : 'all';
function getCommentsOption(options) {
var comments = 'comments' in options ? options.comments : 'exclamation';

if (typeof specialComments === 'boolean') {
specialComments = specialComments ? 'all' : false;
} else if (specialComments !== 'all' && specialComments !== 'first-only') {
specialComments = false;
if (typeof comments === 'boolean') {
comments = comments ? 'exclamation' : false;
} else if (comments !== 'exclamation' && comments !== 'first-exclamation') {
comments = false;
}

return specialComments;
return comments;
}

function getRestructureOption(options) {
Expand All @@ -88,7 +88,7 @@ module.exports = function compress(ast, options) {
ast = ast || { type: 'StyleSheet', rules: new List() };

var logger = typeof options.logger === 'function' ? options.logger : Function();
var specialComments = getSpecialCommentsOption(options);
var specialComments = getCommentsOption(options);
var restructuring = getRestructureOption(options);
var result = new List();
var block;
Expand All @@ -99,10 +99,6 @@ module.exports = function compress(ast, options) {
var usageData = false;
var info = ast.info || null;

if (typeof specialComments === true) {
specialComments = 'all';
}

if (ast.type !== 'StyleSheet') {
blockMode = true;
ast = {
Expand Down Expand Up @@ -169,7 +165,7 @@ module.exports = function compress(ast, options) {
}
}

if (specialComments !== 'all') {
if (specialComments !== 'exclamation') {
specialComments = false;
}

Expand Down
26 changes: 15 additions & 11 deletions test/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,32 +90,36 @@ describe('compress', function() {
});
});

describe('specialComments option', function() {
describe('comments option', function() {
var css = '/*! first *//*! second *//*! third */';
var all = '/*! first */\n/*! second */\n/*! third */';

it('shouldn\'t remove exclamation comments by default', function() {
assert.equal(csso.minify(css).css, all);
});

it('shouldn\'t remove exclamation comments when specialComments is true', function() {
assert.equal(csso.minify(css, { specialComments: true }).css, all);
it('shouldn\'t remove exclamation comments when comments is true', function() {
assert.equal(csso.minify(css, { comments: true }).css, all);
});

it('shouldn\'t remove exclamation comments when specialComments is "all"', function() {
assert.equal(csso.minify(css, { specialComments: 'all' }).css, all);
it('shouldn\'t remove exclamation comments when comments is "exclamation"', function() {
assert.equal(csso.minify(css, { comments: 'exclamation' }).css, all);
});

it('should remove every exclamation comment when specialComments is false', function() {
assert.equal(csso.minify(css, { specialComments: false }).css, '');
it('should remove every exclamation comment when comments is false', function() {
assert.equal(csso.minify(css, { comments: false }).css, '');
});

it('should remove every exclamation comment when specialComments has wrong value', function() {
assert.equal(csso.minify(css, { specialComments: 'foo' }).css, '');
it('should remove every exclamation comment when comments is "none"', function() {
assert.equal(csso.minify(css, { comments: 'none' }).css, '');
});

it('should remove every exclamation comment except first when specialComments is "first-only"', function() {
assert.equal(csso.minify(css, { specialComments: 'first-only' }).css, '/*! first */');
it('should remove every exclamation comment when comments has wrong value', function() {
assert.equal(csso.minify(css, { comments: 'foo' }).css, '');
});

it('should remove every exclamation comment except first when comments is "first-exclamation"', function() {
assert.equal(csso.minify(css, { comments: 'first-exclamation' }).css, '/*! first */');
});
});

Expand Down

0 comments on commit 34745de

Please sign in to comment.