Live data from Hacker News

ASK HN: How to engineer a JavaScript to Python migration?

news.ycombinator.com

1–10 of 42 posts

ASK HN: How to engineer a JavaScript to Python migration?

#1
Context: I was tasked with migrating a legacy workflow system (Broadcom CA Workflow Automation) to Airflow.

There are some jobs that contain rather simple JavaScript snippets, and I was trying to design a first prototype that simply takes the JS parts and runs them in a transpiler.

In this respect, I found a couple of packages that could be leveraged: - js2py: https://github.com/PiotrDabkowski/Js2Py - mini-racer: https://github.com/bpcreech/PyMiniRacer Yet, both seem to be abandoned packages that might not be suitable for usage in production.

Therefore, I was thinking about parsing and translating Javascript's abstract syntax trees to Python. Whereas a colleague suggested I bring up an LLM pipeline.

How much of an overkill that might be? Has anyone else ever dealt with a JavaScript-to-Python migration and could share heads-ups on strategies or pitfalls to avoid?

Re: ASK HN: How to engineer a JavaScript to Python migration?

#2
Hi, is this a monolith or a series of scripts? if the latter, isolate each script enough that it could be ran with python and then do it for all the scripts. if it is a monolith, consider splitting it and working on the pieces one by one.

an old package on prod is not an issue if it is isolated and you have a plan for change, so if you have to transpile, do it and then work from the bottom.

Re: ASK HN: How to engineer a JavaScript to Python migration?

#3
post #2

Hi, is this a monolith or a series of scripts? if the latter, isolate each script enough that it could be ran with python and then do it for all the scripts. if it is a monolith, consider splitting it and working on the pieces one by one. an old package on prod is not an issue if it is isolated and you have a plan for change, so if you have to transpile, do it and then work from the bottom.

It's a bunch of scripts, a few lines long. But again, I haven't seen the totality of them and there might be edge cases with longer scripts.

Thanks for your hint!

Re: ASK HN: How to engineer a JavaScript to Python migration?

#5
post #3
post #2

Hi, is this a monolith or a series of scripts? if the latter, isolate each script enough that it could be ran with python and then do it for all the scripts. if it is a monolith, consider splitting it and working on the pieces one by one. an old package on prod is not an issue if it is isolated and you have a plan for change, so if you have to transpile, do it and then work from the bottom.

It's a bunch of scripts, a few lines long. But again, I haven't seen the totality of them and there might be edge cases with longer scripts. Thanks for your hint!

if we are talking small scripts, just rewrite in python and make sure that they are called with the right interpreter. use ai if you are not acquainted with the language. ask your managers if they might want to use another language which could be compiled and save on compute time. though not popular, nim is python like, and should do the job admirably.

Re: ASK HN: How to engineer a JavaScript to Python migration?

#6

> How much of an overkill that might be? It sounds like a complete waste of time. If you are talking about small code snippets then simply write new original Python to replace them.

Yep, I thought about that... Still, there's a few hundreds of workflows to migrate, so I was looking for a systematic approach

Re: ASK HN: How to engineer a JavaScript to Python migration?

#7
Working on something similar but in reverse (Python to JavaScript). A few tips:

- If the scripts are primarily by the same author, it's likely they copy-pasted a lot of their functions throughout their scripts. Do something like a "Find All" in the repository holding the scripts for the function name. Regex will help a lot in this, and help you see if a function ever has variations on number of arguments, slight name changes or misspells, etc.

- Refine an AI prompt as you convert scripts over and over.

- AI (ChatGPT, Copilot, etc.) is going to be the best automation you can get for this, because often times conversions don't match up one-to-one in a language. Especially if your scripts use npm, you might not find one-to-one matches in PyPI. AI refactors will also force you to examine the conversions.

- Understand what tech debt could be introduced in a one-to-one conversion. There may be some language workarounds that make a lot more sense to introduce over exact conversions of workaround functions for the language. I've had to work with several legacy Python scripts that called functions that can be better written in your target language. For example, one of our functions was conditionally_get_keys(level1, level2, level3) to kind of recursively get a nested value. This didn't need to be a function when I rewrote it in JS, rather I just wrote the variable as something like `city = user.location?.city`. No need to one-to-one convert a function that can be better written with a JS language feature. You'll probably encounter this when you can more succinctly write list manipulations with slicing (e.g. `steppedList = fooList[::2]` instead of converting a function necessary in JS to do the same thing).

Sorry if you have a time-crunch with converting things, but I would recommend a more hands-on conversion strategy, leveraging AI to do the basic conversion and then manually testing, debugging the solutions.

Re: ASK HN: How to engineer a JavaScript to Python migration?

#8
I did a reasonably big rewrite from JavaScript (Nashorn, long story) to Kotlin/JVM recently (with 60x speedup and elimination of huge variance in runtime).

Keys to success in a larger scale translation:

- don't redesign anything, do a port (see also, Typescript compiler to Go port)

- leverage LLMs interactively: per chunk (e.g. function), copy the old code into a comment in the new code, then use LLM completion to quickly fill out the translation

- get something basic up and running ASAP that you can test, ideally data-driven (inputs, expectations) tuples, that you can write scaffolds for execution of the old and the new code

- for every method / control flow ported, add tests that target the newly added code, validating it does the same as the old code

Some of this may be less applicable to scripts or harder to apply to imperative code, for which you might want to spend time converting side-effecting actions into data that can be asserted on (e.g. instead of performing commands, emit a list of commands); do this refactoring on the old code before porting.

Don't get tempted into doing refactorings as you go. When you notice an opportunity to refactor, create a bug for it. What you don't want to do is build up a list of transformations that increases the more code you port, and makes finishing everything harder and harder.

Re: ASK HN: How to engineer a JavaScript to Python migration?

#10
I once needed to convert 2000 line excel formula to PHP but PHP linters sucks and are generally not helpful so I converted it to JS first and then I just added $ signs in front of variable names, few minor tweaks and it worked. It was easier than to go directly to PHP.
Post reply on HN