@@ -50,15 +50,46 @@ def resolve_module_exports_from_url(url: str, max_depth: int) -> Set[str]:
50
50
51
51
def resolve_module_exports_from_source (content : str ) -> Tuple [Set [str ], Set [str ]]:
52
52
names : Set [str ] = set ()
53
- for match in _JS_MODULE_EXPORT_PATTERN .findall (content ):
54
- for export in match .split ("," ):
55
- export_parts = export .split (" as " , 1 )
56
- names .add (export_parts [- 1 ].strip ())
57
- names .update (_JS_MODULE_EXPORT_FUNC_PATTERN .findall (content ))
58
- names .update (_JS_MODULE_EXPORT_NAME_PATTERN .findall (content ))
59
-
60
- references : Set [str ] = set (_JS_MODULE_EXPORT_FROM_REF_PATTERN .findall (content ))
61
- return names , references
53
+ references : Set [str ] = set ()
54
+ for export in _JS_EXPORT_PATTERN .findall (
55
+ # strip comments
56
+ _JS_LINE_COMMENT .sub ("" , content )
57
+ ):
58
+ export = export .rstrip (";" ).strip ()
59
+ # Exporting individual features
60
+ if export .startswith ("let " ):
61
+ names .update (let .split ("=" , 1 )[0 ] for let in export [4 :].split ("," ))
62
+ elif export .startswith ("function " ):
63
+ names .add (export [9 :].split ("(" , 1 )[0 ])
64
+ elif export .startswith ("class " ):
65
+ names .add (export [6 :].split ("{" , 1 )[0 ])
66
+ # Renaming exports and export list
67
+ elif export .startswith ("{" ) and export .endswith ("}" ):
68
+ names .update (
69
+ item .split (" as " , 1 )[- 1 ] for item in export .strip ("{}" ).split ("," )
70
+ )
71
+ # Exporting destructured assignments with renaming
72
+ elif export .startswith ("const " ):
73
+ names .update (
74
+ item .split (":" , 1 )[0 ]
75
+ for item in export [6 :].split ("=" , 1 )[0 ].strip ("{}" ).split ("," )
76
+ )
77
+ # Default exports
78
+ elif export .startswith ("default " ):
79
+ names .add ("default" )
80
+ # Aggregating modules
81
+ elif export .startswith ("* as " ):
82
+ names .add (export [5 :].split (" from " , 1 )[0 ])
83
+ elif export .startswith ("* " ):
84
+ references .add (export [2 :].split ("from " , 1 )[- 1 ].strip ("'\" " ))
85
+ elif export .startswith ("{" ) and " from " in export :
86
+ names .update (
87
+ item .split (" as " , 1 )[- 1 ]
88
+ for item in export .split (" from " )[0 ].strip ("{}" ).split ("," )
89
+ )
90
+ else :
91
+ logger .warning (f"Unknown export type { export !r} " )
92
+ return {n .strip () for n in names }, {r .strip () for r in references }
62
93
63
94
64
95
def _resolve_relative_file_path (base_path : Path , rel_url : str ) -> Path :
@@ -81,16 +112,5 @@ def _resolve_relative_url(base_url: str, rel_url: str) -> str:
81
112
return f"{ base_url } /{ rel_url } "
82
113
83
114
84
- _JS_MODULE_EXPORT_PATTERN = re .compile (
85
- r";?\s*export\s*{([0-9a-zA-Z_$\s,]*)}\s*;" , re .MULTILINE
86
- )
87
- _JS_VAR = r"[a-zA-Z_$][0-9a-zA-Z_$]*"
88
- _JS_MODULE_EXPORT_NAME_PATTERN = re .compile (
89
- fr";?\s*export\s+({ _JS_VAR } )\s+{ _JS_VAR } \s*;" , re .MULTILINE
90
- )
91
- _JS_MODULE_EXPORT_FUNC_PATTERN = re .compile (
92
- fr";?\s*export\s+function\s+({ _JS_VAR } )\s*\(.*?" , re .MULTILINE
93
- )
94
- _JS_MODULE_EXPORT_FROM_REF_PATTERN = re .compile (
95
- r""";?\s*export\s+\*\s+from\s+['"](.*?)['"];"""
96
- )
115
+ _JS_LINE_COMMENT = re .compile (r"//.*$" )
116
+ _JS_EXPORT_PATTERN = re .compile (r";?\s*export(?=\s+|{)(.*?(?:;|}\s*))" , re .MULTILINE )
0 commit comments