babel-core: add options for different parser/generator (#3561)
* babel-core: add options for different parser/generator * test for experiemental plugins, other babylon options * fix passing options into parser * Fix when no code is provided * fixup tests * fix tests again * use filename and fallback to cwd
This commit is contained in:
@@ -5,6 +5,7 @@ var sourceMap = require("source-map");
|
||||
var assert = require("assert");
|
||||
var File = require("../lib/transformation/file").default;
|
||||
var Plugin = require("../lib/transformation/plugin");
|
||||
var generator = require("babel-generator").default;
|
||||
|
||||
function assertIgnored(result) {
|
||||
assert.ok(result.ignored);
|
||||
@@ -23,6 +24,77 @@ function transformAsync(code, opts) {
|
||||
};
|
||||
}
|
||||
|
||||
suite("parser and generator options", function() {
|
||||
var recast = {
|
||||
parse: function(code, opts) {
|
||||
return opts.parser.parse(code);
|
||||
},
|
||||
print: function(ast) {
|
||||
return generator(ast);
|
||||
}
|
||||
};
|
||||
|
||||
function newTransform(string) {
|
||||
return babel.transform(string, {
|
||||
parserOpts: {
|
||||
parser: recast.parse,
|
||||
plugins: ["flow"],
|
||||
allowImportExportEverywhere: true
|
||||
},
|
||||
generatorOpts: {
|
||||
generator: recast.print
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
test("options", function() {
|
||||
var string = "original;";
|
||||
assert.deepEqual(newTransform(string).ast, babel.transform(string).ast);
|
||||
assert.equal(newTransform(string).code, string);
|
||||
});
|
||||
|
||||
test("experimental syntax", function() {
|
||||
var experimental = "var a: number = 1;";
|
||||
|
||||
assert.deepEqual(newTransform(experimental).ast, babel.transform(experimental, {
|
||||
parserOpts: {
|
||||
plugins: ["flow"]
|
||||
}
|
||||
}).ast);
|
||||
assert.equal(newTransform(experimental).code, experimental);
|
||||
|
||||
function newTransformWithPlugins(string) {
|
||||
return babel.transform(string, {
|
||||
plugins: [__dirname + "/../../babel-plugin-syntax-flow"],
|
||||
parserOpts: {
|
||||
parser: recast.parse
|
||||
},
|
||||
generatorOpts: {
|
||||
generator: recast.print
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
assert.deepEqual(newTransformWithPlugins(experimental).ast, babel.transform(experimental, {
|
||||
parserOpts: {
|
||||
plugins: ["flow"]
|
||||
}
|
||||
}).ast);
|
||||
assert.equal(newTransformWithPlugins(experimental).code, experimental);
|
||||
});
|
||||
|
||||
test("other options", function() {
|
||||
var experimental = "if (true) {\n import a from 'a';\n}";
|
||||
|
||||
assert.notEqual(newTransform(experimental).ast, babel.transform(experimental, {
|
||||
parserOpts: {
|
||||
allowImportExportEverywhere: true
|
||||
}
|
||||
}).ast);
|
||||
assert.equal(newTransform(experimental).code, experimental);
|
||||
});
|
||||
});
|
||||
|
||||
suite("api", function () {
|
||||
test("analyze", function () {
|
||||
assert.equal(babel.analyse("foobar;").marked.length, 0);
|
||||
|
||||
Reference in New Issue
Block a user