From 2655b1f55f98e5870d4e124704a21f4d793b4e1c Mon Sep 17 00:00:00 2001 From: sepro Date: Tue, 28 Oct 2025 21:54:48 +0100 Subject: [PATCH] Fix `es6` and `tv_es6` n func extraction (#26) --- src/types.ts | 21 +++++--- src/yt/solver/n.ts | 96 ++++++++++++++++++++++++++----------- src/yt/solver/sig.ts | 6 +-- src/yt/solver/test/tests.ts | 20 ++++++-- 4 files changed, 100 insertions(+), 43 deletions(-) diff --git a/src/types.ts b/src/types.ts index cf414d0..96f6a6c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,7 +1,16 @@ -export type DeepPartial = T extends object - ? Or<{ - [P in keyof T]?: DeepPartial; - }> - : Or; +type DP = T extends (infer U)[] + ? DeepPartial[] + : T extends object + ? { [P in keyof T]?: DeepPartial } + : T; -type Or = T | { or: T[] }; +type ValueOf = T extends (infer U)[] + ? U + : T extends object + ? T[keyof T] + : never; + +export type DeepPartial = + | DP + | { or: DP[] } + | { anykey: DP>[] }; diff --git a/src/yt/solver/n.ts b/src/yt/solver/n.ts index 94e6a7e..f4adbce 100644 --- a/src/yt/solver/n.ts +++ b/src/yt/solver/n.ts @@ -2,26 +2,50 @@ import { type ESTree } from "meriyah"; import { matchesStructure } from "../../utils.ts"; import { type DeepPartial } from "../../types.ts"; -const identifier: DeepPartial = { - type: "VariableDeclaration", - kind: "var", - declarations: [ +const identifier: DeepPartial = { + or: [ { - type: "VariableDeclarator", - id: { - type: "Identifier", - }, - init: { - type: "ArrayExpression", - elements: [ + type: "VariableDeclaration", + kind: "var", + declarations: { + anykey: [ { - type: "Identifier", + type: "VariableDeclarator", + id: { + type: "Identifier", + }, + init: { + type: "ArrayExpression", + elements: [ + { + type: "Identifier", + }, + ], + }, }, ], }, }, + { + type: "ExpressionStatement", + expression: { + type: "AssignmentExpression", + left: { + type: "Identifier", + }, + operator: "=", + right: { + type: "ArrayExpression", + elements: [ + { + type: "Identifier", + }, + ], + }, + }, + }, ], -}; +} as const; const catchBlockBody = [ { @@ -92,23 +116,37 @@ export function extract( return null; } - if (node.type !== "VariableDeclaration") { - return null; + if (node.type === "VariableDeclaration") { + for (const declaration of node.declarations) { + if ( + declaration.type !== "VariableDeclarator" || + !declaration.init || + declaration.init.type !== "ArrayExpression" || + declaration.init.elements.length !== 1 + ) { + continue; + } + const [firstElement] = declaration.init.elements; + if (firstElement && firstElement.type === "Identifier") { + return makeSolverFuncFromName(firstElement.name); + } + } + } else if (node.type === "ExpressionStatement") { + const expr = node.expression; + if ( + expr.type === "AssignmentExpression" && + expr.left.type === "Identifier" && + expr.operator === "=" && + expr.right.type === "ArrayExpression" && + expr.right.elements.length === 1 + ) { + const [firstElement] = expr.right.elements; + if (firstElement && firstElement.type === "Identifier") { + return makeSolverFuncFromName(firstElement.name); + } + } } - const declaration = node.declarations[0]; - if ( - declaration.type !== "VariableDeclarator" || - !declaration.init || - declaration.init.type !== "ArrayExpression" || - declaration.init.elements.length !== 1 - ) { - return null; - } - const [firstElement] = declaration.init.elements; - if (!firstElement || firstElement.type !== "Identifier") { - return null; - } - return makeSolverFuncFromName(firstElement.name); + return null; } function makeSolverFuncFromName(name: string): ESTree.ArrowFunctionExpression { diff --git a/src/yt/solver/sig.ts b/src/yt/solver/sig.ts index d3fc837..9ebbf7b 100644 --- a/src/yt/solver/sig.ts +++ b/src/yt/solver/sig.ts @@ -62,7 +62,7 @@ const logicalExpression: DeepPartial = { }, }; -const identifier = { +const identifier: DeepPartial = { or: [ { type: "ExpressionStatement", @@ -102,9 +102,7 @@ const identifier = { export function extract( node: ESTree.Node, ): ESTree.ArrowFunctionExpression | null { - if ( - !matchesStructure(node, identifier as unknown as DeepPartial) - ) { + if (!matchesStructure(node, identifier)) { return null; } let block: ESTree.BlockStatement | undefined | null; diff --git a/src/yt/solver/test/tests.ts b/src/yt/solver/test/tests.ts index 781c4e3..df2f5e1 100644 --- a/src/yt/solver/test/tests.ts +++ b/src/yt/solver/test/tests.ts @@ -236,9 +236,7 @@ export const tests: { ], }, { - // TODO: es6/tv_es6 variants currently fail player: "2b83d2e0", - variants: ["main", "tcc", "tce", "es5", "tv", "phone", "tablet"], n: [ // Synthetic test { input: "0eRGgQWJGfT5rFHFj", expected: "euHbygrCMLksxd" }, @@ -254,9 +252,7 @@ export const tests: { ], }, { - // TODO: es6/tv_es6 variants currently fail player: "638ec5c6", - variants: ["main", "tcc", "tce", "es5", "tv", "phone", "tablet"], n: [ // Synthetic test { input: "ZdZIqFPQK-Ty8wId", expected: "1qov8-KM-yH" }, @@ -271,6 +267,22 @@ export const tests: { }, ], }, + { + player: "87644c66", + n: [ + // Synthetic test + { input: "ZdZIqFPQK-Ty8wId", expected: "iF5NxEm1BYk" }, + ], + sig: [ + // Synthetic test + { + input: + "gN7a-hudCuAuPH6fByOk1_GNXN0yNMHShjZXS2VOgsEItAJz0tipeavEOmNdYN-wUtcEqD3bCXjc0iyKfAyZxCBGgIARwsSdQfJ2CJtt", + expected: + "atJC2JfQdSswRAtgGBCxZyAfKyi0cjXCb3DqEctUw-NYdNmOEvIepit0zJAtIEsgOV2SXZjhSHMNy0NXNG_1kOyBf6HPuAuCduh-a7Ng", + }, + ], + }, ]; export const players = new Map([