IgniterJs.Helpers (igniter_js v0.5.0)

Copy Markdown View Source

A module that contains helper functions for IgniterJs. For example it helps to normalize the output of the NIFs, read and validate the file, and call the NIF function with the given file path or content.

Summary

Functions

Call the NIF function with the given file path or content and return the result. It helps to change the function name as atom based on its caller function.

Which dialect a source should be parsed as: "js", "jsx", "ts" or "tsx".

The dialect implied by a path's extension, defaulting to "js".

Normalize the output of the NIFs. It is a macro and returns a tuple with the first element as the output, the second element as the caller function name, and the third element as the status.

Read and validate the file. It returns the file content if the file exists and the extension is .js or .ts, otherwise, it returns an error tuple.

Functions

call_nif_fn(file_path, caller_function, processing_fn, type \\ :content)

Call the NIF function with the given file path or content and return the result. It helps to change the function name as atom based on its caller function.

call_nif_fn("/path/to/file.js", __ENV__.function, fn content -> content end, :path)
call_nif_fn("file content", __ENV__.function, fn content -> content end)
call_nif_fn("file content", __ENV__.function, fn content -> content end, :content)

dialect_for(file_path_or_content, type, opts)

@spec dialect_for(String.t(), :content | :path, keyword()) :: String.t()

Which dialect a source should be parsed as: "js", "jsx", "ts" or "tsx".

Resolution order:

  1. an explicit :dialect option -- dialect: :tsx
  2. the file extension, when type is :path
  3. "js"

Step 3 is what keeps this backward compatible. A caller passing content and saying nothing gets exactly the parser configuration this library used before dialects existed, so anything that parsed before parses the same way. Step 2 is new behaviour, but it can only affect files that did not parse at all previously: reading app.tsx and parsing it as JavaScript never produced an answer worth keeping.

iex> IgniterJs.Helpers.dialect_for("x.tsx", :path, [])
"tsx"

iex> IgniterJs.Helpers.dialect_for("const a = 1", :content, [])
"js"

iex> IgniterJs.Helpers.dialect_for("x.js", :path, dialect: :tsx)
"tsx"

dialect_of_path(path)

@spec dialect_of_path(String.t()) :: String.t()

The dialect implied by a path's extension, defaulting to "js".

iex> IgniterJs.Helpers.dialect_of_path("vite.config.ts")
"ts"

iex> IgniterJs.Helpers.dialect_of_path("Makefile")
"js"

normalize_output(output, caller_function)

(macro)

Normalize the output of the NIFs. It is a macro and returns a tuple with the first element as the output, the second element as the caller function name, and the third element as the status.

require IgniterJs.Helpers
normalize_output({:ok, :fun_atom, result}, __ENV__.function)
normalize_output({:error, :fun_atom, result}, __ENV__.function)

read_and_validate_file(file_path)

Read and validate the file. It returns the file content if the file exists and the extension is .js or .ts, otherwise, it returns an error tuple.

read_and_validate_file("/path/to/file.js")