From 9c804aace384fe1c3c3dd84c2a5c644dce368777 Mon Sep 17 00:00:00 2001 From: "Francois (fge)" Date: Tue, 23 Mar 2021 14:14:10 +0000 Subject: [PATCH] [FIX] base: js_transpiler import legacy module In a @odoo-module js file, importing a legacy module that did not have a name respecting the "addon.name" format was not supported. This commit will support all keys that do not start with a "." or "@". This commit will also improve some regexes allowing to extract path. Legacy module name supported: import X from "some/path" import X from "addon.name" Legacy module name not supported: (relative path and @module/path are only used to import @odoo-module js files) import X from "@some/path" import X from "./some/path" import X from "../../some/path" closes odoo/odoo#68152 Signed-off-by: Aaron Bohy (aab) --- .../tests/test_js_transpiler.py | 10 +++++++-- odoo/tools/js_transpiler.py | 22 ++++++++----------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/odoo/addons/test_assetsbundle/tests/test_js_transpiler.py b/odoo/addons/test_assetsbundle/tests/test_js_transpiler.py index 80e0882f92a..94915ae1442 100644 --- a/odoo/addons/test_assetsbundle/tests/test_js_transpiler.py +++ b/odoo/addons/test_assetsbundle/tests/test_js_transpiler.py @@ -255,7 +255,10 @@ import Line12 from "@test.Dialog"; //HELLO import {Line13} from "@test.Dialog" //HELLO -const test = `import { Line13, Notification } from "../src/Dialog";` +const test = `import { Line14, Notification } from "../src/Dialog";` + +import Line15 from "test/Dialog"; +import Line16 from "test.Dialog.error"; """ result = transpile_javascript("/test_assetsbundle/static/src/import.js", input_content) @@ -284,7 +287,10 @@ const Line12 = require("@test.Dialog")[Symbol.for("default")]; //HELLO const {Line13} = require("@test.Dialog") //HELLO -const test = `import { Line13, Notification } from "../src/Dialog";` +const test = `import { Line14, Notification } from "../src/Dialog";` + +const Line15 = require("test/Dialog"); +const Line16 = require("test.Dialog.error"); return __exports; }); diff --git a/odoo/tools/js_transpiler.py b/odoo/tools/js_transpiler.py index 707d46b5585..7be461e796e 100644 --- a/odoo/tools/js_transpiler.py +++ b/odoo/tools/js_transpiler.py @@ -255,7 +255,7 @@ EXPORT_FROM_RE = re.compile(r""" export\s* # export (?P{(\s*\w+\s*,?\s*)*})\s* # { a, b, c as x, ... } from\s* # from - (?P(\".*\")|('.*')|(`.*`)) # "file path" ("some/path.js") + (?P(?P["'`])([^"'`]+)(?P=quote)) # "file path" ("some/path.js") """, re.MULTILINE | re.VERBOSE) @@ -286,7 +286,7 @@ EXPORT_STAR_FROM_RE = re.compile(r""" ^ (?P\s*) # space and empty line export\s*\*\s*from\s* # export * from - (?P(\".*\")|('.*')|(`.*`)) # "file path" ("some/path.js") + (?P(?P["'`])([^"'`]+)(?P=quote)) # "file path" ("some/path.js") """, re.MULTILINE | re.VERBOSE) @@ -346,7 +346,7 @@ IMPORT_BASIC_RE = re.compile(r""" import\s+ # import (?P{(\s*\w+\s*,?\s*)+})\s* # { a, b, c as x, ... } from\s* # from - (?P(\".*\")|('.*')|(`.*`)) # "file path" ("some/path") + (?P(?P["'`])([^"'`]+)(?P=quote)) # "file path" ("some/path") """, re.MULTILINE | re.VERBOSE) @@ -373,7 +373,7 @@ IMPORT_LEGACY_DEFAULT_RE = re.compile(r""" import\s+ # import (?P\w+)\s* # default variable name from\s* # from - (?P(\"\w+\.\w+\")|('\w+\.\w+')|(`\w+\.\w+`)) # legacy alias file ("addon_name.module_name") + (?P(?P["'`])([^@\."'`][^"'`]*)(?P=quote)) # legacy alias file ("addon_name.module_name" or "some/path") """, re.MULTILINE | re.VERBOSE) @@ -400,7 +400,7 @@ IMPORT_DEFAULT = re.compile(r""" import\s+ # import (?P\w+)\s* # default variable name from\s* # from - (?P(\".*\")|('.*')|(`.*`)) # "file path" ("some/path") + (?P(?P["'`])([^"'`]+)(?P=quote)) # "file path" ("some/path") """, re.MULTILINE | re.VERBOSE) @@ -420,7 +420,7 @@ def convert_default_import(content): RELATIVE_REQUIRE_RE = re.compile(r""" - require\((["'`])([^@"'`]+)(\1)\) # require("some/path") + require\((?P["'`])([^@"'`]+)(?P=quote)\) # require("some/path") """, re.VERBOSE) @@ -443,9 +443,9 @@ def convert_relative_require(url, content): require("other_alias") """ new_content = content - for open_quote, path, close_quote in RELATIVE_REQUIRE_RE.findall(new_content): + for quote, path in RELATIVE_REQUIRE_RE.findall(new_content): if path.startswith(".") and "/" in path: - pattern = rf"require\({open_quote}{path}{close_quote}\)" + pattern = rf"require\({quote}{path}{quote}\)" repl = f'require("{relative_path_to_module_path(url, path)}")' new_content = re.sub(pattern, repl, new_content) return new_content @@ -500,11 +500,7 @@ def convert_unnamed_relative_import(content): URL_INDEX_RE = re.compile(r""" require\s* # require \(\s* # ( - (?P # path (three possibles syntax for the string) - (\".*/index/?\")| - ('.*/index/?')| - (`.*/index/?`) - ) + (?P(?P["'`])([^"'`]*/index/?)(?P=quote)) # path ended by /index or /index/ \s*\) # ) """, re.MULTILINE | re.VERBOSE)