Skip to content

Commit

Permalink
missing script
Browse files Browse the repository at this point in the history
  • Loading branch information
lolopinto committed Oct 11, 2023
1 parent 16d99fc commit 7a56b7b
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions ts/src/scripts/fix_action_exports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { getClassInfo } from "../tsc/ast";
import { TransformFile, transform } from "../tsc/transform";
import ts, { SourceFile, Node, isClassDeclaration } from "typescript";

class FixActionExports implements TransformFile {
glob = "src/ent/**/actions/*_action.ts";

prettierGlob = "src/ent/**/actions/*_action.ts";

traverseChild(
sourceFile: SourceFile,
contents: string,
file: string,
node: Node,
) {
if (!isClassDeclaration(node)) {
return { node };
}
// only Action classes
if (!node.name?.text.endsWith("Action")) {
return { node };
}

const modifiers = ts.canHaveModifiers(node)
? ts.getModifiers(node)
: undefined;

const exported = modifiers?.filter(
(mod) => mod.getText(sourceFile) === "export",
);
const defaultExport = modifiers?.filter(
(mod) => mod.getText(sourceFile) === "default",
);

// for now, we're only supporting changing from default -> non-default
// trivial to support the other way around but don't need it yet
if (!exported?.length || !defaultExport?.length) {
return { node };
}

let classInfo = getClassInfo(contents, sourceFile, node, {
hasExport: true,
hasDefault: false,
});

if (!classInfo) {
// somehow don't have classInfo, bye
return { node };
}

let klassContents = "";
for (const mm of node.members) {
klassContents += mm.getFullText(sourceFile);
}

return {
traversed: true,
rawString: classInfo.wrapClassContents(klassContents),
};
}
}

// ts-node-script --swc --project ./tsconfig.json -r tsconfig-paths/register ../../ts/src/scripts/fix_action_exports.ts
function main() {
const f = new FixActionExports();
transform(f);
}

main();

0 comments on commit 7a56b7b

Please sign in to comment.