{"_id":"replace-method","_rev":"92695","name":"replace-method","description":"JavaScript post-processing step to replace specific function/method calls with other bits of JavaScript","dist-tags":{"latest":"0.0.0"},"maintainers":[{"name":"hughsk","email":"hughskennedy@gmail.com"}],"time":{"modified":"2021-06-03T10:30:42.000Z","created":"2014-02-05T04:02:03.794Z","0.0.0":"2014-02-05T04:02:03.794Z"},"users":{},"author":{"name":"Hugh Kennedy","email":"hughskennedy@gmail.com","url":"http://hughsk.io/"},"repository":{"type":"git","url":"git://github.com/hughsk/replace-method"},"versions":{"0.0.0":{"name":"replace-method","description":"JavaScript post-processing step to replace specific function/method calls with other bits of JavaScript","version":"0.0.0","main":"index.js","browser":"index.js","dependencies":{"recast":"~0.5.7"},"devDependencies":{"faucet":"0.0.0"},"scripts":{"test":"faucet test.js"},"author":{"name":"Hugh Kennedy","email":"hughskennedy@gmail.com","url":"http://hughsk.io/"},"license":"MIT","repository":{"type":"git","url":"git://github.com/hughsk/replace-method"},"bugs":{"url":"https://github.com/hughsk/replace-method/issues"},"homepage":"https://github.com/hughsk/replace-method","keywords":["replace","method","call","post-processing","language","macro","transform","meta","inline","inlining","brfs"],"_id":"replace-method@0.0.0","dist":{"shasum":"3704d10711c2d461db0291355061053cee83faf9","size":3338,"noattachment":false,"key":"/replace-method/-/replace-method-0.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/replace-method/download/replace-method-0.0.0.tgz"},"_from":".","_npmVersion":"1.3.25","_npmUser":{"name":"hughsk","email":"hughskennedy@gmail.com"},"maintainers":[{"name":"hughsk","email":"hughskennedy@gmail.com"}],"directories":{},"publish_time":1391572923794,"_cnpm_publish_time":1391572923794,"_hasShrinkwrap":false}},"readme":"# replace-method [![Flattr this!](https://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=hughskennedy&url=http://github.com/hughsk/replace-method&title=replace-method&description=hughsk/replace-method%20on%20GitHub&language=en_GB&tags=flattr,github,javascript&category=software)[![experimental](http://hughsk.github.io/stability-badges/dist/experimental.svg)](http://github.com/hughsk/stability-badges) #\n\nJavaScript post-processing step to replace specific function/method calls with\nother bits of JavaScript. It doesn't take scope into account, but is otherwise\na good starting point for writing your own inlinifying transform modules like\n[`brfs`](http://github.com/substack/brfs).\n\n## Usage ##\n\n[![replace-method](https://nodei.co/npm/replace-method.png?mini=true)](https://nodei.co/npm/replace-method)\n\n### `replace = require('replace-method')(ast)` ###\n\nReturns a function you can use to replace methods with other things.\n\nWhere `ast` is either a string of source code, or an AST object such as one\ngenerated by [esprima](http://github.com/ariya/esprima/).\n\n### `replace(method, found)` ###\n\nWhere `method` is an array of variable names, such that:\n\n* `['__filename']` will match `__filename`.\n* `['fs', 'readFileSync']` will match `fs.readFileSync`.\n* `['a', 'nested', 'property']` will match `a.nested.property`.\n\n`found` is then called once per every node in the AST found – if you return\nnothing or `undefined`, nothing will change. However, if you return an AST\nobject that content will be replaced, e.g.:\n\n``` javascript\nreturn {\n  type: 'Literal',\n  value: 'hello world'\n}\n```\n\nWill replace the matched variable with the string `\"hello world\"`.\n\n### `replace.code()` ###\n\nReturns the updated version of your code after having made the transformations\nyou needed. Note that the supplied `ast` object you provided will be modified\ndirectly so you can convert it to JavaScript yourself or hand it off to other\nprocessing steps if need be.\n\n## Example ##\n\n``` javascript\nvar replaceMethod  = require('replace-method')\nvar evaluate       = require('static-eval')\nvar escodegen      = require('escodegen')\nvar esprima        = require('esprima')\nvar fs             = require('fs')\n\nvar src = replaceMethod(\n  fs.readFileSync(__filename, 'utf8')\n)\n\nsrc.replace(['fs', 'readFileSync'], function(node) {\n  if (!node.arguments.length) return\n\n  var filename = evaluate(node.arguments[0], {\n      __filename: __filename\n    , __dirname:  __dirname\n  })\n\n  if (filename) return {\n      type: 'Literal'\n    , value: fs.readFileSync(filename, 'utf8')\n  }\n})\n\nconsole.log(src.code())\n```\n\n## License ##\n\nMIT. See [LICENSE.md](http://github.com/hughsk/replace-method/blob/master/LICENSE.md) for details.\n","_attachments":{},"homepage":"https://github.com/hughsk/replace-method","bugs":{"url":"https://github.com/hughsk/replace-method/issues"},"license":"MIT"}