Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parser): add function to display AST statistics and fix Clippy warnings #7

Merged
merged 7 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading