| 244 | |
| 245 | |
| 246 | class Javascript(BaseLanguage): |
| 247 | @staticmethod |
| 248 | def assert_dependencies(): |
| 249 | """Assert that acorn is installed and the correct version""" |
| 250 | assert is_installed('acorn'), "Acorn is required to parse javascript files " \ |
| 251 | "but was not found on the path. Install it " \ |
| 252 | "from npm and try again." |
| 253 | version = get_acorn_version() |
| 254 | if not version.startswith('8.'): |
| 255 | logging.warning("Acorn is required to parse javascript files. " |
| 256 | "Version %r was found but code2flow has only been " |
| 257 | "tested on 8.*", version) |
| 258 | logging.info("Using Acorn %s" % version) |
| 259 | |
| 260 | @staticmethod |
| 261 | def get_tree(filename, lang_params): |
| 262 | """ |
| 263 | Get the entire AST for this file |
| 264 | |
| 265 | :param filename str: |
| 266 | :param lang_params LanguageParams: |
| 267 | :rtype: ast |
| 268 | """ |
| 269 | script_loc = os.path.join(os.path.dirname(os.path.realpath(__file__)), |
| 270 | "get_ast.js") |
| 271 | cmd = ["node", script_loc, lang_params.source_type, filename] |
| 272 | try: |
| 273 | output = subprocess.check_output(cmd, stderr=subprocess.PIPE) |
| 274 | except subprocess.CalledProcessError: |
| 275 | raise AssertionError( |
| 276 | "Acorn could not parse file %r. You may have a JS syntax error or " |
| 277 | "if this is an es6-style source, you may need to run code2flow " |
| 278 | "with --source-type=module. " |
| 279 | "For more detail, try running the command " |
| 280 | "\n acorn %s\n" |
| 281 | "Warning: Acorn CANNOT parse all javascript files. See their docs. " % |
| 282 | (filename, filename)) from None |
| 283 | tree = json.loads(output) |
| 284 | assert isinstance(tree, dict) |
| 285 | assert tree['type'] == 'Program' |
| 286 | return tree |
| 287 | |
| 288 | @staticmethod |
| 289 | def separate_namespaces(tree): |
| 290 | """ |
| 291 | Given an AST, recursively separate that AST into lists of ASTs for the |
| 292 | subgroups, nodes, and body. This is an intermediate step to allow for |
| 293 | cleaner processing downstream |
| 294 | |
| 295 | :param tree ast: |
| 296 | :returns: tuple of group, node, and body trees. These are processed |
| 297 | downstream into real Groups and Nodes. |
| 298 | :rtype: (list[ast], list[ast], list[ast]) |
| 299 | """ |
| 300 | |
| 301 | groups = [] |
| 302 | nodes = [] |
| 303 | body = [] |
nothing calls this directly
no outgoing calls
no test coverage detected