From e2486b5f71430483bf1a084934ad134515ef34ac Mon Sep 17 00:00:00 2001 From: Dmitry Soshnikov Date: Thu, 21 Jan 2016 14:54:40 -0800 Subject: [PATCH] Pass per preset: added unit test --- packages/babel-core/test/api.js | 84 +++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index 93fb13abe3..cd6393f16e 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -4,6 +4,7 @@ var Pipeline = require("../lib/transformation/pipeline"); var sourceMap = require("source-map"); var assert = require("assert"); var File = require("../lib/transformation/file").default; +var Plugin = require("../lib/transformation/plugin"); function assertIgnored(result) { assert.ok(result.ignored); @@ -44,6 +45,89 @@ suite("api", function () { }); }); + test("pass per preset", function () { + var aliasBaseType = null; + + function execTest(passPerPreset) { + return babel.transform('type Foo = number; let x = (y): Foo => y;', { + passPerPreset: passPerPreset, + presets: [ + // First preset with our plugin, "before" + { + plugins: [ + new Plugin({ + visitor: { + Function(path) { + var node = path.node; + var scope = path.scope; + + var alias = scope + .getProgramParent() + .getBinding(node.returnType.typeAnnotation.id.name) + .path + .node; + + // In case of `passPerPreset` being `false`, the + // alias node is already removed by Flow plugin. + if (!alias) { + return; + } + + // In case of `passPerPreset` being `true`, the + // alias node should still exist. + aliasBaseType = alias.right.type; // NumberTypeAnnotation + } + } + }) + ] + }, + + // ES2015 preset + require(__dirname + "/../../babel-preset-es2015"), + + // Third preset for Flow. + { + plugins: [ + require(__dirname + "/../../babel-plugin-syntax-flow"), + require(__dirname + "/../../babel-plugin-transform-flow-strip-types"), + ] + } + ], + }); + } + + // 1. passPerPreset: true + + var result = execTest(true); + + assert.equal(aliasBaseType, "NumberTypeAnnotation"); + + assert.deepEqual([ + '"use strict";', + '', + 'var x = function x(y) {', + ' return y;', + '};' + ].join("\n"), result.code); + + // 2. passPerPreset: false + + aliasBaseType = null; + + var result = execTest(false); + + assert.equal(aliasBaseType, null); + + assert.deepEqual([ + '"use strict";', + '', + 'var x = function x(y) {', + ' return y;', + '};' + ].join("\n"), result.code); + + }); + test("source map merging", function () { var result = babel.transform([ 'function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }',