How to use the types function from recast

Find comprehensive JavaScript recast.types code examples handpicked from public code repositorys.

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 => {
fork icon2
star icon11
watch icon0

+ 3 other calls in file

17
18
19
20
21
22
23
24
25
26

const ast = recast.parse(code, {
    parser: require("recast/parsers/babylon")
  });

const b = recast.types.builders;

recast.visit(ast.program.body, {
    visitExportNamedDeclaration(path) {
        const node = path.node
fork icon0
star icon2
watch icon0

5
6
7
8
9
10
11
12
13
14
15
16
// are not correctly handled at this point, and will trigger an assertion
// failure if encountered.


var assert = require("assert");
var recast = require("recast");
var types = recast.types;
var n = types.namedTypes;
var b = types.builders;


recast.run(function(ast, callback) {
fork icon0
star icon1
watch icon0

231
232
233
234
235
236
237
238
239
240
241
const add = ast.program.body[0].type
log(add)
const add = ast.program.body.BlockStatement.body[0]
log(add)


const { variableDeclaration, variableDeclarator, functionExpression } = recast.types.builders
ast.program.body[0] = variableDeclaration('const', [
  variableDeclarator(add.id, functionExpression(
    null, Anonymize the function expression
     add.params,
fork icon0
star icon0
watch icon1

+ 33 other calls in file

-4
fork icon0
star icon0
watch icon2

10
11
12
13
14
15
16
17
18
19
20
const assert = require('assert');
const intersection = require('./utils/intersection');
const recast = require('recast');
const union = require('./utils/union');


const astTypes = recast.types;
var types = astTypes.namedTypes;
const NodePath = astTypes.NodePath;
const Node = types.Node;

fork icon0
star icon0
watch icon1

6
7
8
9
10
11
12
13
14
15
16
  callExpression,
  blockStatement,
  arrowFunctionExpression,
  expressionStatement
} = recast.types.builders
const t = recast.types.namedTypes


// function printSource (source) {
//   console.log(recast.print(source).code)
// }
fork icon0
star icon0
watch icon0

+ 9 other calls in file

15
16
17
18
19
20
21
22
23
24
25
26
const n = recast.types.namedTypes;
n.FunctionDeclaration.assert(add);


// If you choose to use recast.builders to construct new AST nodes, all builder
// arguments will be dynamically type-checked against the Mozilla Parser API.
const B = recast.types.builders;


// This kind of manipulation should seem familiar if you've used Esprima or the
// Mozilla Parser API before.
ast.program.body[0] = B.variableDeclaration("const", [
fork icon0
star icon0
watch icon0

+ 5 other calls in file

4
5
6
7
8
9
10
11
12
13
14
15
 * @visit AST节点遍历
 */


const recast = require('recast')
// TNT判断ast的数据类型
const TNT = recast.types.namedTypes


recast.run(function(ast,printSource){
    //printSource(ast)
    recast.visit(ast,{
fork icon0
star icon0
watch icon0

58
59
60
61
62
63
64
65
66
67
 console.log('\n\nstep4:')
 printSource(expressionStatement(assignmentExpression('=',memberExpression(id('exports'),id('add')),arrowFunctionExpression([],blockStatement([])))))
 */
let funcIds = []
//遍历单个函数调用并进行替换
recast.types.visit(ast,{
 visitFunctionDeclaration(path){
     const node = path.node
     const funcName = node.id
     const params = node.params
fork icon0
star icon0
watch icon0

3
4
5
6
7
8
9
10
11
12
// 针对 npm 包转换。 这里就是 "/__modules/vue"
// import Vue from 'vue' => import Vue from "/__modules/vue"
// 这段代码最终返回的是读取路径 vue-dev-server/node_modules/vue/dist/vue.esm.browser.js 下的文件
function transformModuleImports(code) {
  const ast = recast.parse(code)
  recast.types.visit(ast, {
    visitImportDeclaration(path) {
      const source = path.node.source.value
      if (!/^\.\/?/.test(source) && isPkg(source)) {
        path.node.source = recast.types.builders.literal(`/__modules/${source}`)
fork icon0
star icon0
watch icon0

+ 3 other calls in file

10
11
12
13
14
15
16
17
18
19
20
21
22


const ast = recast.parse(code)
const add = ast.program.body[0]


const { variableDeclaration, variableDeclarator, functionExpression } =
  recast.types.builders


ast.program.body[0] = variableDeclaration('const', [
  variableDeclarator(add.id, functionExpression(null, add.params, add.body)),
])
fork icon0
star icon0
watch icon0