add a top level analyze method for path marking sugar

This commit is contained in:
Sebastian McKenzie
2016-02-03 21:29:47 +00:00
parent 6b76402791
commit 35e8250b57
3 changed files with 31 additions and 0 deletions

View File

@@ -32,6 +32,7 @@ import Pipeline from "../transformation/pipeline";
export { Pipeline };
let pipeline = new Pipeline;
export let analyse = pipeline.analyse.bind(pipeline);
export let transform = pipeline.transform.bind(pipeline);
export let transformFromAst = pipeline.transformFromAst.bind(pipeline);

View File

@@ -1,6 +1,7 @@
/* @noflow */
import normalizeAst from "../helpers/normalize-ast";
import Plugin from "./plugin";
import File from "./file";
export default class Pipeline {
@@ -28,6 +29,15 @@ export default class Pipeline {
});
}
analyse(code: string, opts: Object = {}, visitor?) {
opts.code = false;
if (visitor) {
opts.plugins = opts.plugins || [];
opts.plugins.push(new Plugin({ visitor }));
}
return this.transform(code, opts).metadata;
}
transformFromAst(ast, code: string, opts: Object) {
ast = normalizeAst(ast);

View File

@@ -24,6 +24,26 @@ function transformAsync(code, opts) {
}
suite("api", function () {
test("analyze", function () {
assert.equal(babel.analyse("foobar;").marked.length, 0);
assert.equal(babel.analyse("foobar;", {
plugins: [new Plugin({
visitor: {
Program: function (path) {
path.mark("category", "foobar");
}
}
})]
}).marked[0].message, "foobar");
assert.equal(babel.analyse("foobar;", {}, {
Program: function (path) {
path.mark("category", "foobar");
}
}).marked[0].message, "foobar");
});
test("transformFile", function (done) {
babel.transformFile(__dirname + "/fixtures/api/file.js", {}, function (err, res) {
if (err) return done(err);