Namespace modulify.utils
Namespace for modulify's utility functions.
Defined in: <src/modulify.js>.
Constructor Attributes | Constructor Name and Description |
---|---|
Namespace for modulify's utility functions.
|
Method Attributes | Method Name and Description |
---|---|
<static> |
modulify.utils.evaluator(src)
Wrapper for eval.
|
<static> |
modulify.utils.generateExportsString(ast, src, eol)
Generates a list of `module.exports` statements corresponding to the new
global properties, vars, and functions declared in the script.
|
<static> |
modulify.utils.generateModuleString(ast, src, eol)
Analyzes the given script and generates a module exporting all the new global
properties, vars, and functions from it.
|
<static> |
modulify.utils.getExports(ast, src)
Gets a list of new global properties, top level vars, and functions
declared in the script.
|
<static> |
modulify.utils.getGlobals(src)
Gets names of new properties declared in global scope after executing `src`.
|
<static> |
modulify.utils.getNames(prev, curr)
Reduction function to get declared variables and functions names from a node
in esprima's ast.
|
<static> |
modulify.utils.getTopLevelNames(ast)
Gets the names of functions and variables declared in the top scope.
|
Method Detail
<static>
modulify.utils.evaluator(src)
Wrapper for eval. **Caution** This function
runs the given script through eval so if the script is supposed to do things
like delete all your files, then it will.
- Parameters:
- {String} src
- The source code to evaluate.
<static>
{String}
modulify.utils.generateExportsString(ast, src, eol)
Generates a list of `module.exports` statements corresponding to the new
global properties, vars, and functions declared in the script.
**Caution** This function
runs the given script through eval so if the script is supposed to do things
like delete all your files, then it will.
var src = 'a = "global prop"; ' + 'var d = "I am a declared variable"; function e () {};'; var ast = esprima.parse(src, {tolerant: true}); var str = modulify.utils.generateExportsString(ast, src, '\r\n'); console.log(str);
- Parameters:
- {Object} ast
- The abstract syntax tree produced by esprima, or a compatible ast, must be the ast of the src.
- {String} src
- The source code to evaluate, must be the source used to produce the ast.
- {String} eol
- Optional. The end of line character to use. Defaults to '\r\n';
- Returns:
- {String} Returns a string representing `module.exports` statements so the given src may be turned into a module.
<static>
{String}
modulify.utils.generateModuleString(ast, src, eol)
Analyzes the given script and generates a module exporting all the new global
properties, vars, and functions from it. **Caution** This function
runs the given script through eval so if the script is supposed to do things
like delete all your files, then it will.
var src = 'a = "global prop"; ' + 'var d = "I am a declared variable"; function e () {};'; var ast = esprima.parse(src, {tolerant: true}); var str = modulify.utils.generateModuleString(ast, src, '\r\n'); console.log(str);
- Parameters:
- {Object} ast
- The abstract syntax tree produced by esprima, or a compatible ast, must be the ast of the src.
- {String} src
- The source code to evaluate, must be the source used to produce the ast.
- {String} eol
- Optional. The end of line character to use. Defaults to '\r\n';
- Returns:
- {String} Returns a script as a string. The script will be a commonjs module exporting the new global properties, vars, and functions declared in the top scope. The module exports statements will be wrapped in a try catch block so the script will continue to work in the browser and only export if `module.exports` is assignable.
<static>
{Array}
modulify.utils.getExports(ast, src)
Gets a list of new global properties, top level vars, and functions
declared in the script. **Caution** This function
runs the given script through eval so if the script is supposed to do things
like delete all your files, then it will.
var src = 'a = "global prop"; ' + 'var d = "I am a declared variable"; function e () {};'; var ast = esprima.parse(src, {tolerant: true}); var names = modulify.utils.getExports(ast, src); console.log(names);
- Parameters:
- {Object} ast
- The abstract syntax tree produced by esprima, or a compatible ast, must be the ast of the src.
- {String} src
- The source code to evaluate, must be the source used to produce the ast.
- Returns:
- {Array} Returns an array of names of new properties, functions and vars declared in the global / top scope.
<static>
{Array}
modulify.utils.getGlobals(src)
Gets names of new properties declared in global scope after executing `src`.
**Caution** This function
runs the given script through eval so if the script is supposed to do things
like delete all your files, then it will.
var src = 'a = "global prop"; ' + 'var d = "I am a declared variable"; function e () {};'; var globals = modulify.utils.getGlobals(src); console.log(globals);
- Parameters:
- {String} src
- The source code to evaluate.
- Returns:
- {Array} Returns an array of new properties declared in the global scope by the evaluated script.
<static>
{Array}
modulify.utils.getNames(prev, curr)
Reduction function to get declared variables and functions names from a node
in esprima's ast.
var src = 'a = "global prop"; ' + 'var d = "I am a declared variable"; function e () {};'; var ast = esprima.parse(src, {tolerant: true}); var names = ast.body.reduce(modulify.utils.getNames, []); console.log(names);
- Parameters:
- {Array} prev
- The previous value.
- {Object} curr
- The current object being inspected.
- Returns:
- {Array} Returns an array of the declared names.
<static>
{Array}
modulify.utils.getTopLevelNames(ast)
Gets the names of functions and variables declared in the top scope.
var src = 'a = "global prop"; ' + 'var d = "I am a declared variable"; function e () {};'; var ast = esprima.parse(src, {tolerant: true}); var names = modulify.utils.getTopLevelNames(ast); console.log(names);
- Parameters:
- {Object} ast
- The abstract syntax tree produced by esprima, or a compatible ast.
- Returns:
- {Array} Returns an array of names of functions and vars declared in the top scope.