benjamn / ast-types

Esprima-compatible implementation of the Mozilla JS Parser API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Implement a flexible pattern-matching syntax

benjamn opened this issue · comments

Just as we currently have .namedTypes.MemberExpression and .builders.memberExpression, perhaps we could also have an auto-generated .patterns namespace containing functions like .patterns.MemberExpression for building arbitrary pattern-matching types, e.g.

var types = require("ast-types");
var p = types.patterns;

var thisFooPattern = p.MemberExpression(
  p.ThisExpression(),
  p.Identifier("foo")
);

types.traverse(ast, function(node) {
  if (thisFooPattern.check(node)) {
    console.log("found this.foo:", node);
  }
});

Note that p.ThisExpression() should probably just return types.namedTypes.ThisExpression. In other words, patterns are ultimately just types.Type instances.