grasp icon indicating copy to clipboard operation
grasp copied to clipboard

Please add better usage examples

Open mandricore opened this issue 8 years ago • 2 comments

I'm trying to make the following work, taking the examples from your main docs on lib usage

This works

import test from 'ava'
import grasp from 'grasp'
import _ from 'lodash'
import path from 'path'

const reqCode = `var Abe = require('abe')`
const log = console.log

let processed = grasp.replace('squery', 'call[callee=#require]', (getRaw, node, query) => {
  let req = query('.args')[0]
  let importCode = 'import ' + _.camelCase(path.basename(req.value, '.js')) + ' from ' + getRaw(req)
  log('import code', importCode)
  return importCode
}, reqCode)

test('replace require with import', t => {
  log('code y', processed)
  t.is(processed, `var Abe = import abe from 'abe'`)
})

But here I get an error

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

let code = `var a = 1 + 2`

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

const log = console.log

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

Why is .l not working? deprecated?

Test runner result:

(input): Error during replacement. Error processing selector '.l'..
code x undefined```

Please advice

mandricore avatar Dec 05 '16 11:12 mandricore

You are mixing equery selector with squery replacement selector. There is not option allowing you to mix them at the moment. If you want to use equery to select, you must use its replacement syntax. Eg.

const grasp = require("grasp");
let code = `var a = 1 + 2`;

const replacer = grasp.replace('equery', '$a + $b', '{{a}} - {{b}}');
let processed = replacer(code);
console.log(processed);

.

gkz avatar Dec 06 '16 07:12 gkz

Actually, I will keep this open as a documentation issue

gkz avatar Dec 06 '16 07:12 gkz