Skip to content

Commit

Permalink
Merge pull request #3 from MaijaHeiskanen/fix-usage-of-multiple-sprea…
Browse files Browse the repository at this point in the history
…d-operators

Fix usage of multiple spread operators
  • Loading branch information
Havunen authored Nov 29, 2022
2 parents 1107894 + 5b5e5b9 commit da64692
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"inferno"
],
"scripts": {
"test": "rimraf tests/temp && ts-node tests/index.ts",
"debug": "rimraf tests/temp && npm run build && node --inspect-brk --require ts-node/register tests/index.ts",
"test": "rimraf tests/temp && rimraf tests/tempES6 && ts-node tests/index.ts",
"debug": "rimraf tests/temp && rimraf tests/tempES6 && npm run build && node --inspect-brk --require ts-node/register tests/index.ts",
"build": "tsc",
"overwrite-references": "ts-node tests/overwriteReferences.ts",
"prepublishOnly": "rimraf dist && tsc"
Expand Down
27 changes: 20 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,6 @@ export default () => {
}

function getVNodeProps(astProps: ts.NodeArray<ts.JsxAttributeLike>, isComponent) {
let props = []
let key = null
let ref = null
let className = null
Expand All @@ -492,14 +491,28 @@ export default () => {
let childFlags = null
let contentEditable = false
let assignArgs = []
let propsPropertyAssignments = []
let objectLiteralExpressionAdded = false

for (let i = 0; i < astProps.length; i++) {
let astProp = astProps[i]
let initializer;

if (astProp.kind === ts.SyntaxKind.JsxSpreadAttribute) {
if (propsPropertyAssignments.length) {
assignArgs.push(factory.createObjectLiteralExpression([...propsPropertyAssignments]))

propsPropertyAssignments = []
}

needsNormalization = true
assignArgs = [factory.createObjectLiteralExpression(), astProp.expression]

if (!objectLiteralExpressionAdded) {
assignArgs.unshift(factory.createObjectLiteralExpression())
objectLiteralExpressionAdded = true
}

assignArgs.push(astProp.expression);
} else {
initializer = astProp.initializer
let propName = astProp.name.text
Expand All @@ -510,14 +523,14 @@ export default () => {
) {
className = getValue(initializer, visitor)
} else if (!isComponent && propName === 'htmlFor') {
props.push(
propsPropertyAssignments.push(
factory.createPropertyAssignment(
getName('for'),
getValue(initializer, visitor)
)
)
} else if (!isComponent && propName === 'onDoubleClick') {
props.push(
propsPropertyAssignments.push(
factory.createPropertyAssignment(
getName('onDblClick'),
getValue(initializer, visitor)
Expand All @@ -536,7 +549,7 @@ export default () => {
)
} else if (!isComponent && propName in svgAttributes) {
// React compatibility for SVG Attributes
props.push(
propsPropertyAssignments.push(
factory.createPropertyAssignment(
getName(svgAttributes[propName]),
getValue(initializer, visitor)
Expand Down Expand Up @@ -592,7 +605,7 @@ export default () => {
if (propName.toLowerCase() === 'contenteditable') {
contentEditable = true
}
props.push(
propsPropertyAssignments.push(
factory.createPropertyAssignment(
getName(propName),
initializer
Expand All @@ -605,7 +618,7 @@ export default () => {
}
}

if (props.length) assignArgs.push(factory.createObjectLiteralExpression(props))
if (propsPropertyAssignments.length) assignArgs.push(factory.createObjectLiteralExpression(propsPropertyAssignments))

return {
props: assignArgs,
Expand Down
1 change: 1 addition & 0 deletions tests/cases/spreadAttribute7.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div {...props} {...other} foo="bar" foo2="bar2" {...more} foo3="bar3" />;
2 changes: 1 addition & 1 deletion tests/references/spreadAttribute5.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ var __assign = (this && this.__assign) || Object.assign || function(t) {
import * as inferno from "inferno";
var normalizeProps = inferno.normalizeProps;
var createVNode = inferno.createVNode;
normalizeProps(createVNode(1, "div", "test", null, 1, __assign({}, props, { "foo": "bar" })));
normalizeProps(createVNode(1, "div", "test", null, 1, __assign({}, { "foo": "bar" }, props)));
12 changes: 12 additions & 0 deletions tests/references/spreadAttribute7.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
import * as inferno from "inferno";
var normalizeProps = inferno.normalizeProps;
var createVNode = inferno.createVNode;
normalizeProps(createVNode(1, "div", null, null, 1, __assign({}, props, other, { "foo": "bar", "foo2": "bar2" }, more, { "foo3": "bar3" })));
2 changes: 1 addition & 1 deletion tests/referencesES6/spreadAttribute5.jsx
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import { createVNode, normalizeProps } from "inferno";
normalizeProps(createVNode(1, "div", "test", null, 1, Object.assign({}, props, { "foo": "bar" })));
normalizeProps(createVNode(1, "div", "test", null, 1, Object.assign({}, { "foo": "bar" }, props)));
2 changes: 2 additions & 0 deletions tests/referencesES6/spreadAttribute7.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { createVNode, normalizeProps } from "inferno";
normalizeProps(createVNode(1, "div", null, null, 1, Object.assign({}, props, other, { "foo": "bar", "foo2": "bar2" }, more, { "foo3": "bar3" })));

0 comments on commit da64692

Please sign in to comment.