Skip to content
This repository was archived by the owner on Mar 22, 2019. It is now read-only.

Commit 10309a9

Browse files
committed
Lint
1 parent 8670deb commit 10309a9

File tree

2 files changed

+70
-129
lines changed

2 files changed

+70
-129
lines changed

bin/csswring.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,28 @@ Examples:
5454
}
5555

5656
function wring(s, o) {
57-
csswring.wring(s, o).then(function (result) {
58-
if (!o.to) {
59-
process.stdout.write(result.css);
60-
61-
return;
62-
}
63-
64-
fs.writeFileSync(o.to, result.css);
65-
66-
if (result.map) {
67-
fs.writeFileSync(`${o.to}.map`, result.map);
68-
}
69-
}).catch(function (error) {
70-
if (error.name === "CssSyntaxError") {
71-
console.error(`${error.file}:${error.line}:${error.column}: ${error.reason}`);
72-
process.exit(1);
73-
}
74-
75-
throw error;
76-
});
57+
csswring.wring(s, o)
58+
.then(function (result) {
59+
if (!o.to) {
60+
process.stdout.write(result.css);
61+
62+
return;
63+
}
64+
65+
fs.writeFileSync(o.to, result.css);
66+
67+
if (result.map) {
68+
fs.writeFileSync(`${o.to}.map`, result.map);
69+
}
70+
})
71+
.catch(function (error) {
72+
if (error.name === "CssSyntaxError") {
73+
console.error(`${error.file}:${error.line}:${error.column}: ${error.reason}`);
74+
process.exit(1);
75+
}
76+
77+
throw error;
78+
});
7779
}
7880

7981
if (argv._.length < 1) {

index.js

Lines changed: 48 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ function setQuote(quote) {
3333
// Check string can unquote or not
3434
function canUnquote(str) {
3535
const firstChar = str.slice(0, 1);
36-
let secondChar;
3736

3837
if (re.number.test(firstChar)) {
3938
return false;
4039
}
4140

42-
secondChar = str.slice(1, 2);
41+
const secondChar = str.slice(1, 2);
4342

4443
if (
4544
firstChar === "-" &&
@@ -57,14 +56,12 @@ function canUnquote(str) {
5756

5857
// Unquote font family name if possible
5958
function unquoteFontFamily(family) {
60-
let quote;
61-
6259
if (family.match(re.varFunction)) {
6360
return family;
6461
}
6562

6663
family = family.replace(re.quotedString, "$2");
67-
quote = setQuote(RegExp.$1);
64+
const quote = setQuote(RegExp.$1);
6865

6966
if (!list.space(family).every(canUnquote)) {
7067
family = `${quote}${family}${quote}`;
@@ -143,10 +140,9 @@ function toShortestAngle(m, leading, n, u) {
143140

144141
// Unquote inside `url()` notation if possible
145142
function unquoteURL(m, leading, url) {
146-
let quote;
147-
148143
url = url.replace(re.quotedString, "$2");
149-
quote = setQuote(RegExp.$1);
144+
const quote = setQuote(RegExp.$1);
145+
150146
url = url.replace(re.escapedBraces, "$1");
151147

152148
if (re.urlNeedQuote.test(url)) {
@@ -166,61 +162,31 @@ function removeCalcWhiteSpaces(m, leading, calc) {
166162

167163
// Wring value of declaration
168164
function wringValue(prop, value) {
169-
return value.replace(
170-
re.colorFunction,
171-
toRGBColor
172-
).replace(
173-
re.colorHex,
174-
toShortestColor
175-
).replace(
176-
re.colorTransparent,
177-
"$1transparent "
178-
).trim().replace(
179-
re.whiteSpaces,
180-
" "
181-
).replace(
182-
re.whiteSpacesAfterSymbol,
183-
"$1"
184-
).replace(
185-
re.whiteSpacesBeforeSymbol,
186-
"$1"
187-
).replace(
188-
re.numberLeadingZeros,
189-
"$1$2"
190-
).replace(
191-
re.zeroValueUnit,
192-
removeUnitOfZero.bind(null, prop)
193-
).replace(
194-
re.decimalWithZeros,
195-
"$1$2$3.$4"
196-
).replace(
197-
re.timeEndsWithZero,
198-
toShortestTime
199-
).replace(
200-
re.angle,
201-
toShortestAngle
202-
).replace(
203-
re.freqEndsWithThreeZeros,
204-
"$1$2kHz"
205-
).replace(
206-
re.urlFunction,
207-
unquoteURL
208-
).replace(
209-
re.calcFunction,
210-
removeCalcWhiteSpaces
211-
);
165+
return value.replace(re.colorFunction, toRGBColor)
166+
.replace(re.colorHex, toShortestColor)
167+
.replace(re.colorTransparent, "$1transparent ")
168+
.trim()
169+
.replace(re.whiteSpaces, " ")
170+
.replace(re.whiteSpacesAfterSymbol, "$1")
171+
.replace(re.whiteSpacesBeforeSymbol, "$1")
172+
.replace(re.numberLeadingZeros, "$1$2")
173+
.replace(re.zeroValueUnit, removeUnitOfZero.bind(null, prop))
174+
.replace(re.decimalWithZeros, "$1$2$3.$4")
175+
.replace(re.timeEndsWithZero, toShortestTime)
176+
.replace(re.angle, toShortestAngle)
177+
.replace(re.freqEndsWithThreeZeros, "$1$2kHz")
178+
.replace(re.urlFunction, unquoteURL)
179+
.replace(re.calcFunction, removeCalcWhiteSpaces);
212180
}
213181

214182
// Unquote attribute selector if possible
215183
function unquoteAttributeSelector(m, att, con, val) {
216-
let quote;
217-
218184
if (!con || !val) {
219185
return `[${att}]`;
220186
}
221187

222188
val = val.trim().replace(re.quotedString, "$2");
223-
quote = setQuote(RegExp.$1);
189+
const quote = setQuote(RegExp.$1);
224190

225191
if (!canUnquote(val)) {
226192
val = `${quote}${val}${quote}`;
@@ -250,28 +216,13 @@ function trimSelectorCombinator(m, combinator, backslash) {
250216

251217
// Wring selector of ruleset
252218
function wringSelector(selector) {
253-
return selector.replace(
254-
re.whiteSpaces,
255-
" "
256-
).replace(
257-
re.selectorAtt,
258-
unquoteAttributeSelector
259-
).replace(
260-
re.selectorFunctions,
261-
removeWhiteSpaces
262-
).replace(
263-
re.selectorNegationFunction,
264-
trimNegationFunction
265-
).replace(
266-
re.selectorCombinators,
267-
trimSelectorCombinator
268-
).replace(
269-
re.selectorPseudoElements,
270-
"$1"
271-
).replace(
272-
re.selectorVerboseUniversal,
273-
"$1"
274-
);
219+
return selector.replace(re.whiteSpaces, " ")
220+
.replace(re.selectorAtt, unquoteAttributeSelector)
221+
.replace(re.selectorFunctions, removeWhiteSpaces)
222+
.replace(re.selectorNegationFunction, trimNegationFunction)
223+
.replace(re.selectorCombinators, trimSelectorCombinator)
224+
.replace(re.selectorPseudoElements, "$1")
225+
.replace(re.selectorVerboseUniversal, "$1");
275226
}
276227

277228
// Check keyframe is valid or not
@@ -291,13 +242,11 @@ function isValidKeyframe(keyframe) {
291242

292243
// Unique array element
293244
function uniqueArray(array) {
294-
let i;
295-
let l;
245+
const l = array.length;
296246
const result = [];
297-
let value;
298247

299-
for (i = 0, l = array.length; i < l; i++) {
300-
value = array[i];
248+
for (let i = 0; i < l; i++) {
249+
const value = array[i];
301250

302251
if (result.indexOf(value) < 0) {
303252
result.push(value);
@@ -348,11 +297,10 @@ function quoteImportURL(m, quote, url) {
348297

349298
// Quote `@namespace` URL
350299
function quoteNamespaceURL(param, index, p) {
351-
let quote;
352-
353300
if (param === p[p.length - 1]) {
354301
param = param.replace(re.quotedString, "$2");
355-
quote = setQuote(RegExp.$1);
302+
const quote = setQuote(RegExp.$1);
303+
356304
param = `${quote}${param}${quote}`;
357305
}
358306

@@ -380,7 +328,6 @@ function wringDecl(preserveHacks, decl) {
380328
let before = decl.raws.before;
381329
let between = decl.raws.between;
382330
let value = decl.value;
383-
let values;
384331

385332
if (!prop.match(re.validProp)) {
386333
decl.remove();
@@ -435,12 +382,15 @@ function wringDecl(preserveHacks, decl) {
435382
}
436383

437384
if (prop === "font-family") {
438-
decl.value = list.comma(value).map(unquoteFontFamily).join(",");
385+
decl.value = list.comma(value)
386+
.map(unquoteFontFamily)
387+
.join(",");
439388

440389
return;
441390
}
442391

443-
values = list.comma(value);
392+
let values = list.comma(value);
393+
444394
value = values.map(wringValue.bind(null, prop)).join(",");
445395

446396
if (re.propertyMultipleValues.test(prop)) {
@@ -486,10 +436,6 @@ function wringDeclLike(m, prop, value) {
486436

487437
// Wring ruleset
488438
function wringRule(rule) {
489-
let decls;
490-
let parent;
491-
let selectors;
492-
493439
rule.raws.before = "";
494440
rule.raws.between = "";
495441
rule.raws.semicolon = false;
@@ -501,8 +447,8 @@ function wringRule(rule) {
501447
return;
502448
}
503449

504-
parent = rule.parent;
505-
selectors = rule.selectors.map(wringSelector);
450+
const parent = rule.parent;
451+
let selectors = rule.selectors.map(wringSelector);
506452

507453
if (parent.type === "atrule" && parent.name === "keyframes") {
508454
selectors = selectors.filter(isValidKeyframe);
@@ -515,7 +461,8 @@ function wringRule(rule) {
515461
}
516462

517463
rule.selector = uniqueArray(selectors).join(",");
518-
decls = {};
464+
const decls = {};
465+
519466
rule.each(removeDuplicateDeclaration.bind(null, decls));
520467
}
521468

@@ -552,8 +499,6 @@ function filterAtRule(flag, rule) {
552499

553500
// Wring at-rule
554501
function wringAtRule(atRule) {
555-
let params;
556-
557502
atRule.raws.before = "";
558503
atRule.raws.afterName = " ";
559504
atRule.raws.between = "";
@@ -584,16 +529,10 @@ function wringAtRule(atRule) {
584529
return;
585530
}
586531

587-
params = atRule.params.replace(
588-
re.whiteSpaces,
589-
" "
590-
).replace(
591-
re.whiteSpacesAfterSymbol,
592-
"$1"
593-
).replace(
594-
re.whiteSpacesBeforeSymbol,
595-
"$1"
596-
);
532+
let params = atRule.params
533+
.replace(re.whiteSpaces, " ")
534+
.replace(re.whiteSpacesAfterSymbol, "$1")
535+
.replace(re.whiteSpacesBeforeSymbol, "$1");
597536

598537
if (atRule.name === "import") {
599538
params = params.replace(
@@ -606,9 +545,9 @@ function wringAtRule(atRule) {
606545
}
607546

608547
if (atRule.name === "namespace") {
609-
params = list.space(
610-
params.replace(re.urlFunction, "$1$2")
611-
).map(quoteNamespaceURL).join("");
548+
params = list.space(params.replace(re.urlFunction, "$1$2"))
549+
.map(quoteNamespaceURL)
550+
.join("");
612551
}
613552

614553
if (atRule.name === "keyframes") {

0 commit comments

Comments
 (0)