- Linting (ESLint): Prefer addEventListener, exponentiation operator, avoiding catastrophic regexes, prefer spread, prefer startsWith/endsWith, no fn ref in iterator

- npm: Update devDeps (rollup and eslint-config-ash-nazg)
This commit is contained in:
Brett Zamir
2018-11-21 21:03:14 +08:00
parent efa8cbfb83
commit 845dbbd7d7
31 changed files with 605 additions and 340 deletions

View File

@@ -131,7 +131,7 @@ function build (opts) {
if (d === 'x') return this.width();
if (d === 'y') return this.height();
return Math.sqrt(
Math.pow(this.width(), 2) + Math.pow(this.height(), 2)
(this.width() ** 2) + (this.height() ** 2)
) / Math.sqrt(2);
}
};
@@ -222,7 +222,7 @@ function build (opts) {
if (!this.hasValue()) return 0;
let n = parseFloat(this.value);
if (String(this.value).match(/%$/)) {
if (String(this.value).endsWith('%')) {
n /= 100.0;
}
return n;
@@ -307,15 +307,15 @@ function build (opts) {
toPixels (viewPort, processPercent) {
if (!this.hasValue()) return 0;
const s = String(this.value);
if (s.match(/em$/)) return this.numValue() * this.getEM(viewPort);
if (s.match(/ex$/)) return this.numValue() * this.getEM(viewPort) / 2.0;
if (s.match(/px$/)) return this.numValue();
if (s.match(/pt$/)) return this.numValue() * this.getDPI(viewPort) * (1.0 / 72.0);
if (s.match(/pc$/)) return this.numValue() * 15;
if (s.match(/cm$/)) return this.numValue() * this.getDPI(viewPort) / 2.54;
if (s.match(/mm$/)) return this.numValue() * this.getDPI(viewPort) / 25.4;
if (s.match(/in$/)) return this.numValue() * this.getDPI(viewPort);
if (s.match(/%$/)) return this.numValue() * svg.ViewPort.ComputeSize(viewPort);
if (s.endsWith('em')) return this.numValue() * this.getEM(viewPort);
if (s.endsWith('ex')) return this.numValue() * this.getEM(viewPort) / 2.0;
if (s.endsWith('px')) return this.numValue();
if (s.endsWith('pt')) return this.numValue() * this.getDPI(viewPort) * (1.0 / 72.0);
if (s.endsWith('pc')) return this.numValue() * 15;
if (s.endsWith('cm')) return this.numValue() * this.getDPI(viewPort) / 2.54;
if (s.endsWith('mm')) return this.numValue() * this.getDPI(viewPort) / 25.4;
if (s.endsWith('in')) return this.numValue() * this.getDPI(viewPort);
if (s.endsWith('%')) return this.numValue() * svg.ViewPort.ComputeSize(viewPort);
const n = this.numValue();
if (processPercent && n < 1.0) return n * svg.ViewPort.ComputeSize(viewPort);
return n;
@@ -326,8 +326,8 @@ function build (opts) {
toMilliseconds () {
if (!this.hasValue()) return 0;
const s = String(this.value);
if (s.match(/s$/)) return this.numValue() * 1000;
if (s.match(/ms$/)) return this.numValue();
if (s.endsWith('ms')) return this.numValue();
if (s.endsWith('s')) return this.numValue() * 1000;
return this.numValue();
}
@@ -336,9 +336,9 @@ function build (opts) {
toRadians () {
if (!this.hasValue()) return 0;
const s = String(this.value);
if (s.match(/deg$/)) return this.numValue() * (Math.PI / 180.0);
if (s.match(/grad$/)) return this.numValue() * (Math.PI / 200.0);
if (s.match(/rad$/)) return this.numValue();
if (s.endsWith('deg')) return this.numValue() * (Math.PI / 180.0);
if (s.endsWith('grad')) return this.numValue() * (Math.PI / 200.0);
if (s.endsWith('rad')) return this.numValue();
return this.numValue() * (Math.PI / 180.0);
}
@@ -503,10 +503,10 @@ function build (opts) {
for (let i = 0; i <= 1; i++) {
const f = function (t) {
return Math.pow(1 - t, 3) * p0[i] +
3 * Math.pow(1 - t, 2) * t * p1[i] +
3 * (1 - t) * Math.pow(t, 2) * p2[i] +
Math.pow(t, 3) * p3[i];
return ((1 - t) ** 3) * p0[i] +
3 * ((1 - t) ** 2) * t * p1[i] +
3 * (1 - t) * (t ** 2) * p2[i] +
(t ** 3) * p3[i];
};
const b = 6 * p0[i] - 12 * p1[i] + 6 * p2[i];
@@ -523,7 +523,7 @@ function build (opts) {
continue;
}
const b2ac = Math.pow(b, 2) - 4 * c * a;
const b2ac = (b ** 2) - 4 * c * a;
if (b2ac < 0) continue;
const t1 = (-b + Math.sqrt(b2ac)) / (2 * a);
if (t1 > 0 && t1 < 1) {
@@ -685,10 +685,10 @@ function build (opts) {
ctx.translate(-scaleMin * refX.toPixels('x'), -scaleMin * refY.toPixels('y'));
} else {
// align
if (align.match(/^xMid/) && ((meetOrSlice === 'meet' && scaleMin === scaleY) || (meetOrSlice === 'slice' && scaleMax === scaleY))) ctx.translate(width / 2.0 - desiredWidth / 2.0, 0);
if (align.match(/YMid$/) && ((meetOrSlice === 'meet' && scaleMin === scaleX) || (meetOrSlice === 'slice' && scaleMax === scaleX))) ctx.translate(0, height / 2.0 - desiredHeight / 2.0);
if (align.match(/^xMax/) && ((meetOrSlice === 'meet' && scaleMin === scaleY) || (meetOrSlice === 'slice' && scaleMax === scaleY))) ctx.translate(width - desiredWidth, 0);
if (align.match(/YMax$/) && ((meetOrSlice === 'meet' && scaleMin === scaleX) || (meetOrSlice === 'slice' && scaleMax === scaleX))) ctx.translate(0, height - desiredHeight);
if (align.startsWith('xMid') && ((meetOrSlice === 'meet' && scaleMin === scaleY) || (meetOrSlice === 'slice' && scaleMax === scaleY))) ctx.translate(width / 2.0 - desiredWidth / 2.0, 0);
if (align.endsWith('YMid') && ((meetOrSlice === 'meet' && scaleMin === scaleX) || (meetOrSlice === 'slice' && scaleMax === scaleX))) ctx.translate(0, height / 2.0 - desiredHeight / 2.0);
if (align.startsWith('xMax') && ((meetOrSlice === 'meet' && scaleMin === scaleY) || (meetOrSlice === 'slice' && scaleMax === scaleY))) ctx.translate(width - desiredWidth, 0);
if (align.endsWith('YMax') && ((meetOrSlice === 'meet' && scaleMin === scaleX) || (meetOrSlice === 'slice' && scaleMax === scaleX))) ctx.translate(0, height - desiredHeight);
}
// scale
@@ -805,7 +805,7 @@ function build (opts) {
getHrefAttribute () {
for (const a in this.attributes) {
if (a.match(/:href$/)) {
if (a.endsWith(':href')) {
return this.attributes[a];
}
}
@@ -1254,9 +1254,9 @@ function build (opts) {
.replace(/([MmZzLlHhVvCcSsQqTtAa])([MmZzLlHhVvCcSsQqTtAa])/gm, '$1 $2') // separate commands from commands
.replace(/([MmZzLlHhVvCcSsQqTtAa])([^\s])/gm, '$1 $2') // separate commands from points
.replace(/([^\s])([MmZzLlHhVvCcSsQqTtAa])/gm, '$1 $2') // separate commands from points
.replace(/([0-9])([+-])/gm, '$1 $2') // separate digits when no comma
.replace(/(\.[0-9]*)(\.)/gm, '$1 $2') // separate digits when no comma
.replace(/([Aa](\s+[0-9]+){3})\s+([01])\s*([01])/gm, '$1 $3 $4 '); // shorthand elliptical arc path syntax
.replace(/(\d)([+-])/gm, '$1 $2') // separate digits when no comma
.replace(/(\.\d*)(\.)/gm, '$1 $2') // separate digits when no comma
.replace(/([Aa](\s+\d+)(\s+\d+)(\s+\d+))\s+([01])\s*([01])/gm, '$1 $5 $6 '); // shorthand elliptical arc path syntax
d = svg.compressSpaces(d); // compress multiple spaces
d = svg.trim(d);
this.PathParser = {
@@ -1501,15 +1501,15 @@ function build (opts) {
-Math.sin(xAxisRotation) * (curr.x - cp.x) / 2.0 + Math.cos(xAxisRotation) * (curr.y - cp.y) / 2.0
);
// adjust radii
const l = Math.pow(currp.x, 2) / Math.pow(rx, 2) + Math.pow(currp.y, 2) / Math.pow(ry, 2);
const l = (currp.x ** 2) / (rx ** 2) + (currp.y ** 2) / (ry ** 2);
if (l > 1) {
rx *= Math.sqrt(l);
ry *= Math.sqrt(l);
}
// cx', cy'
let s = (largeArcFlag === sweepFlag ? -1 : 1) * Math.sqrt(
((Math.pow(rx, 2) * Math.pow(ry, 2)) - (Math.pow(rx, 2) * Math.pow(currp.y, 2)) - (Math.pow(ry, 2) * Math.pow(currp.x, 2))) /
(Math.pow(rx, 2) * Math.pow(currp.y, 2) + Math.pow(ry, 2) * Math.pow(currp.x, 2))
(((rx ** 2) * (ry ** 2)) - ((rx ** 2) * (currp.y ** 2)) - ((ry ** 2) * (currp.x ** 2))) /
((rx ** 2) * (currp.y ** 2) + (ry ** 2) * (currp.x ** 2))
);
if (isNaN(s)) s = 0;
const cpp = new svg.Point(s * rx * currp.y / ry, s * -ry * currp.x / rx);
@@ -1520,7 +1520,7 @@ function build (opts) {
);
// vector magnitude
const m = function (v) {
return Math.sqrt(Math.pow(v[0], 2) + Math.pow(v[1], 2));
return Math.sqrt((v[0] ** 2) + (v[1] ** 2));
};
// ratio between two vectors
const r = function (u, v) {
@@ -2281,13 +2281,13 @@ function build (opts) {
if (svg.opts.useCORS === true) {
this.img.crossOrigin = 'Anonymous';
}
this.img.onload = () => {
this.img.addEventListener('load', () => {
this.loaded = true;
};
this.img.onerror = () => {
});
this.img.addEventListener('error', () => {
svg.log('ERROR: image "' + href + '" not found');
this.loaded = true;
};
});
this.img.src = href;
} else {
svg.ajax(href, true).then((img) => { // eslint-disable-line promise/prefer-await-to-then, promise/always-return
@@ -2769,20 +2769,20 @@ function build (opts) {
// bind mouse
if (svg.opts.ignoreMouse !== true) {
ctx.canvas.onclick = function (e) {
ctx.canvas.addEventListener('click', function (e) {
const args = !isNullish(e)
? [e.clientX, e.clientY]
: [event.clientX, event.clientY]; // eslint-disable-line no-restricted-globals
const {x, y} = mapXY(new svg.Point(...args));
svg.Mouse.onclick(x, y);
};
ctx.canvas.onmousemove = function (e) {
});
ctx.canvas.addEventListener('mousemove', function (e) {
const args = !isNullish(e)
? [e.clientX, e.clientY]
: [event.clientX, event.clientY]; // eslint-disable-line no-restricted-globals
const {x, y} = mapXY(new svg.Point(...args));
svg.Mouse.onmousemove(x, y);
};
});
}
const e = svg.CreateElement(dom.documentElement);

View File

@@ -1,3 +1,4 @@
/* eslint-disable unicorn/no-fn-reference-in-iterator */
/**
* ext-connector.js
*

View File

@@ -20,7 +20,7 @@ $('a').click(function () {
});
if (!href.includes('.svg')) {
const img = new Image();
img.onload = function () {
img.addEventListener('load', function () {
const canvas = document.createElement('canvas');
canvas.width = this.width;
canvas.height = this.height;
@@ -37,7 +37,7 @@ $('a').click(function () {
data = '';
}
post({href, data});
};
});
img.src = href;
} else {
// Do ajax request for image's href value

View File

@@ -79,22 +79,32 @@ export function importScript (url, atts = {}) {
}
return new Promise((resolve, reject) => { // eslint-disable-line promise/avoid-new
const script = document.createElement('script');
/**
*
* @returns {undefined}
*/
function scriptOnError () {
reject(new Error(`Failed to import: ${url}`));
destructor();
}
/**
*
* @returns {undefined}
*/
function scriptOnLoad () {
resolve();
destructor();
}
const destructor = () => {
script.onerror = null;
script.onload = null;
script.removeEventListener('error', scriptOnError);
script.removeEventListener('load', scriptOnLoad);
script.remove();
script.src = '';
};
script.defer = 'defer';
addScriptAtts(script, atts);
script.onerror = () => {
reject(new Error(`Failed to import: ${url}`));
destructor();
};
script.onload = () => {
resolve();
destructor();
};
script.addEventListener('error', scriptOnError);
script.addEventListener('load', scriptOnLoad);
script.src = url;
document.head.append(script);
@@ -119,10 +129,26 @@ export function importModule (url, atts = {}, {returnDefault = false} = {}) {
return new Promise((resolve, reject) => { // eslint-disable-line promise/avoid-new
const vector = '$importModule$' + Math.random().toString(32).slice(2);
const script = document.createElement('script');
/**
*
* @returns {undefined}
*/
function scriptOnError () {
reject(new Error(`Failed to import: ${url}`));
destructor();
}
/**
*
* @returns {undefined}
*/
function scriptOnLoad () {
resolve(window[vector]);
destructor();
}
const destructor = () => {
delete window[vector];
script.onerror = null;
script.onload = null;
script.removeEventListener('error', scriptOnError);
script.removeEventListener('load', scriptOnLoad);
script.remove();
URL.revokeObjectURL(script.src);
script.src = '';
@@ -130,14 +156,8 @@ export function importModule (url, atts = {}, {returnDefault = false} = {}) {
addScriptAtts(script, atts);
script.defer = 'defer';
script.type = 'module';
script.onerror = () => {
reject(new Error(`Failed to import: ${url}`));
destructor();
};
script.onload = () => {
resolve(window[vector]);
destructor();
};
script.addEventListener('error', scriptOnError);
script.addEventListener('load', scriptOnLoad);
const absURL = toAbsoluteURL(url);
const loader = `import * as m from '${absURL.replace(/'/g, "\\'")}'; window.${vector} = ${returnDefault ? 'm.default || ' : ''}m;`; // export Module
const blob = new Blob([loader], {type: 'text/javascript'});

View File

@@ -1052,7 +1052,7 @@ export default function jQueryPluginJGraduate ($) {
switch (slider.type) {
case 'radius':
x = Math.pow(x * 2, 2.5);
x = (x * 2) ** 2.5;
if (x > 0.98 && x < 1.02) x = 1;
if (x <= 0.01) x = 0.01;
curGradient.setAttribute('r', x);
@@ -1171,7 +1171,7 @@ export default function jQueryPluginJGraduate ($) {
switch (type) {
case 'radius':
if (isRad) curGradient.setAttribute('r', val / 100);
xpos = (Math.pow(val / 100, 1 / 2.5) / 2) * SLIDERW;
xpos = (((val / 100) ** (1 / 2.5)) / 2) * SLIDERW;
break;
case 'opacity':

View File

@@ -26,7 +26,7 @@
*/
function toFixedNumeric (value, precision) {
if (precision === undefined) precision = 0;
return Math.round(value * Math.pow(10, precision)) / Math.pow(10, precision);
return Math.round(value * (10 ** precision)) / (10 ** precision);
}
/**

View File

@@ -57,12 +57,12 @@ const removeAttributes = function (node, attributes) {
});
};
const numRgx = /[+-]?(?:\d+\.\d*|\d+|\.\d+)(?:[eE][+-]?\d+)?/g;
const numRgx = /[+-]?(?:\d+\.\d*|\d+|\.\d+)(?:[eE]\d+|[eE][+-]\d+)?/g;
const getLinesOptionsOfPoly = function (node) {
let nums = node.getAttribute('points');
nums = (nums && nums.match(numRgx)) || [];
if (nums && nums.length) {
nums = nums.map(Number);
nums = nums.map((n) => Number(n));
if (nums.length % 2) {
nums.length--;
}

View File

@@ -1,3 +1,4 @@
/* eslint-disable unicorn/no-fn-reference-in-iterator */
/* globals jQuery */
/**
* Localizing script for SVG-edit UI

View File

@@ -6138,11 +6138,11 @@ editor.init = function () {
let imgHeight = 100;
const img = new Image();
img.style.opacity = 0;
img.onload = function () {
img.addEventListener('load', function () {
imgWidth = img.offsetWidth || img.naturalWidth || img.width;
imgHeight = img.offsetHeight || img.naturalHeight || img.height;
insertNewImage(imgWidth, imgHeight);
};
});
img.src = result;
};
reader.readAsDataURL(file);
@@ -6431,7 +6431,7 @@ editor.loadFromDataURI = function (str, {noAlert} = {}) {
if (pre) {
base64 = true;
} else {
pre = str.match(/^data:image\/svg\+xml(?:;(?:utf8)?)?,/);
pre = str.match(/^data:image\/svg\+xml(?:;|;utf8)?,/);
}
if (pre) {
pre = pre[0];

View File

@@ -1,4 +1,4 @@
/* eslint-disable indent */
/* eslint-disable indent, unicorn/no-fn-reference-in-iterator */
/* globals jQuery, jsPDF */
/**
* Numerous tools for working with the editor's "canvas"
@@ -3656,7 +3656,7 @@ this.svgToString = function (elem, indent) {
if (elem) {
cleanupElement(elem);
const attrs = Array.from(elem.attributes);
const attrs = [...elem.attributes];
const childs = elem.childNodes;
attrs.sort((a, b) => {
return a.name > b.name ? -1 : 1;

View File

@@ -804,7 +804,7 @@ if (!('SVGPathSegList' in window) || !('appendItem' in window.SVGPathSegList.pro
number *= sign;
if (exponent) {
number *= Math.pow(10, expsign * exponent);
number *= 10 ** (expsign * exponent);
}
if (startIndex === this._currentIndex) {

View File

@@ -497,10 +497,10 @@ export const getPathBBox = function (path) {
const getCalc = function (j, P1, P2, P3) {
return function (t) {
return Math.pow(1 - t, 3) * P0[j] +
3 * Math.pow(1 - t, 2) * t * P1[j] +
3 * (1 - t) * Math.pow(t, 2) * P2[j] +
Math.pow(t, 3) * P3[j];
return 1 - (t ** 3) * P0[j] +
3 * 1 - (t ** 2) * t * P1[j] +
3 * (1 - t) * (t ** 2) * P2[j] +
(t ** 3) * P3[j];
};
};
@@ -533,7 +533,7 @@ export const getPathBBox = function (path) {
}
continue;
}
const b2ac = Math.pow(b, 2) - 4 * c * a;
const b2ac = (b ** 2) - 4 * c * a;
if (b2ac < 0) { continue; }
const t1 = (-b + Math.sqrt(b2ac)) / (2 * a);
if (t1 > 0 && t1 < 1) { bounds[j].push(calc(t1)); }

View File

@@ -8331,7 +8331,7 @@
var getCalc = function getCalc(j, P1, P2, P3) {
return function (t) {
return Math.pow(1 - t, 3) * P0[j] + 3 * Math.pow(1 - t, 2) * t * P1[j] + 3 * (1 - t) * Math.pow(t, 2) * P2[j] + Math.pow(t, 3) * P3[j];
return 1 - Math.pow(t, 3) * P0[j] + 3 * 1 - Math.pow(t, 2) * t * P1[j] + 3 * (1 - t) * Math.pow(t, 2) * P2[j] + Math.pow(t, 3) * P3[j];
};
};
@@ -9461,27 +9461,37 @@
return new Promise(function (resolve, reject) {
// eslint-disable-line promise/avoid-new
var script = document.createElement('script');
/**
*
* @returns {undefined}
*/
function scriptOnError() {
reject(new Error("Failed to import: ".concat(url)));
destructor();
}
/**
*
* @returns {undefined}
*/
function scriptOnLoad() {
resolve();
destructor();
}
var destructor = function destructor() {
script.onerror = null;
script.onload = null;
script.removeEventListener('error', scriptOnError);
script.removeEventListener('load', scriptOnLoad);
script.remove();
script.src = '';
};
script.defer = 'defer';
addScriptAtts(script, atts);
script.onerror = function () {
reject(new Error("Failed to import: ".concat(url)));
destructor();
};
script.onload = function () {
resolve();
destructor();
};
script.addEventListener('error', scriptOnError);
script.addEventListener('load', scriptOnLoad);
script.src = url;
document.head.append(script);
});
@@ -9513,11 +9523,30 @@
// eslint-disable-line promise/avoid-new
var vector = '$importModule$' + Math.random().toString(32).slice(2);
var script = document.createElement('script');
/**
*
* @returns {undefined}
*/
function scriptOnError() {
reject(new Error("Failed to import: ".concat(url)));
destructor();
}
/**
*
* @returns {undefined}
*/
function scriptOnLoad() {
resolve(window[vector]);
destructor();
}
var destructor = function destructor() {
delete window[vector];
script.onerror = null;
script.onload = null;
script.removeEventListener('error', scriptOnError);
script.removeEventListener('load', scriptOnLoad);
script.remove();
URL.revokeObjectURL(script.src);
script.src = '';
@@ -9526,17 +9555,8 @@
addScriptAtts(script, atts);
script.defer = 'defer';
script.type = 'module';
script.onerror = function () {
reject(new Error("Failed to import: ".concat(url)));
destructor();
};
script.onload = function () {
resolve(window[vector]);
destructor();
};
script.addEventListener('error', scriptOnError);
script.addEventListener('load', scriptOnLoad);
var absURL = toAbsoluteURL(url);
var loader = "import * as m from '".concat(absURL.replace(/'/g, "\\'"), "'; window.").concat(vector, " = ").concat(returnDefault ? 'm.default || ' : '', "m;"); // export Module
@@ -17437,7 +17457,9 @@
if (elem) {
cleanupElement(elem);
var attrs = Array.from(elem.attributes);
var attrs = _toConsumableArray(elem.attributes);
var childs = elem.childNodes;
attrs.sort(function (a, b) {
return a.name > b.name ? -1 : 1;
@@ -35857,13 +35879,11 @@
var imgHeight = 100;
var img = new Image();
img.style.opacity = 0;
img.onload = function () {
img.addEventListener('load', function () {
imgWidth = img.offsetWidth || img.naturalWidth || img.width;
imgHeight = img.offsetHeight || img.naturalHeight || img.height;
insertNewImage(imgWidth, imgHeight);
};
});
img.src = result;
};
@@ -36339,7 +36359,7 @@
if (pre) {
base64 = true;
} else {
pre = str.match(/^data:image\/svg\+xml(?:;(?:utf8)?)?,/);
pre = str.match(/^data:image\/svg\+xml(?:;|;utf8)?,/);
}
if (pre) {