rse / astq

Abstract Syntax Tree (AST) Query Engine
https://npmjs.com/astq
201 stars 15 forks source link

Your readme's example is incorrect #5

Closed mrpotatoes closed 5 years ago

mrpotatoes commented 5 years ago

Not sure if this is still activly maintained but your example does not work.

I modified it slightly to log the entire object and I can't get anything no matter what selector I use.

const acorn = require("acorn")
const ASTQ = require("astq")

let source = `
    class Foo {
        foo () {
            const bar = "quux"
            let baz = 42
        }
    }
`

let ast = acorn.parse(source, { ecmaVersion: 6 })

// console.log(JSON.stringify(ast, null, '  '))

let astq = new ASTQ()
const hey = astq.query(ast, astq.query(ast, `
    // VariableDeclarator [
           /:id   Identifier [ @name  ]
        && /:init Literal    [ @value ]
    ]
`)
// .forEach(function (node) {
//   console.log(`${node.id.name}: ${node.init.value}`)
// })

console.log('hey', hey)
// This also doesn't work
const hey = astq.query(ast, astq.query(ast, `Program`)
rse commented 5 years ago

There was the configuration of the adapter missing. Now fixed. See the commit https://github.com/rse/astq/commit/0089a949b6c875481296877138b80d934a12ed4a