gkz / grasp

JavaScript structural search, replace, and refactor
http://graspjs.com
MIT License
1.28k stars 33 forks source link

Replace with .l or .left does not work #103

Closed kristianmandrup closed 7 years ago

kristianmandrup commented 7 years ago

Been trying various ways from the docs

grasp.replace('equery', '__ + __', '{{.left}} - {{.right}}')

(input): Error during replacement. Error processing selector '.left'

grasp.replace('equery', '__ + __', '{{.l}} - {{.r}}')

(input): Error during replacement. Error processing selector '.l'

Where can I find examples that are NOT command line?


import test from 'ava'
import grasp from 'grasp'

let code = `var a = 1 + 2`

const replacer = grasp.replace('equery', '__ + __', '{{.left}} - {{.right}}')
let processed = replacer(code)

const log = console.log

test('replace + with -', t => {
  log('code x', processed)
  t.is(processed, `var a = 1 - 2`)
})
kristianmandrup commented 7 years ago

I finally made it work like this:

let code = `var a = 1 + 2`
const find = '$x + $y'
const replace = '{{x}} - {{y}}'

const replacer = grasp.replace('equery', find, replace)
let processed = replacer(code)

But please, oh please add better docs with real examples such as rename, add or remove an identifier node such as a function or variable or the same within a class.

Cheers!