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 5, 2024
1 parent 2d4b11a commit 00c0763
Show file tree
Hide file tree
Showing 7 changed files with 360 additions and 33 deletions.
24 changes: 24 additions & 0 deletions Example/Input/Expected.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// @ts-nocheck

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

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",
),
);
11 changes: 11 additions & 0 deletions Example/Output/Expected.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @ts-nocheck
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
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(path.dirname(path.dirname(__dirname)), ".build", "node", `v${/^target="(.*)"$/m.exec(fs.readFileSync(path.join(path.dirname(path.dirname(__dirname)), "remote", ".npmrc"), "utf8"))![1]}`, `${process.platform}-${process.arch}`, process.platform
=== "win32" ? "node.exe" : "node"));
21 changes: 19 additions & 2 deletions Example/Output/Predefined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,29 @@
*--------------------------------------------------------------------------------------------*/
import * as fs from "fs";
import * as path from "path";

const root = path.dirname(path.dirname(__dirname));
const npmrcPath = path.join(root, "remote", ".npmrc");
const npmrc = fs.readFileSync(npmrcPath, "utf8");
const version = /^target="(.*)"$/m.exec(npmrc)![1];
const platform = process.platform;
const arch = process.arch;
const node = platform === "win32" ? "node.exe" : "node";
const nodePath = path.join(root, ".build", "node", `v${version}`, `${platform}-${arch}`, node);
console.log(path.join(root, ".build", "node", `v${version}`, `${platform}-${arch}`, node));
const nodePath = path.join(
root,
".build",
"node",
`v${version}`,
`${platform}-${arch}`,
node,
);
console.log(
path.join(
path.dirname(path.dirname(__dirname)),
".build",
"node",
`v${/^target="(.*)"$/m.exec(fs.readFileSync(path.join(path.dirname(path.dirname(__dirname)), "remote", ".npmrc"), "utf8"))![1]}`,
`${process.platform}-${process.arch}`,
process.platform === "win32" ? "node.exe" : "node",
),
);
85 changes: 55 additions & 30 deletions Source/Function/Output/Transformer/Visit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,22 @@ class DeclarationTracker {
}>;

isInlined: boolean;

declarationNode: VariableStatement;
}
>();

trackVariable(name: string, initializer: Expression): void {
trackVariable(
name: string,
initializer: Expression,
declarationNode: VariableStatement,
): void {
if (!this.variableMap.has(name)) {
this.variableMap.set(name, {
initializer,
uses: new Set(),
isInlined: false,
declarationNode,
});
}
}
Expand Down Expand Up @@ -68,6 +75,9 @@ class DeclarationTracker {
);
});

console.log(entry.initializer.getText());
console.log(referenceUses.length);

// Only inline if:
// 1. Has initializer
// 2. Not reassigned
Expand All @@ -91,9 +101,24 @@ class DeclarationTracker {
}
}

shouldRemoveDeclaration(statement: VariableStatement): boolean {
// Check if all variables in this statement have been inlined
return statement.declarationList.declarations.every((decl) => {
if (!ts.isIdentifier(decl.name)) return false;

return this.variableMap.get(decl.name.text)?.isInlined === true;
});
}

clear(): void {
this.variableMap.clear();
}

hasInlinedVariables(): boolean {
return Array.from(this.variableMap.values()).some(
(entry) => entry.isInlined,
);
}
}

class Transformer {
Expand Down Expand Up @@ -140,39 +165,22 @@ class Transformer {
}

private visitVariableStatement(node: VariableStatement): Statement {
const declarations = node.declarationList.declarations;

// Track all variable declarations and their initializers
declarations.forEach((decl) => {
// First track all declarations in this statement
node.declarationList.declarations.forEach((decl) => {
if (ts.isIdentifier(decl.name) && decl.initializer) {
const name = decl.name.text;

this.tracker.trackVariable(name, decl.initializer);
this.tracker.trackVariable(
decl.name.text,
decl.initializer,
node,
);
}
});

// Filter out declarations that have been inlined
const remainingDeclarations = declarations.filter((decl) => {
if (!ts.isIdentifier(decl.name)) return true;

return !this.tracker.shouldInline(decl.name.text);
});

if (remainingDeclarations.length === 0) {
// If all declarations in this statement have been inlined, remove it
if (this.tracker.shouldRemoveDeclaration(node)) {
return ts.factory.createEmptyStatement();
}

if (remainingDeclarations.length !== declarations.length) {
return ts.factory.updateVariableStatement(
node,
node.modifiers,
ts.factory.createVariableDeclarationList(
remainingDeclarations,
node.declarationList.flags,
),
);
}

return node;
}

Expand All @@ -199,7 +207,16 @@ class Transformer {
);
}

transform(sourceFile: Node): Node {
transform(sourceFile: Node, passCount: number = 0): Node {
const MAX_PASSES = 10;

if (passCount >= MAX_PASSES) {
return sourceFile;
}

// Clear the tracker for this pass
this.tracker.clear();

// First pass: collect all declarations and uses
ts.visitNode(sourceFile, (node) => {
if (ts.isVariableStatement(node)) {
Expand All @@ -208,6 +225,7 @@ class Transformer {
this.tracker.trackVariable(
decl.name.text,
decl.initializer,
node,
);
}
});
Expand All @@ -223,8 +241,15 @@ class Transformer {
return node;
});

// Second pass: perform the actual transformation
return ts.visitNode(sourceFile, (node) => this.visitNode(node));
// Second pass: perform the transformation
const result = ts.visitNode(sourceFile, (node) => this.visitNode(node));

// If we made any changes in this pass, do another pass
if (this.tracker.hasInlinedVariables() && result !== sourceFile) {
return this.transform(result, passCount + 1);
}

return result;
}
}

Expand Down
Loading

0 comments on commit 00c0763

Please sign in to comment.