82 lines
2.7 KiB
JavaScript
82 lines
2.7 KiB
JavaScript
"use strict";
|
|
|
|
var _babelTypes = require("babel-types");
|
|
|
|
var t = _interopRequireWildcard(_babelTypes);
|
|
|
|
var _babelGenerator = require("babel-generator");
|
|
|
|
var _babelGenerator2 = _interopRequireDefault(_babelGenerator);
|
|
|
|
var _traverseFast = require("../lib/utils/traverse-fast.js");
|
|
|
|
var _traverseFast2 = _interopRequireDefault(_traverseFast);
|
|
|
|
var _babylon = require("babylon");
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
|
|
|
|
/**
|
|
* Copyright (c) 2017-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*/
|
|
|
|
var fs = require("fs");
|
|
|
|
|
|
function createLogStatement(loc) {
|
|
return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier("console"), t.identifier("log")), [t.stringLiteral("[instrumentation] #" + loc.start.line)]));
|
|
}
|
|
|
|
function instrument(inputFilename, outputFilename) {
|
|
var code = fs.readFileSync(inputFilename, "utf8");
|
|
var ast = (0, _babylon.parse)(code, { inputFilename: inputFilename, sourceType: "script" });
|
|
(0, _traverseFast2.default)(ast, function (node) {
|
|
if (node.type === "BlockStatement") {
|
|
if (node.loc) node.body.unshift(createLogStatement(node.loc));
|
|
}
|
|
return false;
|
|
});
|
|
code = (0, _babelGenerator2.default)(ast, {}, "").code;
|
|
if (!outputFilename) outputFilename = inputFilename + "-instrumented.js";
|
|
fs.writeFileSync(outputFilename, code);
|
|
console.log("Instrumented source code written to " + outputFilename + ".");
|
|
}
|
|
|
|
var args = Array.from(process.argv);
|
|
args.splice(0, 2);
|
|
var inputFilename = void 0;
|
|
var outputFilename = void 0;
|
|
while (args.length) {
|
|
var arg = args[0];
|
|
args.shift();
|
|
if (arg === "--out") {
|
|
arg = args[0];
|
|
args.shift();
|
|
outputFilename = arg;
|
|
} else if (arg === "--help") {
|
|
console.log("Usage: instrumentor.js [ --out output.js ] [ -- | input.js ]");
|
|
} else if (!arg.startsWith("--")) {
|
|
inputFilename = arg;
|
|
} else {
|
|
console.error("Unknown option: " + arg);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
if (!inputFilename) {
|
|
console.error("Missing input file.");
|
|
process.exit(1);
|
|
} else if (!outputFilename) {
|
|
console.error("Missing output file.");
|
|
process.exit(1);
|
|
} else {
|
|
instrument(inputFilename, outputFilename);
|
|
}
|
|
//# sourceMappingURL=instrumentor.js.map
|