Skip to content

chore(maintenance): migrate jmespath utility to biome #2807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions packages/jmespath/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"build:cjs": "tsc --build tsconfig.json && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json",
"build:esm": "tsc --build tsconfig.esm.json && echo '{ \"type\": \"module\" }' > lib/esm/package.json",
"build": "npm run build:esm & npm run build:cjs",
"lint": "eslint --ext .ts,.js --no-error-on-unmatched-pattern .",
"lint-fix": "eslint --fix --ext .ts,.js --no-error-on-unmatched-pattern .",
"lint": "biome lint .",
"lint:fix": "biome check --write .",
"prepack": "node ../../.github/scripts/release_patch_package_json.js ."
},
"homepage": "https://github.com/aws-powertools/powertools-lambda-typescript",
Expand Down Expand Up @@ -51,25 +51,17 @@
},
"typesVersions": {
"*": {
"envelopes": [
"lib/cjs/envelopes.d.ts",
"lib/esm/envelopes.d.ts"
],
"envelopes": ["lib/cjs/envelopes.d.ts", "lib/esm/envelopes.d.ts"],
"functions": [
"lib/cjs/PowertoolsFunctions.d.ts",
"lib/esm/PowertoolsFunctions.d.ts"
],
"types": [
"lib/cjs/types.d.ts",
"lib/esm/types.d.ts"
]
"types": ["lib/cjs/types.d.ts", "lib/esm/types.d.ts"]
}
},
"types": "./lib/cjs/index.d.ts",
"main": "./lib/cjs/index.js",
"files": [
"lib"
],
"files": ["lib"],
"dependencies": {
"@aws-lambda-powertools/commons": "^2.5.0"
},
Expand Down
61 changes: 29 additions & 32 deletions packages/jmespath/src/Functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,8 @@ class Functions {
): number {
if (isRecord(arg)) {
return Object.keys(arg).length;
} else {
return arg.length;
}
return arg.length;
}

/**
Expand Down Expand Up @@ -194,12 +193,12 @@ class Functions {
if (arg.length === 0) {
return null;
// The signature decorator already enforces that all elements are of the same type
} else if (isNumber(arg[0])) {
}
if (isNumber(arg[0])) {
return Math.max(...(arg as number[]));
} else {
// local compare function to handle string comparison
return arg.reduce((a, b) => (a > b ? a : b));
}
// local compare function to handle string comparison
return arg.reduce((a, b) => (a > b ? a : b));
}

/**
Expand Down Expand Up @@ -236,14 +235,13 @@ class Functions {

if (max.visited === current.visited) {
return max;
} else {
// We can safely cast visited to number | string here because we've already
// checked the type at runtime above and we know that it's either a number or a string
return (max.visited as number | string) >
(current.visited as number | string)
? max
: current;
}
// We can safely cast visited to number | string here because we've already
// checked the type at runtime above and we know that it's either a number or a string
return (max.visited as number | string) >
(current.visited as number | string)
? max
: current;
}, visitedArgs[0]);

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

Expand All @@ -276,11 +275,11 @@ class Functions {
if (arg.length === 0) {
return null;
// The signature decorator already enforces that all elements are of the same type
} else if (isNumber(arg[0])) {
}
if (isNumber(arg[0])) {
return Math.min(...arg);
} else {
return arg.reduce((a, b) => (a < b ? a : b));
}
return arg.reduce((a, b) => (a < b ? a : b));
}

/**
Expand Down Expand Up @@ -317,14 +316,13 @@ class Functions {

if (min.visited === current.visited) {
return min;
} else {
// We can safely cast visited to number | string here because we've already
// checked the type at runtime above and we know that it's either a number or a string
return (min.visited as string | number) <
(current.visited as string | number)
? min
: current;
}
// We can safely cast visited to number | string here because we've already
// checked the type at runtime above and we know that it's either a number or a string
return (min.visited as string | number) <
(current.visited as string | number)
? min
: current;
}, visitedArgs[0]);

return min.arg;
Expand Down Expand Up @@ -415,13 +413,12 @@ class Functions {
.sort((a, b) => {
if (a.visited === b.visited) {
return a.index - b.index; // Make the sort stable
} else {
// We can safely cast visited to number | string here because we've already
// checked the type at runtime above and we know that it's either a number or a string
return (a.visited as string | number) > (b.visited as string | number)
? 1
: -1;
}
// We can safely cast visited to number | string here because we've already
// checked the type at runtime above and we know that it's either a number or a string
return (a.visited as string | number) > (b.visited as string | number)
? 1
: -1;
})
.map(({ value }) => value); // Extract the original values
}
Expand Down Expand Up @@ -484,13 +481,13 @@ class Functions {
public funcToNumber(arg: JSONValue): number | null {
if (typeof arg === 'number') {
return arg;
} else if (typeof arg === 'string') {
}
if (typeof arg === 'string') {
const num = Number(arg);

return Number.isNaN(num) ? null : num;
} else {
return null;
}
return null;
}

/**
Expand Down
26 changes: 11 additions & 15 deletions packages/jmespath/src/Lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ class Lexer {
while (this.#current !== '' && this.#current !== undefined) {
if (SIMPLE_TOKENS.has(this.#current)) {
yield {
// We know that SIMPLE_TOKENS has this.#current as a key because
// we checked for that above.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
// biome-ignore lint/style/noNonNullAssertion: We know that SIMPLE_TOKENS has this.#current as a key because we checked for that above.
type: SIMPLE_TOKENS.get(this.#current)!,
value: this.#current,
start: this.#position,
Expand All @@ -57,7 +55,7 @@ class Lexer {
const buff = this.#consumeNumber();
yield {
type: 'number',
value: parseInt(buff),
value: Number.parseInt(buff),
start: start,
end: start + buff.length,
};
Expand Down Expand Up @@ -119,9 +117,8 @@ class Lexer {
start: this.#position - 1,
end: this.#position,
};
} else {
throw new LexerError(this.#position - 1, '=');
}
throw new LexerError(this.#position - 1, '=');
}

/**
Expand Down Expand Up @@ -158,14 +155,13 @@ class Lexer {
if (buff.length > 1) {
return {
type: 'number',
value: parseInt(buff),
value: Number.parseInt(buff),
start: start,
end: start + buff.length,
};
} else {
// If the negative sign is not followed by a number, it is an error.
throw new LexerError(start, 'Unknown token after "-"');
}
// If the negative sign is not followed by a number, it is an error.
throw new LexerError(start, 'Unknown token after "-"');
}

/**
Expand Down Expand Up @@ -194,17 +190,17 @@ class Lexer {
#consumeSquareBracket(): Token {
const start = this.#position;
const nextChar = this.#next();
if (nextChar == ']') {
if (nextChar === ']') {
this.#next();

return { type: 'flatten', value: '[]', start: start, end: start + 2 };
} else if (nextChar == '?') {
}
if (nextChar === '?') {
this.#next();

return { type: 'filter', value: '[?', start: start, end: start + 2 };
} else {
return { type: 'lbracket', value: '[', start: start, end: start + 1 };
}
return { type: 'lbracket', value: '[', start: start, end: start + 1 };
}

/**
Expand Down Expand Up @@ -301,7 +297,7 @@ class Lexer {
*/
#consumeQuotedIdentifier(): Token {
const start = this.#position;
const lexeme = '"' + this.#consumeUntil('"') + '"';
const lexeme = `"${this.#consumeUntil('"')}"`;
const tokenLen = this.#position - start;

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/jmespath/src/ParsedResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
UnknownFunctionError,
VariadicArityError,
} from './errors.js';
import type { Node, JMESPathParsingOptions, JSONObject } from './types.js';
import type { JMESPathParsingOptions, JSONObject, Node } from './types.js';

class ParsedResult {
public expression: string;
Expand Down
Loading