Extract import targets as module/path strings.
(self, node, language: str, source: bytes)
| 6212 | return bases |
| 6213 | |
| 6214 | def _extract_import(self, node, language: str, source: bytes) -> list[str]: |
| 6215 | """Extract import targets as module/path strings.""" |
| 6216 | imports = [] |
| 6217 | text = node.text.decode("utf-8", errors="replace").strip() |
| 6218 | |
| 6219 | if language == "python": |
| 6220 | # import x.y.z or from x.y import z |
| 6221 | if node.type == "import_from_statement": |
| 6222 | for child in node.children: |
| 6223 | if child.type == "dotted_name": |
| 6224 | imports.append(child.text.decode("utf-8", errors="replace")) |
| 6225 | break |
| 6226 | else: |
| 6227 | for child in node.children: |
| 6228 | if child.type == "dotted_name": |
| 6229 | imports.append(child.text.decode("utf-8", errors="replace")) |
| 6230 | elif language in ("javascript", "typescript", "tsx"): |
| 6231 | # import ... from 'module' |
| 6232 | for child in node.children: |
| 6233 | if child.type == "string": |
| 6234 | val = child.text.decode("utf-8", errors="replace").strip("'\"") |
| 6235 | imports.append(val) |
| 6236 | elif language == "go": |
| 6237 | for child in node.children: |
| 6238 | if child.type == "import_spec_list": |
| 6239 | for spec in child.children: |
| 6240 | if spec.type == "import_spec": |
| 6241 | for s in spec.children: |
| 6242 | if s.type == "interpreted_string_literal": |
| 6243 | val = s.text.decode("utf-8", errors="replace") |
| 6244 | imports.append(val.strip('"')) |
| 6245 | elif child.type == "import_spec": |
| 6246 | for s in child.children: |
| 6247 | if s.type == "interpreted_string_literal": |
| 6248 | val = s.text.decode("utf-8", errors="replace") |
| 6249 | imports.append(val.strip('"')) |
| 6250 | elif language == "rust": |
| 6251 | # use crate::module::item |
| 6252 | imports.append(text.replace("use ", "").rstrip(";").strip()) |
| 6253 | elif language in ("c", "cpp"): |
| 6254 | # #include <header> or #include "header" |
| 6255 | for child in node.children: |
| 6256 | if child.type in ("system_lib_string", "string_literal"): |
| 6257 | val = child.text.decode("utf-8", errors="replace").strip("<>\"") |
| 6258 | imports.append(val) |
| 6259 | elif language in ("java", "csharp"): |
| 6260 | # import/using package.Class |
| 6261 | parts = text.split() |
| 6262 | if len(parts) >= 2: |
| 6263 | imports.append(parts[-1].rstrip(";")) |
| 6264 | elif language == "solidity": |
| 6265 | # import "path/to/file.sol" or import {Symbol} from "path" |
| 6266 | for child in node.children: |
| 6267 | if child.type == "string": |
| 6268 | val = child.text.decode("utf-8", errors="replace").strip('"') |
| 6269 | if val: |
| 6270 | imports.append(val) |
| 6271 | elif language == "scala": |
no test coverage detected