Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaRHristov committed Nov 2, 2024
1 parent c850639 commit 511644d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 82 deletions.
77 changes: 6 additions & 71 deletions Example/Output/Dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1351,82 +1351,17 @@ export function finalHandler<T extends Event>(fn: (event: T) => unknown): (event
}
export function domContentLoaded(targetWindow: Window): Promise<void> {
return new Promise<void>((resolve) => {
if (targetWindow.document.readyState
=== "complete" ||
const readyState = targetWindow.document.readyState;
if (readyState === "complete" ||
(targetWindow.document && targetWindow.document.body !== null)) {
resolve(undefined);
}
else {
targetWindow.window.addEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", () => {
targetWindow.window.removeEventListener("DOMContentLoaded", listener, false);
resolve();
}, false);
resolve();
}, false);
resolve();
}, false);
resolve();
}, false);
resolve();
}, false);
resolve();
}, false);
resolve();
}, false);
resolve();
}, false);
resolve();
}, false);
resolve();
}, false);
resolve();
}, false);
resolve();
}, false);
resolve();
}, false);
resolve();
}, false);
resolve();
}, false);
resolve();
}, false);
resolve();
}, false);
resolve();
}, false);
resolve();
}, false);
resolve();
}, false);
resolve();
}, false);
resolve();
}, false);
const listener = () => {
targetWindow.window.removeEventListener("DOMContentLoaded", listener, false);
resolve();
}, false);
};
targetWindow.window.addEventListener("DOMContentLoaded", listener, false);
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion Example/Output/Predefined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ import * as fs from "fs";
import * as path from "path";
const root = path.dirname(path.dirname(__dirname));
const platform = process.platform;
console.log(path.join(root, ".build", "node", `v${/^target="(.*)"$/m.exec(fs.readFileSync(path.join(root, "remote", ".npmrc"), "utf8"))![1]}`, `${platform}-${process.arch}`, platform === "win32" ? "node.exe" : "node"));
const arch = process.arch;
console.log(path.join(root, ".build", "node", `v${/^target="(.*)"$/m.exec(fs.readFileSync(path.join(root, "remote", ".npmrc"), "utf8"))![1]}`, `${platform}-${arch}`, platform === "win32" ? "node.exe" : "node"));
66 changes: 57 additions & 9 deletions Source/Function/Output/Visit.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
import type Interface from "@Interface/Output/Visit.js";
import type { Node } from "typescript";

/**
* @module Output
*
*/
export const Fn = ((...[Usage, Initializer]) =>
(...[Node]) => {
const MAX_USAGE_COUNT = 1000; // Prevent runaway usage counts
const MAX_INITIALIZER_SIZE = 1000; // Prevent too many initializers
const MAX_USAGE_COUNT = 1000;

const MAX_INITIALIZER_SIZE = 1000;

const visitedNodes = new Set<string>();

// Add node path tracking to prevent infinite recursion
const nodeId = `${ts.SyntaxKind[Node.kind]}-${Node.pos}-${Node.end}`;

if (visitedNodes.has(nodeId)) {
console.warn("Warning: Circular reference detected", {
nodeType: ts.SyntaxKind[Node.kind],
position: Node.pos,
});
return;
}

visitedNodes.add(nodeId);

ts.forEachChild(Node, Fn(Usage, Initializer));

// Check Maps size before processing
if (
Usage.size >= MAX_USAGE_COUNT ||
Initializer.size >= MAX_INITIALIZER_SIZE
Expand All @@ -18,29 +36,59 @@ export const Fn = ((...[Usage, Initializer]) =>
usageSize: Usage.size,
initializerSize: Initializer.size,
});
return; // Early exit if maps are too large
}

ts.forEachChild(Node, Fn(Usage, Initializer));
return;
}

if (ts.isVariableDeclaration(Node) && Node.initializer) {
const NameNode = Node.name.getText();

// Reset the usage, but first check if the name exists
// Improved self-reference check
const containsSelfReference = (() => {
let hasSelfRef = false;

const checkNode = (node: Node) => {
if (hasSelfRef) return;

if (ts.isIdentifier(node) && node.getText() === NameNode) {
// Skip if this identifier is part of a property access
const parent = node.parent;
if (
!ts.isPropertyAccessExpression(parent) ||
parent.name === node
) {
hasSelfRef = true;
}
}

ts.forEachChild(node, checkNode);
};

checkNode(Node.initializer);
return hasSelfRef;
})();

// Skip self-referential initializers
if (containsSelfReference) {
console.debug(
`Skipping self-referential initializer for: ${NameNode}`,
);

return;
}

if (Usage.has(NameNode)) {
Usage.set(NameNode, 0);
} else if (Usage.size < MAX_USAGE_COUNT) {
Usage.set(NameNode, 0);
}

// Set the initializer with size check
if (Initializer.size < MAX_INITIALIZER_SIZE) {
Initializer.set(Node.initializer, NameNode);
}
} else if (ts.isIdentifier(Node)) {
const NameNode = Node.getText();

// Increment if usage is found, with bounds checking
if (!ts.isVariableDeclaration(Node.parent)) {
const currentCount = Usage.get(NameNode) ?? 0;

Expand Down
2 changes: 1 addition & 1 deletion Target/Function/Output/Visit.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 511644d

Please sign in to comment.