Skip to content

Commit

Permalink
Add exist_var = contains_variable_from_ast_nif
Browse files Browse the repository at this point in the history
  • Loading branch information
shahryarjb committed Jan 14, 2025
1 parent ce2be05 commit 925e33d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/igniter_js/native.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ defmodule IgniterJs.Native do

def find_live_socket_node_from_ast_nif(_file_content), do: error()

def contains_variable_from_ast_nif(_file_content, _variable_name), do: error()

def extend_hook_object_to_ast_nif(_file_content, _names), do: error()

def remove_objects_of_hooks_from_ast_nif(_file_content, _object_names), do: error()
Expand Down
35 changes: 35 additions & 0 deletions lib/igniter_js/parsers/javascript/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,41 @@ defmodule IgniterJs.Parsers.Javascript.Parser do
)
end

@doc """
Check if a specific var exists in the given file or content and returns boolean.
```elixir
alias IgniterJs.Parsers.Javascript.Parser
Parser.exist_live_socket?(js_content)
Parser.exist_live_socket?(js_content, :content)
Parser.exist_live_socket?("/path/to/file.js", :path)
```
"""
def exist_var?(file_path_or_content, type \\ :content) do
elem(exist_var(file_path_or_content, type), 0) == :ok
end

@doc """
Check if an specific var exists in the given file or content and returns tuple.
```elixir
alias IgniterJs.Parsers.Javascript.Parser
Parser.exist_live_socket(js_content, var_name)
Parser.exist_live_socket(js_content, var_name, :content)
Parser.exist_live_socket("/path/to/file.js", var_name, :path)
```
"""
def exist_var(file_path_or_content, var_name, type \\ :content) do
call_nif_fn(
file_path_or_content,
__ENV__.function,
fn file_content ->
Native.contains_variable_from_ast_nif(file_content, var_name)
end,
type
)
end

@doc """
Extend the hook object in the given file or content. It accepts a single object
or a list of objects.
Expand Down
1 change: 1 addition & 0 deletions native/igniter_js/src/atoms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ rustler::atoms! {
remove_objects_of_hooks_from_ast_nif,
statistics_from_ast_nif,
extend_var_object_property_by_names_to_ast_nif,
contains_variable_from_ast_nif,
// Resource Atoms
}
16 changes: 16 additions & 0 deletions native/igniter_js/src/parsers/javascript/ast_ex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ pub fn find_live_socket_node_from_ast_nif(env: Env, file_content: String) -> Nif
encode_response(env, status, fn_atom, result)
}

#[rustler::nif]
pub fn contains_variable_from_ast_nif(
env: Env,
file_content: String,
variable_name: String,
) -> NifResult<Term> {
let fn_atom = atoms::contains_variable_from_ast_nif();

let (status, result) = match contains_variable_from_ast(&file_content, &variable_name) {
Ok(true) => (atoms::ok(), true),
_ => (atoms::error(), false),
};

encode_response(env, status, fn_atom, result)
}

#[rustler::nif]
pub fn extend_hook_object_to_ast_nif(
env: Env,
Expand Down
22 changes: 22 additions & 0 deletions test/parsers/javascript/parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,26 @@ defmodule IgniterJSTest.Parsers.Javascript.ParserTest do
:path
)
end

test "Check existing vars :: exist_var" do
code = """
import { foo } from "module-name";
import bar from "another-module";
"""

{:error, :exist_var, false} = assert Parser.exist_var(code, "test_name")
assert !Parser.exist_var?(code, "test_name")

code = """
import { foo } from "module-name";
import bar from "another-module";
let mishka_ash = () => {1 + 1}
let igniterJS = %{stack: ["rust", "elixir", "js"]}
"""

assert Parser.exist_var?(code, "igniterJS")
{:ok, :exist_var, true} = assert Parser.exist_var(code, "igniterJS")
end
end

0 comments on commit 925e33d

Please sign in to comment.