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
+212 -118
View File
@@ -85,7 +85,7 @@ export const ValueTypes = {
BOOLEAN: 0b01000,
NUMBER_ARRAY: 0b10000,
ANY: 0b11111,
NONE: 0
NONE: 0,
};
/**
@@ -126,8 +126,8 @@ export function getValueType(value) {
if (!Array.isArray(value)) {
throw new Error(`Unhandled value type: ${JSON.stringify(value)}`);
}
const valueArr = /** @type {Array<*>} */(value);
const onlyNumbers = valueArr.every(function(v) {
const valueArr = /** @type {Array<*>} */ (value);
const onlyNumbers = valueArr.every(function (v) {
return typeof v === 'number';
});
if (onlyNumbers) {
@@ -137,11 +137,17 @@ export function getValueType(value) {
return ValueTypes.NUMBER_ARRAY;
}
if (typeof valueArr[0] !== 'string') {
throw new Error(`Expected an expression operator but received: ${JSON.stringify(valueArr)}`);
throw new Error(
`Expected an expression operator but received: ${JSON.stringify(
valueArr
)}`
);
}
const operator = Operators[valueArr[0]];
if (operator === undefined) {
throw new Error(`Unrecognized expression operator: ${JSON.stringify(valueArr)}`);
throw new Error(
`Unrecognized expression operator: ${JSON.stringify(valueArr)}`
);
}
return operator.getReturnType(valueArr.slice(1));
}
@@ -181,7 +187,9 @@ export function numberToGlsl(v) {
*/
export function arrayToGlsl(array) {
if (array.length < 2 || array.length > 4) {
throw new Error('`formatArray` can only output `vec2`, `vec3` or `vec4` arrays.');
throw new Error(
'`formatArray` can only output `vec2`, `vec3` or `vec4` arrays.'
);
}
return `vec${array.length}(${array.map(numberToGlsl).join(', ')})`;
}
@@ -199,7 +207,7 @@ export function colorToGlsl(color) {
array.push(1);
}
return arrayToGlsl(
array.map(function(c, i) {
array.map(function (c, i) {
return i < 3 ? c / 255 : c;
})
);
@@ -213,7 +221,9 @@ export function colorToGlsl(color) {
*/
export function getStringNumberEquivalent(context, string) {
if (context.stringLiteralsMap[string] === undefined) {
context.stringLiteralsMap[string] = Object.keys(context.stringLiteralsMap).length;
context.stringLiteralsMap[string] = Object.keys(
context.stringLiteralsMap
).length;
}
return context.stringLiteralsMap[string];
}
@@ -242,31 +252,35 @@ export function expressionToGlsl(context, value, typeHint) {
if (Array.isArray(value) && typeof value[0] === 'string') {
const operator = Operators[value[0]];
if (operator === undefined) {
throw new Error(`Unrecognized expression operator: ${JSON.stringify(value)}`);
throw new Error(
`Unrecognized expression operator: ${JSON.stringify(value)}`
);
}
return operator.toGlsl(context, value.slice(1), typeHint);
} else if ((getValueType(value) & ValueTypes.NUMBER) > 0) {
return numberToGlsl(/** @type {number} */(value));
return numberToGlsl(/** @type {number} */ (value));
} else if ((getValueType(value) & ValueTypes.BOOLEAN) > 0) {
return value.toString();
} else if (
((getValueType(value) & ValueTypes.STRING) > 0) &&
(getValueType(value) & ValueTypes.STRING) > 0 &&
(typeHint === undefined || typeHint == ValueTypes.STRING)
) {
return stringToGlsl(context, value.toString());
} else if (
((getValueType(value) & ValueTypes.COLOR) > 0) &&
(getValueType(value) & ValueTypes.COLOR) > 0 &&
(typeHint === undefined || typeHint == ValueTypes.COLOR)
) {
return colorToGlsl(/** @type {number[]|string} */(value));
return colorToGlsl(/** @type {number[]|string} */ (value));
} else if ((getValueType(value) & ValueTypes.NUMBER_ARRAY) > 0) {
return arrayToGlsl(/** @type {number[]} */(value));
return arrayToGlsl(/** @type {number[]} */ (value));
}
}
function assertNumber(value) {
if (!(getValueType(value) & ValueTypes.NUMBER)) {
throw new Error(`A numeric value was expected, got ${JSON.stringify(value)} instead`);
throw new Error(
`A numeric value was expected, got ${JSON.stringify(value)} instead`
);
}
}
function assertNumbers(values) {
@@ -276,50 +290,68 @@ function assertNumbers(values) {
}
function assertString(value) {
if (!(getValueType(value) & ValueTypes.STRING)) {
throw new Error(`A string value was expected, got ${JSON.stringify(value)} instead`);
throw new Error(
`A string value was expected, got ${JSON.stringify(value)} instead`
);
}
}
function assertBoolean(value) {
if (!(getValueType(value) & ValueTypes.BOOLEAN)) {
throw new Error(`A boolean value was expected, got ${JSON.stringify(value)} instead`);
throw new Error(
`A boolean value was expected, got ${JSON.stringify(value)} instead`
);
}
}
function assertArgsCount(args, count) {
if (args.length !== count) {
throw new Error(`Exactly ${count} arguments were expected, got ${args.length} instead`);
throw new Error(
`Exactly ${count} arguments were expected, got ${args.length} instead`
);
}
}
function assertArgsMinCount(args, count) {
if (args.length < count) {
throw new Error(`At least ${count} arguments were expected, got ${args.length} instead`);
throw new Error(
`At least ${count} arguments were expected, got ${args.length} instead`
);
}
}
function assertArgsMaxCount(args, count) {
if (args.length > count) {
throw new Error(`At most ${count} arguments were expected, got ${args.length} instead`);
throw new Error(
`At most ${count} arguments were expected, got ${args.length} instead`
);
}
}
function assertArgsEven(args) {
if (args.length % 2 !== 0) {
throw new Error(`An even amount of arguments was expected, got ${args} instead`);
throw new Error(
`An even amount of arguments was expected, got ${args} instead`
);
}
}
function assertArgsOdd(args) {
if (args.length % 2 === 0) {
throw new Error(`An even amount of arguments was expected, got ${args} instead`);
throw new Error(
`An even amount of arguments was expected, got ${args} instead`
);
}
}
function assertUniqueInferredType(args, types) {
if (!isTypeUnique(types)) {
throw new Error(`Could not infer only one type from the following expression: ${JSON.stringify(args)}`);
throw new Error(
`Could not infer only one type from the following expression: ${JSON.stringify(
args
)}`
);
}
}
Operators['get'] = {
getReturnType: function(args) {
getReturnType: function (args) {
return ValueTypes.ANY;
},
toGlsl: function(context, args) {
toGlsl: function (context, args) {
assertArgsCount(args, 1);
assertString(args[0]);
const value = args[0].toString();
@@ -328,13 +360,13 @@ Operators['get'] = {
}
const prefix = context.inFragmentShader ? 'v_' : 'a_';
return prefix + value;
}
},
};
Operators['var'] = {
getReturnType: function(args) {
getReturnType: function (args) {
return ValueTypes.ANY;
},
toGlsl: function(context, args) {
toGlsl: function (context, args) {
assertArgsCount(args, 1);
assertString(args[0]);
const value = args[0].toString();
@@ -342,156 +374,186 @@ Operators['var'] = {
context.variables.push(value);
}
return `u_${value}`;
}
},
};
Operators['time'] = {
getReturnType: function(args) {
getReturnType: function (args) {
return ValueTypes.NUMBER;
},
toGlsl: function(context, args) {
toGlsl: function (context, args) {
assertArgsCount(args, 0);
return 'u_time';
}
},
};
Operators['zoom'] = {
getReturnType: function(args) {
getReturnType: function (args) {
return ValueTypes.NUMBER;
},
toGlsl: function(context, args) {
toGlsl: function (context, args) {
assertArgsCount(args, 0);
return 'u_zoom';
}
},
};
Operators['resolution'] = {
getReturnType: function(args) {
getReturnType: function (args) {
return ValueTypes.NUMBER;
},
toGlsl: function(context, args) {
toGlsl: function (context, args) {
assertArgsCount(args, 0);
return 'u_resolution';
}
},
};
Operators['*'] = {
getReturnType: function(args) {
getReturnType: function (args) {
return ValueTypes.NUMBER;
},
toGlsl: function(context, args) {
toGlsl: function (context, args) {
assertArgsCount(args, 2);
assertNumbers(args);
return `(${expressionToGlsl(context, args[0])} * ${expressionToGlsl(context, args[1])})`;
}
return `(${expressionToGlsl(context, args[0])} * ${expressionToGlsl(
context,
args[1]
)})`;
},
};
Operators['/'] = {
getReturnType: function(args) {
getReturnType: function (args) {
return ValueTypes.NUMBER;
},
toGlsl: function(context, args) {
toGlsl: function (context, args) {
assertArgsCount(args, 2);
assertNumbers(args);
return `(${expressionToGlsl(context, args[0])} / ${expressionToGlsl(context, args[1])})`;
}
return `(${expressionToGlsl(context, args[0])} / ${expressionToGlsl(
context,
args[1]
)})`;
},
};
Operators['+'] = {
getReturnType: function(args) {
getReturnType: function (args) {
return ValueTypes.NUMBER;
},
toGlsl: function(context, args) {
toGlsl: function (context, args) {
assertArgsCount(args, 2);
assertNumbers(args);
return `(${expressionToGlsl(context, args[0])} + ${expressionToGlsl(context, args[1])})`;
}
return `(${expressionToGlsl(context, args[0])} + ${expressionToGlsl(
context,
args[1]
)})`;
},
};
Operators['-'] = {
getReturnType: function(args) {
getReturnType: function (args) {
return ValueTypes.NUMBER;
},
toGlsl: function(context, args) {
toGlsl: function (context, args) {
assertArgsCount(args, 2);
assertNumbers(args);
return `(${expressionToGlsl(context, args[0])} - ${expressionToGlsl(context, args[1])})`;
}
return `(${expressionToGlsl(context, args[0])} - ${expressionToGlsl(
context,
args[1]
)})`;
},
};
Operators['clamp'] = {
getReturnType: function(args) {
getReturnType: function (args) {
return ValueTypes.NUMBER;
},
toGlsl: function(context, args) {
toGlsl: function (context, args) {
assertArgsCount(args, 3);
assertNumbers(args);
const min = expressionToGlsl(context, args[1]);
const max = expressionToGlsl(context, args[2]);
return `clamp(${expressionToGlsl(context, args[0])}, ${min}, ${max})`;
}
},
};
Operators['%'] = {
getReturnType: function(args) {
getReturnType: function (args) {
return ValueTypes.NUMBER;
},
toGlsl: function(context, args) {
toGlsl: function (context, args) {
assertArgsCount(args, 2);
assertNumbers(args);
return `mod(${expressionToGlsl(context, args[0])}, ${expressionToGlsl(context, args[1])})`;
}
return `mod(${expressionToGlsl(context, args[0])}, ${expressionToGlsl(
context,
args[1]
)})`;
},
};
Operators['^'] = {
getReturnType: function(args) {
getReturnType: function (args) {
return ValueTypes.NUMBER;
},
toGlsl: function(context, args) {
toGlsl: function (context, args) {
assertArgsCount(args, 2);
assertNumbers(args);
return `pow(${expressionToGlsl(context, args[0])}, ${expressionToGlsl(context, args[1])})`;
}
return `pow(${expressionToGlsl(context, args[0])}, ${expressionToGlsl(
context,
args[1]
)})`;
},
};
Operators['>'] = {
getReturnType: function(args) {
getReturnType: function (args) {
return ValueTypes.BOOLEAN;
},
toGlsl: function(context, args) {
toGlsl: function (context, args) {
assertArgsCount(args, 2);
assertNumbers(args);
return `(${expressionToGlsl(context, args[0])} > ${expressionToGlsl(context, args[1])})`;
}
return `(${expressionToGlsl(context, args[0])} > ${expressionToGlsl(
context,
args[1]
)})`;
},
};
Operators['>='] = {
getReturnType: function(args) {
getReturnType: function (args) {
return ValueTypes.BOOLEAN;
},
toGlsl: function(context, args) {
toGlsl: function (context, args) {
assertArgsCount(args, 2);
assertNumbers(args);
return `(${expressionToGlsl(context, args[0])} >= ${expressionToGlsl(context, args[1])})`;
}
return `(${expressionToGlsl(context, args[0])} >= ${expressionToGlsl(
context,
args[1]
)})`;
},
};
Operators['<'] = {
getReturnType: function(args) {
getReturnType: function (args) {
return ValueTypes.BOOLEAN;
},
toGlsl: function(context, args) {
toGlsl: function (context, args) {
assertArgsCount(args, 2);
assertNumbers(args);
return `(${expressionToGlsl(context, args[0])} < ${expressionToGlsl(context, args[1])})`;
}
return `(${expressionToGlsl(context, args[0])} < ${expressionToGlsl(
context,
args[1]
)})`;
},
};
Operators['<='] = {
getReturnType: function(args) {
getReturnType: function (args) {
return ValueTypes.BOOLEAN;
},
toGlsl: function(context, args) {
toGlsl: function (context, args) {
assertArgsCount(args, 2);
assertNumbers(args);
return `(${expressionToGlsl(context, args[0])} <= ${expressionToGlsl(context, args[1])})`;
}
return `(${expressionToGlsl(context, args[0])} <= ${expressionToGlsl(
context,
args[1]
)})`;
},
};
function getEqualOperator(operator) {
return {
getReturnType: function(args) {
getReturnType: function (args) {
return ValueTypes.BOOLEAN;
},
toGlsl: function(context, args) {
toGlsl: function (context, args) {
assertArgsCount(args, 2);
// find common type
@@ -500,82 +562,93 @@ function getEqualOperator(operator) {
type = type & getValueType(args[i]);
}
if (type === 0) {
throw new Error(`All arguments should be of compatible type, got ${JSON.stringify(args)} instead`);
throw new Error(
`All arguments should be of compatible type, got ${JSON.stringify(
args
)} instead`
);
}
return `(${expressionToGlsl(context, args[0], type)} ${operator} ${expressionToGlsl(context, args[1], type)})`;
}
return `(${expressionToGlsl(
context,
args[0],
type
)} ${operator} ${expressionToGlsl(context, args[1], type)})`;
},
};
}
Operators['=='] = getEqualOperator('==');
Operators['!='] = getEqualOperator('!=');
Operators['!'] = {
getReturnType: function(args) {
getReturnType: function (args) {
return ValueTypes.BOOLEAN;
},
toGlsl: function(context, args) {
toGlsl: function (context, args) {
assertArgsCount(args, 1);
assertBoolean(args[0]);
return `(!${expressionToGlsl(context, args[0])})`;
}
},
};
Operators['between'] = {
getReturnType: function(args) {
getReturnType: function (args) {
return ValueTypes.BOOLEAN;
},
toGlsl: function(context, args) {
toGlsl: function (context, args) {
assertArgsCount(args, 3);
assertNumbers(args);
const min = expressionToGlsl(context, args[1]);
const max = expressionToGlsl(context, args[2]);
const value = expressionToGlsl(context, args[0]);
return `(${value} >= ${min} && ${value} <= ${max})`;
}
},
};
Operators['array'] = {
getReturnType: function(args) {
getReturnType: function (args) {
return ValueTypes.NUMBER_ARRAY;
},
toGlsl: function(context, args) {
toGlsl: function (context, args) {
assertArgsMinCount(args, 2);
assertArgsMaxCount(args, 4);
assertNumbers(args);
const parsedArgs = args.map(function(val) {
const parsedArgs = args.map(function (val) {
return expressionToGlsl(context, val, ValueTypes.NUMBER);
});
return `vec${args.length}(${parsedArgs.join(', ')})`;
}
},
};
Operators['color'] = {
getReturnType: function(args) {
getReturnType: function (args) {
return ValueTypes.COLOR;
},
toGlsl: function(context, args) {
toGlsl: function (context, args) {
assertArgsMinCount(args, 3);
assertArgsMaxCount(args, 4);
assertNumbers(args);
const array = /** @type {number[]} */(args);
const array = /** @type {number[]} */ (args);
if (args.length === 3) {
array.push(1);
}
const parsedArgs = args.map(function(val, i) {
return expressionToGlsl(context, val, ValueTypes.NUMBER) + (i < 3 ? ' / 255.0' : '');
const parsedArgs = args.map(function (val, i) {
return (
expressionToGlsl(context, val, ValueTypes.NUMBER) +
(i < 3 ? ' / 255.0' : '')
);
});
return `vec${args.length}(${parsedArgs.join(', ')})`;
}
},
};
Operators['interpolate'] = {
getReturnType: function(args) {
getReturnType: function (args) {
let type = ValueTypes.COLOR | ValueTypes.NUMBER;
for (let i = 3; i < args.length; i += 2) {
type = type & getValueType(args[i]);
}
return type;
},
toGlsl: function(context, args, opt_typeHint) {
toGlsl: function (context, args, opt_typeHint) {
assertArgsEven(args);
assertArgsMinCount(args, 6);
@@ -583,12 +656,21 @@ Operators['interpolate'] = {
const type = args[0];
let interpolation;
switch (type[0]) {
case 'linear': interpolation = 1; break;
case 'exponential': interpolation = type[1]; break;
default: interpolation = null;
case 'linear':
interpolation = 1;
break;
case 'exponential':
interpolation = type[1];
break;
default:
interpolation = null;
}
if (!interpolation) {
throw new Error(`Invalid interpolation type for "interpolate" operator, received: ${JSON.stringify(type)}`);
throw new Error(
`Invalid interpolation type for "interpolate" operator, received: ${JSON.stringify(
type
)}`
);
}
// compute input/output types
@@ -603,13 +685,17 @@ Operators['interpolate'] = {
const output1 = expressionToGlsl(context, args[i + 1], outputType);
const stop2 = expressionToGlsl(context, args[i + 2]);
const output2 = expressionToGlsl(context, args[i + 3], outputType);
result = `mix(${result || output1}, ${output2}, pow(clamp((${input} - ${stop1}) / (${stop2} - ${stop1}), 0.0, 1.0), ${numberToGlsl(interpolation)}))`;
result = `mix(${
result || output1
}, ${output2}, pow(clamp((${input} - ${stop1}) / (${stop2} - ${stop1}), 0.0, 1.0), ${numberToGlsl(
interpolation
)}))`;
}
return result;
}
},
};
Operators['match'] = {
getReturnType: function(args) {
getReturnType: function (args) {
let type = ValueTypes.ANY;
for (let i = 2; i < args.length; i += 2) {
type = type & getValueType(args[i]);
@@ -617,7 +703,7 @@ Operators['match'] = {
type = type & getValueType(args[args.length - 1]);
return type;
},
toGlsl: function(context, args, opt_typeHint) {
toGlsl: function (context, args, opt_typeHint) {
assertArgsEven(args);
assertArgsMinCount(args, 4);
@@ -626,7 +712,11 @@ Operators['match'] = {
assertUniqueInferredType(args, outputType);
const input = expressionToGlsl(context, args[0]);
const fallback = expressionToGlsl(context, args[args.length - 1], outputType);
const fallback = expressionToGlsl(
context,
args[args.length - 1],
outputType
);
let result = null;
for (let i = args.length - 3; i >= 1; i -= 2) {
const match = expressionToGlsl(context, args[i]);
@@ -634,10 +724,10 @@ Operators['match'] = {
result = `(${input} == ${match} ? ${output} : ${result || fallback})`;
}
return result;
}
},
};
Operators['case'] = {
getReturnType: function(args) {
getReturnType: function (args) {
let type = ValueTypes.ANY;
for (let i = 1; i < args.length; i += 2) {
type = type & getValueType(args[i]);
@@ -645,7 +735,7 @@ Operators['case'] = {
type = type & getValueType(args[args.length - 1]);
return type;
},
toGlsl: function(context, args, opt_typeHint) {
toGlsl: function (context, args, opt_typeHint) {
assertArgsOdd(args);
assertArgsMinCount(args, 3);
@@ -656,7 +746,11 @@ Operators['case'] = {
assertBoolean(args[i]);
}
const fallback = expressionToGlsl(context, args[args.length - 1], outputType);
const fallback = expressionToGlsl(
context,
args[args.length - 1],
outputType
);
let result = null;
for (let i = args.length - 3; i >= 0; i -= 2) {
const condition = expressionToGlsl(context, args[i]);
@@ -664,5 +758,5 @@ Operators['case'] = {
result = `(${condition} ? ${output} : ${result || fallback})`;
}
return result;
}
},
};