@@ -18,119 +18,125 @@ def module_name_suffix(name: str) -> str:
1818 return PurePosixPath (tail or head ).suffix or ".js"
1919
2020
21- def resolve_module_exports_from_file (
21+ def resolve_from_module_file (
2222 file : Path ,
2323 max_depth : int ,
24- is_re_export : bool = False ,
24+ is_regex_import : bool = False ,
2525) -> set [str ]:
2626 if max_depth == 0 :
27- logger .warning (f"Did not resolve all exports for { file } - max depth reached" )
27+ logger .warning (f"Did not resolve all imports for { file } - max depth reached" )
2828 return set ()
2929 elif not file .exists ():
30- logger .warning (f"Did not resolve exports for unknown file { file } " )
30+ logger .warning (f"Did not resolve imports for unknown file { file } " )
3131 return set ()
3232
33- export_names , references = resolve_module_exports_from_source (
34- file .read_text (encoding = "utf-8" ), exclude_default = is_re_export
33+ names , references = resolve_from_module_source (
34+ file .read_text (encoding = "utf-8" ), exclude_default = is_regex_import
3535 )
3636
3737 for ref in references :
3838 if urlparse (ref ).scheme : # is an absolute URL
39- export_names .update (
40- resolve_module_exports_from_url (ref , max_depth - 1 , is_re_export = True )
39+ names .update (
40+ resolve_from_module_url (ref , max_depth - 1 , is_regex_import = True )
4141 )
4242 else :
4343 path = file .parent .joinpath (* ref .split ("/" ))
44- export_names .update (
45- resolve_module_exports_from_file (path , max_depth - 1 , is_re_export = True )
44+ names .update (
45+ resolve_from_module_file (path , max_depth - 1 , is_regex_import = True )
4646 )
4747
48- return export_names
48+ return names
4949
5050
51- def resolve_module_exports_from_url (
51+ def resolve_from_module_url (
5252 url : str ,
5353 max_depth : int ,
54- is_re_export : bool = False ,
54+ is_regex_import : bool = False ,
5555) -> set [str ]:
5656 if max_depth == 0 :
57- logger .warning (f"Did not resolve all exports for { url } - max depth reached" )
57+ logger .warning (f"Did not resolve all imports for { url } - max depth reached" )
5858 return set ()
5959
6060 try :
6161 text = requests .get (url , timeout = 5 ).text
6262 except requests .exceptions .ConnectionError as error :
6363 reason = "" if error is None else " - {error.errno}"
64- logger .warning (f"Did not resolve exports for url { url } { reason } " )
64+ logger .warning (f"Did not resolve imports for url { url } { reason } " )
6565 return set ()
6666
67- export_names , references = resolve_module_exports_from_source (
68- text , exclude_default = is_re_export
67+ names , references = resolve_from_module_source (
68+ text , exclude_default = is_regex_import
6969 )
7070
7171 for ref in references :
72- url = resolve_relative_url (url , ref )
73- export_names .update (
74- resolve_module_exports_from_url (url , max_depth - 1 , is_re_export = True )
75- )
72+ url = normalize_url_path (url , ref )
73+ names .update (resolve_from_module_url (url , max_depth - 1 , is_regex_import = True ))
7674
77- return export_names
75+ return names
7876
7977
80- def resolve_module_exports_from_source (
78+ def resolve_from_module_source (
8179 content : str , exclude_default : bool
8280) -> tuple [set [str ], set [str ]]:
83- names : set [str ] = set ()
81+ """Find names exported by the given JavaScript module content to assist with ReactPy import resolution.
82+
83+ Parmeters:
84+ content: The content of the JavaScript module.
85+ Returns:
86+ A tuple where the first item is a set of exported names and the second item is a set of
87+ referenced module paths.
88+ """
89+ all_names : set [str ] = set ()
8490 references : set [str ] = set ()
8591
8692 if _JS_DEFAULT_EXPORT_PATTERN .search (content ):
87- names .add ("default" )
93+ all_names .add ("default" )
8894
8995 # Exporting functions and classes
90- names .update (_JS_FUNC_OR_CLS_EXPORT_PATTERN .findall (content ))
96+ all_names .update (_JS_FUNC_OR_CLS_EXPORT_PATTERN .findall (content ))
9197
92- for export in _JS_GENERAL_EXPORT_PATTERN .findall (content ):
93- export = export .rstrip (";" ).strip ()
98+ for name in _JS_GENERAL_EXPORT_PATTERN .findall (content ):
99+ name = name .rstrip (";" ).strip ()
94100 # Exporting individual features
95- if export .startswith ("let " ):
96- names .update (let .split ("=" , 1 )[0 ] for let in export [4 :].split ("," ))
101+ if name .startswith ("let " ):
102+ all_names .update (let .split ("=" , 1 )[0 ] for let in name [4 :].split ("," ))
97103 # Renaming exports and export list
98- elif export .startswith ("{" ) and export .endswith ("}" ):
99- names .update (
100- item .split (" as " , 1 )[- 1 ] for item in export .strip ("{}" ).split ("," )
104+ elif name .startswith ("{" ) and name .endswith ("}" ):
105+ all_names .update (
106+ item .split (" as " , 1 )[- 1 ] for item in name .strip ("{}" ).split ("," )
101107 )
102108 # Exporting destructured assignments with renaming
103- elif export .startswith ("const " ):
104- names .update (
109+ elif name .startswith ("const " ):
110+ all_names .update (
105111 item .split (":" , 1 )[0 ]
106- for item in export [6 :].split ("=" , 1 )[0 ].strip ("{}" ).split ("," )
112+ for item in name [6 :].split ("=" , 1 )[0 ].strip ("{}" ).split ("," )
107113 )
108114 # Default exports
109- elif export .startswith ("default " ):
110- names .add ("default" )
115+ elif name .startswith ("default " ):
116+ all_names .add ("default" )
111117 # Aggregating modules
112- elif export .startswith ("* as " ):
113- names .add (export [5 :].split (" from " , 1 )[0 ])
114- elif export .startswith ("* " ):
115- references .add (export [2 :].split ("from " , 1 )[- 1 ].strip ("'\" " ))
116- elif export .startswith ("{" ) and " from " in export :
117- names .update (
118+ elif name .startswith ("* as " ):
119+ all_names .add (name [5 :].split (" from " , 1 )[0 ])
120+ elif name .startswith ("* " ):
121+ references .add (name [2 :].split ("from " , 1 )[- 1 ].strip ("'\" " ))
122+ elif name .startswith ("{" ) and " from " in name :
123+ all_names .update (
118124 item .split (" as " , 1 )[- 1 ]
119- for item in export .split (" from " )[0 ].strip ("{}" ).split ("," )
125+ for item in name .split (" from " )[0 ].strip ("{}" ).split ("," )
120126 )
121- elif not (export .startswith ("function " ) or export .startswith ("class " )):
122- logger .warning (f"Unknown export type { export !r} " )
127+ elif not (name .startswith ("function " ) or name .startswith ("class " )):
128+ logger .warning (f"Found unknown export type { name !r} " )
123129
124- names = {n .strip () for n in names }
130+ all_names = {n .strip () for n in all_names }
125131 references = {r .strip () for r in references }
126132
127- if exclude_default and "default" in names :
128- names .remove ("default" )
133+ if exclude_default and "default" in all_names :
134+ all_names .remove ("default" )
129135
130- return names , references
136+ return all_names , references
131137
132138
133- def resolve_relative_url (base_url : str , rel_url : str ) -> str :
139+ def normalize_url_path (base_url : str , rel_url : str ) -> str :
134140 if not rel_url .startswith ("." ):
135141 if rel_url .startswith ("/" ):
136142 # copy scheme and hostname from base_url
0 commit comments