diff --git a/packages/babel-core/src/api/node.js b/packages/babel-core/src/api/node.js index f7d74a7894..c22062222f 100644 --- a/packages/babel-core/src/api/node.js +++ b/packages/babel-core/src/api/node.js @@ -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); diff --git a/packages/babel-core/src/transformation/pipeline.js b/packages/babel-core/src/transformation/pipeline.js index 93f864d2e9..642dcd8c43 100644 --- a/packages/babel-core/src/transformation/pipeline.js +++ b/packages/babel-core/src/transformation/pipeline.js @@ -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); diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index 41544f16f3..5142714286 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -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);