Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
replace-method
Advanced tools
JavaScript post-processing step to replace specific function/method calls with other bits of JavaScript
JavaScript post-processing step to replace specific function/method calls with
other bits of JavaScript. It doesn't take scope into account, but is otherwise
a good starting point for writing your own inlinifying transform modules like
brfs
.
replace = require('replace-method')(ast)
Returns a function you can use to replace methods with other things.
Where ast
is either a string of source code, or an AST object such as one
generated by esprima.
replace(method, found)
Where method
is an array of variable names, such that:
['__filename']
will match __filename
.['fs', 'readFileSync']
will match fs.readFileSync
.['a', 'nested', 'property']
will match a.nested.property
.found
is then called once per every node in the AST found – if you return
nothing or undefined
, nothing will change. However, if you return an AST
object that content will be replaced, e.g.:
return {
type: 'Literal',
value: 'hello world'
}
Will replace the matched variable with the string "hello world"
.
replace.code()
Returns the updated version of your code after having made the transformations
you needed. Note that the supplied ast
object you provided will be modified
directly so you can convert it to JavaScript yourself or hand it off to other
processing steps if need be.
var replaceMethod = require('replace-method')
var evaluate = require('static-eval')
var escodegen = require('escodegen')
var esprima = require('esprima')
var fs = require('fs')
var src = replaceMethod(
fs.readFileSync(__filename, 'utf8')
)
src.replace(['fs', 'readFileSync'], function(node) {
if (!node.arguments.length) return
var filename = evaluate(node.arguments[0], {
__filename: __filename
, __dirname: __dirname
})
if (filename) return {
type: 'Literal'
, value: fs.readFileSync(filename, 'utf8')
}
})
console.log(src.code())
MIT. See LICENSE.md for details.
FAQs
JavaScript post-processing step to replace specific function/method calls with other bits of JavaScript
We found that replace-method demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.