Make code prettier

This updates ESLint and our shared eslint-config-openlayers to use Prettier.  Most formatting changes were automatically applied with this:

    npm run lint -- --fix

A few manual changes were required:

 * In `examples/offscreen-canvas.js`, the `//eslint-disable-line` comment needed to be moved to the appropriate line to disable the error about the `'worker-loader!./offscreen-canvas.worker.js'` import.
 * In `examples/webpack/exapmle-builder.js`, spaces could not be added after a couple `function`s for some reason.  While editing this, I reworked `ExampleBuilder` to be a class.
 * In `src/ol/format/WMSGetFeatureInfo.js`, the `// @ts-ignore` comment needed to be moved down one line so it applied to the `parsersNS` argument.
This commit is contained in:
Tim Schaub
2020-04-06 12:25:12 -06:00
parent 53b48baf62
commit 054af09032
790 changed files with 46833 additions and 33765 deletions

View File

@@ -1,13 +1,12 @@
// avoid importing anything that results in an instanceof check
// since these extensions are global, instanceof checks fail with modules
(function(global) {
(function (global) {
// show generated maps for rendering tests
const showMap = (global.location.search.indexOf('generate') >= 0);
const showMap = global.location.search.indexOf('generate') >= 0;
// show a diff when rendering tests fail
const showDiff = (global.location.search.indexOf('showdiff') >= 0);
const showDiff = global.location.search.indexOf('showdiff') >= 0;
/**
* The default tolerance for image comparisons.
@@ -17,7 +16,7 @@
function afterLoad(type, path, next) {
const client = new XMLHttpRequest();
client.open('GET', path, true);
client.onload = function() {
client.onload = function () {
let data;
if (type === 'xml') {
data = client.responseXML;
@@ -37,56 +36,64 @@
* @param {function(Object)} next Function to call with response object on
* success. On failure, an error is thrown with the reason.
*/
global.afterLoadJson = function(path, next) {
global.afterLoadJson = function (path, next) {
afterLoad('json', path, next);
};
/**
* @param {string} path Relative path to file (e.g. 'spec/ol/foo.txt').
* @param {function(string)} next Function to call with response text on
* success. On failure, an error is thrown with the reason.
*/
global.afterLoadText = function(path, next) {
global.afterLoadText = function (path, next) {
afterLoad('text', path, next);
};
/**
* @param {string} path Relative path to file (e.g. 'spec/ol/foo.xml').
* @param {function(Document)} next Function to call with response xml on
* success. On failure, an error is thrown with the reason.
*/
global.afterLoadXml = function(path, next) {
global.afterLoadXml = function (path, next) {
afterLoad('xml', path, next);
};
// extensions to expect.js
const expect = global.expect;
/**
* Assert value is within some tolerance of a number.
* @param {Number} n Number.
* @param {Number} tol Tolerance.
* @return {expect.Assertion} The assertion.
*/
expect.Assertion.prototype.roughlyEqual = function(n, tol) {
expect.Assertion.prototype.roughlyEqual = function (n, tol) {
this.assert(
Math.abs(this.obj - n) <= tol,
function() {
return 'expected ' + expect.stringify(this.obj) + ' to be within ' +
tol + ' of ' + n;
function () {
return (
'expected ' +
expect.stringify(this.obj) +
' to be within ' +
tol +
' of ' +
n
);
},
function() {
return 'expected ' + expect.stringify(this.obj) +
' not to be within ' + tol + ' of ' + n;
});
function () {
return (
'expected ' +
expect.stringify(this.obj) +
' not to be within ' +
tol +
' of ' +
n
);
}
);
return this;
};
function getChildNodes(node, options) {
// check whitespace
if (options && options.includeWhiteSpace) {
@@ -100,14 +107,16 @@
nodes.push(child);
} else if (child.nodeType == 3) {
// text node, add if non empty
if (child.nodeValue &&
child.nodeValue.replace(/^\s*(.*?)\s*$/, '$1') !== '') {
if (
child.nodeValue &&
child.nodeValue.replace(/^\s*(.*?)\s*$/, '$1') !== ''
) {
nodes.push(child);
}
}
}
if (options && options.ignoreElementOrder) {
nodes.sort(function(a, b) {
nodes.sort(function (a, b) {
return a.nodeName > b.nodeName ? 1 : a.nodeName < b.nodeName ? -1 : 0;
});
}
@@ -116,24 +125,46 @@
}
function assertElementNodesEqual(node1, node2, options, errors) {
const testPrefix = (options && options.prefix === true);
const testPrefix = options && options.prefix === true;
if (node1.nodeType !== node2.nodeType) {
errors.push('nodeType test failed for: ' + node1.nodeName + ' | ' +
node2.nodeName + ' | expected ' + node1.nodeType + ' to equal ' +
node2.nodeType);
errors.push(
'nodeType test failed for: ' +
node1.nodeName +
' | ' +
node2.nodeName +
' | expected ' +
node1.nodeType +
' to equal ' +
node2.nodeType
);
}
if (testPrefix) {
if (node1.nodeName !== node2.nodeName) {
errors.push('nodeName test failed for: ' + node1.nodeName + ' | ' +
node2.nodeName + ' | expected ' + node1.nodeName + ' to equal ' +
node2.nodeName);
errors.push(
'nodeName test failed for: ' +
node1.nodeName +
' | ' +
node2.nodeName +
' | expected ' +
node1.nodeName +
' to equal ' +
node2.nodeName
);
}
} else {
const n1 = node1.nodeName.split(':').pop();
const n2 = node2.nodeName.split(':').pop();
if (n1 !== n2) {
errors.push('nodeName test failed for: ' + node1.nodeName + ' | ' +
node2.nodeName + ' | expected ' + n1 + ' to equal ' + n2);
errors.push(
'nodeName test failed for: ' +
node1.nodeName +
' | ' +
node2.nodeName +
' | expected ' +
n1 +
' to equal ' +
n2
);
}
}
// for text nodes compare value
@@ -141,8 +172,9 @@
const nv1 = node1.nodeValue.replace(/\s/g, '');
const nv2 = node2.nodeValue.replace(/\s/g, '');
if (nv1 !== nv2) {
errors.push('nodeValue test failed | expected ' + nv1 + ' to equal ' +
nv2);
errors.push(
'nodeValue test failed | expected ' + nv1 + ' to equal ' + nv2
);
}
} else if (node1.nodeType === 1) {
// for element type nodes compare namespace, attributes, and children
@@ -150,16 +182,27 @@
if (node1.prefix || node2.prefix) {
if (testPrefix) {
if (node1.prefix !== node2.prefix) {
errors.push('Prefix test failed for: ' + node1.nodeName +
' | expected ' + node1.prefix + ' to equal ' + node2.prefix);
errors.push(
'Prefix test failed for: ' +
node1.nodeName +
' | expected ' +
node1.prefix +
' to equal ' +
node2.prefix
);
}
}
}
if (node1.namespaceURI || node2.namespaceURI) {
if (node1.namespaceURI !== node2.namespaceURI) {
errors.push('namespaceURI test failed for: ' + node1.nodeName +
' | expected ' + node1.namespaceURI + ' to equal ' +
node2.namespaceURI);
errors.push(
'namespaceURI test failed for: ' +
node1.nodeName +
' | expected ' +
node1.namespaceURI +
' to equal ' +
node2.namespaceURI
);
}
}
// compare attributes - disregard xmlns given namespace handling above
@@ -194,13 +237,23 @@
}
}
if (node1AttrLen !== node2AttrLen) {
errors.push('Number of attributes test failed for: ' + node1.nodeName +
' | expected ' + node1AttrLen + ' to equal ' + node2AttrLen);
errors.push(
'Number of attributes test failed for: ' +
node1.nodeName +
' | expected ' +
node1AttrLen +
' to equal ' +
node2AttrLen
);
}
for (const name in node1Attr) {
if (node2Attr[name] === undefined) {
errors.push('Attribute name ' + node1Attr[name].name +
' expected for element ' + node1.nodeName);
errors.push(
'Attribute name ' +
node1Attr[name].name +
' expected for element ' +
node1.nodeName
);
break;
}
// test attribute namespace
@@ -208,16 +261,28 @@
// null for namespaceURI some tests will fail in IE9 otherwise
// see also
// http://msdn.microsoft.com/en-us/library/ff460650(v=vs.85).aspx
if ((node1Attr[name].namespaceURI || null) !==
(node2Attr[name].namespaceURI || null)) {
errors.push('namespaceURI attribute test failed for: ' +
node1.nodeName + ' | expected ' + node1Attr[name].namespaceURI +
' to equal ' + node2Attr[name].namespaceURI);
if (
(node1Attr[name].namespaceURI || null) !==
(node2Attr[name].namespaceURI || null)
) {
errors.push(
'namespaceURI attribute test failed for: ' +
node1.nodeName +
' | expected ' +
node1Attr[name].namespaceURI +
' to equal ' +
node2Attr[name].namespaceURI
);
}
if (node1Attr[name].value !== node2Attr[name].value) {
errors.push('Attribute value test failed for: ' + node1.nodeName +
' | expected ' + node1Attr[name].value + ' to equal ' +
node2Attr[name].value);
errors.push(
'Attribute value test failed for: ' +
node1.nodeName +
' | expected ' +
node1Attr[name].value +
' to equal ' +
node2Attr[name].value
);
}
}
// compare children
@@ -242,22 +307,30 @@
}
}
if (!allText) {
errors.push('Number of childNodes test failed for: ' +
node1.nodeName + ' | expected ' + node1ChildNodes.length +
' to equal ' + node2ChildNodes.length);
errors.push(
'Number of childNodes test failed for: ' +
node1.nodeName +
' | expected ' +
node1ChildNodes.length +
' to equal ' +
node2ChildNodes.length
);
}
}
// only compare if they are equal
if (node1ChildNodes.length === node2ChildNodes.length) {
for (let j = 0, jj = node1ChildNodes.length; j < jj; ++j) {
assertElementNodesEqual(
node1ChildNodes[j], node2ChildNodes[j], options, errors);
node1ChildNodes[j],
node2ChildNodes[j],
options,
errors
);
}
}
}
}
/**
* Checks if the XML document sort of equals another XML document.
* @param {Object} obj The other object.
@@ -265,7 +338,7 @@
* ignoreElementOrder: (boolean|undefined)}=} options The options.
* @return {expect.Assertion} The assertion.
*/
expect.Assertion.prototype.xmleql = function(obj, options) {
expect.Assertion.prototype.xmleql = function (obj, options) {
if (obj && obj.nodeType == 9) {
obj = obj.documentElement;
}
@@ -274,24 +347,34 @@
}
const errors = [];
assertElementNodesEqual(obj, this.obj, options, errors);
const result = (errors.length === 0);
const result = errors.length === 0;
this.assert(
!!result,
function() {
return 'expected ' + expect.stringify(this.obj) +
' to sort of equal ' + expect.stringify(obj) + '\n' +
errors.join('\n');
function () {
return (
'expected ' +
expect.stringify(this.obj) +
' to sort of equal ' +
expect.stringify(obj) +
'\n' +
errors.join('\n')
);
},
function() {
return 'expected ' + expect.stringify(this.obj) +
' to sort of not equal ' + expect.stringify(obj) + '\n' +
errors.join('\n');
});
function () {
return (
'expected ' +
expect.stringify(this.obj) +
' to sort of not equal ' +
expect.stringify(obj) +
'\n' +
errors.join('\n')
);
}
);
return this;
};
global.createMapDiv = function(width, height) {
global.createMapDiv = function (width, height) {
const target = document.createElement('div');
const style = target.style;
style.position = 'absolute';
@@ -304,7 +387,7 @@
return target;
};
global.disposeMap = function(map) {
global.disposeMap = function (map) {
const target = map.getTarget();
map.setTarget(null);
if (target && target.parentNode) {
@@ -325,7 +408,7 @@
const width = canvas.width;
const height = canvas.height;
const image = new Image();
image.addEventListener('load', function() {
image.addEventListener('load', function () {
expect(image.width).to.be(width);
expect(image.height).to.be(height);
const referenceCanvas = document.createElement('canvas');
@@ -338,8 +421,11 @@
const mismatchPx = pixelmatch(
context.getImageData(0, 0, width, height).data,
referenceContext.getImageData(0, 0, width, height).data,
output.data, width, height);
const mismatchPct = mismatchPx / (width * height) * 100;
output.data,
width,
height
);
const mismatchPct = (mismatchPx / (width * height)) * 100;
if (showDiff && mismatchPct > tolerance) {
const diffCanvas = document.createElement('canvas');
diffCanvas.width = width;
@@ -350,7 +436,7 @@
expect(mismatchPct).to.be.below(tolerance);
done();
});
image.addEventListener('error', function() {
image.addEventListener('error', function () {
expect().fail('Reference image could not be loaded');
done();
});
@@ -366,9 +452,9 @@
* @param {number} tolerance The accepted mismatch tolerance.
* @param {function} done A callback to indicate that the test is done.
*/
global.expectResemble = function(map, referenceImage, tolerance, done) {
global.expectResemble = function (map, referenceImage, tolerance, done) {
map.render();
map.on('postcompose', function(event) {
map.on('postcompose', function (event) {
if (event.frameState.animate) {
// make sure the tile-queue is empty
return;
@@ -385,8 +471,8 @@
ArrayBuffer: 'ArrayBuffer' in global,
'ArrayBuffer.isView': 'ArrayBuffer' in global && !!ArrayBuffer.isView,
FileReader: 'FileReader' in global,
Uint8ClampedArray: ('Uint8ClampedArray' in global),
WebGL: false
Uint8ClampedArray: 'Uint8ClampedArray' in global,
WebGL: false,
};
/**
@@ -397,22 +483,21 @@
* @return {Object} An object with a `describe` function that will run tests
* if the required feature is available and skip them otherwise.
*/
global.where = function(key) {
global.where = function (key) {
if (!(key in features)) {
throw new Error('where() called with unknown key: ' + key);
}
return {
describe: features[key] ? global.describe : global.xdescribe,
it: features[key] ? global.it : global.xit
it: features[key] ? global.it : global.xit,
};
};
// throw if anybody appends a div to the body and doesn't remove it
afterEach(function() {
afterEach(function () {
const garbage = document.body.getElementsByTagName('div');
if (garbage.length) {
throw new Error('Found extra <div> elements in the body');
}
});
})(window);