Socket
Socket
Sign inDemoInstall

@babel/plugin-transform-react-jsx

Package Overview
Dependencies
58
Maintainers
4
Versions
97
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 7.22.5 to 8.0.0-alpha.0

228

lib/create-plugin.js

@@ -1,12 +0,6 @@

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = createPlugin;
var _pluginSyntaxJsx = require("@babel/plugin-syntax-jsx");
var _helperPluginUtils = require("@babel/helper-plugin-utils");
var _core = require("@babel/core");
var _helperModuleImports = require("@babel/helper-module-imports");
var _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure");
import jsx from "@babel/plugin-syntax-jsx";
import { declare } from "@babel/helper-plugin-utils";
import { template, types as t } from "@babel/core";
import { addNamed, addNamespace, isModule } from "@babel/helper-module-imports";
import annotateAsPure from "@babel/helper-annotate-as-pure";
const DEFAULT = {

@@ -25,16 +19,16 @@ importSource: "react",

function hasProto(node) {
return node.properties.some(value => _core.types.isObjectProperty(value, {
return node.properties.some(value => t.isObjectProperty(value, {
computed: false,
shorthand: false
}) && (_core.types.isIdentifier(value.key, {
}) && (t.isIdentifier(value.key, {
name: "__proto__"
}) || _core.types.isStringLiteral(value.key, {
}) || t.isStringLiteral(value.key, {
value: "__proto__"
})));
}
function createPlugin({
export default function createPlugin({
name,
development
}) {
return (0, _helperPluginUtils.declare)((_, options) => {
return declare((_, options) => {
const {

@@ -44,3 +38,3 @@ pure: PURE_ANNOTATION,

filter,
runtime: RUNTIME_DEFAULT = development ? "automatic" : "classic",
runtime: RUNTIME_DEFAULT = "automatic",
importSource: IMPORT_SOURCE_DEFAULT = DEFAULT.importSource,

@@ -51,17 +45,20 @@ pragma: PRAGMA_DEFAULT = DEFAULT.pragma,

{
var {
useSpread = false,
useBuiltIns = false
} = options;
if (RUNTIME_DEFAULT === "classic") {
if (typeof useSpread !== "boolean") {
throw new Error("transform-react-jsx currently only accepts a boolean option for " + "useSpread (defaults to false)");
}
if (typeof useBuiltIns !== "boolean") {
throw new Error("transform-react-jsx currently only accepts a boolean option for " + "useBuiltIns (defaults to false)");
}
if (useSpread && useBuiltIns) {
throw new Error("transform-react-jsx currently only accepts useBuiltIns or useSpread " + "but not both");
}
if ("useSpread" in options) {
throw new Error('@babel/plugin-transform-react-jsx: Since Babel 8, an inline object with spread elements is always used, and the "useSpread" option is no longer available. Please remove it from your config.');
}
if ("useBuiltIns" in options) {
const useBuiltInsFormatted = JSON.stringify(options.useBuiltIns);
throw new Error(`@babel/plugin-transform-react-jsx: Since "useBuiltIns" is removed in Babel 8, you can remove it from the config.
- Babel 8 now transforms JSX spread to object spread. If you need to transpile object spread with
\`useBuiltIns: ${useBuiltInsFormatted}\`, you can use the following config
{
"plugins": [
"@babel/plugin-transform-react-jsx"
["@babel/plugin-transform-object-rest-spread", { "loose": true, "useBuiltIns": ${useBuiltInsFormatted} }]
]
}`);
}
if (filter != null && RUNTIME_DEFAULT === "automatic") {
throw new Error('@babel/plugin-transform-react-jsx: "filter" option can not be used with automatic runtime. If you are upgrading from Babel 7, please specify `runtime: "classic"`.');
}
}

@@ -72,5 +69,5 @@ const injectMetaPropertiesVisitor = {

if (isThisAllowed(path.scope)) {
attributes.push(_core.types.jsxAttribute(_core.types.jsxIdentifier("__self"), _core.types.jsxExpressionContainer(_core.types.thisExpression())));
attributes.push(t.jsxAttribute(t.jsxIdentifier("__self"), t.jsxExpressionContainer(t.thisExpression())));
}
attributes.push(_core.types.jsxAttribute(_core.types.jsxIdentifier("__source"), _core.types.jsxExpressionContainer(makeSource(path, state))));
attributes.push(t.jsxAttribute(t.jsxIdentifier("__source"), t.jsxExpressionContainer(makeSource(path, state))));
path.pushContainer("attributes", attributes);

@@ -81,3 +78,3 @@ }

name,
inherits: _pluginSyntaxJsx.default,
inherits: jsx,
visitor: {

@@ -135,4 +132,4 @@ JSXNamespacedName(path) {

const fragment = toMemberExpression(pragmaFrag);
set(state, "id/createElement", () => _core.types.cloneNode(createElement));
set(state, "id/fragment", () => _core.types.cloneNode(fragment));
set(state, "id/createElement", () => t.cloneNode(createElement));
set(state, "id/fragment", () => t.cloneNode(fragment));
set(state, "defaultPure", pragma === DEFAULT.pragma);

@@ -165,3 +162,3 @@ } else if (runtime === "automatic") {

}
path.replaceWith(_core.types.inherits(callExpr, path.node));
path.replaceWith(t.inherits(callExpr, path.node));
}

@@ -177,8 +174,8 @@ },

}
path.replaceWith(_core.types.inherits(callExpr, path.node));
path.replaceWith(t.inherits(callExpr, path.node));
}
},
JSXAttribute(path) {
if (_core.types.isJSXElement(path.node.value)) {
path.node.value = _core.types.jsxExpressionContainer(path.node.value);
if (t.isJSXElement(path.node.value)) {
path.node.value = t.jsxExpressionContainer(path.node.value);
}

@@ -212,4 +209,4 @@ }

function call(pass, name, args) {
const node = _core.types.callExpression(get(pass, `id/${name}`)(), args);
if (PURE_ANNOTATION != null ? PURE_ANNOTATION : get(pass, "defaultPure")) (0, _helperAnnotateAsPure.default)(node);
const node = t.callExpression(get(pass, `id/${name}`)(), args);
if (PURE_ANNOTATION ?? get(pass, "defaultPure")) annotateAsPure(node);
return node;

@@ -223,5 +220,5 @@ }

const attr = attributes[i];
if (seenPropsSpread && _core.types.isJSXAttribute(attr) && attr.name.name === "key") {
if (seenPropsSpread && t.isJSXAttribute(attr) && attr.name.name === "key") {
return true;
} else if (_core.types.isJSXSpreadAttribute(attr)) {
} else if (t.isJSXSpreadAttribute(attr)) {
seenPropsSpread = true;

@@ -233,15 +230,15 @@ }

function convertJSXIdentifier(node, parent) {
if (_core.types.isJSXIdentifier(node)) {
if (node.name === "this" && _core.types.isReferenced(node, parent)) {
return _core.types.thisExpression();
} else if (_core.types.isValidIdentifier(node.name, false)) {
if (t.isJSXIdentifier(node)) {
if (node.name === "this" && t.isReferenced(node, parent)) {
return t.thisExpression();
} else if (t.isValidIdentifier(node.name, false)) {
node.type = "Identifier";
return node;
} else {
return _core.types.stringLiteral(node.name);
return t.stringLiteral(node.name);
}
} else if (_core.types.isJSXMemberExpression(node)) {
return _core.types.memberExpression(convertJSXIdentifier(node.object, node), convertJSXIdentifier(node.property, node));
} else if (_core.types.isJSXNamespacedName(node)) {
return _core.types.stringLiteral(`${node.namespace.name}:${node.name.name}`);
} else if (t.isJSXMemberExpression(node)) {
return t.memberExpression(convertJSXIdentifier(node.object, node), convertJSXIdentifier(node.property, node));
} else if (t.isJSXNamespacedName(node)) {
return t.stringLiteral(`${node.namespace.name}:${node.name.name}`);
}

@@ -251,3 +248,3 @@ return node;

function convertAttributeValue(node) {
if (_core.types.isJSXExpressionContainer(node)) {
if (t.isJSXExpressionContainer(node)) {
return node.expression;

@@ -259,28 +256,27 @@ } else {

function accumulateAttribute(array, attribute) {
if (_core.types.isJSXSpreadAttribute(attribute.node)) {
if (t.isJSXSpreadAttribute(attribute.node)) {
const arg = attribute.node.argument;
if (_core.types.isObjectExpression(arg) && !hasProto(arg)) {
if (t.isObjectExpression(arg) && !hasProto(arg)) {
array.push(...arg.properties);
} else {
array.push(_core.types.spreadElement(arg));
array.push(t.spreadElement(arg));
}
return array;
}
const value = convertAttributeValue(attribute.node.name.name !== "key" ? attribute.node.value || _core.types.booleanLiteral(true) : attribute.node.value);
const value = convertAttributeValue(attribute.node.name.name !== "key" ? attribute.node.value || t.booleanLiteral(true) : attribute.node.value);
if (attribute.node.name.name === "key" && value === null) {
throw attribute.buildCodeFrameError('Please provide an explicit key value. Using "key" as a shorthand for "key={true}" is not allowed.');
}
if (_core.types.isStringLiteral(value) && !_core.types.isJSXExpressionContainer(attribute.node.value)) {
var _value$extra;
if (t.isStringLiteral(value) && !t.isJSXExpressionContainer(attribute.node.value)) {
value.value = value.value.replace(/\n\s+/g, " ");
(_value$extra = value.extra) == null ? true : delete _value$extra.raw;
delete value.extra?.raw;
}
if (_core.types.isJSXNamespacedName(attribute.node.name)) {
attribute.node.name = _core.types.stringLiteral(attribute.node.name.namespace.name + ":" + attribute.node.name.name.name);
} else if (_core.types.isValidIdentifier(attribute.node.name.name, false)) {
if (t.isJSXNamespacedName(attribute.node.name)) {
attribute.node.name = t.stringLiteral(attribute.node.name.namespace.name + ":" + attribute.node.name.name.name);
} else if (t.isValidIdentifier(attribute.node.name.name, false)) {
attribute.node.name.type = "Identifier";
} else {
attribute.node.name = _core.types.stringLiteral(attribute.node.name.name);
attribute.node.name = t.stringLiteral(attribute.node.name.name);
}
array.push(_core.types.inherits(_core.types.objectProperty(attribute.node.name, value), attribute.node));
array.push(t.inherits(t.objectProperty(attribute.node.name, value), attribute.node));
return array;

@@ -293,7 +289,7 @@ }

} else if (children.length > 1) {
childrenNode = _core.types.arrayExpression(children);
childrenNode = t.arrayExpression(children);
} else {
return undefined;
}
return _core.types.objectProperty(_core.types.identifier("children"), childrenNode);
return t.objectProperty(t.identifier("children"), childrenNode);
}

@@ -306,3 +302,3 @@ function buildJSXElementCall(path, file) {

for (const attr of openingPath.get("attributes")) {
if (attr.isJSXAttribute() && _core.types.isJSXIdentifier(attr.node.name)) {
if (attr.isJSXAttribute() && t.isJSXIdentifier(attr.node.name)) {
const {

@@ -331,3 +327,3 @@ name

}
const children = _core.types.react.buildChildren(path.node);
const children = t.react.buildChildren(path.node);
let attribs;

@@ -337,8 +333,7 @@ if (attribsArray.length || children.length) {

} else {
attribs = _core.types.objectExpression([]);
attribs = t.objectExpression([]);
}
args.push(attribs);
if (development) {
var _extracted$key;
args.push((_extracted$key = extracted.key) != null ? _extracted$key : path.scope.buildUndefinedNode(), _core.types.booleanLiteral(children.length > 1));
args.push(extracted.key ?? path.scope.buildUndefinedNode(), t.booleanLiteral(children.length > 1));
if (extracted.__source) {

@@ -357,13 +352,13 @@ args.push(extracted.__source);

const props = attribs.reduce(accumulateAttribute, []);
if ((children == null ? void 0 : children.length) > 0) {
if (children?.length > 0) {
props.push(buildChildrenProperty(children));
}
return _core.types.objectExpression(props);
return t.objectExpression(props);
}
function buildJSXFragmentCall(path, file) {
const args = [get(file, "id/fragment")()];
const children = _core.types.react.buildChildren(path.node);
args.push(_core.types.objectExpression(children.length > 0 ? [buildChildrenProperty(children)] : []));
const children = t.react.buildChildren(path.node);
args.push(t.objectExpression(children.length > 0 ? [buildChildrenProperty(children)] : []));
if (development) {
args.push(path.scope.buildUndefinedNode(), _core.types.booleanLiteral(children.length > 1));
args.push(path.scope.buildUndefinedNode(), t.booleanLiteral(children.length > 1));
}

@@ -374,7 +369,7 @@ return call(file, children.length > 1 ? "jsxs" : "jsx", args);

if (filter && !filter(path.node, file)) return;
return call(file, "createElement", [get(file, "id/fragment")(), _core.types.nullLiteral(), ..._core.types.react.buildChildren(path.node)]);
return call(file, "createElement", [get(file, "id/fragment")(), t.nullLiteral(), ...t.react.buildChildren(path.node)]);
}
function buildCreateElementCall(path, file) {
const openingPath = path.get("openingElement");
return call(file, "createElement", [getTag(openingPath), buildCreateElementOpeningElementAttributes(file, path, openingPath.get("attributes")), ..._core.types.react.buildChildren(path.node)]);
return call(file, "createElement", [getTag(openingPath), buildCreateElementOpeningElementAttributes(file, path, openingPath.get("attributes")), ...t.react.buildChildren(path.node)]);
}

@@ -384,9 +379,9 @@ function getTag(openingPath) {

let tagName;
if (_core.types.isIdentifier(tagExpr)) {
if (t.isIdentifier(tagExpr)) {
tagName = tagExpr.name;
} else if (_core.types.isStringLiteral(tagExpr)) {
} else if (t.isStringLiteral(tagExpr)) {
tagName = tagExpr.value;
}
if (_core.types.react.isCompatTag(tagName)) {
return _core.types.stringLiteral(tagName);
if (t.react.isCompatTag(tagName)) {
return t.stringLiteral(tagName);
} else {

@@ -398,38 +393,3 @@ return tagExpr;

const runtime = get(file, "runtime");
{
if (runtime !== "automatic") {
const objs = [];
const props = attribs.reduce(accumulateAttribute, []);
if (!useSpread) {
let start = 0;
props.forEach((prop, i) => {
if (_core.types.isSpreadElement(prop)) {
if (i > start) {
objs.push(_core.types.objectExpression(props.slice(start, i)));
}
objs.push(prop.argument);
start = i + 1;
}
});
if (props.length > start) {
objs.push(_core.types.objectExpression(props.slice(start)));
}
} else if (props.length) {
objs.push(_core.types.objectExpression(props));
}
if (!objs.length) {
return _core.types.nullLiteral();
}
if (objs.length === 1) {
if (!(_core.types.isSpreadElement(props[0]) && _core.types.isObjectExpression(props[0].argument))) {
return objs[0];
}
}
if (!_core.types.isObjectExpression(objs[0])) {
objs.unshift(_core.types.objectExpression([]));
}
const helper = useBuiltIns ? _core.types.memberExpression(_core.types.identifier("Object"), _core.types.identifier("assign")) : file.addHelper("extends");
return _core.types.callExpression(helper, objs);
}
}
;
const props = [];

@@ -441,3 +401,3 @@ const found = Object.create(null);

} = attr;
const name = _core.types.isJSXAttribute(node) && _core.types.isJSXIdentifier(node.name) && node.name.name;
const name = t.isJSXAttribute(node) && t.isJSXIdentifier(node.name) && node.name.name;
if (runtime === "automatic" && (name === "__source" || name === "__self")) {

@@ -449,3 +409,3 @@ if (found[name]) throw sourceSelfError(path, name);

}
return props.length === 1 && _core.types.isSpreadElement(props[0]) && !_core.types.isObjectExpression(props[0].argument) ? props[0].argument : props.length > 0 ? _core.types.objectExpression(props) : _core.types.nullLiteral();
return props.length === 1 && t.isSpreadElement(props[0]) && !t.isObjectExpression(props[0].argument) ? props[0].argument : props.length > 0 ? t.objectExpression(props) : t.nullLiteral();
}

@@ -469,6 +429,6 @@ });

const actualSource = getSource(source, importName);
if ((0, _helperModuleImports.isModule)(path)) {
if (isModule(path)) {
let reference = get(pass, `imports/${importName}`);
if (reference) return _core.types.cloneNode(reference);
reference = (0, _helperModuleImports.addNamed)(path, importName, actualSource, {
if (reference) return t.cloneNode(reference);
reference = addNamed(path, importName, actualSource, {
importedInterop: "uncompiled",

@@ -482,5 +442,5 @@ importPosition: "after"

if (reference) {
reference = _core.types.cloneNode(reference);
reference = t.cloneNode(reference);
} else {
reference = (0, _helperModuleImports.addNamespace)(path, actualSource, {
reference = addNamespace(path, actualSource, {
importedInterop: "uncompiled"

@@ -490,3 +450,3 @@ });

}
return _core.types.memberExpression(reference, _core.types.identifier(importName));
return t.memberExpression(reference, t.identifier(importName));
}

@@ -497,3 +457,3 @@ };

function toMemberExpression(id) {
return id.split(".").map(name => _core.types.identifier(name)).reduce((object, property) => _core.types.memberExpression(object, property));
return id.split(".").map(name => t.identifier(name)).reduce((object, property) => t.memberExpression(object, property));
}

@@ -512,12 +472,12 @@ function makeSource(path, state) {

id: fileNameIdentifier,
init: _core.types.stringLiteral(filename)
init: t.stringLiteral(filename)
});
state.fileNameIdentifier = fileNameIdentifier;
}
return makeTrace(_core.types.cloneNode(state.fileNameIdentifier), location.start.line, location.start.column);
return makeTrace(t.cloneNode(state.fileNameIdentifier), location.start.line, location.start.column);
}
function makeTrace(fileNameIdentifier, lineNumber, column0Based) {
const fileLineLiteral = lineNumber != null ? _core.types.numericLiteral(lineNumber) : _core.types.nullLiteral();
const fileColumnLiteral = column0Based != null ? _core.types.numericLiteral(column0Based + 1) : _core.types.nullLiteral();
return _core.template.expression.ast`{
const fileLineLiteral = lineNumber != null ? t.numericLiteral(lineNumber) : t.nullLiteral();
const fileColumnLiteral = column0Based != null ? t.numericLiteral(column0Based + 1) : t.nullLiteral();
return template.expression.ast`{
fileName: ${fileNameIdentifier},

@@ -524,0 +484,0 @@ lineNumber: ${fileLineLiteral},

@@ -1,14 +0,7 @@

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _createPlugin = require("./create-plugin");
var _default = (0, _createPlugin.default)({
import createPlugin from "./create-plugin.js";
export default createPlugin({
name: "transform-react-jsx/development",
development: true
});
exports.default = _default;
//# sourceMappingURL=development.js.map

@@ -1,14 +0,7 @@

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _createPlugin = require("./create-plugin");
var _default = (0, _createPlugin.default)({
import createPlugin from "./create-plugin.js";
export default createPlugin({
name: "transform-react-jsx",
development: false
});
exports.default = _default;
//# sourceMappingURL=index.js.map
{
"name": "@babel/plugin-transform-react-jsx",
"version": "7.22.5",
"version": "8.0.0-alpha.0",
"description": "Turn JSX into React function calls",

@@ -20,21 +20,26 @@ "repository": {

"dependencies": {
"@babel/helper-annotate-as-pure": "^7.22.5",
"@babel/helper-module-imports": "^7.22.5",
"@babel/helper-plugin-utils": "^7.22.5",
"@babel/plugin-syntax-jsx": "^7.22.5",
"@babel/types": "^7.22.5"
"@babel/helper-annotate-as-pure": "^8.0.0-alpha.0",
"@babel/helper-module-imports": "^8.0.0-alpha.0",
"@babel/helper-plugin-utils": "^8.0.0-alpha.0",
"@babel/plugin-syntax-jsx": "^8.0.0-alpha.0",
"@babel/types": "^8.0.0-alpha.0"
},
"peerDependencies": {
"@babel/core": "^7.0.0-0"
"@babel/core": "^8.0.0-alpha.0"
},
"devDependencies": {
"@babel/core": "^7.22.5",
"@babel/helper-plugin-test-runner": "^7.22.5",
"@babel/traverse": "^7.22.5"
"@babel/core": "^8.0.0-alpha.0",
"@babel/helper-plugin-test-runner": "^8.0.0-alpha.0",
"@babel/traverse": "^8.0.0-alpha.0"
},
"engines": {
"node": ">=6.9.0"
"node": "^16.20.0 || ^18.16.0 || >=20.0.0"
},
"author": "The Babel Team (https://babel.dev/team)",
"type": "commonjs"
"exports": {
".": "./lib/index.js",
"./lib/development": "./lib/development.js",
"./package.json": "./package.json"
},
"type": "module"
}

@@ -5,3 +5,3 @@ # @babel/plugin-transform-react-jsx

See our website [@babel/plugin-transform-react-jsx](https://babeljs.io/docs/en/babel-plugin-transform-react-jsx) for more information.
See our website [@babel/plugin-transform-react-jsx](https://babeljs.io/docs/babel-plugin-transform-react-jsx) for more information.

@@ -8,0 +8,0 @@ ## Install

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc