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 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)
Which dialect a source should be parsed as: "js", "jsx", "ts" or "tsx".
Resolution order:
- an explicit
:dialectoption --dialect: :tsx - the file extension, when
typeis:path "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"
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 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 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")