Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade babel to v7 and to support fragment #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion jsx-pragmatic.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ module.exports = function jsxPragmatic (babel) {
state.set('jsxDetected', true);
},
// JSXElement

JSXFragment: function (path, state) {
state.set('jsxDetected', true);
},
};

return {
Expand All @@ -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,
};
};
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/fragment-only.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(<></>)
43 changes: 40 additions & 3 deletions test/integration.js
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down Expand Up @@ -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, {
Expand All @@ -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