Code Monkey home page Code Monkey logo

Comments (3)

tolmasky avatar tolmasky commented on August 25, 2024

I suppose even this wouldn't be sufficient since MemberExpression's property could be an identifier, but the identifier behaves like an expression only if computed is true. It seems like every node simply needs to be special cased which is a bit strange.

from ast-types.

benjamn avatar benjamn commented on August 25, 2024

In the specific case of Identifier nodes, the ast-util project has a utility function for determining if the identifier is a reference: https://github.com/eventualbuddha/ast-util#user-content-isReference. And yeah, it is pretty tricky.

from ast-types.

benjamn avatar benjamn commented on August 25, 2024

In general, if you're writing a visitor, you can use the NodePath to inspect the parent node(s):

var MemberExpression = types.namedTypes.MemberExpression;
types.visit(ast, {
  visitIdentifier: function(path) {
    this.traverse(path);

    var node = path.node;
    var parentNode = path.parent.node;

    if (MemberExpression.check(parentNode) &&
        path.name === "property" &&
        !parentNode.computed) {
      // Do something with identifiers that are not member expression .properties.
    }
  }
});

Since nodes can appear multiple times in the same AST, there's no safe way for AST nodes themselves to have a .parent property. Instead, you have to keep track of the ancestor path as you traverse, which is what NodePath is for :)

Specifically, path is a NodePath object, path.node is the current node, path.parent is a NodePath for the parent node, path.parent.node is the parent node, path.parent.parent.node is the grandparent node, and so on.

The path.name property is useful for pathological (punny?) cases where you can't tell children apart using ===:

var b = types.builders;
var id = b.identifier("foo");

// The expression foo.foo:
var mx = b.memberExpression(id, id, false);

assert.strictEqual(mx.object, mx.property);

var path = new NodePath(mx);
var objPath = path.get("object");
var propPath = path.get("property");

assert.strictEqual(objPath.node, id);
assert.strictEqual(propPath.node, id);

assert.strictEqual(objPath.name, "object");
assert.strictEqual(propPath.name, "property");

from ast-types.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.