Skip to content

Commit 8b0cae4

Browse files
authored
chore(maintenance): migrate jmespath utility to biome (#2807)
1 parent b9db070 commit 8b0cae4

15 files changed

+161
-191
lines changed

packages/jmespath/package.json

+5-13
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
"build:cjs": "tsc --build tsconfig.json && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json",
1919
"build:esm": "tsc --build tsconfig.esm.json && echo '{ \"type\": \"module\" }' > lib/esm/package.json",
2020
"build": "npm run build:esm & npm run build:cjs",
21-
"lint": "eslint --ext .ts,.js --no-error-on-unmatched-pattern .",
22-
"lint-fix": "eslint --fix --ext .ts,.js --no-error-on-unmatched-pattern .",
21+
"lint": "biome lint .",
22+
"lint:fix": "biome check --write .",
2323
"prepack": "node ../../.github/scripts/release_patch_package_json.js ."
2424
},
2525
"homepage": "https://github.com/aws-powertools/powertools-lambda-typescript",
@@ -51,25 +51,17 @@
5151
},
5252
"typesVersions": {
5353
"*": {
54-
"envelopes": [
55-
"lib/cjs/envelopes.d.ts",
56-
"lib/esm/envelopes.d.ts"
57-
],
54+
"envelopes": ["lib/cjs/envelopes.d.ts", "lib/esm/envelopes.d.ts"],
5855
"functions": [
5956
"lib/cjs/PowertoolsFunctions.d.ts",
6057
"lib/esm/PowertoolsFunctions.d.ts"
6158
],
62-
"types": [
63-
"lib/cjs/types.d.ts",
64-
"lib/esm/types.d.ts"
65-
]
59+
"types": ["lib/cjs/types.d.ts", "lib/esm/types.d.ts"]
6660
}
6761
},
6862
"types": "./lib/cjs/index.d.ts",
6963
"main": "./lib/cjs/index.js",
70-
"files": [
71-
"lib"
72-
],
64+
"files": ["lib"],
7365
"dependencies": {
7466
"@aws-lambda-powertools/commons": "^2.5.0"
7567
},

packages/jmespath/src/Functions.ts

+29-32
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,8 @@ class Functions {
159159
): number {
160160
if (isRecord(arg)) {
161161
return Object.keys(arg).length;
162-
} else {
163-
return arg.length;
164162
}
163+
return arg.length;
165164
}
166165

167166
/**
@@ -194,12 +193,12 @@ class Functions {
194193
if (arg.length === 0) {
195194
return null;
196195
// The signature decorator already enforces that all elements are of the same type
197-
} else if (isNumber(arg[0])) {
196+
}
197+
if (isNumber(arg[0])) {
198198
return Math.max(...(arg as number[]));
199-
} else {
200-
// local compare function to handle string comparison
201-
return arg.reduce((a, b) => (a > b ? a : b));
202199
}
200+
// local compare function to handle string comparison
201+
return arg.reduce((a, b) => (a > b ? a : b));
203202
}
204203

205204
/**
@@ -236,14 +235,13 @@ class Functions {
236235

237236
if (max.visited === current.visited) {
238237
return max;
239-
} else {
240-
// We can safely cast visited to number | string here because we've already
241-
// checked the type at runtime above and we know that it's either a number or a string
242-
return (max.visited as number | string) >
243-
(current.visited as number | string)
244-
? max
245-
: current;
246238
}
239+
// We can safely cast visited to number | string here because we've already
240+
// checked the type at runtime above and we know that it's either a number or a string
241+
return (max.visited as number | string) >
242+
(current.visited as number | string)
243+
? max
244+
: current;
247245
}, visitedArgs[0]);
248246

249247
return max.arg;
@@ -261,6 +259,7 @@ class Functions {
261259
variadic: true,
262260
})
263261
public funcMerge(...args: Array<JSONObject>): JSONObject {
262+
// biome-ignore lint/performance/noAccumulatingSpread: This is a shallow merge so the tradeoff is acceptable
264263
return args.reduce((a, b) => ({ ...a, ...b }), {});
265264
}
266265

@@ -276,11 +275,11 @@ class Functions {
276275
if (arg.length === 0) {
277276
return null;
278277
// The signature decorator already enforces that all elements are of the same type
279-
} else if (isNumber(arg[0])) {
278+
}
279+
if (isNumber(arg[0])) {
280280
return Math.min(...arg);
281-
} else {
282-
return arg.reduce((a, b) => (a < b ? a : b));
283281
}
282+
return arg.reduce((a, b) => (a < b ? a : b));
284283
}
285284

286285
/**
@@ -317,14 +316,13 @@ class Functions {
317316

318317
if (min.visited === current.visited) {
319318
return min;
320-
} else {
321-
// We can safely cast visited to number | string here because we've already
322-
// checked the type at runtime above and we know that it's either a number or a string
323-
return (min.visited as string | number) <
324-
(current.visited as string | number)
325-
? min
326-
: current;
327319
}
320+
// We can safely cast visited to number | string here because we've already
321+
// checked the type at runtime above and we know that it's either a number or a string
322+
return (min.visited as string | number) <
323+
(current.visited as string | number)
324+
? min
325+
: current;
328326
}, visitedArgs[0]);
329327

330328
return min.arg;
@@ -415,13 +413,12 @@ class Functions {
415413
.sort((a, b) => {
416414
if (a.visited === b.visited) {
417415
return a.index - b.index; // Make the sort stable
418-
} else {
419-
// We can safely cast visited to number | string here because we've already
420-
// checked the type at runtime above and we know that it's either a number or a string
421-
return (a.visited as string | number) > (b.visited as string | number)
422-
? 1
423-
: -1;
424416
}
417+
// We can safely cast visited to number | string here because we've already
418+
// checked the type at runtime above and we know that it's either a number or a string
419+
return (a.visited as string | number) > (b.visited as string | number)
420+
? 1
421+
: -1;
425422
})
426423
.map(({ value }) => value); // Extract the original values
427424
}
@@ -484,13 +481,13 @@ class Functions {
484481
public funcToNumber(arg: JSONValue): number | null {
485482
if (typeof arg === 'number') {
486483
return arg;
487-
} else if (typeof arg === 'string') {
484+
}
485+
if (typeof arg === 'string') {
488486
const num = Number(arg);
489487

490488
return Number.isNaN(num) ? null : num;
491-
} else {
492-
return null;
493489
}
490+
return null;
494491
}
495492

496493
/**

packages/jmespath/src/Lexer.ts

+11-15
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ class Lexer {
3232
while (this.#current !== '' && this.#current !== undefined) {
3333
if (SIMPLE_TOKENS.has(this.#current)) {
3434
yield {
35-
// We know that SIMPLE_TOKENS has this.#current as a key because
36-
// we checked for that above.
37-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
35+
// biome-ignore lint/style/noNonNullAssertion: We know that SIMPLE_TOKENS has this.#current as a key because we checked for that above.
3836
type: SIMPLE_TOKENS.get(this.#current)!,
3937
value: this.#current,
4038
start: this.#position,
@@ -57,7 +55,7 @@ class Lexer {
5755
const buff = this.#consumeNumber();
5856
yield {
5957
type: 'number',
60-
value: parseInt(buff),
58+
value: Number.parseInt(buff),
6159
start: start,
6260
end: start + buff.length,
6361
};
@@ -119,9 +117,8 @@ class Lexer {
119117
start: this.#position - 1,
120118
end: this.#position,
121119
};
122-
} else {
123-
throw new LexerError(this.#position - 1, '=');
124120
}
121+
throw new LexerError(this.#position - 1, '=');
125122
}
126123

127124
/**
@@ -158,14 +155,13 @@ class Lexer {
158155
if (buff.length > 1) {
159156
return {
160157
type: 'number',
161-
value: parseInt(buff),
158+
value: Number.parseInt(buff),
162159
start: start,
163160
end: start + buff.length,
164161
};
165-
} else {
166-
// If the negative sign is not followed by a number, it is an error.
167-
throw new LexerError(start, 'Unknown token after "-"');
168162
}
163+
// If the negative sign is not followed by a number, it is an error.
164+
throw new LexerError(start, 'Unknown token after "-"');
169165
}
170166

171167
/**
@@ -194,17 +190,17 @@ class Lexer {
194190
#consumeSquareBracket(): Token {
195191
const start = this.#position;
196192
const nextChar = this.#next();
197-
if (nextChar == ']') {
193+
if (nextChar === ']') {
198194
this.#next();
199195

200196
return { type: 'flatten', value: '[]', start: start, end: start + 2 };
201-
} else if (nextChar == '?') {
197+
}
198+
if (nextChar === '?') {
202199
this.#next();
203200

204201
return { type: 'filter', value: '[?', start: start, end: start + 2 };
205-
} else {
206-
return { type: 'lbracket', value: '[', start: start, end: start + 1 };
207202
}
203+
return { type: 'lbracket', value: '[', start: start, end: start + 1 };
208204
}
209205

210206
/**
@@ -301,7 +297,7 @@ class Lexer {
301297
*/
302298
#consumeQuotedIdentifier(): Token {
303299
const start = this.#position;
304-
const lexeme = '"' + this.#consumeUntil('"') + '"';
300+
const lexeme = `"${this.#consumeUntil('"')}"`;
305301
const tokenLen = this.#position - start;
306302

307303
return {

packages/jmespath/src/ParsedResult.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
UnknownFunctionError,
66
VariadicArityError,
77
} from './errors.js';
8-
import type { Node, JMESPathParsingOptions, JSONObject } from './types.js';
8+
import type { JMESPathParsingOptions, JSONObject, Node } from './types.js';
99

1010
class ParsedResult {
1111
public expression: string;

0 commit comments

Comments
 (0)