🌐
Stack Overflow
stackoverflow.com › questions › 75870756 › how-to-parse-a-typescript-file-using-a-python-script
How to parse a Typescript file using a Python script? - Stack Overflow
import ast import typescript def get_functions(code): js_code = typescript.transpile(code) js_ast = ast.parse(js_code) functions = [] for node in js_ast.body: if isinstance(node, ast.FunctionDef): functions.append((node.lineno, node.name, typescript.transpile(ast.unparse(node)))) return functions · I'm very new with python, so If you can guide me with this it will be very appreciated.
🌐
GitHub
github.com › anntnzrb › tsparxser
GitHub - anntnzrb/tsparxser: TypeScript mini lexer+parser implementation using Python's PLY library.
July 13, 2023 - TypeScript mini lexer+parser implementation using Python's PLY library. - anntnzrb/tsparxser
Forked by 2 users
Languages   Python 96.2% | TypeScript 3.5% | Nix 0.3% | Python 96.2% | TypeScript 3.5% | Nix 0.3%
Discussions

JavaScript parser in Python - Stack Overflow
There is a JavaScript parser at least in C and Java (Mozilla), in JavaScript (Mozilla again) and Ruby. Is there any currently out there for Python? I don't need a JavaScript interpreter, per se, j... More on stackoverflow.com
🌐 stackoverflow.com
A good javascript/typescript parser in Python

Are you seeking artists or developers to help you with your game? We run a monthly game jam in this Discord where we actively pair people with other creators.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

More on reddit.com
🌐 r/developer
1
2
April 24, 2024
library - TypeScript compiler for Python - Software Recommendations Stack Exchange
Platform: Python 2.7.x. If the library depends on an external executable, the SO is Ubuntu 14.04 or upper. Otherwise, SO doesn't matter. ... Given a TypeScript filename, generate compiled JS. Given a TypeScript chunk (string), generate compiled JS chunk (string). Package/repository actively maintained. ... Your question and title do not match: are you looking for a compiler or a parser... More on softwarerecs.stackexchange.com
🌐 softwarerecs.stackexchange.com
Typing for JSON Payloads
Well, you can just have type MyStuff = { propA: type1 } etc with your json props and types. Then, const data = JSON.parse(myData) as MyStuff or const data: MyStuff = JSON.parse(myData). Both works and the latter is typesafer, as it will avoid assigning wrong values types to data, if you do any related mistake. No need to have a .d.ts if you are using .ts. As a matter of fact, for typical usage, using .d.ts is very rare. I only use it when a lib don't have its typings, each day more uncommon. You can have a function that fetches this api data and returns it with the right type. async function getMyData(): Promise { \* fetch API data and then do the typecast said above */ }. This is a good practice, you will have a pretty code and you may reuse it in a better way. More on reddit.com
🌐 r/typescript
26
15
January 18, 2022
🌐
GitHub
github.com › lexanth › python-ast
GitHub - lexanth/python-ast: Python (3) Parser for JavaScript/TypeScript (based on antlr4ts)
Python (3) Parser for JavaScript/TypeScript, based on antlr4ts, grammar taken from antlr4's python grammar too (so please report bugs and open pull requests related to grammars upstream)
Starred by 15 users
Forked by 5 users
Languages   TypeScript 98.9% | JavaScript 1.1% | TypeScript 98.9% | JavaScript 1.1%
🌐
npm
npmjs.com › package › @qoretechnologies › python-parser
@qoretechnologies/python-parser - npm
September 21, 2021 - A Typescript library for parsing Python 3 and doing basic program analysis, like forming control-flow graphs and def-use chains.. Latest version: 0.4.10, last published: 4 years ago.
      » npm install @qoretechnologies/python-parser
    
🌐
PyPI
pypi.org › project › ts-interface-parser
ts-interface-parser
October 15, 2019 - JavaScript is disabled in your browser · Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
🌐
GitHub
github.com › gridaco › pyast-ts
GitHub - gridaco/pyast-ts: python typescript (ts / js / jsx / tsx) ast parser using standard typescript package
This package is a python package for parsing ts ast using standard typescript module ported with python.
Author   gridaco
🌐
GitHub
github.com › TheLartians › TypeScript2Python
GitHub - TheLartians/TypeScript2Python: 🚃 Transpile TypeScript types to Python! A TypeScript to Python type transpiler.
🚃 Transpile TypeScript types to Python! A TypeScript to Python type transpiler. - TheLartians/TypeScript2Python
Starred by 27 users
Forked by 3 users
Languages   TypeScript 91.3% | JavaScript 8.7%
🌐
npm
npmjs.com › package › dt-python-parser
dt-python-parser - npm
In addition, several auxiliary methods are provided, for example, to filter comments of type # and """ in Python statements. ... Tip: The current Parser is the Javascript language version, if necessary, you can try to compile the Grammar file to other target languages
      » npm install dt-python-parser
    
Published   Apr 07, 2022
Version   0.9.0
Find elsewhere
🌐
PyPI
pypi.org › project › ts2python
ts2python
JavaScript is disabled in your browser. Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
Top answer
1 of 5
52

Nowadays, there is at least one better tool, called slimit:

SlimIt is a JavaScript minifier written in Python. It compiles JavaScript into more compact code so that it downloads and runs faster.

SlimIt also provides a library that includes a JavaScript parser, lexer, pretty printer and a tree visitor.

Demo:

Imagine we have the following javascript code:

$.ajax({
    type: "POST",
    url: 'http://www.example.com',
    data: {
        email: '[email protected]',
        phone: '9999999999',
        name: 'XYZ'
    }
});

And now we need to get email, phone and name values from the data object.

The idea here would be to instantiate a slimit parser, visit all nodes, filter all assignments and put them into the dictionary:

from slimit import ast
from slimit.parser import Parser
from slimit.visitors import nodevisitor


data = """
$.ajax({
    type: "POST",
    url: 'http://www.example.com',
    data: {
        email: '[email protected]',
        phone: '9999999999',
        name: 'XYZ'
    }
});
"""

parser = Parser()
tree = parser.parse(data)
fields = {getattr(node.left, 'value', ''): getattr(node.right, 'value', '')
          for node in nodevisitor.visit(tree)
          if isinstance(node, ast.Assign)}

print fields

It prints:

{'name': "'XYZ'", 
 'url': "'http://www.example.com'", 
 'type': '"POST"', 
 'phone': "'9999999999'", 
 'data': '', 
 'email': "'[email protected]'"}
2 of 5
23

ANTLR, ANother Tool for Language Recognition, is a language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions containing actions in a variety of target languages.

The ANTLR site provides many grammars, including one for JavaScript.

As it happens, there is a Python API available - so you can call the lexer (recognizer) generated from the grammar directly from Python (good luck).

🌐
Webdevtutor
webdevtutor.net › blog › typescript-parser-in-python
Parsing TypeScript with Python: A Comprehensive Guide
Pyparsing: Pyparsing is a library for creating text parsers in Python. It offers a simple and flexible way to define grammars and parse structured data. Parsing TypeScript code in Python opens up new possibilities for developers seeking to combine the strengths of both languages in their projects.
🌐
Webdevtutor
webdevtutor.net › blog › typescript-parser-python
Using Python to Parse TypeScript Code: A Comprehensive Guide
Python offers various tools and libraries that can aid in parsing TypeScript code. Consider using popular libraries like ply or pyparsing for creating lexers and parsers.
🌐
Alibaba Cloud Community
alibabacloud.com › blog › an-introduction-to-parsing-tools-for-python-static-types-and-practices_598202
An Introduction to Parsing Tools for Python Static Types and Practices - Alibaba Cloud Community
November 2, 2021 - If current files are __init__ files, conduct global search for all files in the directory.') print('import_path_map: ', import_path_map) print('\n\n\n By using pytype, parse AST of pyi files to analyze the returned types of third-party dependencies and find out the type of current variables.\n\n') # Use pytype to parse dependent pyi files and obtain the return value of call methods fname = '/path/to/parsed_file' with open(fname, 'r') as reader: lines = reader.readlines() sourcecode = '\n'.join(lines) ret = parser.parse_string(sourcecode, filename=fname, python_version=3) constant_map = dict()
🌐
JSR
jsr.io › @kriss-u › py-ast
@kriss-u/py-ast - JSR
A comprehensive TypeScript-based Python source code parser that generates Abstract Syntax Trees (AST) following the Python ASDL grammar specification.
🌐
Medium
medium.com › @almenon214 › converting-python-shell-to-typescript-8768162d3a55
Converting python-shell to typescript | by Almenon | Medium
September 4, 2018 - The parser from before became “parser:(param:string)=>any”. It is a function that accepts a string and returns any type. Or for the mode, “mode?: ‘text’|’json’|’binary”. A nullable variable that only accepts three possible strings. Typescript can get far more powerful and specific than this, but this is as complex as I needed.
🌐
Reddit
reddit.com › r/developer › a good javascript/typescript parser in python
r/developer on Reddit: A good javascript/typescript parser in Python
April 24, 2024 -

I'm officially losing it. I spent the past 2 days looking for a javascript/typescript parser in Python. Most of the ones I found are outdated and incompatible with current updates, as arrowfunctions and regular notations give errors. The use case for this is being able to scan an entire codebase in JS/TS and extracting valuable info such as variable names, classes, methods, invocations, etc...

🌐
GitHub
github.com › buehler › node-typescript-parser
GitHub - buehler/node-typescript-parser: Parser for typescript (and javascript) files, that compiles those files and generates a human understandable AST.
const parser = new TypescriptParser(); // either: const parsed = await parser.parseSource(/* typescript source code as string */); // or a filepath const parsed = await parser.parseFile('/user/myfile.ts', 'workspace root');
Starred by 150 users
Forked by 43 users
Languages   TypeScript 99.8% | JavaScript 0.2% | TypeScript 99.8% | JavaScript 0.2%
🌐
Webdevtutor
webdevtutor.net › blog › typescript-parser-for-python
Integrating a TypeScript Parser for Python Projects: A Comprehensive Guide
Enhanced Compatibility: Integrating TypeScript code into Python projects becomes more seamless, ensuring compatibility and consistency across the codebase. To integrate a TypeScript parser in Python projects, developers can utilize libraries such as py-ts-parser or typescript-ast.
🌐
Npm
npm.io › search › keyword:python+parser
Python parser | npm.io
Python Parser for JavaScript/TypeScript, based on antlr4ts