IgniterJs.Parsers.Javascript.Parser (igniter_js v0.4.6)
View SourceCodemods for JavaScript files.
Summary
Functions
Converts a JavaScript AST into the ESTree format.
Check if a LiveSocket var exists in the given file or content and returns tuple.
Check if a LiveSocket var exists in the given file or content and returns boolean.
Check if an specific var exists in the given file or content and returns tuple.
Extend the hook object in the given file or content. It accepts a single object or a list of objects. It returns a tuple.
Extend a variable of object type in the given file or content by adding additional objects to it, based on their names.
Inserts a JavaScript AST at the specified index within an existing AST.
Insert imports to the given file or content and returns tuple.
Check if a module is imported in the given file or content or contents and return tuple.
Check if a module is imported in the given file or content or content and returns boolean.
Remove imports from the given file or content. it accepts a single module or a list of modules. It returns a tuple.
Remove objects from the hooks in the given file or content. It accepts a single o bject or a list of objects. It returns a tuple.
Replaces a JavaScript AST node at the specified index with new JavaScript code.
Retrieve statistical information about the JavaScript source code, such as the number of functions, classes, debugger statements, imports, try-catch blocks, and throw statements.
Check if a specific var exists in the given file or content and returns boolean.
Functions
Converts a JavaScript AST into the ESTree format.
This function takes either a file path or raw JavaScript content, processes it and returns the resulting ESTree Map structure.
alias IgniterJs.Parsers.Javascript.Parser
Parser.ast_to_estree(js_content)
Parser.ast_to_estree("/path/to/file.js", :path)
Check if a LiveSocket var exists in the given file or content and returns tuple.
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)
Check if a LiveSocket var exists in the given file or content and returns boolean.
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)
Check if an specific var exists in the given file or content and returns tuple.
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)
Extend the hook object in the given file or content. It accepts a single object or a list of objects. It returns a tuple.
alias IgniterJs.Parsers.Javascript.Parser
Parser.extend_hook_object(js_content, "SomeObject")
Parser.extend_hook_object(js_content, ["SomeObject", "AnotherObject"], :content)
Parser.extend_hook_object("/path/to/file.js", "SomeObject", :path)
Extend a variable of object type in the given file or content by adding additional objects to it, based on their names.
This function ensures that duplicate entries are not added during the process.
This function accepts:
- The content or path of the JavaScript file.
- The name of the variable to be extended.
- A single object name or a list of object names to be added.
- The type indicating whether it's content (
:content
) or a path (:path
).
It returns a tuple with the status, function atom, and the updated content or an error message if the variable could not be found or modified.
## Examples
alias IgniterJs.Parsers.Javascript.Parser
objects_names = ["OXCTestHook", "MishkaHooks", "MishkaHooks", "OXCTestHook"]
Parser.extend_var_object_by_object_names(js_content, "Components", "TestHook")
Parser.extend_var_object_by_object_names("/path/to/file.js", "Components", objects_names, :path)
{:error, :extend_var_object_by_object_names, _output} =
Parser.extend_var_object_by_object_names("None", "Components", objects_names)
Inserts a JavaScript AST at the specified index within an existing AST.
This function takes either a file path or raw JavaScript content, along with the JavaScript code to be inserted and the target index. It processes the AST and inserts the new code at the specified position.
alias IgniterJs.Parsers.Javascript.Parser
# Insert after index 1
Parser.insert_at_index(js_content, "function newFunc() {}", 1)
# Insert before all code
Parser.insert_at_index(js_content, "function newFunc() {}", 0)
# Insert using a file path
Parser.insert_at_index("/path/to/file.js", "function newFunc() {}", 2, :path)
Insert imports to the given file or content and returns tuple.
alias IgniterJs.Parsers.Javascript.Parser
Parser.insert_imports(js_content, imports_lines)
Parser.insert_imports(js_content, imports_lines, :content)
Parser.insert_imports("/path/to/file.js", imports_lines, :path)
Check if a module is imported in the given file or content or contents and return tuple.
alias IgniterJs.Parsers.Javascript.Parser
Parser.module_imported(js_content, "module")
Parser.module_imported(js_content, "module", :content)
Parser.module_imported("/path/to/file.js", "module", :path)
Check if a module is imported in the given file or content or content and returns boolean.
alias IgniterJs.Parsers.Javascript.Parser
Parser.module_imported?(js_content, "module")
Parser.module_imported?(js_content, "module", :content)
Parser.module_imported?("/path/to/file.js", "module", :path)
Remove imports from the given file or content. it accepts a single module or a list of modules. It returns a tuple.
alias IgniterJs.Parsers.Javascript.Parser
Parser.remove_imports(js_content, "SomeModule")
Parser.remove_imports("/path/to/file.js", "SomeModule", :path)
Remove objects from the hooks in the given file or content. It accepts a single o bject or a list of objects. It returns a tuple.
alias IgniterJs.Parsers.Javascript.Parser
Parser.remove_objects_from_hooks(js_content, "SomeObject")
Parser.remove_objects_from_hooks(js_content, ["SomeObject", "AnotherObject"], :content)
Parser.remove_objects_from_hooks("/path/to/file.js", "SomeObject", :path)
Replaces a JavaScript AST node at the specified index with new JavaScript code.
This function takes either a file path or raw JavaScript content, along with the JavaScript code that should replace an existing node at the given index. It processes the AST and replaces the target node while preserving the surrounding structure.
If the index
is out of bounds, an error is returned.
alias IgniterJs.Parsers.Javascript.Parser
# Replace node at index 1
Parser.replace_at_index(js_content, "function newFunc() {}", 1)
# Replace node at index 0 (first item)
Parser.replace_at_index(js_content, "function newFunc() {}", 0)
# Replace using a file path
Parser.replace_at_index("/path/to/file.js", "function newFunc() {}", 2, :path)
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
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)
Check if a specific var exists in the given file or content and returns boolean.
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)