npm

Share private packages across your team with npm Orgs, now with simplified billing via the aws marketplace!Learn more »

eselement

0.2.1 • Public • Published

eselement

npm version Build Status Coverage Status Dependency Status License

DOM-like wrapper around the Javascript AST

Install

$ npm install eselement --save

Example

// amd2cjs.js
// convert AMD modules to CommonJS.
var fs = require("fs");
 
var eselement = require("eselement");
 
var content = fs.readFileSync(process.argv[2])
  , program = eselement.createElement(content);
 
var define = program.querySelector("CallExpression[callee.name='define']");
 
if (define) {
  var arr = define.arguments[0]
      , fn = define.arguments[1];
 
  var libs = arr.elements.map(function (e) { return e.value })
    , names = fn.params.map(function (p) { return p.name; })
    , body = fn.body.body;
 
  define.parentElement.removeChild(define);
 
  libs.forEach(function (lib, i) {
    var el = names[i] ? eselement.createElement("var " + names[i] + " = require('" + lib + "')")
                        : eselement.createElement("require('" + lib + "')");
    program.appendChild("body", el);
  });
 
  program.appendChild("body", body);
 
  console.log(program.outerCode());
}
$ cat amd_module.js
define(["crypto"], function (crypto) {
  var ciphers = crypto.getCiphers();
  console.log(ciphers);
});
 
$ node amd2cjs.js amd_module.js
var crypto = require('crypto');
var ciphers = crypto.getCiphers();
console.log(ciphers);

API

createElement(string || object)

Creates and element from either Javascript code or Mozilla Parser AST.

Element

Attributes

All attributes from the AST are copied onto the Element, for example an element of type Program will have an attribute body.

Element.parentElement

A reference to the Elements parent, if it has no parent it's null.

Element.parentAttribute

A string denoting the attribute on the parent in which this element is in.

Methods

Element.childElements()

Get all children of an element.

Element.firstChild()

Get the first child of an element.

Element.lastChild()

Get the last child of an element.

Element.querySelectorAll(selector)

Select elements using esquery.

Element.querySelector(selector)

Select the first matching element using esquery.

Element.setAttribute(attribute, value)

Sets the attribute attribute to value. Does checking to see if value is of the correct type for attribute.

Element.appendChild(attribute, child)

Append child to element attribute attribute.

Element.removeChild(child)

Remove child from element.

Element.replaceChild(newChild, oldChild)

Replace oldChild with newChild in element.

Element.cloneElement()

Return a clone of element.

Element.outerAST()

Return the AST for element.

Element.outerCode()

Return the javascript code for element.

install

npm i eselement

Downloadsweekly downloads

5

version

0.2.1

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability