/usr/lib/nodejs/ast-util/bin/make-builder is in node-ast-util 0.6.0-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | #!/usr/bin/nodejs
/* jshint node:true, unused:true, undef:true */
var recast = require('recast');
var types = recast.types;
var b = types.builders;
var n = types.namedTypes;
var assert = require('assert');
var vm = require('vm');
var BUILD_PARAMS_CACHE = {
'ThisExpression': [],
'BreakStatement': []
};
/**
* Get the named parameters to pass the named builder.
*
* @param {!string} type
* @return {[string]}
*/
function buildParamsForType(type) {
var entry = BUILD_PARAMS_CACHE[type];
if (entry) {
return entry;
}
try {
b[builderNameForType(type)]();
assert.ok(false, 'should have failed to build ' + type + ' with no params');
} catch (ex) {
var message = ex.message;
var typeIndex = message.indexOf(type);
var openParenIndex = message.indexOf('(', typeIndex);
var closeParenIndex = message.indexOf(')', openParenIndex);
assert.ok(
closeParenIndex >= 0,
'unexpected exception format trying to parse build params: ' + message
);
var paramsList = message.slice(openParenIndex, closeParenIndex);
var result = [];
paramsList.replace(/"([^"]+)"/g, function(_, name) {
result.push(name);
});
BUILD_PARAMS_CACHE[type] = result;
return result;
}
}
/**
* Get the name of the builder given a node type. For example, "ThisExpression"
* becomes "thisExpression".
*
* @param {!string} type
* @return {!string}
*/
function builderNameForType(type) {
return type[0].toLowerCase() + type.slice(1);
}
/**
* Read all the contents of STDIN and call back with it.
*
* @param {function(!string)} callback
*/
function readStdin(callback) {
var stdin = '';
process.stdin.setEncoding('utf8');
process.stdin.on('readable', function() {
var chunk = process.stdin.read();
if (chunk !== null) {
stdin += chunk;
}
});
process.stdin.on('end', function() {
callback(stdin);
});
}
/**
* Makes the AST for a JavaScript program that will build the given node using
* the builder functions from the ast-types package.
*
* @param {ast-types.Node} node
* @param {{string: string}} replacements
* @return {ast-types.Node}
*/
function makeBuilder(node, replacements) {
if (n.File.check(node)) {
return b.expressionStatement(makeBuilder(node.program, replacements));
} else if (n.Program.check(node)) {
return b.callExpression(
b.memberExpression(
b.identifier('b'),
b.identifier('program'),
false
),
[b.arrayExpression(node.body.map(function(statement) {
return makeBuilder(statement, replacements);
}))]
);
} else if (Array.isArray(node)) {
return b.arrayExpression(
node.map(function(item) { return makeBuilder(item, replacements); })
);
} else if (node && node.constructor === RegExp) {
return b.literal(node);
}
assert.ok(
n.Node.check(node),
'unexpected node type: ' + JSON.stringify(node)
);
if (n.Identifier.check(node)) {
var replacement = replacements[node.name];
if (replacement) {
var newReplacements = Object.create(replacements);
newReplacements[node.name] = undefined;
return recast.parse(replacement).program.body[0].expression;
}
}
return b.callExpression(
b.memberExpression(
b.identifier('b'),
b.identifier(node.type[0].toLowerCase() + node.type.slice(1)),
false
),
buildParamsForType(node.type).reduce(function(result, key) {
if (key !== 'type' && key !== 'loc') {
var value;
if (node[key] !== null && typeof node[key] === 'object') {
value = makeBuilder(node[key], replacements);
} else if (node[key] !== undefined) {
value = b.literal(node[key]);
}
if (value) {
result.push(value);
}
}
return result;
}, [])
);
}
/**
* Determines whether, when printed, the node should be on multiple lines.
*
* @param {ast-types.Node}
* @param {boolean}
*/
function isMultiline(node) {
switch (node.type) {
case 'ExpressionStatement':
return isMultiline(node.expression);
case 'CallExpression':
return node.arguments.length > 1 || node.arguments.some(isMultiline);
case 'MemberExpression':
return isMultiline(node.object) || isMultiline(node.property);
case 'Identifier':
return false;
case 'ArrayExpression':
return node.elements.length > 1 || (node.elements.length === 1 && isMultiline(node.elements[0]));
case 'Literal':
return (node.raw || JSON.stringify(node.value)).indexOf('\n') >= 0;
default:
throw new Error('unexpected node type: ' + node.type);
}
}
/**
* @const
*/
var INDENT = ' ';
/**
* Prints the given list of AST nodes as JavaScript as part of a list of array
* elements or function arguments.
*
* @param {[ast-types.Node]} list
* @param {string=} indent
*/
function printListLines(list, indent) {
if (!indent) { indent = ''; }
var output = '';
list.forEach(function(item, i) {
output += indent + print(item, indent);
if (i !== list.length - 1) {
output += ',';
}
output += '\n';
});
return output;
}
/**
* Prints the given AST node as JavaScript, formatted so as to favor shorter
* lines. For example, this:
*
* b.callExpression(b.identifier('a'), [b.literal(1), b.literal(2)]);
*
* Would be printed as:
*
* b.callExpression(
* b.identifier('a'),
* [
* b.literal(1),
* b.literal(2)
* ]
* )
*
* @param {ast-types.Node} node
* @param {string=} indent
* @return {string}
*/
function print(node, indent) {
if (!indent) { indent = ''; }
switch (node.type) {
case 'ExpressionStatement':
return print(node.expression, indent) + ';';
case 'CallExpression':
if (isMultiline(node)) {
if (node.arguments.length === 1 && node.arguments[0].type === 'ArrayExpression') {
return print(node.callee, indent) + '([\n' +
printListLines(node.arguments[0].elements, indent + INDENT) +
indent + '])';
} else {
return print(node.callee, indent) + '(\n' +
printListLines(node.arguments, indent + INDENT) +
indent + ')';
}
}
return print(node.callee, indent) + '(' +
node.arguments.map(function(arg) { return print(arg, indent); }).join(', ') +
')';
case 'MemberExpression':
if (node.computed) {
return print(node.object, indent) + '[' + print(node.property, indent) + ']';
} else {
return print(node.object, indent) + '.' + print(node.property, indent);
}
break;
case 'Identifier':
return node.name;
case 'ArrayExpression':
if (isMultiline(node)) {
return '[\n' +
printListLines(node.elements, indent + INDENT) +
indent + ']';
} else {
return '[' +
node.elements.map(function(element) { return print(element, indent); }).join(', ') +
']';
}
break;
case 'Literal':
if (typeof node.value === 'string') {
return "'" + node.value.replace(/'/g, "\\'") + "'";
} else {
return node.raw || JSON.stringify(node.value);
}
break;
default:
throw new Error('unexpected node type: ' + node.type);
}
}
var replacements = process.argv.slice(2).reduce(function(map, arg) {
var parts = arg.split('=');
if (parts.length === 2) {
map[parts[0]] = parts[1];
}
return map;
}, {});
var TEST = process.argv.indexOf('--test') >= 0;
readStdin(function(stdin) {
var inputSource = stdin;
var inputAST = recast.parse(inputSource);
var body = inputAST.program.body;
var ast = inputAST.program;
if (body.length === 1) {
var statement = body[0];
// Favor processing just an expression if possible.
ast = n.ExpressionStatement.check(statement) ?
statement.expression : statement;
}
var code = print(makeBuilder(ast, replacements));
if (TEST) {
// verify the result
var context = { b: b };
vm.runInNewContext('result = ' + code, context);
var normalizedInputSource = recast.prettyPrint(inputAST).code;
var normalizedBuiltSource = recast.prettyPrint(context.result).code;
assert.equal(
normalizedBuiltSource,
normalizedInputSource
);
}
process.stdout.write(code);
});
|