From 91ca0238597ddfdcc73f43b9e38a8300837a11cb Mon Sep 17 00:00:00 2001 From: Morlay Date: Mon, 31 Dec 2018 18:04:11 +0800 Subject: [PATCH] upgrade babel to v7 and to support fragment --- jsx-pragmatic.js | 6 ++++- package.json | 8 +++--- test/fixtures/fragment-only.jsx | 1 + test/integration.js | 43 ++++++++++++++++++++++++++++++--- 4 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 test/fixtures/fragment-only.jsx diff --git a/jsx-pragmatic.js b/jsx-pragmatic.js index 981c64f..e6235c2 100644 --- a/jsx-pragmatic.js +++ b/jsx-pragmatic.js @@ -41,6 +41,10 @@ module.exports = function jsxPragmatic (babel) { state.set('jsxDetected', true); }, // JSXElement + + JSXFragment: function (path, state) { + state.set('jsxDetected', true); + }, }; return { @@ -49,7 +53,7 @@ module.exports = function jsxPragmatic (babel) { throw new Error("babel-plugin-jsx-pragmatic: You must specify `module` and `import`"); } }, - inherits: require("babel-plugin-syntax-jsx"), + inherits: require("@babel/plugin-syntax-jsx").default, visitor: visitor, }; }; diff --git a/package.json b/package.json index 75e8c1b..bbf90c1 100644 --- a/package.json +++ b/package.json @@ -10,12 +10,12 @@ "url": "https://github.com/jmm/babel-plugin-jsx-pragmatic" }, "dependencies": { - "babel-plugin-syntax-jsx": "^6.0.0" + "@babel/plugin-syntax-jsx": "^7.2.0" }, "devDependencies": { - "babel-core": "^6.0.0", - "babel-plugin-transform-react-jsx": "^6.0.0", - "babel-preset-es2015": "^6.0.0", + "@babel/core": "^7.2.2", + "@babel/plugin-transform-react-jsx": "^7.2.0", + "@babel/preset-env": "^7.2.3", "lodash": "^3.10.0", "mocha": "^2.3.0", "sinon": "^1.17.0" diff --git a/test/fixtures/fragment-only.jsx b/test/fixtures/fragment-only.jsx new file mode 100644 index 0000000..03eb252 --- /dev/null +++ b/test/fixtures/fragment-only.jsx @@ -0,0 +1 @@ +(<>) \ No newline at end of file diff --git a/test/integration.js b/test/integration.js index 9f59701..6a66339 100644 --- a/test/integration.js +++ b/test/integration.js @@ -1,7 +1,7 @@ var _ = require("lodash"), assert = require("assert"), - babel = require("babel-core"), + babel = require("@babel/core"), createElement = require("./helpers/create-element"), jsxPragmatic = require("../"), loadFixture = require("./helpers/load-fixture"), @@ -75,12 +75,13 @@ describe("Transformed module", function () { code = babel.transform(loadFixture("minimal.jsx"), { plugins: [ [jsxPragmatic, opts], - ["babel-plugin-transform-react-jsx", { + ["@babel/plugin-transform-react-jsx", { pragma: pragma, + pragmaFrag: "Fragment", }] ], - presets: ["es2015"], + presets: ["@babel/preset-env"], }).code; vm.runInNewContext(code, { @@ -97,5 +98,41 @@ describe("Transformed module", function () { // Would rather do spy.restore() but it's undocumented. createElement.createElement.restore(); }); + + it("Should call createElement() with simple `pragma` for <>", function () { + var + code, + pragma = "createElement", + spy = sinon.spy(createElement, "createElement"), + opts = { + module: path.join(__dirname, "helpers", "create-element"), + import: pragma.split(".")[0], + }; + + code = babel.transform(loadFixture("fragment-only.jsx"), { + plugins: [ + [jsxPragmatic, opts], + ["@babel/plugin-transform-react-jsx", { + pragma: pragma, + pragmaFrag: "createElement", // todo just for testing + }] + ], + presets: ["@babel/preset-env"], + }).code; + + vm.runInNewContext(code, { + require: function (id) { + if (id === opts.module) return createElement; + }, + }); + + assert( + spy.callCount, + "createElement() wasn't called" + ); + + // Would rather do spy.restore() but it's undocumented. + createElement.createElement.restore(); + }); }); // describe