The story of automatic language conversion of TypeScript / JavaScript / Python

This article is from GW Advent Calendar "Calendar to Challenge Something New" / 67) 5/6 article.


When it comes to formulating APIs and creating SDKs, the question of which language to support comes up. I want to minimize the effort to create the SDK, so if possible, I would like to create it in one language and automatically convert it to another language.

Can be converted automatically! !! Although it doesn't go as far as that, I tried to put together a library that seems to be quite regrettable.

JavaScript -> TypeScript

There is a story that 60% of Javascript people have also used TypeScript, but the old library is still JavaScript, isn't it? If you can't migrate because of the migration cost, it may be convenient if you can use automatic conversion.

For the time being, this library was convenient for converting from JavaScript to TypeScript.

https://github.com/gregjacobs/js-to-ts-converter

I'm disassembling the JavaScript AST and rebuilding TypeScript. It's the opposite of TypeScript compilation. It is written that the type is guessed from the surroundings, but it is almost useless and all are any. But it seems to move for the time being! It will convert up to that point. (* It doesn't work. There were some errors)

Once you get used to AST, it's not that difficult, so it's easy to do something like Partially customize according to your project.

JavaScript <-> Python

JavaScript and TypeScript are the same language, right? You may hear that, but JavaScript and Python are completely different languages.

Jiphy looked good for this

https://github.com/timothycrosley/jiphy

I can't read the contents, but I think this is also probably building and converting AST. It is also a high point that there is a list of syntax that can be converted.

スクリーンショット 2020-05-06 20.00.44.png

Any program at the level listed in this list will work fine, but the sad thing is that it doesn't support class.

Jiphy does not implement stdlib components, classes, etc. It's SYNTAX ONLY.

Since the class does not support it, you will need to use it in a really small program or fix the class later.

TypeScript -> Java, Python, C#

It is amazing that it supports various languages jsii https://github.com/aws/jsii

Until now, the conversion was just the conversion of the program contents, but this one has a slightly different mechanism. TypeScript (or node.js) always runs in the background, and its interface is converted to Java, Python, C #.

I thought I was calling a Python function, but it was passed through to the node.js function running behind the scenes! It's like that.

Since there is only one language running in the background, the bug encounter rate seems to be low.

It's a fairly epoch-making mechanism, and I'm sorry to say that It also has the disadvantages of consuming a lot of memory and limiting the types that can be used for interface arguments (because it is dynamically translated into languages).

Also, the biggest problem is that if you don't create a project exclusively for this, it will be difficult to deal with it later. You have to create a program according to the language specifications set by jsii. For example

--It is necessary to write a program that takes on all the restrictions of each language. (Even if you want only python, you are subject to java restrictions and c # restrictions) --The naming convention is fixed. Example: getXXX () is prohibited. Switch is a reserved word in C #, so use is prohibited. --Callbacks and TypeScript-specific writing are prohibited. --default export, ʻexport =` is prohibited (probably because it will not be named)

etc. But it's a pretty good library if it works from the beginning.

Summary

I tried 3 language conversion libraries. All are regrettable! I've come to the place, so I'm hoping that it will be complete in a year or two.

Recommended Posts

The story of automatic language conversion of TypeScript / JavaScript / Python
The story of Python and the story of NaN
The story of making Python an exe
The story of manipulating python global variables
The story of blackjack A processing (python)
The story of low learning costs for Python
Image processing? The story of starting Python for
The story of reading HSPICE data in Python
the zen of Python
The story of sys.path.append ()
The story of FileNotFound in Python open () mode ='w'
The story of building Zabbix 4.4
Towards the retirement of Python2
[Apache] The story of prefork
Automatic update of Python module
About the ease of Python
The Power of Pandas: Python
The story of implementing the popular Facebook Messenger Bot with python
The story of how the Python bottle worked on Sakura Internet
The story of introducing jedi (python auto-completion package) to emacs
[python] plot the values ​​before and after the conversion of yeojohnson conversion
The story of rubyist struggling with python :: Dict data with pycall
[Python] Tensorflow 2.0 did not support Python 3.8, so the story of downgrading Python
The story that the version of python 3.7.7 was not adapted to Heroku
First Python 3 ~ The beginning of repetition ~
March 14th is Pi Day. The story of calculating pi with python
The story of participating in AtCoder
A story that struggled to handle the Python package of PocketSphinx
Existence from the viewpoint of Python
[Python] LINE notification of the latest information using Twitter automatic search
pyenv-change the python version of virtualenv
The story of making a standard driver for db with python.
The story of running python and displaying the results without closing vim
Change the Python version of Homebrew
The story of the "hole" in the file
[Python] Understanding the potential_field_planning of Python Robotics
10 functions of "language with battery" python
Review of the basics of Python (FizzBuzz)
The story of remounting the application server
A story about a person who uses Python addicted to the judgment of an empty JavaScript dictionary
About the basics list of Python basics
Story of power approximation by Python
The story of writing a program
The story of making a module that skips mail with python
Learn the basics of Python ① Beginners
The story of making a university 100 yen breakfast LINE bot with Python
Conversion of string <-> date (date, datetime) in Python
Automatic collection of stock prices using python
Change the length of Python csv strings
The story of trying to reconnect the client
Check the behavior of destructor in Python
The story of an error in PyOCR
[Python3] Understand the basics of Beautiful Soup
Closure 4 language comparison (Python, JavaScript, Java, C ++)
(Java, JavaScript, Python) Comparison of string processing
Pass the path of the imported python module
The story of verifying the open data of COVID-19
The story of adding MeCab to ubuntu 16.04
Comparison of Japanese conversion module in Python3
Learning notes from the beginning of Python 1
Check the existence of the file with python