Skip to content

Commit

Permalink
feat(parser): add function to display AST statistics and fix Clippy
Browse files Browse the repository at this point in the history
… warnings (#7)
  • Loading branch information
shahryarjb authored Jan 5, 2025
1 parent 18c184d commit 4904883
Show file tree
Hide file tree
Showing 14 changed files with 444 additions and 151 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<img src="https://github.com/ash-project/igniter/blob/main/logos/igniter-logo-small.png?raw=true#gh-light-mode-only" alt="Logo Light" width="250">
<img src="https://github.com/ash-project/igniter/blob/main/logos/igniter-logo-small.png?raw=true#gh-dark-mode-only" alt="Logo Dark" width="250">

[![CI](https://github.com/ash-project/igniter/actions/workflows/elixir.yml/badge.svg)](https://github.com/ash-project/igniter/actions/workflows/elixir.yml)
[![Hex version badge](https://img.shields.io/hexpm/v/igniter.svg)](https://hex.pm/packages/igniter)
[![Hexdocs badge](https://img.shields.io/badge/docs-hexdocs-purple)](https://hexdocs.pm/igniter)
[![CI](https://github.com/ash-project/igniter_js/actions/workflows/elixir.yml/badge.svg)](https://github.com/ash-project/igniter_js/actions/workflows/elixir.yml)
[![Hex version badge](https://img.shields.io/hexpm/v/igniter.svg)](https://hex.pm/packages/igniter_js)
[![Hexdocs badge](https://img.shields.io/badge/docs-hexdocs-purple)](https://hexdocs.pm/igniter_js)

# IgniterJs

Expand Down
2 changes: 2 additions & 0 deletions lib/igniter_js/native.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ defmodule IgniterJs.Native do

def remove_objects_of_hooks_from_ast_nif(_file_content, _object_names), do: error()

def statistics_from_ast_nif(_file_content), do: error()

defp error, do: :erlang.nif_error(:nif_not_loaded)
end
34 changes: 34 additions & 0 deletions lib/igniter_js/parsers/javascript/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,38 @@ defmodule IgniterJs.Parsers.Javascript.Parser do
type
)
end

@doc """
Retrieve statistical information about the JavaScript source code, such as the number of
functions, classes, debugger statements, imports, try-catch blocks, and throw statements.
This function accepts either the content of the JavaScript file or the path to the file,
and returns a tuple with the status, function atom, and the extracted data as a map.
## Examples
```elixir
alias IgniterJs.Parsers.Javascript.Parser
# Analyze a JavaScript source file by providing its content
Parser.statistics(js_content)
# Analyze a JavaScript source file by providing its file path
Parser.statistics("/path/to/file.js", :path)
```
"""
def statistics(file_path_or_content, type \\ :content) do
{status, fn_atom, {_, data}} =
call_nif_fn(
file_path_or_content,
__ENV__.function,
fn file_content ->
Native.statistics_from_ast_nif(file_content)
end,
type
)

converted = if is_map(data), do: Map.drop(data, [:__struct__]), else: data
{status, fn_atom, converted}
end
end
Loading

0 comments on commit 4904883

Please sign in to comment.