rse / astq

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

Typings #9

Closed cancerberoSgx closed 5 years ago

cancerberoSgx commented 5 years ago

https://github.com/rse/astq/issues/8

import ASTQ from 'astq'

class Node {
  constructor(public nodeType: string, 
    public attrs: { [name: string]: any } = {}, public childNodes: Node[] = []) {
      childNodes.forEach(c=>{
        c.parentNode = this
      })
  }
  parentNode: Node|undefined
  get outerHTML(): string{
    return `<${this.nodeType} ${Object.keys(this.attrs).map(k=>`${k}="${this.attrs[k]}"`).join(' ')}>${this.childNodes.map(c=>c.outerHTML).join('')}</${this.nodeType}>`
  }
}

var astq = new ASTQ<Node>()

astq.adapter({
  taste(node: any) {
    return node && typeof node.nodeType==='string' && node.attrs && Array.isArray(node.childNodes)
  },
  getParentNode(node: Node) {
    return node && node.parentNode
  },
  getChildNodes(node: Node) {
    return node && node.childNodes
  },
  getNodeType(node: Node) {
    return node && node.nodeType 
  },
  getNodeAttrNames(node: Node) {
    return node && Object.keys(node.attrs||{})
  },
  getNodeAttrValue(node: Node, attr: string) {
    return node && node.attrs&& node.attrs[attr]
  }
})

const doc1 = new Node('document', {foo: 'bar'}, [
  new Node('body', {id: 'id33', style: {border: '2px solid pink'}}, [
    new Node('text', {textContent: 'TODO list'}),
    new Node('ul', {}, [
      new Node('li',{textContent: 'Do the dishes'}, []),
      new Node('li',{textContent: 'Backup photos'}, [])
    ])
  ])
])

let q = '// li '
console.log('RESULTS for query ', q);
astq.query(doc1, q).forEach(n=> {
  console.log(' * ' + n.outerHTML)
})

I think these kind of examples, perhaps as tests or wiki pages would be of great help to newcomers (like me) to understand the API , implement new adapters, etc. Would you accept such kind of contributions , and if so, which would be the best format ? I think it could be done as tests, but before I would like you opinion,

Thanks!

cancerberoSgx commented 5 years ago

I?m closing this PR since my branch get mixed up with another with features, will try to open another with just the typings. IN the meanwhile the final typings are here: https://github.com/cancerberoSgx/astq/blob/master/astq.d.ts Thanks