diff --git a/.jscsrc b/.jscsrc new file mode 100644 index 00000000..ab97edfe --- /dev/null +++ b/.jscsrc @@ -0,0 +1,207 @@ +{ + "excludeFiles": [ + "./docs", + "./dist", + "./node_modules", + "./lib/parser" + ], + + "disallowEmptyBlocks": true, + + "disallowImplicitTypeConversion": ["numeric", "boolean", "binary", "string"], + + "disallowKeywords": ["with"], + + "disallowKeywordsOnNewLine": ["else", "catch", "finally"], + + "disallowMixedSpacesAndTabs": true, + + "disallowMultipleLineBreaks": true, + + "disallowMultipleLineStrings": true, + + "disallowNewlineBeforeBlockStatements": true, + + "disallowOperatorBeforeLineBreak": ["."], + + "disallowSpaceAfterObjectKeys": true, + + "disallowSpaceAfterPrefixUnaryOperators": ["++", "--", "+", "-", "~", "!"], + + "disallowSpaceBeforePostfixUnaryOperators": ["++", "--"], + + "disallowSpacesInCallExpression": true, + + "disallowSpacesInFunction": { + "beforeOpeningRoundBrace": true + }, + + "disallowSpacesInFunctionDeclaration": { + "beforeOpeningRoundBrace": true + }, + + "disallowSpacesInFunctionExpression": { + "beforeOpeningRoundBrace": true + }, + + "disallowSpacesInNamedFunctionExpression": { + "beforeOpeningRoundBrace": true + }, + + "disallowSpacesInsideArrayBrackets": "all", + + "disallowSpacesInsideParentheses": true, + + "disallowTrailingComma": true, + + "disallowTrailingWhitespace": true, + + "disallowYodaConditions": true, + + "requireBlocksOnNewline": true, + + "requireCamelCaseOrUpperCaseIdentifiers": true, + + "requireCapitalizedConstructors": true, + + "requireCommaBeforeLineBreak": true, + + "requireCurlyBraces": [ + "if", + "else", + "for", + "while", + "do", + "try", + "catch", + "finally" + ], + + "requireDotNotation": true, + + "requireKeywordsOnNewLine": ["return", "for", "try", "do", "switch", "case"], + + "requireLineBreakAfterVariableAssignment": true, + + "requireLineFeedAtFileEnd": true, + + "disallowMultipleVarDecl": true, + + "requireOperatorBeforeLineBreak": [ + "=", + "+", + "-", + "/", + "*", + "==", + "===", + "!=", + "!==", + ">", + ">=", + "<", + "<=" + ], + + "requireParenthesesAroundIIFE": true, + + "requireSpaceAfterBinaryOperators": [ + "=", + ",", + "+", + "-", + "/", + "*", + "==", + "===", + "!=", + "!==", + "<", + ">", + "<=", + ">=", + "%" + ], + + "requireSpaceAfterKeywords": [ + "if", + "else", + "for", + "while", + "do", + "switch", + "case", + "return", + "try", + "catch", + "finally", + "typeof" + ], + + "requireSpaceAfterLineComment": true, + + "requireSpaceBeforeBinaryOperators": [ + "=", + "+", + "-", + "/", + "*", + "==", + "===", + "!=", + "!==", + "<", + ">", + "<=", + ">=", + "%" + ], + + "requireSpaceBeforeBlockStatements": true, + + "requireSpaceBeforeKeywords": [ + "else", + "while", + "catch", + "finally" + ], + + "requireSpaceBeforeObjectValues": true, + + "requireSpaceBetweenArguments": true, + + "requireSpacesInConditionalExpression": { + "afterTest": true, + "beforeConsequent": true, + "afterConsequent": true, + "beforeAlternate": true + }, + + "requireSpacesInForStatement": true, + + "requireSpacesInFunction": { + "beforeOpeningCurlyBrace": true + }, + + "requireSpacesInFunctionDeclaration": { + "beforeOpeningCurlyBrace": true + }, + + "requireSpacesInFunctionExpression": { + "beforeOpeningCurlyBrace": true + }, + + "requireSpacesInNamedFunctionExpression": { + "beforeOpeningCurlyBrace": true + }, + + "requireSpacesInsideObjectBrackets": "all", + + "validateIndentation": 4, + + "validateLineBreaks": "LF", + + "validateParameterSeparator": ", ", + + "validateQuoteMarks": { "mark": "'", "escape": true } +} diff --git a/bin/csso b/bin/csso index 0fa35837..79fe3842 100755 --- a/bin/csso +++ b/bin/csso @@ -4,13 +4,13 @@ var cli = require('../lib/cli.js'); try { cli.run(); -} catch(e) { +} catch (e) { // output user frendly message if cli error if (cli.isCliError(e)) { console.error(e.message || e); process.exit(2); } - + // otherwise re-throw exception throw e; } diff --git a/docs/.bem/level.js b/docs/.bem/level.js deleted file mode 100644 index 9228c82b..00000000 --- a/docs/.bem/level.js +++ /dev/null @@ -1,5 +0,0 @@ -exports.getTechs = function() { - return { - 'md': require.resolve('./techs/md.js') - }; -}; diff --git a/docs/.bem/techs/md.js b/docs/.bem/techs/md.js deleted file mode 100644 index df9aba2a..00000000 --- a/docs/.bem/techs/md.js +++ /dev/null @@ -1,19 +0,0 @@ -var DEFAULT_LANGS = ['ru', 'en']; - -exports.techMixin = { - - getLangs: function() { - var env = process.env.BEM_I18N_LANGS; - return env? env.split(' ') : [].concat(DEFAULT_LANGS); - }, - - getSuffixes: function() { - - return this.getLangs() - .map(function(lang) { - return lang + '.md'; - }); - - } - -}; diff --git a/lib/cli.js b/lib/cli.js index c59abf24..705234f5 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -33,7 +33,7 @@ var command = cli.create('csso', '[input] [output]') var structureOptimisationOff = this.values.restructureOff; var debug = this.values.debug; var input = inputFile ? fs.createReadStream(inputFile) : process.stdin; - + readFromStream(input, function(source) { var result = csso.minify(source, { restruturing: !structureOptimisationOff, @@ -50,7 +50,7 @@ var command = cli.create('csso', '[input] [output]') module.exports = { run: command.run.bind(command), - isCliError: function(err){ + isCliError: function(err) { return err instanceof cli.Error; } }; diff --git a/lib/compressor/const.js b/lib/compressor/const.js index 85239c9a..1c2b3f3a 100644 --- a/lib/compressor/const.js +++ b/lib/compressor/const.js @@ -411,42 +411,42 @@ exports.colorNameToHex = { }; exports.colorHexToName = { - "800000": "maroon", - "800080": "purple", - "808000": "olive", - "808080": "gray", - "00ffff": "cyan", - "f0ffff": "azure", - "f5f5dc": "beige", - "ffe4c4": "bisque", - "000000": "black", - "0000ff": "blue", - "a52a2a": "brown", - "ff7f50": "coral", - "ffd700": "gold", - "008000": "green", - "4b0082": "indigo", - "fffff0": "ivory", - "f0e68c": "khaki", - "00ff00": "lime", - "faf0e6": "linen", - "000080": "navy", - "ffa500": "orange", - "da70d6": "orchid", - "cd853f": "peru", - "ffc0cb": "pink", - "dda0dd": "plum", - "f00": "red", - "ff0000": "red", - "fa8072": "salmon", - "a0522d": "sienna", - "c0c0c0": "silver", - "fffafa": "snow", - "d2b48c": "tan", - "008080": "teal", - "ff6347": "tomato", - "ee82ee": "violet", - "f5deb3": "wheat", - "ffffff": "white", - "ffff00": "yellow" + '800000': 'maroon', + '800080': 'purple', + '808000': 'olive', + '808080': 'gray', + '00ffff': 'cyan', + 'f0ffff': 'azure', + 'f5f5dc': 'beige', + 'ffe4c4': 'bisque', + '000000': 'black', + '0000ff': 'blue', + 'a52a2a': 'brown', + 'ff7f50': 'coral', + 'ffd700': 'gold', + '008000': 'green', + '4b0082': 'indigo', + 'fffff0': 'ivory', + 'f0e68c': 'khaki', + '00ff00': 'lime', + 'faf0e6': 'linen', + '000080': 'navy', + 'ffa500': 'orange', + 'da70d6': 'orchid', + 'cd853f': 'peru', + 'ffc0cb': 'pink', + 'dda0dd': 'plum', + 'f00': 'red', + 'ff0000': 'red', + 'fa8072': 'salmon', + 'a0522d': 'sienna', + 'c0c0c0': 'silver', + 'fffafa': 'snow', + 'd2b48c': 'tan', + '008080': 'teal', + 'ff6347': 'tomato', + 'ee82ee': 'violet', + 'f5deb3': 'wheat', + 'ffffff': 'white', + 'ffff00': 'yellow' }; diff --git a/lib/compressor/index.js b/lib/compressor/index.js index 8ba2481b..0a465840 100644 --- a/lib/compressor/index.js +++ b/lib/compressor/index.js @@ -27,7 +27,7 @@ CSSOCompressor.prototype.process = function(rules, token, container, idx, path) if (tmp === null) { return null; } - + if (tmp !== undefined) { result = tmp; } @@ -327,7 +327,7 @@ CSSOCompressor.prototype.cleanImport = function(token, rule, parent, i) { if (type !== 's' && type !== 'comment') { if (type === 'atrules') { var atrule = parent[i][2][2][2]; - + if (atrule !== 'import' && atrule !== 'charset') { return null; } @@ -420,12 +420,12 @@ CSSOCompressor.prototype.issue16 = function(prevType, nextType) { return nextType && prevType === 'uri'; }; -//See https://github.com/css/csso/issues/165 +// See https://github.com/css/csso/issues/165 CSSOCompressor.prototype.issue165 = function(container, prevType, nextType) { return container[1] === 'atrulerq' && prevType === 'braces' && nextType === 'ident'; }; -//See https://github.com/css/csso/issues/134 +// See https://github.com/css/csso/issues/134 CSSOCompressor.prototype.issue134 = function(prevType, nextType) { return prevType === 'funktion' && (nextType === 'funktion' || nextType === 'vhash'); }; @@ -476,7 +476,7 @@ CSSOCompressor.prototype.cleanDecldelim = function(token) { }; function packNumber(value) { - value = value + value = value .replace(/^0+/, '') .replace(/\.0*$/, '') .replace(/(\..*\d+)0+$/, '$1'); @@ -907,7 +907,6 @@ CSSOCompressor.prototype.markShorthands = function(token, rule, parent, j, path) shorts.push(sh); } - childInfo.removeByShort = true; childInfo.shorthandKey = { key: key, i: shortsI }; sh.add(property, value[0].s, value.slice(2), important); @@ -1045,7 +1044,7 @@ CSSOCompressor.prototype.buildPPre = function(pre, property, value, d, freeze) { break; case 'funktion': - switch(value[i][2][2]) { + switch (value[i][2][2]) { case 'rgb': colorMark[0] = 1; break; @@ -1103,7 +1102,7 @@ CSSOCompressor.prototype.getVendorFromString = function(string) { CSSOCompressor.prototype.deleteProperty = function(block, id) { for (var i = block.length - 1; i > 1; i--) { var child = block[i]; - + if (Array.isArray(child) && child[1] === 'declaration' && child[0].id === id) { @@ -1132,7 +1131,7 @@ CSSOCompressor.prototype.needless = function(name, props, pre, important, v, d, for (var i = 0; i < table.length; i++) { var ppre = this.buildPPre(pre, hack + vendor + table[i], v, d, freeze); var property = props[ppre]; - + if (property) { return (!important || property.important); } @@ -1284,10 +1283,10 @@ CSSOCompressor.prototype.restructureRuleset = function(token, rule, parent, i) { // ok, it's good enough to extract if (blockLength >= newSelectorLength) { var newRuleset = [ - {f:0, l:0}, + {}, 'ruleset', newSelector, - [{f:0,l:0}, 'block'].concat(analyzeInfo.eq) + [{}, 'block'].concat(analyzeInfo.eq) ]; newSelector[0].s = newSelectorStr; @@ -1374,7 +1373,7 @@ CSSOCompressor.prototype.equalHash = function(h0, h1) { return false; } } - + for (var key in h1) { if (key in h0 === false) { return false; diff --git a/lib/compressor/trbl.js b/lib/compressor/trbl.js index 89bb3417..9ea20134 100644 --- a/lib/compressor/trbl.js +++ b/lib/compressor/trbl.js @@ -81,7 +81,7 @@ TRBL.prototype.add = function(name, sValue, tValue, important) { var side = SIDE[name]; var wasUnary = false; var values = []; - + important = important ? 1 : 0; if (side) { @@ -209,7 +209,7 @@ TRBL.prototype.getValue = function() { } if (this.impSum()) { - result.push([{ s: '!important'}, 'important']); + result.push([{ s: '!important' }, 'important']); } return result; diff --git a/lib/utils/cleanInfo.js b/lib/utils/cleanInfo.js index 960fc003..51f6560a 100644 --- a/lib/utils/cleanInfo.js +++ b/lib/utils/cleanInfo.js @@ -1,4 +1,4 @@ -module.exports = function cleanInfo(tree){ +module.exports = function cleanInfo(tree) { var res = tree.slice(1); for (var i = 1, token; token = res[i]; i++) { diff --git a/lib/utils/stringify.js b/lib/utils/stringify.js index b0ca03c5..809e3b10 100644 --- a/lib/utils/stringify.js +++ b/lib/utils/stringify.js @@ -2,7 +2,7 @@ function indent(num) { return new Array(num + 1).join(' '); } -function escape(str){ +function escape(str) { return str .replace(/\\/g, '\\\\') .replace(/\r/g, '\\r') @@ -11,7 +11,7 @@ function escape(str){ .replace(/"/g, '\\"'); } -module.exports = function stringify(val, level){ +module.exports = function stringify(val, level) { level = level || 0; if (typeof val == 'string') { @@ -19,7 +19,7 @@ module.exports = function stringify(val, level){ } if (val && val.constructor === Object) { - var body = Object.keys(val).map(function(k){ + var body = Object.keys(val).map(function(k) { return indent(level + 1) + '"' + escape(k) + '": ' + stringify(val[k], level + 1); }).join(',\n'); @@ -28,7 +28,7 @@ module.exports = function stringify(val, level){ if (Array.isArray(val)) { var join = true; - var body = val.map(function(item, idx){ + var body = val.map(function(item, idx) { var prefix = idx ? ' ' : ''; if (Array.isArray(item) && (!join || !item.some(Array.isArray))) { @@ -40,8 +40,8 @@ module.exports = function stringify(val, level){ }).join(','); if (/\n/.test(body)) { - body = - '\n' + indent(level + 1) + + body = + '\n' + indent(level + 1) + body + '\n' + indent(level); } diff --git a/lib/utils/translate.js b/lib/utils/translate.js index 8af483c2..3b0e5502 100644 --- a/lib/utils/translate.js +++ b/lib/utils/translate.js @@ -47,77 +47,79 @@ var typeHandlers = { uri: uri, functionExpression: functionExpression, - cdo: function(){ + cdo: function() { buffer.push('cdo'); }, - cdc: function(){ + cdc: function() { buffer.push('cdc'); }, - decldelim: function(){ + decldelim: function() { buffer.push(';'); }, - namespace: function(){ + namespace: function() { buffer.push('|'); }, - delim: function(){ + delim: function() { buffer.push(','); } }; -function simple(token){ +function simple(token) { buffer.push(token[useInfo + 1]); } -function composite(token){ - for (var i = useInfo + 1; i < token.length; i++) +function composite(token) { + for (var i = useInfo + 1; i < token.length; i++) { translate(token[i]); + } } -function compositeFrom(token, i){ - for (; i < token.length; i++) +function compositeFrom(token, i) { + for (; i < token.length; i++) { translate(token[i]); + } } -function percentage(token){ +function percentage(token) { translate(token[useInfo + 1]); buffer.push('%'); } -function comment(token){ +function comment(token) { buffer.push('/*', token[useInfo + 1], '*/'); } -function clazz(token){ +function clazz(token) { buffer.push('.'); translate(token[useInfo + 1]); } -function atkeyword(token){ +function atkeyword(token) { buffer.push('@'); translate(token[useInfo + 1]); } -function shash(token){ +function shash(token) { buffer.push('#', token[useInfo + 1]); } -function vhash(token){ +function vhash(token) { buffer.push('#', token[useInfo + 1]); } -function attrib(token){ +function attrib(token) { buffer.push('['); composite(token); buffer.push(']'); } -function important(token){ +function important(token) { buffer.push('!'); composite(token); buffer.push('important'); } -function nthselector(token){ +function nthselector(token) { buffer.push(':'); simple(token[useInfo + 1]); buffer.push('('); @@ -125,43 +127,43 @@ function nthselector(token){ buffer.push(')'); } -function funktion(token){ +function funktion(token) { simple(token[useInfo + 1]); buffer.push('('); composite(token[useInfo + 2]); buffer.push(')'); } -function declaration(token){ +function declaration(token) { translate(token[useInfo + 1]); buffer.push(':'); translate(token[useInfo + 2]); } -function filter(token){ +function filter(token) { translate(token[useInfo + 1]); buffer.push(':'); translate(token[useInfo + 2]); } -function block(token){ +function block(token) { buffer.push('{'); composite(token); buffer.push('}'); } -function braces(token){ +function braces(token) { buffer.push(token[useInfo + 1]); compositeFrom(token, useInfo + 3); buffer.push(token[useInfo + 2]); } -function atrules(token){ +function atrules(token) { composite(token); buffer.push(';'); } -function atruler(token){ +function atruler(token) { translate(token[useInfo + 1]); translate(token[useInfo + 2]); buffer.push('{'); @@ -169,31 +171,31 @@ function atruler(token){ buffer.push('}'); } -function pseudoe(token){ +function pseudoe(token) { buffer.push('::'); translate(token[useInfo + 1]); } -function pseudoc(token){ +function pseudoc(token) { buffer.push(':'); translate(token[useInfo + 1]); } -function uri(token){ +function uri(token) { buffer.push('url('); composite(token); buffer.push(')'); } -function functionExpression(token){ +function functionExpression(token) { buffer.push('expression(', token[useInfo + 1], ')'); } -function translate(token){ +function translate(token) { typeHandlers[token[useInfo]](token); } -module.exports = function(tree, hasInfo){ +module.exports = function(tree, hasInfo) { useInfo = hasInfo ? 1 : 0; buffer = []; diff --git a/package.json b/package.json index 292003f7..1f83a632 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ }, "main": "./lib/index", "scripts": { - "test": "mocha --reporter dot", + "test": "jscs . && mocha --reporter dot", "browserify": "browserify --standalone csso lib/index.js | uglifyjs --compress --mangle -o dist/csso-browser.js", "gh-pages": "git clone -b gh-pages https://github.com/css/csso.git .gh-pages && npm run browserify && cp dist/csso-browser.js .gh-pages/ && cd .gh-pages && git commit -am \"update\" && git push && cd .. && rm -rf .gh-pages" }, @@ -22,8 +22,9 @@ "clap": "^1.0.8" }, "devDependencies": { - "mocha": "~2.3.3", "browserify": "11.x", + "jscs": "^2.3.0", + "mocha": "~2.3.3", "uglify-js": ">= 2.4.24" }, "engines": { diff --git a/test/test.js b/test/test.js index 07fad2a4..d6ed19d4 100644 --- a/test/test.js +++ b/test/test.js @@ -24,7 +24,6 @@ function createCompressTest(name, test) { } describe('csso', function() { - describe('parse', function() { var testDir = path.join(__dirname, 'fixture/parse'); fs.readdirSync(testDir).forEach(function(rule) { @@ -37,7 +36,7 @@ describe('csso', function() { }); }); - describe('compress', function(){ + describe('compress', function() { var testDir = path.join(__dirname, 'fixture/compress'); var tests = fs.readdirSync(testDir).reduce(function(list, filename) { var name = filename.replace(/(\.min)?\.css$/, '');