mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-06-17 20:00:13 +00:00
for automatic review
This commit is contained in:
@@ -45,7 +45,7 @@ const TARGET_GLOBALS = new Set([
|
||||
"navigator",
|
||||
"location",
|
||||
"document",
|
||||
"window"
|
||||
"window",
|
||||
]);
|
||||
|
||||
let modifiedFilesCount = 0;
|
||||
@@ -59,22 +59,13 @@ for (const sourceFile of project.getSourceFiles()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Exclude submodule files under src/lib/
|
||||
if (posixFilePath.startsWith(posixLibSrc)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Exclude independent application modules under src/apps/
|
||||
if (posixFilePath.startsWith(`${posixSrc}/apps/`)) {
|
||||
// Exclude coreEnvFunctions.ts to avoid self-referential definitions
|
||||
if (posixFilePath.endsWith("/coreEnvFunctions.ts") || posixFilePath.endsWith("/coreEnvFunctions")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Exclude unit and integration test files
|
||||
if (
|
||||
posixFilePath.endsWith(".spec.ts") ||
|
||||
posixFilePath.endsWith(".test.ts") ||
|
||||
posixFilePath.includes("/_test/")
|
||||
) {
|
||||
if (posixFilePath.endsWith(".spec.ts") || posixFilePath.endsWith(".test.ts") || posixFilePath.includes("/_test/")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -101,6 +92,14 @@ for (const sourceFile of project.getSourceFiles()) {
|
||||
}
|
||||
}
|
||||
|
||||
// 1.5. Skip if it is the right-hand side of a QualifiedName (e.g. the "requestAnimationFrame" in "typeof compatGlobal.requestAnimationFrame")
|
||||
if (parent.getKind() === SyntaxKind.QualifiedName) {
|
||||
const qualified = parent.asKindOrThrow(SyntaxKind.QualifiedName);
|
||||
if (qualified.getRight() === idNode) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Skip if it is the operand of a typeof expression (e.g. "typeof window")
|
||||
if (parent.getKind() === SyntaxKind.TypeOfExpression) {
|
||||
continue;
|
||||
@@ -153,6 +152,8 @@ for (const sourceFile of project.getSourceFiles()) {
|
||||
let replacement = "";
|
||||
if (name === "window" || name === "globalThis") {
|
||||
replacement = "compatGlobal";
|
||||
} else if (name === "document") {
|
||||
replacement = "_activeDocument";
|
||||
} else {
|
||||
replacement = `compatGlobal.${name}`;
|
||||
}
|
||||
@@ -174,23 +175,33 @@ for (const sourceFile of project.getSourceFiles()) {
|
||||
node.replaceWithText(replacement);
|
||||
}
|
||||
|
||||
// Ensure compatGlobal is imported
|
||||
const hasCompatGlobalImport = sourceFile.getImportDeclarations().some((imp) => {
|
||||
return imp.getNamedImports().some((ni) => ni.getName() === "compatGlobal");
|
||||
});
|
||||
// Determine what needs to be imported based on replacements
|
||||
const needsCompatGlobal = nodesToReplace.some((r) => r.replacement.includes("compatGlobal"));
|
||||
const needsActiveDocument = nodesToReplace.some((r) => r.replacement.includes("_activeDocument"));
|
||||
|
||||
if (!hasCompatGlobalImport) {
|
||||
const requiredImports: string[] = [];
|
||||
if (needsCompatGlobal) requiredImports.push("compatGlobal");
|
||||
if (needsActiveDocument) requiredImports.push("_activeDocument");
|
||||
|
||||
if (requiredImports.length > 0) {
|
||||
const existingImport = sourceFile.getImportDeclarations().find((imp) => {
|
||||
const spec = imp.getModuleSpecifierValue();
|
||||
return spec === "@lib/common/coreEnvFunctions" || spec === "@lib/common/coreEnvFunctions.ts";
|
||||
});
|
||||
|
||||
if (existingImport) {
|
||||
existingImport.addNamedImport("compatGlobal");
|
||||
for (const nameToImport of requiredImports) {
|
||||
const alreadyImported = existingImport
|
||||
.getNamedImports()
|
||||
.some((ni) => ni.getName() === nameToImport);
|
||||
if (!alreadyImported) {
|
||||
existingImport.addNamedImport(nameToImport);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
sourceFile.addImportDeclaration({
|
||||
namedImports: ["compatGlobal"],
|
||||
moduleSpecifier: "@lib/common/coreEnvFunctions.ts"
|
||||
namedImports: requiredImports,
|
||||
moduleSpecifier: "@lib/common/coreEnvFunctions.ts",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ function matchStyleAccess(node: Node): { element: Node; propertyName: string; is
|
||||
return {
|
||||
element: expr.getExpression(),
|
||||
propertyName: node.getName(),
|
||||
isComputed: false
|
||||
isComputed: false,
|
||||
};
|
||||
}
|
||||
} else if (Node.isElementAccessExpression(node)) {
|
||||
@@ -52,7 +52,7 @@ function matchStyleAccess(node: Node): { element: Node; propertyName: string; is
|
||||
return {
|
||||
element: expr.getExpression(),
|
||||
propertyName: arg.getText(),
|
||||
isComputed: true
|
||||
isComputed: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -74,7 +74,7 @@ function getStyleAssignment(statement: Node) {
|
||||
property: styleAccess.propertyName,
|
||||
valueText: expr.getRight().getText(),
|
||||
isComputed: styleAccess.isComputed,
|
||||
statementNode: statement
|
||||
statementNode: statement,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -100,11 +100,7 @@ for (const sourceFile of project.getSourceFiles()) {
|
||||
}
|
||||
|
||||
// Exclude unit and integration test files
|
||||
if (
|
||||
posixFilePath.endsWith(".spec.ts") ||
|
||||
posixFilePath.endsWith(".test.ts") ||
|
||||
posixFilePath.includes("/_test/")
|
||||
) {
|
||||
if (posixFilePath.endsWith(".spec.ts") || posixFilePath.endsWith(".test.ts") || posixFilePath.includes("/_test/")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -126,12 +122,14 @@ for (const sourceFile of project.getSourceFiles()) {
|
||||
if (assignment) {
|
||||
const currentGroup: StyleGroup = {
|
||||
elementText: assignment.elementText,
|
||||
assignments: [{
|
||||
property: assignment.property,
|
||||
valueText: assignment.valueText,
|
||||
isComputed: assignment.isComputed,
|
||||
statementNode: assignment.statementNode
|
||||
}]
|
||||
assignments: [
|
||||
{
|
||||
property: assignment.property,
|
||||
valueText: assignment.valueText,
|
||||
isComputed: assignment.isComputed,
|
||||
statementNode: assignment.statementNode,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
// Look ahead to collect consecutive assignments to the same element
|
||||
@@ -143,7 +141,7 @@ for (const sourceFile of project.getSourceFiles()) {
|
||||
property: nextAssignment.property,
|
||||
valueText: nextAssignment.valueText,
|
||||
isComputed: nextAssignment.isComputed,
|
||||
statementNode: nextAssignment.statementNode
|
||||
statementNode: nextAssignment.statementNode,
|
||||
});
|
||||
j++;
|
||||
} else {
|
||||
@@ -190,7 +188,12 @@ for (const sourceFile of project.getSourceFiles()) {
|
||||
const { line } = sourceFile.getLineAndColumnAtPos(firstNode.getStart());
|
||||
|
||||
console.log(` Line ${line}: Replacing consecutive style assignments on "${group.elementText}" with:`);
|
||||
console.log(newText.split("\n").map((l) => ` ${l}`).join("\n"));
|
||||
console.log(
|
||||
newText
|
||||
.split("\n")
|
||||
.map((l) => ` ${l}`)
|
||||
.join("\n")
|
||||
);
|
||||
|
||||
if (!isDryRun) {
|
||||
firstNode.replaceWithText(newText);
|
||||
|
||||
Reference in New Issue
Block a user