From b8e6d5f839e090170fd20a949f0467bcf8b27246 Mon Sep 17 00:00:00 2001 From: KOLANICH Date: Tue, 28 Feb 2023 21:12:13 +0300 Subject: [PATCH] Replace JS-implemented esprima-serializer with `escodegen` python package, which is claimed to be a rewrite of `jscodegen` into Python. --- opener/__main__.py | 22 +++++++++------------- opener/codegen.js | 23 ----------------------- 2 files changed, 9 insertions(+), 36 deletions(-) delete mode 100755 opener/codegen.js diff --git a/opener/__main__.py b/opener/__main__.py index 6fd2fc9..2140f31 100644 --- a/opener/__main__.py +++ b/opener/__main__.py @@ -63,18 +63,6 @@ def usage(*, exit: bool, error: bool): sys.exit(int(error)) -def run_codegen_js(ast_dict: dict): - path = pkg_resources.resource_filename(__name__, "codegen.js") - proc = subprocess.Popen( - ["node", path], - stdin=subprocess.PIPE, - encoding="utf8", - ) - json.dump(ast_dict, proc.stdin, cls=JsonEncoder) - proc.stdin.close() - proc.wait() - - def main(): positional_args = [] temp_prefix = DEFAULT_TEMP_PREFIX @@ -147,7 +135,15 @@ def main(): else: if verbose: print("Formatting code...", file=sys.stderr) - run_codegen_js(ast_dict) + + try: + import escodegen + except ImportError: + from warnings import warn + warn("jscodegen has some issues and produces incorrect results (at least the version in the upstream repo when this remark was added here), better use https://github.com/0o120/escodegen-python") + import jscodegen as escodegen + + print(escodegen.generate(ast_dict)) if __name__ == "__main__": diff --git a/opener/codegen.js b/opener/codegen.js deleted file mode 100755 index fefad01..0000000 --- a/opener/codegen.js +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env node -/* - * Copyright (C) 2021 taylor.fish - * - * This file is part of Opener. - * - * Opener is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Opener is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Opener. If not, see . - */ - -const escodegen = require("escodegen"); -const fs = require("fs"); -console.log(escodegen.generate(JSON.parse(fs.readFileSync(0, "utf8"))));