Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
hail2u committed Feb 14, 2018
1 parent 686f27c commit 2e73471
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 113 deletions.
50 changes: 25 additions & 25 deletions bin/mqpacker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env node

"use strict";

const mqpacker = require("../index");
const fs = require("fs");
const minimist = require("minimist");
Expand All @@ -20,10 +18,10 @@ const argv = minimist(process.argv.slice(2), {
version: false
}
});
const binname = Object.keys(pkg.bin)[0];
const [binname] = Object.keys(pkg.bin);
const options = {};

function showHelp() {
const showHelp = () => {
console.log(`Usage: ${binname} [options] INPUT [OUTPUT]
Description:
Expand All @@ -40,11 +38,9 @@ Use a single dash for INPUT to read CSS from standard input.
Examples:
$ ${binname} fragmented.css
$ ${binname} fragmented.css > packed.css`);
};

return;
}

function pack(s, o) {
const pack = (s, o) => {
mqpacker
.pack(s, o)
.then(result => {
Expand All @@ -61,16 +57,16 @@ function pack(s, o) {
}
})
.catch(error => {
if (error.name === "CssSyntaxError") {
console.error(
`${error.file}:${error.line}:${error.column}: ${error.reason}`
);
process.exit(1);
if (error.name !== "CssSyntaxError") {
throw error;
}

throw error;
process.exitCode = 1;
console.error(
`${error.file}:${error.line}:${error.column}: ${error.reason}`
);
});
}
};

if (argv._.length < 1) {
argv.help = true;
Expand All @@ -87,7 +83,7 @@ switch (true) {

break;

default:
default: {
if (argv.sort) {
options.sort = true;
}
Expand All @@ -96,10 +92,16 @@ switch (true) {
options.map = true;
}

options.from = argv._[0];
[options.from, options.to] = argv._;
let input = options.from;

if (argv._[1]) {
options.to = argv._[1];
if (input === "-") {
delete options.from;
input = process.stdin.fd;
}

if (!options.to) {
delete options.to;
}

if (options.map && options.to) {
Expand All @@ -108,10 +110,8 @@ switch (true) {
};
}

if (options.from === "-") {
delete options.from;
argv._[0] = process.stdin.fd;
}

pack(fs.readFileSync(argv._[0], "utf8"), options);
pack(fs.readFileSync(input, "utf8"), options);
}
}

/* eslint no-console: "off" */
110 changes: 42 additions & 68 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"use strict";

const list = require("postcss/lib/list");
const pkg = require("./package.json");
const postcss = require("postcss");

function isSourceMapAnnotation(rule) {
const isSourceMapAnnotation = rule => {
if (!rule) {
return false;
}
Expand All @@ -18,30 +16,29 @@ function isSourceMapAnnotation(rule) {
}

return true;
}
};

function parseQueryList(queryList) {
const parseQueryList = queryList => {
const queries = [];

list.comma(queryList).forEach(query => {
const expressions = {};

list.space(query).forEach(expression => {
expression = expression.toLowerCase();
let newExpression = expression.toLowerCase();

if (expression === "and") {
if (newExpression === "and") {
return;
}

if (/^\w+$/.test(expression)) {
expressions[expression] = true;
if (/^\w+$/.test(newExpression)) {
expressions[newExpression] = true;

return;
}

expression = list.split(expression.replace(/^\(|\)$/g, ""), [":"]);
const feature = expression[0];
const value = expression[1];
newExpression = list.split(newExpression.replace(/^\(|\)$/g, ""), [":"]);
const [feature, value] = newExpression;

if (!expressions[feature]) {
expressions[feature] = [];
Expand All @@ -53,49 +50,50 @@ function parseQueryList(queryList) {
});

return queries;
}
};

function inspectLength(length) {
const inspectLength = length => {
if (length === "0") {
return 0;
}

length = /(-?\d*\.?\d+)(ch|em|ex|px|rem)/.exec(length);
const matches = /(-?\d*\.?\d+)(ch|em|ex|px|rem)/.exec(length);

if (!length) {
if (!matches) {
return Number.MAX_VALUE;
}

let num = length[1];
const unit = length[2];
matches.shift();
const [num, unit] = matches;
let newNum = num;

switch (unit) {
case "ch":
num = parseFloat(num) * 8.8984375;
newNum = parseFloat(newNum) * 8.8984375;

break;

case "em":
case "rem":
num = parseFloat(num) * 16;
newNum = parseFloat(newNum) * 16;

break;

case "ex":
num = parseFloat(num) * 8.296875;
newNum = parseFloat(newNum) * 8.296875;

break;

case "px":
num = parseFloat(num);
newNum = parseFloat(newNum);

break;
}

return num;
}
return newNum;
};

function pickMinimumMinWidth(expressions) {
const pickMinimumMinWidth = expressions => {
const minWidths = [];

expressions.forEach(feature => {
Expand All @@ -105,19 +103,13 @@ function pickMinimumMinWidth(expressions) {
minWidth = [null];
}

minWidths.push(
minWidth.map(inspectLength).sort((a, b) => {
return b - a;
})[0]
);
minWidths.push(minWidth.map(inspectLength).sort((a, b) => b - a)[0]);
});

return minWidths.sort((a, b) => {
return a - b;
})[0];
}
return minWidths.sort((a, b) => a - b)[0];
};

function sortQueryLists(queryLists, sort) {
const sortQueryLists = (queryLists, sort) => {
const mapQueryLists = [];

if (!sort) {
Expand All @@ -133,33 +125,21 @@ function sortQueryLists(queryLists, sort) {
});

return mapQueryLists
.map((e, i) => {
return {
index: i,
value: pickMinimumMinWidth(e)
};
})
.sort((a, b) => {
return a.value - b.value;
})
.map(e => {
return queryLists[e.index];
});
}

module.exports = postcss.plugin(pkg.name, opts => {
if (!opts) {
opts = {};
}
.map((e, i) => ({
index: i,
value: pickMinimumMinWidth(e)
}))
.sort((a, b) => a.value - b.value)
.map(e => queryLists[e.index]);
};

opts = Object.assign(
{
sort: false
},
opts
);
module.exports = postcss.plugin(pkg.name, options => {
const opts = {
sort: false,
...options
};

return function(css) {
return css => {
const queries = {};
const queryLists = [];

Expand All @@ -184,12 +164,8 @@ module.exports = postcss.plugin(pkg.name, opts => {
newAtRule.append(rule);
});
atRule.remove();
atRule = postcss
.atRule({
name: atRule.name,
params: atRule.params
})
.append(newAtRule);
atRule.removeAll();
atRule.append(newAtRule);
}

const queryList = atRule.params;
Expand All @@ -214,8 +190,6 @@ module.exports = postcss.plugin(pkg.name, opts => {
if (sourceMap) {
css.append(sourceMap);
}

return css;
};
});

Expand Down
25 changes: 5 additions & 20 deletions test/css-mqpacker_test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
"use strict";

const fs = require("fs");
const path = require("path");
const postcss = require("postcss");

const mqpacker = require("../index");

exports["Public API"] = test => {
Expand All @@ -18,7 +15,6 @@ exports["Public API"] = test => {
}
`;
const expected = postcss().process(input).css;

test.expect(2);
test.strictEqual(postcss([mqpacker()]).process(input).css, expected);
test.strictEqual(mqpacker.pack(input).css, expected);
Expand Down Expand Up @@ -46,7 +42,6 @@ exports["Option: PostCSS options"] = test => {
};
const expected = postcss().process(input, opts);
const processed = mqpacker.pack(input, opts);

test.expect(2);
test.strictEqual(processed.css, expected.css);
test.deepEqual(processed.map, expected.map);
Expand Down Expand Up @@ -89,7 +84,6 @@ exports["Option: sort"] = test => {
const opts = {
sort: true
};

test.expect(4);
test.notStrictEqual(mqpacker.pack(input).css, expected);
test.strictEqual(mqpacker.pack(input, opts).css, expected);
Expand All @@ -99,9 +93,7 @@ exports["Option: sort"] = test => {
);
test.strictEqual(
mqpacker.pack(input, {
sort: function(c, d) {
return c.localeCompare(d);
}
sort: (c, d) => c.localeCompare(d)
}).css,
expected
);
Expand All @@ -110,17 +102,10 @@ exports["Option: sort"] = test => {

exports["Real CSS"] = test => {
const testCases = fs.readdirSync(path.join(__dirname, "fixtures"));
const loadExpected = file => {
file = path.join(__dirname, "expected", file);

return fs.readFileSync(file, "utf8");
};
const loadInput = file => {
file = path.join(__dirname, "fixtures", file);

return fs.readFileSync(file, "utf8");
};

const loadExpected = file =>
fs.readFileSync(path.join(__dirname, "expected", file), "utf8");
const loadInput = file =>
fs.readFileSync(path.join(__dirname, "fixtures", file), "utf8");
test.expect(testCases.length);
testCases.forEach(testCase => {
const opts = {
Expand Down

0 comments on commit 2e73471

Please sign in to comment.