Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please add better usage examples #102

Open
mandricore opened this issue Dec 5, 2016 · 2 comments
Open

Please add better usage examples #102

mandricore opened this issue Dec 5, 2016 · 2 comments
Labels

Comments

@mandricore
Copy link

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
@gkz
Copy link
Owner

gkz commented Dec 6, 2016

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 gkz closed this as completed Dec 6, 2016
@gkz
Copy link
Owner

gkz commented Dec 6, 2016

Actually, I will keep this open as a documentation issue

@gkz gkz reopened this Dec 6, 2016
@gkz gkz added the docs label Dec 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants