actually prettier

This commit is contained in:
Toshit Chawda 2024-07-14 16:22:34 -07:00
parent fb53b44869
commit 56767f5b31
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
41 changed files with 1914 additions and 1919 deletions

View file

@ -1,8 +1,8 @@
import { parseModule } from "meriyah";
import { generate } from "astring";
import { makeTraveler } from "astravel";
import { encodeUrl } from "./url";
import * as ESTree from "estree";
import { parseModule } from "meriyah"
import { generate } from "astring"
import { makeTraveler } from "astravel"
import { encodeUrl } from "./url"
import * as ESTree from "estree"
// i am a cat. i like to be petted. i like to be fed. i like to be
@ -17,93 +17,93 @@ import * as ESTree from "estree";
// parent
export function rewriteJs(js: string, origin?: URL) {
const htmlcomment = /<!--[\s\S]*?-->/g;
js = js.replace(htmlcomment, "");
try {
const ast = parseModule(js, {
module: true,
webcompat: true,
});
const htmlcomment = /<!--[\s\S]*?-->/g
js = js.replace(htmlcomment, "")
try {
const ast = parseModule(js, {
module: true,
webcompat: true,
})
const identifierList = [
"window",
"self",
"globalThis",
"this",
"parent",
"top",
"location",
];
const identifierList = [
"window",
"self",
"globalThis",
"this",
"parent",
"top",
"location",
]
const customTraveler = makeTraveler({
ImportDeclaration: (node: ESTree.ImportDeclaration) => {
node.source.value = encodeUrl(node.source.value as string, origin);
},
const customTraveler = makeTraveler({
ImportDeclaration: (node: ESTree.ImportDeclaration) => {
node.source.value = encodeUrl(node.source.value as string, origin)
},
ImportExpression: (node: ESTree.ImportExpression) => {
if (node.source.type === "Literal") {
node.source.value = encodeUrl(node.source.value as string, origin);
} else if (node.source.type === "Identifier") {
// this is for things that import something like
// const moduleName = "name";
// await import(moduleName);
node.source.name = `__wrapImport(${node.source.name})`;
}
},
ImportExpression: (node: ESTree.ImportExpression) => {
if (node.source.type === "Literal") {
node.source.value = encodeUrl(node.source.value as string, origin)
} else if (node.source.type === "Identifier") {
// this is for things that import something like
// const moduleName = "name";
// await import(moduleName);
node.source.name = `__wrapImport(${node.source.name})`
}
},
ExportAllDeclaration: (node: ESTree.ExportAllDeclaration) => {
node.source.value = encodeUrl(node.source.value as string, origin);
},
ExportAllDeclaration: (node: ESTree.ExportAllDeclaration) => {
node.source.value = encodeUrl(node.source.value as string, origin)
},
ExportNamedDeclaration: (node: ESTree.ExportNamedDeclaration) => {
// strings are Literals in ESTree syntax but these will always be strings
if (node.source)
node.source.value = encodeUrl(node.source.value as string, origin);
},
ExportNamedDeclaration: (node: ESTree.ExportNamedDeclaration) => {
// strings are Literals in ESTree syntax but these will always be strings
if (node.source)
node.source.value = encodeUrl(node.source.value as string, origin)
},
MemberExpression: (node: ESTree.MemberExpression) => {
if (
node.object.type === "Identifier" &&
identifierList.includes(node.object.name)
) {
node.object.name = `globalThis.$s(${node.object.name})`;
}
},
MemberExpression: (node: ESTree.MemberExpression) => {
if (
node.object.type === "Identifier" &&
identifierList.includes(node.object.name)
) {
node.object.name = `globalThis.$s(${node.object.name})`
}
},
AssignmentExpression: (node: ESTree.AssignmentExpression) => {
if (
node.left.type === "Identifier" &&
identifierList.includes(node.left.name)
) {
node.left.name = `globalThis.$s(${node.left.name})`;
}
AssignmentExpression: (node: ESTree.AssignmentExpression) => {
if (
node.left.type === "Identifier" &&
identifierList.includes(node.left.name)
) {
node.left.name = `globalThis.$s(${node.left.name})`
}
if (
node.right.type === "Identifier" &&
identifierList.includes(node.right.name)
) {
node.right.name = `globalThis.$s(${node.right.name})`;
}
},
if (
node.right.type === "Identifier" &&
identifierList.includes(node.right.name)
) {
node.right.name = `globalThis.$s(${node.right.name})`
}
},
VariableDeclarator: (node: ESTree.VariableDeclarator) => {
if (
node.init &&
node.init.type === "Identifier" &&
identifierList.includes(node.init.name)
) {
node.init.name = `globalThis.$s(${node.init.name})`;
}
},
});
VariableDeclarator: (node: ESTree.VariableDeclarator) => {
if (
node.init &&
node.init.type === "Identifier" &&
identifierList.includes(node.init.name)
) {
node.init.name = `globalThis.$s(${node.init.name})`
}
},
})
customTraveler.go(ast);
customTraveler.go(ast)
return generate(ast);
} catch (e) {
console.error(e);
console.log(js);
return generate(ast)
} catch (e) {
console.error(e)
console.log(js)
return js;
}
return js
}
}