Skip to content

Commit

Permalink
Modify jshint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k committed Jun 13, 2015
1 parent 3f8e6fd commit 73c5a73
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 46 deletions.
4 changes: 3 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"camelcase": true,
"eqeqeq": false,
"-W041": false,
"-W008": false,
"forin": false,
"immed": true,
"multistr": true,
Expand All @@ -21,5 +22,6 @@
"undef": true,
"unused": true,
"loopfunc": true,
"trailing": true
"trailing": true,
"lastsemic": true
}
2 changes: 1 addition & 1 deletion lib/svgo/jsAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ JSAPI.prototype.renameElem = function(name) {
if (!Array.isArray(insertion))
insertion = Array.apply(null, arguments).slice(2);

insertion.forEach(function(inner) { inner.parentNode = this; }, this);
insertion.forEach(function(inner) { inner.parentNode = this }, this);

return this.content.splice.apply(this.content, [start, n].concat(insertion));

Expand Down
63 changes: 31 additions & 32 deletions plugins/_path.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global a2c */
'use strict';

var regPathInstructions = /([MmLlHhVvCcSsQqTtAaZz])\s*/,
Expand Down Expand Up @@ -198,13 +199,13 @@ exports.applyTransforms = function(elem, path, params) {

if (elem.hasAttr('stroke-width')) {
elem.attrs['stroke-width'].value = elem.attrs['stroke-width'].value.trim()
.replace(regNumericValues, function(num) { return removeLeadingZero(num * scale); });
.replace(regNumericValues, function(num) { return removeLeadingZero(num * scale) });
} else {
elem.addAttr({
name: 'stroke-width',
prefix: '',
local: 'stroke-width',
value: strokeWidth.replace(regNumericValues, function(num) { return removeLeadingZero(num * scale); })
value: strokeWidth.replace(regNumericValues, function(num) { return removeLeadingZero(num * scale) })
});
}
}
Expand Down Expand Up @@ -746,27 +747,27 @@ function gatherPoints(points, item, index, path) {
break;
case 'C':
// Approximate quibic Bezier curve with middle points between control points
addPoint(subPath, [0.5 * (basePoint[0] + data[0]), 0.5 * (basePoint[1] + data[1])]);
addPoint(subPath, [0.5 * (data[0] + data[2]), 0.5 * (data[1] + data[3])]);
addPoint(subPath, [0.5 * (data[2] + data[4]), 0.5 * (data[3] + data[5])]);
addPoint(subPath, [.5 * (basePoint[0] + data[0]), .5 * (basePoint[1] + data[1])]);
addPoint(subPath, [.5 * (data[0] + data[2]), .5 * (data[1] + data[3])]);
addPoint(subPath, [.5 * (data[2] + data[4]), .5 * (data[3] + data[5])]);
prevCtrlPoint = [data[4] - data[2], data[5] - data[3]]; // Save control point for shorthand
break;
case 'S':
if (prev.instruction == 'C' && prev.instruction == 'S') {
addPoint(subPath, [basePoint[0] + 0.5 * prevCtrlPoint[0], basePoint[1] + 0.5 * prevCtrlPoint[1]]);
addPoint(subPath, [basePoint[0] + .5 * prevCtrlPoint[0], basePoint[1] + .5 * prevCtrlPoint[1]]);
ctrlPoint = [basePoint[0] + prevCtrlPoint[0], basePoint[1] + prevCtrlPoint[1]];
}
addPoint(subPath, [0.5 * (ctrlPoint[0] + data[0]), 0.5 * (ctrlPoint[1]+ data[1])]);
addPoint(subPath, [0.5 * (data[0] + data[2]), 0.5 * (data[1] + data[3])]);
addPoint(subPath, [.5 * (ctrlPoint[0] + data[0]), .5 * (ctrlPoint[1]+ data[1])]);
addPoint(subPath, [.5 * (data[0] + data[2]), .5 * (data[1] + data[3])]);
prevCtrlPoint = [data[2] - data[0], data[3] - data[1]];
break;
case 'A':
// Convert the arc to bezier curves and use the same approximation
var curves = a2c.apply(0, basePoint.concat(data));
for (var cData; (cData = curves.splice(0,6).map(toAbsolute)).length;) {
addPoint(subPath, [0.5 * (basePoint[0] + cData[0]), 0.5 * (basePoint[1] + cData[1])]);
addPoint(subPath, [0.5 * (cData[0] + cData[2]), 0.5 * (cData[1] + cData[3])]);
addPoint(subPath, [0.5 * (cData[2] + cData[4]), 0.5 * (cData[3] + cData[5])]);
addPoint(subPath, [.5 * (basePoint[0] + cData[0]), .5 * (basePoint[1] + cData[1])]);
addPoint(subPath, [.5 * (cData[0] + cData[2]), .5 * (cData[1] + cData[3])]);
addPoint(subPath, [.5 * (cData[2] + cData[4]), .5 * (cData[3] + cData[5])]);
if (curves.length) addPoint(subPath, basePoint = cData.slice(-2));
}
break;
Expand All @@ -775,7 +776,7 @@ function gatherPoints(points, item, index, path) {
if (data && data.length >= 2) addPoint(subPath, data.slice(-2));
return points;

function toAbsolute(n, i) { return n + basePoint[i % 2]; }
function toAbsolute(n, i) { return n + basePoint[i % 2] }

// Writes data about the extreme points on each axle
function addPoint(path, point) {
Expand Down Expand Up @@ -806,6 +807,8 @@ function gatherPoints(points, item, index, path) {
* @param points An array of [X, Y] coordinates
*/
function convexHull(points) {
/* jshint -W004 */

points.sort(function(a, b) {
return a[0] == b[0] ? a[1] - b[1] : a[0] - b[0];
});
Expand All @@ -827,7 +830,7 @@ function convexHull(points) {
var upper = [],
maxY = points.length - 1,
top = 0;
for (i = points.length; i--;) {
for (var i = points.length; i--;) {
while (upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], points[i]) <= 0) {
upper.pop();
}
Expand Down Expand Up @@ -860,18 +863,15 @@ function cross(o, a, b) {
* Thanks to Dmitry Baranovskiy for his great work!
*/

function a2c(x1, y1, rx, ry, angle, largeArcFlag, sweepFlag, x2, y2, recursive) {
// jshint ignore: start
function a2c(x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
// for more information of where this Math came from visit:
// http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
var _120 = Math.PI * 120 / 180,
rad = Math.PI / 180 * (+angle || 0),
res = [],
rotateX = function(x, y, rad) { return x * Math.cos(rad) - y * Math.sin(rad); },
rotateY = function(x, y, rad) { return x * Math.sin(rad) + y * Math.cos(rad); },
cx,
cy,
f1,
f2;
rotateX = function(x, y, rad) { return x * Math.cos(rad) - y * Math.sin(rad) },
rotateY = function(x, y, rad) { return x * Math.sin(rad) + y * Math.cos(rad) };
if (!recursive) {
x1 = rotateX(x1, y1, -rad);
y1 = rotateY(x1, y1, -rad);
Expand All @@ -887,23 +887,21 @@ function a2c(x1, y1, rx, ry, angle, largeArcFlag, sweepFlag, x2, y2, recursive)
}
var rx2 = rx * rx,
ry2 = ry * ry,
k = (largeArcFlag == sweepFlag ? -1 : 1) *
Math.sqrt(Math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x)));

cx = k * rx * y / ry + (x1 + x2) / 2;
cy = k * -ry * x / rx + (y1 + y2) / 2;

f1 = Math.asin(((y1 - cy) / ry).toFixed(9));
f2 = Math.asin(((y2 - cy) / ry).toFixed(9));
k = (large_arc_flag == sweep_flag ? -1 : 1) *
Math.sqrt(Math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
cx = k * rx * y / ry + (x1 + x2) / 2,
cy = k * -ry * x / rx + (y1 + y2) / 2,
f1 = Math.asin(((y1 - cy) / ry).toFixed(9)),
f2 = Math.asin(((y2 - cy) / ry).toFixed(9));

f1 = x1 < cx ? Math.PI - f1 : f1;
f2 = x2 < cx ? Math.PI - f2 : f2;
f1 < 0 && (f1 = Math.PI * 2 + f1);
f2 < 0 && (f2 = Math.PI * 2 + f2);
if (sweepFlag && f1 > f2) {
if (sweep_flag && f1 > f2) {
f1 = f1 - Math.PI * 2;
}
if (!sweepFlag && f2 > f1) {
if (!sweep_flag && f2 > f1) {
f2 = f2 - Math.PI * 2;
}
} else {
Expand All @@ -917,10 +915,10 @@ function a2c(x1, y1, rx, ry, angle, largeArcFlag, sweepFlag, x2, y2, recursive)
var f2old = f2,
x2old = x2,
y2old = y2;
f2 = f1 + _120 * (sweepFlag && f2 > f1 ? 1 : -1);
f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
x2 = cx + rx * Math.cos(f2);
y2 = cy + ry * Math.sin(f2);
res = a2c(x2, y2, rx, ry, angle, 0, sweepFlag, x2old, y2old, [f2, f2old, cx, cy]);
res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
}
df = f2 - f1;
var c1 = Math.cos(f1),
Expand All @@ -946,3 +944,4 @@ function a2c(x1, y1, rx, ry, angle, largeArcFlag, sweepFlag, x2, y2, recursive)
return newres;
}
}
// jshint ignore: end
2 changes: 1 addition & 1 deletion plugins/collapseGroups.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ exports.fn = function(item) {
}

// collapse groups without attributes
if (!g.hasAttr() && !g.content.some(function(item) { return item.isElem(animationElems); })) {
if (!g.hasAttr() && !g.content.some(function(item) { return item.isElem(animationElems) })) {
item.spliceContent(i, 1, g.content);
}
}
Expand Down
12 changes: 3 additions & 9 deletions plugins/convertPathData.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ exports.fn = function(item, params) {
if (item.isElem(pathElems) && item.hasAttr('d')) {

precision = params.floatPrecision;
error = precision !== false ? +Math.pow(0.1, precision).toFixed(precision) : 1e-2;
error = precision !== false ? +Math.pow(.1, precision).toFixed(precision) : 1e-2;
hasMarkerMid = item.hasAttr('marker-mid');

var data = path2js(item);
Expand Down Expand Up @@ -641,15 +641,9 @@ function roundData(data) {
return data;
}

if (precision > 0) {
roundData = precision > 0 ? strongRound : round; // jshint ignore: line

return strongRound(data);

} else {

return round(data);

}
return roundData(data);

}

Expand Down
2 changes: 1 addition & 1 deletion plugins/convertStyleToAttrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ exports.fn = function(item) {

if (styles.length) {
item.attr('style').value = styles
.map(function(declaration) { return declaration.join(':'); })
.map(function(declaration) { return declaration.join(':') })
.join(';');
} else {
item.removeAttr('style');
Expand Down
2 changes: 1 addition & 1 deletion plugins/convertTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ function round(data) {
* @return {Array} output data array
*/
function smartRound(precision, data) {
for (var i = data.length, tolerance = Math.pow(0.1, precision); i--;) {
for (var i = data.length, tolerance = Math.pow(.1, precision); i--;) {
var rounded = +data[i].toFixed(precision - 1);
data[i] = +Math.abs(rounded - data[i]).toFixed(precision) >= tolerance ?
+data[i].toFixed(precision) :
Expand Down

0 comments on commit 73c5a73

Please sign in to comment.