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

Add a 'throwIfNamespace' option for JSX transform #6563

Merged
merged 8 commits into from Oct 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions packages/babel-helper-builder-react-jsx/src/index.js
Expand Up @@ -14,11 +14,13 @@ export default function(opts) {
const visitor = {};

visitor.JSXNamespacedName = function(path) {
throw path.buildCodeFrameError(
"Namespace tags are not supported. ReactJSX is not XML.",
);
if (opts.throwIfNamespace) {
throw path.buildCodeFrameError(
`Namespace tags are not supported by default. React's JSX doesn't support namespace tags. \
You can turn on the 'throwIfNamespace' flag to bypass this warning.`,
);
}
};

visitor.JSXElement = {
exit(path, file) {
const callExpr = buildElementCall(path, file);
Expand All @@ -44,6 +46,12 @@ export default function(opts) {
convertJSXIdentifier(node.object, node),
convertJSXIdentifier(node.property, node),
);
} else if (t.isJSXNamespacedName(node)) {
/**
* If there is flag "throwIfNamespace"
* print XMLNamespace like string literal
*/
return t.stringLiteral(`${node.namespace.name}:${node.name.name}`);
}

return node;
Expand Down
13 changes: 12 additions & 1 deletion packages/babel-plugin-transform-react-jsx/README.md
Expand Up @@ -78,7 +78,8 @@ With options:
{
"plugins": [
["@babel/transform-react-jsx", {
"pragma": "dom" // default pragma is React.createElement
"pragma": "dom", // default pragma is React.createElement
"throwIfNamespace": false // defaults to true
}]
]
}
Expand Down Expand Up @@ -113,3 +114,13 @@ Note that the `@jsx React.DOM` pragma has been deprecated as of React v0.12
`boolean`, defaults to `false`.

When spreading props, use `Object.assign` directly instead of Babel's extend helper.

### `throwIfNamespace`

`boolean`, defaults to `true`.

Toggles whether or not to throw an error if a XML namespaced tag name is used. For example:

<f:image />

Though the JSX spec allows this, it is disabled by default since React's JSX does not currently have support for it.
4 changes: 4 additions & 0 deletions packages/babel-plugin-transform-react-jsx/src/index.js
Expand Up @@ -3,6 +3,8 @@ import helper from "@babel/helper-builder-react-jsx";

export default function({ types: t }, options) {
const pragma = options.pragma || "React.createElement";
const throwIfNamespace =
options.throwIfNamespace === undefined ? true : !!options.throwIfNamespace;

const JSX_ANNOTATION_REGEX = /\*?\s*@jsx\s+([^\s]+)/;

Expand All @@ -20,6 +22,8 @@ export default function({ types: t }, options) {
post(state, pass) {
state.callee = pass.get("jsxIdentifier")();
},

throwIfNamespace,
});

visitor.Program = function(path, state) {
Expand Down
@@ -1,3 +1,3 @@
{
"throws": "Namespace tags are not supported. ReactJSX is not XML."
"throws": "Namespace tags are not supported by default. React's JSX doesn't support namespace tags. You can turn on the 'throwIfNamespace' flag to bypass this warning."
}
@@ -0,0 +1 @@
<f:image />;
@@ -0,0 +1 @@
h("f:image", null);
@@ -0,0 +1,11 @@
{
"plugins": [
[
"transform-react-jsx",
{
"pragma": "h",
"throwIfNamespace": false
}
]
]
}
@@ -0,0 +1 @@
<f:image />;
@@ -0,0 +1,12 @@
{
"plugins": [
[
"transform-react-jsx",
{
"pragma": "h",
"throwIfNamespace": true
}
]
],
"throws": "Namespace tags are not supported by default. React's JSX doesn't support namespace tags. You can turn on the 'throwIfNamespace' flag to bypass this warning."
}
4 changes: 3 additions & 1 deletion packages/babel-types/src/definitions/core.js
Expand Up @@ -129,7 +129,9 @@ defineType("CallExpression", {
arguments: {
validate: chain(
assertValueType("array"),
assertEach(assertNodeType("Expression", "SpreadElement")),
assertEach(
assertNodeType("Expression", "SpreadElement", "JSXNamespacedName"),
),
),
},
optional: {
Expand Down