Skip to content

Commit 17b342c

Browse files
committed
lint hand-written option checks outside options.ts
1 parent 73d888d commit 17b342c

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

‎eslint.config.mjs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ export default tseslint.config(
2222
// ({ removed: _, ...rest }) leaves out a property
2323
'@typescript-eslint/no-unused-vars': ['error', { ignoreRestSiblings: true }],
2424

25+
// option checks live in util/options.ts, so one option is checked (and worded) the same everywhere: use a
26+
// helper there, or add one (a check that only one place needs can say why with an eslint-disable)
27+
'no-restricted-syntax': ['error', {
28+
selector: "ThrowStatement > NewExpression[callee.name=/^(Range|Type)Error$/] > :matches(TemplateLiteral, Literal)[quasis.0.value.raw=/must be/], ThrowStatement > NewExpression[callee.name=/^(Range|Type)Error$/] > Literal[value=/must be/]",
29+
message: "Check options with the helpers of util/options.ts (oneOf, finite, pixelLimit, ...) instead of a hand-written 'must be' error."
30+
}],
31+
2532
// SonarJS: code that does nothing or something else than it reads like
2633
'sonarjs/array-callback-without-return': 'error',
2734
'sonarjs/no-all-duplicated-branches': 'error',
@@ -41,5 +48,16 @@ export default tseslint.config(
4148
// regular expressions that backtrack superlinearly (ReDoS)
4249
'sonarjs/slow-regex': 'error'
4350
}
51+
},
52+
{
53+
// the dimension limits of a format aren't options (quantize.ts holds the dithering options, so it stays checked)
54+
files: ['src/codecs/**/*.ts'],
55+
ignores: ['src/codecs/quantize.ts'],
56+
rules: { 'no-restricted-syntax': 'off' }
57+
},
58+
{
59+
// (where the helpers are)
60+
files: ['src/util/options.ts'],
61+
rules: { 'no-restricted-syntax': 'off' }
4462
}
4563
);

‎src/gainmap.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export class GainMap {
9898
*/
9999
set gamma(value: GainMapChannelValues) {
100100
const gamma = perChannel(value, 'gamma');
101+
// eslint-disable-next-line no-restricted-syntax -- a rule of the gain map's values, not a shared option
101102
if (gamma.some(g => g <= 0)) throw new RangeError('The gain map\'s gamma must be above 0');
102103
this.__gamma__ = gamma;
103104
}
@@ -140,6 +141,7 @@ export class GainMap {
140141
* @param value Whether the boost applies in the SDR image's color space
141142
*/
142143
set useBaseColorSpace(value: boolean) {
144+
// eslint-disable-next-line no-restricted-syntax -- the only boolean option that is validated
143145
if (typeof value !== 'boolean') throw new TypeError('useBaseColorSpace must be a boolean');
144146
this.__useBaseColorSpace__ = value;
145147
}
@@ -229,8 +231,10 @@ export class GainMap {
229231
private __check__(): void {
230232
// (each value is checked when it is set; these are the checks between them)
231233
for (let c = 0; c < 3; c++)
234+
// eslint-disable-next-line no-restricted-syntax -- a relation between two values of the gain map
232235
if (this.__max__[c] < this.__min__[c]) throw new RangeError(`The gain map's max must be at least its min (${this.__max__[c]} < ${this.__min__[c]}; the values are checked together when the map is cloned or encoded)`);
233236
if (this.__capacityMax__ <= this.__capacityMin__)
237+
// eslint-disable-next-line no-restricted-syntax -- a relation between two values of the gain map
234238
throw new RangeError(`The gain map's hdrCapacityMax (by default the largest max) must be above its hdrCapacityMin (${this.__capacityMax__} <= ${this.__capacityMin__})`);
235239
}
236240

0 commit comments

Comments
 (0)