add a top level analyze method for path marking sugar
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user