@teleporthq/teleport-plugin-common 0.44.15 → 0.45.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/node-handlers/node-to-jsx/chart-data-guard.ts +101 -0
- package/dist/cjs/node-handlers/node-to-jsx/index.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-jsx/index.js +32 -0
- package/dist/cjs/node-handlers/node-to-jsx/index.js.map +1 -1
- package/dist/cjs/tsconfig.tsbuildinfo +1 -1
- package/dist/cjs/utils/ast-utils.d.ts.map +1 -1
- package/dist/cjs/utils/ast-utils.js +1 -0
- package/dist/cjs/utils/ast-utils.js.map +1 -1
- package/dist/esm/node-handlers/node-to-jsx/index.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-jsx/index.js +32 -0
- package/dist/esm/node-handlers/node-to-jsx/index.js.map +1 -1
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/dist/esm/utils/ast-utils.d.ts.map +1 -1
- package/dist/esm/utils/ast-utils.js +1 -0
- package/dist/esm/utils/ast-utils.js.map +1 -1
- package/package.json +4 -4
- package/src/node-handlers/node-to-jsx/index.ts +51 -0
- package/src/utils/ast-utils.ts +1 -0
|
@@ -282,6 +282,7 @@ const trackGlobalRefsInExpression = (expression: string, params: JSXGenerationPa
|
|
|
282
282
|
const KNOWN_GLOBAL_REF_IDS = new Set([
|
|
283
283
|
'locale',
|
|
284
284
|
'locales',
|
|
285
|
+
'company',
|
|
285
286
|
'currentUser',
|
|
286
287
|
'userIsLoggedIn',
|
|
287
288
|
'ecommerce',
|
|
@@ -358,6 +359,55 @@ const isReactChartjs2Chart = (
|
|
|
358
359
|
return REACT_CHARTJS2_CHART_TAGS.has(elementType)
|
|
359
360
|
}
|
|
360
361
|
|
|
362
|
+
/**
|
|
363
|
+
* A chart whose data did not arrive — a query the data source refused, a
|
|
364
|
+
* state still holding an empty fallback — draws an empty chart instead of
|
|
365
|
+
* taking the page down: react-chartjs-2 reads `data.labels` on its first
|
|
366
|
+
* render, and on the server that throw fails the render of the whole page.
|
|
367
|
+
* Rewrites `data={X}` into
|
|
368
|
+
* data={Array.isArray(X?.datasets) ? X : { labels: [], datasets: [] }}
|
|
369
|
+
* A data object written out in place, or hoisted to a constant of the
|
|
370
|
+
* module, needs no guard and is left as is.
|
|
371
|
+
*/
|
|
372
|
+
const guardReactChartJs2DataAttr = (
|
|
373
|
+
chartElement: types.JSXElement,
|
|
374
|
+
hoistedConstants: JSXGenerationParams['hoistedConstants']
|
|
375
|
+
): void => {
|
|
376
|
+
const attr = chartElement.openingElement.attributes.find(
|
|
377
|
+
(a): a is types.JSXAttribute =>
|
|
378
|
+
a.type === 'JSXAttribute' && a.name.type === 'JSXIdentifier' && a.name.name === 'data'
|
|
379
|
+
)
|
|
380
|
+
if (!attr || attr.value?.type !== 'JSXExpressionContainer') {
|
|
381
|
+
return
|
|
382
|
+
}
|
|
383
|
+
const source = attr.value.expression
|
|
384
|
+
const hoisted =
|
|
385
|
+
types.isIdentifier(source) && hoistedConstants.some((constant) => constant.name === source.name)
|
|
386
|
+
if (!types.isExpression(source) || types.isObjectExpression(source) || hoisted) {
|
|
387
|
+
return
|
|
388
|
+
}
|
|
389
|
+
attr.value = types.jsxExpressionContainer(
|
|
390
|
+
types.conditionalExpression(
|
|
391
|
+
types.callExpression(
|
|
392
|
+
types.memberExpression(types.identifier('Array'), types.identifier('isArray')),
|
|
393
|
+
[
|
|
394
|
+
types.optionalMemberExpression(
|
|
395
|
+
types.cloneNode(source),
|
|
396
|
+
types.identifier('datasets'),
|
|
397
|
+
false,
|
|
398
|
+
true
|
|
399
|
+
),
|
|
400
|
+
]
|
|
401
|
+
),
|
|
402
|
+
types.cloneNode(source),
|
|
403
|
+
types.objectExpression([
|
|
404
|
+
types.objectProperty(types.identifier('labels'), types.arrayExpression([])),
|
|
405
|
+
types.objectProperty(types.identifier('datasets'), types.arrayExpression([])),
|
|
406
|
+
])
|
|
407
|
+
)
|
|
408
|
+
)
|
|
409
|
+
}
|
|
410
|
+
|
|
361
411
|
/**
|
|
362
412
|
* Chart.js with maintainAspectRatio:false fills the parent; if height is on the chart
|
|
363
413
|
* component via className, react-chartjs-2 may not apply it to the canvas host correctly
|
|
@@ -1034,6 +1084,7 @@ const generateElementNode: NodeToJSX<UIDLElementNode, types.JSXElement> = (
|
|
|
1034
1084
|
}
|
|
1035
1085
|
|
|
1036
1086
|
if (dependency && isReactChartjs2Chart(dependency, originalElementName)) {
|
|
1087
|
+
guardReactChartJs2DataAttr(elementTag, params.hoistedConstants)
|
|
1037
1088
|
const wrapped = wrapReactChartJs2Element(elementTag, node)
|
|
1038
1089
|
nodesLookup[key] = wrapped
|
|
1039
1090
|
return wrapped
|
package/src/utils/ast-utils.ts
CHANGED