Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

deep-ast

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

deep-ast

Get an AST with require statements resolved into respective ASTs

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
Maintainers
1
Weekly downloads
 
Created
Source

deep-ast Travis branch

Get an AST with require statements resolved into respective ASTs

NOTE: This module uses synchronous I/O and should not be used at request time in a service.

API

readFile : (
  filename: String,
  options?: {
    includeExternalDependencies?: Boolean(true)
  }
) => Object

Passing includeExternalDependencies will cause the ASTs of 3rd party modules to also be parsed. Note that this is a recursive algorithm, so the dependencies of each depdendency will also be parsed. As a warning, setting this flag to true can produce extremely large ASTs.

Usage

var deepAST = require('deep-ast'); var AST = deepAST('./myModule.js');

Examples

Take the file structure where module 'a' requires module 'b' like so:

b.js:

module.exports = 'panic';

a.js:

var b = require('./b');
module.exports = 'dont';

The ASTs of these two modules (generated by esprima would be):

b.js:

{
  "type": "Program",
  "body": [{
    "type": "ExpressionStatement",
    "expression": {
      "type": "AssignmentExpression",
      "operator": "=",
      "left": {
        "type": "MemberExpression",
        "computed": false,
        "object": {
          "type": "Identifier",
          "name": "module"
        },
        "property": {
          "type": "Identifier",
          "name": "exports"
        }
      },
      "right": {
        "type": "Literal",
        "value": "panic",
        "raw": "'panic'"
      }
    }
  }]
}

a.js:

{
  "type": "Program",
  "body": [{
    "type": "VariableDeclaration",
    "declarations": [{
      "type": "VariableDeclarator",
      "id": {
        "type": "Identifier",
        "name": "b"
      },
      "init": {
        "type": "CallExpression",
        "callee": {
          "type": "Identifier",
          "name": "require"
        },
        "arguments": [{
            "type": "Literal",
            "value": "./b",
            "raw": "'./b'"
        }]
      }
    }],
    "kind": "var"
  },
  {
    "type": "ExpressionStatement",
    "expression": {
      "type": "AssignmentExpression",
      "operator": "=",
      "left": {
        "type": "MemberExpression",
        "computed": false,
        "object": {
          "type": "Identifier",
          "name": "module"
        },
        "property": {
          "type": "Identifier",
          "name": "exports"
        }
      },
      "right": {
        "type": "Literal",
        "value": "dont",
        "raw": "'dont'"
      }
    }
  }]
}

Running deep-ast on a.js will result in the require statement for 'b' being replaced with the syntax tree which is generated from running 'b' through deep-ast. Or:

deep-ast of a (b) === deep-ast of b

Like so:

{
  "type": "Program",
  "body": [{
    "type": "VariableDeclaration",
    "declarations": [{
      "type": "VariableDeclarator",
      "id": {"type": "Identifier", name: "b"},
      "init": {
        "type": "Program",
        "body": [{
          "type": "ExpressionStatement",
          "expression": {
            "type": "AssignmentExpression",
            "operator": "=",
            "left": {
              "type": "MemberExpression",
              "computed": false,
              "object": {"type": "Identifier", "name": "module"},
              "property": {"type": "Identifier", "name": "exports"}
            },
            "right": {"type": "Literal", "value": "panic", "raw": "panic"}
          }
        }]
      }
    }],
    "kind": "var"
  },
  {
    "type": "ExpressionStatement",
    "expression": {
      "type": "AssignmentExpression",
      "operator": "=",
      "left": {
        "type": "MemberExpression",
        "computed": false,
        "object": {"type": "Identifier", "name": "module"},
        "property": {"type": "Identifier", "name": "exports"}
      },
      "right": {"type": "Literal", "value": "dont", "raw": "dont"}
    }
  }]
}

FAQs

Package last updated on 17 Mar 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc