How to use recast

Comprehensive recast code examples:

How to use recast.mixVisitors:

39
40
41
42
43
44
45
46
47
48
49
50
    return recast.chainVisitors(transformer1, transformer2)
  }
};


// the visitors object should define cases where a collision otherwise occurs
var visitors = recast.mixVisitors(resolveVisitorConflicts);


// alternatively, you can run two visitors per node one after the other:
var altVisitors = recast.chainVisitors(transformer1, transformer2);

How to use recast.visitMixed:

45
46
47
48
49
50
51
52
53
54
55
56


// alternatively, you can run two visitors per node one after the other:
var altVisitors = recast.chainVisitors(transformer1, transformer2);


function transform(ast) {
  var visitedAst = recast.visitMixed(ast, visitors);
  return visitedAst;
}
module.exports = {
  transform: transform,

How to use recast.eachField:

31
32
33
34
35
36
37
38
39
40
    result = predicate(child);
  }
  return result;
}

types.eachField(node, function(name, child) {
  check(child);
});

return result;

How to use recast.genParse:

50
51
52
53
54
55
56
57
58
  var visitedAst = recast.visitMixed(ast, visitors);
  return visitedAst;
}
module.exports = {
  transform: transform,
  parse: recast.genParse(transform),
  compile: recast.genCompile(transform),
  visitors: visitors
};

How to use recast.genCompile:

51
52
53
54
55
56
57
58
  return visitedAst;
}
module.exports = {
  transform: transform,
  parse: recast.genParse(transform),
  compile: recast.genCompile(transform),
  visitors: visitors
};

How to use recast.NodePath:

614
615
616
617
618
619
620
621
622
623
    caseLocs[i] = defaultLoc;
  }
}

self.jump(self.explodeExpression(
  new types.NodePath(condition, path, "discriminant")
));

self.leapManager.withEntry(
  new leap.SwitchEntry(after),

How to use recast.builders:

How to use recast.builtInTypes:

7
8
9
10
11
12
13
14
15
16
17
 * the same directory.
 */


var assert = require("assert");
var types = require("recast").types;
var isArray = types.builtInTypes.array;
var b = types.builders;
var n = types.namedTypes;
var leap = require("./leap");
var meta = require("./meta");

How to use recast.chainVisitors:

30
31
32
33
34
35
36
37
38
39
40


var resolveVisitorConflicts = function(node, nodeName) {
  // n.nodeName.check should be sufficient, i'm not sure if having `nodeName`
  // as a parameter is needed or a good idea
  if (n.functionExpression.check(node)) {
    return recast.chainVisitors(transformer2, transformer1 /*[, etc.]*/)
  } else if(n.identifier.check(node)) {
    return null; // return non-function in order to do nothing for a node type
  } else { // default behavior
    return recast.chainVisitors(transformer1, transformer2)

How to use recast.prettyPrint:

239
240
241
242
243
244
245
246
247
248
    null, Anonymize the function expression
     add.params,
    add.body
  ))
])
const output = recast.prettyPrint(ast, { tabWidth: 2 }).code
log(output)
log(output(1, 3))
recast.run(function (ast, printSource) {
  printSource(ast)

How to use recast.namedTypes:

How to use recast.types:

8
9
10
11
12
13
14
15
16
17
18
19
20
  normalizeClassDefinition,
  resolveExportDeclaration,
  resolveToValue,
} = utils;


const n = recast.types.namedTypes;


const ERROR_MULTIPLE_DEFINITIONS = 'Multiple exported component definitions found.';


const isReactComponentExtendedClass = path => {

How to use recast.visit:

106
107
108
109
110
111
112
113
114

  definition = resolveDefinition(definitions[0]);
  return false;
};

recast.visit(ast, {
  visitExportDeclaration: exportDeclaration,
  visitExportNamedDeclaration: exportDeclaration,
  visitExportDefaultDeclaration: exportDeclaration,

How to use recast.print:

26
27
28
29
30
31
32
33
34
35
  sourceMapName: filename + '.map'
};

var ast = recast.parse(source, recastOptions);
ast = es6defaultParams.transform(es6restParams.transform(es6class.transform(ast)));
var result = recast.print(ast, recastOptions);

fs.writeFileSync(path.join(RESULTS, testName + '.js'), result.code, 'utf8');
fs.writeFileSync(path.join(RESULTS, testName + '.js.map'), JSON.stringify(result.map), 'utf8');
return result.code;

How to use recast.parse:

55
56
57
58
59
60
61
62
63
64
65
  return result;
}


const parseOptions = {
  parser: {
    parse(src) {
      return flowParser.parse(src, {
        // Comments can't cause UI strings to appear in the app; ignore them.
        all_comments: false,
        comments: false,