Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions packages/esbuild-plugins/src/parsers/remove-unsafe-references.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import ts from "typescript";

export function removeUnsafeReferences(sourceText: string): string {
const sourceFile = ts.createSourceFile(
"file.ts",
sourceText,
ts.ScriptTarget.ES2015,
true,
);
const printer = ts.createPrinter();

let infraImports: Set<string> = new Set();

function transformer(context: ts.TransformationContext) {
return (node: ts.Node, isTopLevel: boolean = true): ts.Node => {
if (
ts.isImportDeclaration(node) &&
ts.isStringLiteral(node.moduleSpecifier)
) {
const path = node.moduleSpecifier.text;

// todo: enable relative paths by resolving path to validate location
if (path.startsWith("infra/")) {
node.importClause?.namedBindings?.forEachChild((namedBinding) => {
if (ts.isImportSpecifier(namedBinding)) {
infraImports.add(namedBinding.name.text);
}
});
return node;
} else {
return ts.factory.createNotEmittedStatement(node);
}
}

if (
ts.isStatement(node) &&
containsUnsafeReferences(node, infraImports)
) {
return ts.factory.createNotEmittedStatement(node);
}

return ts.visitEachChild(
node,
(child) => transformer(context)(child),
context,
);
};
}

const transformedSourceFile = ts.transform(sourceFile, [transformer])
.transformed[0];

return printer.printFile(transformedSourceFile as ts.SourceFile);
}

function containsUnsafeReferences(
node: ts.Node,
safeReferences: Set<string>,
): boolean {
let hasUnsafeReference = false;

function visit(n: ts.Node, parent: ts.Node | undefined = undefined): void {
if (ts.isIdentifier(n)) {
if (isReference(n, parent) && !safeReferences.has(n.text)) {
hasUnsafeReference = true;
}
} else {
n.forEachChild((child) => visit(child, n));
}
}

visit(node);

return hasUnsafeReference;
}

function isReference(
node: ts.Identifier,
parent: ts.Node | undefined,
): boolean {
if (!parent) {
return false;
}

if (ts.isTypeReferenceNode(parent)) {
return false;
}

if (ts.isTypeParameterDeclaration(parent) && parent.name === node) {
return false;
}

if (
(ts.isInterfaceDeclaration(parent) || ts.isTypeAliasDeclaration(parent)) &&
parent.name === node
) {
return false;
}

if (ts.isPropertyAssignment(parent) && parent.name === node) {
return false;
}

if (ts.isPropertyAccessExpression(parent) && parent.name === node) {
return false;
}

if (ts.isMethodDeclaration(parent) && parent.name === node) {
return false;
}

if (ts.isParameter(parent) && parent.name === node) {
return false;
}

if (ts.isVariableDeclaration(parent) && parent.name === node) {
return false;
}

if (ts.isFunctionDeclaration(parent) && parent.name === node) {
return false;
}

return true;
}
10 changes: 6 additions & 4 deletions packages/esbuild-plugins/src/plugins/function-infra-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from "node:path";
import { Plugin } from "esbuild";
import { parseFnModule } from "src/parsers/parse-fn-module";
import { removeUnsafeReferences } from "src/parsers/remove-unsafe-references";
import { GetFile, fsGetFile, withFileCheck } from "src/utils/get-file";
import { filePaths } from "@notation/core";

Expand All @@ -17,18 +18,19 @@ export function functionInfraPlugin(opts: PluginOpts = {}): Plugin {
const fileContent = await getFile(args.path);
const fileName = path.relative(process.cwd(), args.path);
const outFileName = filePaths.dist.runtime.index(fileName);
const { config, configRaw, exports } = parseFnModule(fileContent);
const safeFnModule = removeUnsafeReferences(fileContent);
const { config, exports } = parseFnModule(fileContent);

const reservedNames = ["preload", "config"];
const [platform, service] = (config.service as string).split("/");

let infraCode = `import { ${service} } from "@notation/${platform}/${service}";`;
infraCode = infraCode.concat(`\nconst config = ${configRaw};`);
let infraCode = `import { ${service} } from "@notation/${platform}/${service}";\n`;
infraCode = infraCode.concat(`\n${safeFnModule}\n`);

for (const handlerName of exports) {
if (reservedNames.includes(handlerName)) continue;
infraCode = infraCode.concat(
`\nexport const ${handlerName} = ${service}({ fileName: "${outFileName}", handler: "${handlerName}", ...config });`,
`export const ${handlerName} = ${service}({ fileName: "${outFileName}", handler: "${handlerName}", ...config });\n`,
);
}

Expand Down