Attempts to extract an embedded swagger spec from a JavaScript file. Removes comments, looks for object definitions with braces, and tries to parse them as JSON after minor adjustments.
(js_text)
| 1074 | return None |
| 1075 | |
| 1076 | def extract_spec_from_js(js_text): |
| 1077 | """ |
| 1078 | Attempts to extract an embedded swagger spec from a JavaScript file. |
| 1079 | Removes comments, looks for object definitions with braces, and tries |
| 1080 | to parse them as JSON after minor adjustments. |
| 1081 | """ |
| 1082 | js_text = re.sub(r'/\*[\s\S]*?\*/', '', js_text) |
| 1083 | js_text = re.sub(r'//.*', '', js_text) |
| 1084 | |
| 1085 | patterns = [ |
| 1086 | r'(?:var|let|const)\s+(\w+)\s*=\s*({[\s\S]*?});', |
| 1087 | r'(\w+)\s*=\s*({[\s\S]*?});', |
| 1088 | ] |
| 1089 | for pat in patterns: |
| 1090 | matches = re.findall(pat, js_text, re.DOTALL) |
| 1091 | for var_name, obj_str in matches: |
| 1092 | cleaned_str = js_object_to_json(obj_str) |
| 1093 | if cleaned_str: |
| 1094 | try: |
| 1095 | spec = json.loads(cleaned_str) |
| 1096 | return spec |
| 1097 | except json.JSONDecodeError: |
| 1098 | continue |
| 1099 | return None |
| 1100 | |
| 1101 | def js_object_to_json(js_object_str): |
| 1102 | """ |
no test coverage detected