@teleporthq/teleport-plugin-common 0.45.5-alpha.0 → 0.45.15
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__/builders/html-builders.ts +9 -2
- package/__tests__/builders/style-builders.ts +135 -1
- package/__tests__/node-handlers/node-to-html/index.ts +2 -0
- package/__tests__/node-handlers/node-to-jsx/attribute-value-mapper.ts +77 -0
- package/__tests__/node-handlers/node-to-jsx/calendar-events-reviver.ts +176 -0
- package/__tests__/node-handlers/node-to-jsx/chart-data-guard.ts +101 -0
- package/__tests__/node-handlers/node-to-jsx/cms-list-repeater-ecommerce-source.ts +79 -0
- package/__tests__/node-handlers/node-to-jsx/cms-list-repeater-loading.ts +122 -0
- package/__tests__/node-handlers/node-to-jsx/index.ts +902 -3
- package/__tests__/node-handlers/node-to-jsx/url-search-params-expression.ts +59 -0
- package/__tests__/node-handlers/node-to-jsx/utils.ts +635 -3
- package/__tests__/utils/ast-utils.ts +188 -0
- package/__tests__/utils/content-property-sanitiser.ts +156 -0
- package/__tests__/utils/page-seeds-state-from-data-source.ts +70 -0
- package/__tests__/utils/parse-string-with-template-expressions.ts +82 -0
- package/__tests__/utils/resource-auth-scheme.ts +55 -0
- package/__tests__/utils/route-utils.ts +118 -0
- package/__tests__/utils/style-utils.ts +59 -0
- package/__tests__/utils/template-expression-balance.ts +83 -0
- package/__tests__/utils/url-query-writer.ts +239 -0
- package/__tests__/utils/url-search-param-sync.ts +138 -0
- package/__tests__/utils/url-search-params.ts +48 -0
- package/dist/cjs/builders/ast-builders.d.ts +2 -0
- package/dist/cjs/builders/ast-builders.d.ts.map +1 -1
- package/dist/cjs/builders/ast-builders.js +41 -3
- package/dist/cjs/builders/ast-builders.js.map +1 -1
- package/dist/cjs/builders/hast-builders.d.ts +1 -0
- package/dist/cjs/builders/hast-builders.d.ts.map +1 -1
- package/dist/cjs/builders/hast-builders.js +11 -1
- package/dist/cjs/builders/hast-builders.js.map +1 -1
- package/dist/cjs/builders/style-builders.d.ts +8 -2
- package/dist/cjs/builders/style-builders.d.ts.map +1 -1
- package/dist/cjs/builders/style-builders.js +80 -33
- package/dist/cjs/builders/style-builders.js.map +1 -1
- package/dist/cjs/index.d.ts +6 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +13 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/constants.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/constants.js +2 -0
- package/dist/cjs/node-handlers/node-to-html/constants.js.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/index.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/index.js +69 -1
- package/dist/cjs/node-handlers/node-to-html/index.js.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/types.d.ts +5 -1
- package/dist/cjs/node-handlers/node-to-html/types.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/utils.d.ts +2 -1
- package/dist/cjs/node-handlers/node-to-html/utils.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/utils.js +174 -13
- package/dist/cjs/node-handlers/node-to-html/utils.js.map +1 -1
- package/dist/cjs/node-handlers/node-to-jsx/index.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-jsx/index.js +1401 -45
- package/dist/cjs/node-handlers/node-to-jsx/index.js.map +1 -1
- package/dist/cjs/node-handlers/node-to-jsx/types.d.ts +60 -6
- package/dist/cjs/node-handlers/node-to-jsx/types.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-jsx/utils.d.ts +95 -7
- package/dist/cjs/node-handlers/node-to-jsx/utils.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-jsx/utils.js +966 -72
- package/dist/cjs/node-handlers/node-to-jsx/utils.js.map +1 -1
- package/dist/cjs/tsconfig.tsbuildinfo +1 -1
- package/dist/cjs/utils/ast-utils.d.ts +163 -11
- package/dist/cjs/utils/ast-utils.d.ts.map +1 -1
- package/dist/cjs/utils/ast-utils.js +1013 -75
- package/dist/cjs/utils/ast-utils.js.map +1 -1
- package/dist/cjs/utils/hast-utils.d.ts +8 -6
- package/dist/cjs/utils/hast-utils.d.ts.map +1 -1
- package/dist/cjs/utils/hast-utils.js +28 -4
- package/dist/cjs/utils/hast-utils.js.map +1 -1
- package/dist/cjs/utils/nested-selector-commas.d.ts +2 -0
- package/dist/cjs/utils/nested-selector-commas.d.ts.map +1 -0
- package/dist/cjs/utils/nested-selector-commas.js +81 -0
- package/dist/cjs/utils/nested-selector-commas.js.map +1 -0
- package/dist/cjs/utils/route-utils.d.ts +117 -0
- package/dist/cjs/utils/route-utils.d.ts.map +1 -0
- package/dist/cjs/utils/route-utils.js +212 -0
- package/dist/cjs/utils/route-utils.js.map +1 -0
- package/dist/cjs/utils/style-utils.d.ts +1 -0
- package/dist/cjs/utils/style-utils.d.ts.map +1 -1
- package/dist/cjs/utils/style-utils.js +181 -2
- package/dist/cjs/utils/style-utils.js.map +1 -1
- package/dist/cjs/utils/template-expression-balance.d.ts +58 -0
- package/dist/cjs/utils/template-expression-balance.d.ts.map +1 -0
- package/dist/cjs/utils/template-expression-balance.js +181 -0
- package/dist/cjs/utils/template-expression-balance.js.map +1 -0
- package/dist/cjs/utils/types.d.ts +3 -0
- package/dist/cjs/utils/types.d.ts.map +1 -0
- package/dist/cjs/utils/types.js +3 -0
- package/dist/cjs/utils/types.js.map +1 -0
- package/dist/cjs/utils/url-query-writer.d.ts +123 -0
- package/dist/cjs/utils/url-query-writer.d.ts.map +1 -0
- package/dist/cjs/utils/url-query-writer.js +260 -0
- package/dist/cjs/utils/url-query-writer.js.map +1 -0
- package/dist/cjs/utils/url-search-param-sync.d.ts +4 -0
- package/dist/cjs/utils/url-search-param-sync.d.ts.map +1 -0
- package/dist/cjs/utils/url-search-param-sync.js +241 -0
- package/dist/cjs/utils/url-search-param-sync.js.map +1 -0
- package/dist/cjs/utils/url-search-params.d.ts +48 -0
- package/dist/cjs/utils/url-search-params.d.ts.map +1 -0
- package/dist/cjs/utils/url-search-params.js +109 -0
- package/dist/cjs/utils/url-search-params.js.map +1 -0
- package/dist/esm/builders/ast-builders.d.ts +2 -0
- package/dist/esm/builders/ast-builders.d.ts.map +1 -1
- package/dist/esm/builders/ast-builders.js +40 -4
- package/dist/esm/builders/ast-builders.js.map +1 -1
- package/dist/esm/builders/hast-builders.d.ts +1 -0
- package/dist/esm/builders/hast-builders.d.ts.map +1 -1
- package/dist/esm/builders/hast-builders.js +9 -0
- package/dist/esm/builders/hast-builders.js.map +1 -1
- package/dist/esm/builders/style-builders.d.ts +8 -2
- package/dist/esm/builders/style-builders.d.ts.map +1 -1
- package/dist/esm/builders/style-builders.js +78 -32
- package/dist/esm/builders/style-builders.js.map +1 -1
- package/dist/esm/index.d.ts +6 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +5 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/node-handlers/node-to-html/constants.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-html/constants.js +2 -0
- package/dist/esm/node-handlers/node-to-html/constants.js.map +1 -1
- package/dist/esm/node-handlers/node-to-html/index.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-html/index.js +71 -3
- package/dist/esm/node-handlers/node-to-html/index.js.map +1 -1
- package/dist/esm/node-handlers/node-to-html/types.d.ts +5 -1
- package/dist/esm/node-handlers/node-to-html/types.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-html/utils.d.ts +2 -1
- package/dist/esm/node-handlers/node-to-html/utils.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-html/utils.js +170 -13
- package/dist/esm/node-handlers/node-to-html/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 +1400 -47
- package/dist/esm/node-handlers/node-to-jsx/index.js.map +1 -1
- package/dist/esm/node-handlers/node-to-jsx/types.d.ts +60 -6
- package/dist/esm/node-handlers/node-to-jsx/types.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-jsx/utils.d.ts +95 -7
- package/dist/esm/node-handlers/node-to-jsx/utils.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-jsx/utils.js +950 -70
- package/dist/esm/node-handlers/node-to-jsx/utils.js.map +1 -1
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/dist/esm/utils/ast-utils.d.ts +163 -11
- package/dist/esm/utils/ast-utils.d.ts.map +1 -1
- package/dist/esm/utils/ast-utils.js +989 -75
- package/dist/esm/utils/ast-utils.js.map +1 -1
- package/dist/esm/utils/hast-utils.d.ts +8 -6
- package/dist/esm/utils/hast-utils.d.ts.map +1 -1
- package/dist/esm/utils/hast-utils.js +26 -3
- package/dist/esm/utils/hast-utils.js.map +1 -1
- package/dist/esm/utils/nested-selector-commas.d.ts +2 -0
- package/dist/esm/utils/nested-selector-commas.d.ts.map +1 -0
- package/dist/esm/utils/nested-selector-commas.js +77 -0
- package/dist/esm/utils/nested-selector-commas.js.map +1 -0
- package/dist/esm/utils/route-utils.d.ts +117 -0
- package/dist/esm/utils/route-utils.d.ts.map +1 -0
- package/dist/esm/utils/route-utils.js +179 -0
- package/dist/esm/utils/route-utils.js.map +1 -0
- package/dist/esm/utils/style-utils.d.ts +1 -0
- package/dist/esm/utils/style-utils.d.ts.map +1 -1
- package/dist/esm/utils/style-utils.js +179 -1
- package/dist/esm/utils/style-utils.js.map +1 -1
- package/dist/esm/utils/template-expression-balance.d.ts +58 -0
- package/dist/esm/utils/template-expression-balance.d.ts.map +1 -0
- package/dist/esm/utils/template-expression-balance.js +174 -0
- package/dist/esm/utils/template-expression-balance.js.map +1 -0
- package/dist/esm/utils/types.d.ts +3 -0
- package/dist/esm/utils/types.d.ts.map +1 -0
- package/dist/esm/utils/types.js +2 -0
- package/dist/esm/utils/types.js.map +1 -0
- package/dist/esm/utils/url-query-writer.d.ts +123 -0
- package/dist/esm/utils/url-query-writer.d.ts.map +1 -0
- package/dist/esm/utils/url-query-writer.js +229 -0
- package/dist/esm/utils/url-query-writer.js.map +1 -0
- package/dist/esm/utils/url-search-param-sync.d.ts +4 -0
- package/dist/esm/utils/url-search-param-sync.d.ts.map +1 -0
- package/dist/esm/utils/url-search-param-sync.js +213 -0
- package/dist/esm/utils/url-search-param-sync.js.map +1 -0
- package/dist/esm/utils/url-search-params.d.ts +48 -0
- package/dist/esm/utils/url-search-params.d.ts.map +1 -0
- package/dist/esm/utils/url-search-params.js +81 -0
- package/dist/esm/utils/url-search-params.js.map +1 -0
- package/package.json +4 -4
- package/src/builders/ast-builders.ts +80 -3
- package/src/builders/hast-builders.ts +11 -0
- package/src/builders/style-builders.ts +121 -43
- package/src/index.ts +29 -1
- package/src/node-handlers/node-to-html/constants.ts +2 -0
- package/src/node-handlers/node-to-html/index.ts +126 -6
- package/src/node-handlers/node-to-html/types.ts +6 -0
- package/src/node-handlers/node-to-html/utils.ts +228 -14
- package/src/node-handlers/node-to-jsx/index.ts +2077 -60
- package/src/node-handlers/node-to-jsx/types.ts +75 -32
- package/src/node-handlers/node-to-jsx/utils.ts +1298 -83
- package/src/utils/ast-utils.ts +1316 -127
- package/src/utils/hast-utils.ts +37 -9
- package/src/utils/nested-selector-commas.ts +88 -0
- package/src/utils/route-utils.ts +214 -0
- package/src/utils/style-utils.ts +183 -1
- package/src/utils/template-expression-balance.ts +196 -0
- package/src/utils/types.ts +25 -0
- package/src/utils/url-query-writer.ts +318 -0
- package/src/utils/url-search-param-sync.ts +303 -0
- package/src/utils/url-search-params.ts +122 -0
|
@@ -3,7 +3,7 @@ import { createHTMLNode, createTextNode } from '../../src/builders/hast-builders
|
|
|
3
3
|
describe('HTML Builders', () => {
|
|
4
4
|
describe('createHTMLNode', () => {
|
|
5
5
|
it('should create html node', () => {
|
|
6
|
-
const htmlNode = createHTMLNode('span', ['span'])
|
|
6
|
+
const htmlNode = createHTMLNode('span', [createHTMLNode('span', [])])
|
|
7
7
|
|
|
8
8
|
expect(htmlNode).toHaveProperty('type')
|
|
9
9
|
expect(htmlNode).toHaveProperty('tagName')
|
|
@@ -12,7 +12,14 @@ describe('HTML Builders', () => {
|
|
|
12
12
|
expect(htmlNode.type).toBe('element')
|
|
13
13
|
expect(htmlNode.tagName).toBe('span')
|
|
14
14
|
expect(htmlNode.properties).toEqual({})
|
|
15
|
-
expect(htmlNode.children).toEqual([
|
|
15
|
+
expect(htmlNode.children).toEqual([
|
|
16
|
+
{
|
|
17
|
+
children: [],
|
|
18
|
+
tagName: 'span',
|
|
19
|
+
type: 'element',
|
|
20
|
+
properties: {},
|
|
21
|
+
},
|
|
22
|
+
])
|
|
16
23
|
})
|
|
17
24
|
})
|
|
18
25
|
describe('createTextNode', () => {
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
createCSSClass,
|
|
3
|
+
createCSSClassWithSelector,
|
|
4
|
+
createCSSClassWithMediaQuery,
|
|
5
|
+
generateMediaStyle,
|
|
6
|
+
generateStylesFromStyleSetDefinitions,
|
|
7
|
+
} from '../../src/builders/style-builders'
|
|
2
8
|
|
|
3
9
|
describe('CSS Class Generation', () => {
|
|
4
10
|
it('with empty styles', () => {
|
|
@@ -26,4 +32,132 @@ describe('CSS Class Generation', () => {
|
|
|
26
32
|
other-key: otherValue;
|
|
27
33
|
}`)
|
|
28
34
|
})
|
|
35
|
+
|
|
36
|
+
it('attaches an ATTRIBUTE subselector with no separating space', () => {
|
|
37
|
+
// ⛔ THE SELECTED-PAGE CONTRACT. A numbered pagination strip repeats one
|
|
38
|
+
// authored node per visible page, so the copies cannot differ in the
|
|
39
|
+
// document. Both runtimes instead mark the showing page with
|
|
40
|
+
// `aria-current="page"` (`numbered-pagination.ts` here, `element-node.tsx`
|
|
41
|
+
// in the canvas editor), and the LOOK is a compound project class whose
|
|
42
|
+
// subselector is that attribute.
|
|
43
|
+
//
|
|
44
|
+
// A space before the bracket would turn this into a DESCENDANT selector —
|
|
45
|
+
// `.thq-page-number [aria-current='page']` matches something INSIDE the
|
|
46
|
+
// button, never the button — and the selected page would silently render
|
|
47
|
+
// identically to every other one. Which is exactly the bug the marker was
|
|
48
|
+
// added to fix.
|
|
49
|
+
const result = createCSSClassWithSelector('thq-page-number', "&[aria-current='page']", {
|
|
50
|
+
background: '#2563eb',
|
|
51
|
+
fontWeight: '600',
|
|
52
|
+
})
|
|
53
|
+
expect(result).toEqual(`.thq-page-number[aria-current='page'] {
|
|
54
|
+
background: #2563eb;
|
|
55
|
+
font-weight: 600;
|
|
56
|
+
}`)
|
|
57
|
+
})
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
describe('commas inside brackets or quotes survive selector nesting', () => {
|
|
61
|
+
const styles = { color: 'red' }
|
|
62
|
+
|
|
63
|
+
it('keeps a selector list inside :is() as authored', () => {
|
|
64
|
+
expect(createCSSClassWithSelector('root', '& > a:is(.x, .y)', styles))
|
|
65
|
+
.toEqual(`.root > a:is(.x, .y) {
|
|
66
|
+
color: red;
|
|
67
|
+
}`)
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('keeps a selector list inside :not() followed by a state', () => {
|
|
71
|
+
expect(createCSSClassWithSelector('root', '&:not(.a, .b):hover', styles))
|
|
72
|
+
.toEqual(`.root:not(.a, .b):hover {
|
|
73
|
+
color: red;
|
|
74
|
+
}`)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('keeps a comma inside a quoted attribute value', () => {
|
|
78
|
+
expect(createCSSClassWithSelector('root', '&[data-x="a, b"]', styles))
|
|
79
|
+
.toEqual(`.root[data-x="a, b"] {
|
|
80
|
+
color: red;
|
|
81
|
+
}`)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
it('keeps commas in nested brackets, after an escaped quote and after an escaped bracket', () => {
|
|
85
|
+
expect(
|
|
86
|
+
createCSSClassWithSelector('root', '& a:is(.x, :not(.y, .z))[title="a\\", b"]', styles)
|
|
87
|
+
).toContain('.root a:is(.x, :not(.y, .z))[title="a\\", b"] {')
|
|
88
|
+
expect(createCSSClassWithSelector('root', '& .a\\(b, .c', styles)).toContain(
|
|
89
|
+
'.root .a\\(b, .root .c {'
|
|
90
|
+
)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('still gives every piece of a top-level list the parent', () => {
|
|
94
|
+
expect(createCSSClassWithSelector('root', '& h1, h2', styles)).toEqual(`.root h1, .root h2 {
|
|
95
|
+
color: red;
|
|
96
|
+
}`)
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('splits a mixed selector only on its top-level comma', () => {
|
|
100
|
+
expect(createCSSClassWithSelector('root', '& h1, & h2:is(.a, .b)', styles))
|
|
101
|
+
.toEqual(`.root h1, .root h2:is(.a, .b) {
|
|
102
|
+
color: red;
|
|
103
|
+
}`)
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it('leaves a selector that already holds the placeholder character untouched', () => {
|
|
107
|
+
const result = createCSSClassWithSelector('root', '& a[title="\uE0C1"]:is(.x, .y)', styles)
|
|
108
|
+
expect(result).toContain('\uE0C1')
|
|
109
|
+
expect(result).toContain('.root a[title="\uE0C1"]:is(.x, .root .y) {')
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it('never rewrites a declaration value that holds the placeholder character', () => {
|
|
113
|
+
const result = createCSSClassWithSelector('root', '&::before', { content: '"\uE0C1"' })
|
|
114
|
+
expect(result).toContain('content: "\uE0C1";')
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it('keeps them inside a media query', () => {
|
|
118
|
+
const result = createCSSClassWithMediaQuery('max-width: 767px', {
|
|
119
|
+
root: { '& > a:is(.x, .y)': styles } as unknown as Record<string, string | number>,
|
|
120
|
+
plain: styles,
|
|
121
|
+
})
|
|
122
|
+
expect(result).toContain('@media(max-width: 767px)')
|
|
123
|
+
expect(result).toContain('.root > a:is(.x, .y) {')
|
|
124
|
+
expect(result).toContain('.plain {')
|
|
125
|
+
expect(result).not.toContain('.root .y')
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
it('keeps them through a project style sheet: base rule, state and screen size', () => {
|
|
129
|
+
const cssMap: string[] = []
|
|
130
|
+
const mediaStylesMap: Parameters<typeof generateMediaStyle>[0] = {}
|
|
131
|
+
generateStylesFromStyleSetDefinitions(
|
|
132
|
+
{
|
|
133
|
+
links: {
|
|
134
|
+
type: 'reusable-project-style-map',
|
|
135
|
+
className: 'links',
|
|
136
|
+
subselectors: ' > a:is(.a, .b)',
|
|
137
|
+
content: { color: { type: 'static', content: 'red' } },
|
|
138
|
+
conditions: [
|
|
139
|
+
{
|
|
140
|
+
type: 'element-state',
|
|
141
|
+
meta: { state: 'hover' },
|
|
142
|
+
content: { color: { type: 'static', content: 'blue' } },
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
type: 'screen-size',
|
|
146
|
+
meta: { maxWidth: 767 },
|
|
147
|
+
content: { color: { type: 'static', content: 'green' } },
|
|
148
|
+
},
|
|
149
|
+
],
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
cssMap,
|
|
153
|
+
mediaStylesMap,
|
|
154
|
+
(name) => name
|
|
155
|
+
)
|
|
156
|
+
const css = [...cssMap, ...generateMediaStyle(mediaStylesMap)].join('\n')
|
|
157
|
+
|
|
158
|
+
expect(css).toContain('.links > a:is(.a, .b) {')
|
|
159
|
+
expect(css).toContain('.links > a:is(.a, .b):hover {')
|
|
160
|
+
expect(css).toMatch(/@media\(max-width: 767px\) \{\s+\.links > a:is\(\.a, \.b\) \{/)
|
|
161
|
+
expect(css).not.toContain('.links .b')
|
|
162
|
+
})
|
|
29
163
|
})
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import generator from '@babel/generator'
|
|
2
|
+
import * as types from '@babel/types'
|
|
3
|
+
import generateJSXSyntax from '../../../src/node-handlers/node-to-jsx'
|
|
4
|
+
import {
|
|
5
|
+
JSXGenerationParams,
|
|
6
|
+
JSXGenerationOptions,
|
|
7
|
+
} from '../../../src/node-handlers/node-to-jsx/types'
|
|
8
|
+
import { UIDLElementNode } from '@teleporthq/teleport-types'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* An attribute bound through a value mapper renders through it, as a text
|
|
12
|
+
* binding does: a filter chip's `aria-pressed` reads the filter's state as
|
|
13
|
+
* "true" or "false", never as the filter's value.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const MAPPER = [
|
|
17
|
+
'function mapValue(value) {',
|
|
18
|
+
" return String(value) === 'On track' ? 'true' : 'false';",
|
|
19
|
+
'}',
|
|
20
|
+
].join('\n')
|
|
21
|
+
|
|
22
|
+
const chip = (valueMapper?: string): UIDLElementNode => ({
|
|
23
|
+
type: 'element',
|
|
24
|
+
content: {
|
|
25
|
+
key: 'chip',
|
|
26
|
+
elementType: 'button',
|
|
27
|
+
attrs: {
|
|
28
|
+
'aria-pressed': {
|
|
29
|
+
type: 'dynamic',
|
|
30
|
+
content: {
|
|
31
|
+
referenceType: 'state',
|
|
32
|
+
id: 'jobFilterStatus',
|
|
33
|
+
refPath: [],
|
|
34
|
+
...(valueMapper ? { valueMapper } : {}),
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
children: [],
|
|
39
|
+
},
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
const params = (): JSXGenerationParams => ({
|
|
43
|
+
dependencies: {},
|
|
44
|
+
propDefinitions: {},
|
|
45
|
+
stateDefinitions: {
|
|
46
|
+
jobFilterStatus: { type: 'string', defaultValue: '' },
|
|
47
|
+
},
|
|
48
|
+
globalStateDefinitions: {},
|
|
49
|
+
nodesLookup: {},
|
|
50
|
+
windowImports: {},
|
|
51
|
+
localeReferences: [],
|
|
52
|
+
localeAttributeReferences: [],
|
|
53
|
+
globalReferences: [],
|
|
54
|
+
globalStateReferences: [],
|
|
55
|
+
hoistedConstants: [],
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
const options: JSXGenerationOptions = {
|
|
59
|
+
dynamicReferencePrefixMap: { prop: 'props', state: '', local: '' },
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const render = (node: UIDLElementNode): string =>
|
|
63
|
+
generator(generateJSXSyntax(node, params(), options) as types.Node).code.replace(/\s+/g, ' ')
|
|
64
|
+
|
|
65
|
+
describe('a dynamic attribute with a value mapper', () => {
|
|
66
|
+
it('renders the mapped value', () => {
|
|
67
|
+
const code = render(chip(MAPPER))
|
|
68
|
+
expect(code).toContain('aria-pressed={')
|
|
69
|
+
expect(code).toContain('mapValue(value)')
|
|
70
|
+
expect(code).toContain('jobFilterStatus')
|
|
71
|
+
expect(code).not.toContain('aria-pressed={jobFilterStatus}')
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('keeps a bare reference without one', () => {
|
|
75
|
+
expect(render(chip())).toContain('aria-pressed={jobFilterStatus}')
|
|
76
|
+
})
|
|
77
|
+
})
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import * as types from '@babel/types'
|
|
2
|
+
import generateJSXSyntax from '../../../src/node-handlers/node-to-jsx'
|
|
3
|
+
import {
|
|
4
|
+
JSXGenerationParams,
|
|
5
|
+
JSXGenerationOptions,
|
|
6
|
+
} from '../../../src/node-handlers/node-to-jsx/types'
|
|
7
|
+
import { UIDLAttributeValue, UIDLDependency, UIDLElementNode } from '@teleporthq/teleport-types'
|
|
8
|
+
|
|
9
|
+
const CALENDARKIT_DEPENDENCY: UIDLDependency = {
|
|
10
|
+
type: 'package',
|
|
11
|
+
path: 'calendarkit-basic',
|
|
12
|
+
version: '1.1.0',
|
|
13
|
+
meta: {
|
|
14
|
+
namedImport: true,
|
|
15
|
+
},
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Mirrors the post-resolver shape produced by the GUI's calendar-to-uidl
|
|
19
|
+
// mapper: elementType is the React component name, dependency is embedded.
|
|
20
|
+
const calendarNode = (
|
|
21
|
+
attrs: Record<string, UIDLAttributeValue>,
|
|
22
|
+
dependency: UIDLDependency = CALENDARKIT_DEPENDENCY,
|
|
23
|
+
elementType = 'BasicScheduler'
|
|
24
|
+
): UIDLElementNode => ({
|
|
25
|
+
type: 'element',
|
|
26
|
+
content: {
|
|
27
|
+
key: 'calendar',
|
|
28
|
+
elementType,
|
|
29
|
+
dependency,
|
|
30
|
+
attrs,
|
|
31
|
+
children: [],
|
|
32
|
+
},
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
const buildParams = (): JSXGenerationParams => ({
|
|
36
|
+
dependencies: {},
|
|
37
|
+
propDefinitions: {},
|
|
38
|
+
stateDefinitions: {},
|
|
39
|
+
globalStateDefinitions: {},
|
|
40
|
+
nodesLookup: {},
|
|
41
|
+
windowImports: {},
|
|
42
|
+
localeReferences: [],
|
|
43
|
+
localeAttributeReferences: [],
|
|
44
|
+
globalReferences: [],
|
|
45
|
+
globalStateReferences: [],
|
|
46
|
+
hoistedConstants: [],
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
const options: JSXGenerationOptions = {
|
|
50
|
+
dynamicReferencePrefixMap: {
|
|
51
|
+
prop: 'props',
|
|
52
|
+
state: '',
|
|
53
|
+
local: '',
|
|
54
|
+
},
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const getEventsAttr = (element: types.JSXElement): types.JSXAttribute | undefined =>
|
|
58
|
+
element.openingElement.attributes.find(
|
|
59
|
+
(attr): attr is types.JSXAttribute =>
|
|
60
|
+
attr.type === 'JSXAttribute' &&
|
|
61
|
+
attr.name.type === 'JSXIdentifier' &&
|
|
62
|
+
attr.name.name === 'events'
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
// Asserts the `.map((e) => ({ ...e, start: new Date(e.start), end: new Date(e.end) }))`
|
|
66
|
+
// wrapper and returns the expression being mapped over (the guard or the array).
|
|
67
|
+
const expectEventsReviver = (eventsAttr: types.JSXAttribute): types.Expression => {
|
|
68
|
+
expect(eventsAttr.value?.type).toBe('JSXExpressionContainer')
|
|
69
|
+
const expression = (eventsAttr.value as types.JSXExpressionContainer).expression
|
|
70
|
+
|
|
71
|
+
expect(expression.type).toBe('CallExpression')
|
|
72
|
+
const mapCall = expression as types.CallExpression
|
|
73
|
+
expect(mapCall.callee.type).toBe('MemberExpression')
|
|
74
|
+
const mapCallee = mapCall.callee as types.MemberExpression
|
|
75
|
+
expect((mapCallee.property as types.Identifier).name).toBe('map')
|
|
76
|
+
|
|
77
|
+
const arrow = mapCall.arguments[0] as types.ArrowFunctionExpression
|
|
78
|
+
expect(arrow.type).toBe('ArrowFunctionExpression')
|
|
79
|
+
const body = arrow.body as types.ObjectExpression
|
|
80
|
+
expect(body.type).toBe('ObjectExpression')
|
|
81
|
+
expect(body.properties[0].type).toBe('SpreadElement')
|
|
82
|
+
|
|
83
|
+
const dateProps = body.properties.slice(1) as types.ObjectProperty[]
|
|
84
|
+
const propNames = dateProps.map((prop) => (prop.key as types.Identifier).name)
|
|
85
|
+
expect(propNames).toEqual(['start', 'end'])
|
|
86
|
+
dateProps.forEach((prop) => {
|
|
87
|
+
expect(prop.value.type).toBe('NewExpression')
|
|
88
|
+
expect(((prop.value as types.NewExpression).callee as types.Identifier).name).toBe('Date')
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
return mapCallee.object as types.Expression
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const expectArrayIsArrayGuard = (mapSource: types.Expression) => {
|
|
95
|
+
expect(mapSource.type).toBe('ConditionalExpression')
|
|
96
|
+
const guard = mapSource as types.ConditionalExpression
|
|
97
|
+
expect(guard.test.type).toBe('CallExpression')
|
|
98
|
+
const isArrayCall = guard.test as types.CallExpression
|
|
99
|
+
const isArrayCallee = isArrayCall.callee as types.MemberExpression
|
|
100
|
+
expect((isArrayCallee.object as types.Identifier).name).toBe('Array')
|
|
101
|
+
expect((isArrayCallee.property as types.Identifier).name).toBe('isArray')
|
|
102
|
+
expect(guard.alternate.type).toBe('ArrayExpression')
|
|
103
|
+
expect((guard.alternate as types.ArrayExpression).elements.length).toBe(0)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
describe('calendarkit-basic BasicScheduler events revival', () => {
|
|
107
|
+
it('wraps a static events array with the Date revival map (no redundant guard)', () => {
|
|
108
|
+
const node = calendarNode({
|
|
109
|
+
view: { type: 'static', content: 'month' },
|
|
110
|
+
events: {
|
|
111
|
+
type: 'static',
|
|
112
|
+
content: [
|
|
113
|
+
{ id: '1', title: 'Standup', start: '2026-06-10T09:00:00', end: '2026-06-10T09:30:00' },
|
|
114
|
+
],
|
|
115
|
+
} as unknown as UIDLAttributeValue,
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
const result = generateJSXSyntax(node, buildParams(), options) as types.JSXElement
|
|
119
|
+
const eventsAttr = getEventsAttr(result)
|
|
120
|
+
|
|
121
|
+
expect(eventsAttr).toBeDefined()
|
|
122
|
+
const mapSource = expectEventsReviver(eventsAttr as types.JSXAttribute)
|
|
123
|
+
expect(mapSource.type).toBe('ArrayExpression')
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('wraps a dynamic state-bound events reference with the Array.isArray guard', () => {
|
|
127
|
+
const node = calendarNode({
|
|
128
|
+
events: {
|
|
129
|
+
type: 'dynamic',
|
|
130
|
+
content: { referenceType: 'state', id: 'calendarEvents' },
|
|
131
|
+
},
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
const result = generateJSXSyntax(node, buildParams(), options) as types.JSXElement
|
|
135
|
+
const eventsAttr = getEventsAttr(result)
|
|
136
|
+
|
|
137
|
+
expect(eventsAttr).toBeDefined()
|
|
138
|
+
const mapSource = expectEventsReviver(eventsAttr as types.JSXAttribute)
|
|
139
|
+
expectArrayIsArrayGuard(mapSource)
|
|
140
|
+
|
|
141
|
+
const guard = mapSource as types.ConditionalExpression
|
|
142
|
+
const isArrayArg = (guard.test as types.CallExpression).arguments[0] as types.Identifier
|
|
143
|
+
expect(isArrayArg.name).toBe('calendarEvents')
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
it('leaves the element untouched when events is missing — the library defaults to []', () => {
|
|
147
|
+
const node = calendarNode({
|
|
148
|
+
view: { type: 'static', content: 'week' },
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
const result = generateJSXSyntax(node, buildParams(), options) as types.JSXElement
|
|
152
|
+
|
|
153
|
+
expect(getEventsAttr(result)).toBeUndefined()
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
it('does not touch events attrs on elements from other packages', () => {
|
|
157
|
+
const node = calendarNode(
|
|
158
|
+
{
|
|
159
|
+
events: {
|
|
160
|
+
type: 'dynamic',
|
|
161
|
+
content: { referenceType: 'state', id: 'calendarEvents' },
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
{ type: 'package', path: 'some-other-lib', version: '1.0.0' },
|
|
165
|
+
'OtherScheduler'
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
const result = generateJSXSyntax(node, buildParams(), options) as types.JSXElement
|
|
169
|
+
const eventsAttr = getEventsAttr(result)
|
|
170
|
+
|
|
171
|
+
expect(eventsAttr).toBeDefined()
|
|
172
|
+
const expression = ((eventsAttr as types.JSXAttribute).value as types.JSXExpressionContainer)
|
|
173
|
+
.expression
|
|
174
|
+
expect(expression.type).not.toBe('CallExpression')
|
|
175
|
+
})
|
|
176
|
+
})
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import * as types from '@babel/types'
|
|
2
|
+
import generate from '@babel/generator'
|
|
3
|
+
import generateJSXSyntax from '../../../src/node-handlers/node-to-jsx'
|
|
4
|
+
import {
|
|
5
|
+
JSXGenerationParams,
|
|
6
|
+
JSXGenerationOptions,
|
|
7
|
+
} from '../../../src/node-handlers/node-to-jsx/types'
|
|
8
|
+
import { UIDLAttributeValue, UIDLDependency, UIDLElementNode } from '@teleporthq/teleport-types'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A react-chartjs-2 chart whose data did not arrive draws empty: the page's
|
|
12
|
+
* server render must never fail because a query was refused.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const CHARTJS_DEPENDENCY: UIDLDependency = {
|
|
16
|
+
type: 'package',
|
|
17
|
+
path: 'react-chartjs-2',
|
|
18
|
+
version: '5.2.0',
|
|
19
|
+
meta: { namedImport: true },
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const chartNode = (
|
|
23
|
+
attrs: Record<string, UIDLAttributeValue>,
|
|
24
|
+
dependency: UIDLDependency = CHARTJS_DEPENDENCY,
|
|
25
|
+
elementType = 'Bar'
|
|
26
|
+
): UIDLElementNode => ({
|
|
27
|
+
type: 'element',
|
|
28
|
+
content: { key: 'chart', elementType, dependency, attrs, children: [] },
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
const buildParams = (): JSXGenerationParams => ({
|
|
32
|
+
dependencies: {},
|
|
33
|
+
propDefinitions: {},
|
|
34
|
+
stateDefinitions: {},
|
|
35
|
+
globalStateDefinitions: {},
|
|
36
|
+
nodesLookup: {},
|
|
37
|
+
windowImports: {},
|
|
38
|
+
localeReferences: [],
|
|
39
|
+
localeAttributeReferences: [],
|
|
40
|
+
globalReferences: [],
|
|
41
|
+
globalStateReferences: [],
|
|
42
|
+
hoistedConstants: [],
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
const options: JSXGenerationOptions = {
|
|
46
|
+
dynamicReferencePrefixMap: { prop: 'props', state: '', local: '' },
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The chart element inside the bounded wrapper the generator draws around it. */
|
|
50
|
+
const chartOf = (result: types.JSXElement): types.JSXElement =>
|
|
51
|
+
result.children.find((child): child is types.JSXElement => child.type === 'JSXElement')
|
|
52
|
+
|
|
53
|
+
const dataCode = (chart: types.JSXElement): string => {
|
|
54
|
+
const attr = chart.openingElement.attributes.find(
|
|
55
|
+
(candidate): candidate is types.JSXAttribute =>
|
|
56
|
+
candidate.type === 'JSXAttribute' &&
|
|
57
|
+
candidate.name.type === 'JSXIdentifier' &&
|
|
58
|
+
candidate.name.name === 'data'
|
|
59
|
+
)
|
|
60
|
+
const value = attr?.value as types.JSXExpressionContainer
|
|
61
|
+
return generate(value.expression as types.Expression).code.replace(/\s+/g, ' ')
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
describe('react-chartjs-2 data guard', () => {
|
|
65
|
+
it('draws an empty chart when the bound data is missing', () => {
|
|
66
|
+
const node = chartNode({
|
|
67
|
+
data: { type: 'dynamic', content: { referenceType: 'state', id: 'salesChart' } },
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
const result = generateJSXSyntax(node, buildParams(), options) as types.JSXElement
|
|
71
|
+
|
|
72
|
+
expect(dataCode(chartOf(result))).toBe(
|
|
73
|
+
'Array.isArray(salesChart?.datasets) ? salesChart : { labels: [], datasets: [] }'
|
|
74
|
+
)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('leaves a data object written out in place, hoisted to a constant, as it is', () => {
|
|
78
|
+
const node = chartNode({
|
|
79
|
+
data: {
|
|
80
|
+
type: 'static',
|
|
81
|
+
content: { labels: ['Mon'], datasets: [{ label: 'Sales', data: [3] }] },
|
|
82
|
+
} as unknown as UIDLAttributeValue,
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
const result = generateJSXSyntax(node, buildParams(), options) as types.JSXElement
|
|
86
|
+
|
|
87
|
+
expect(dataCode(chartOf(result))).toBe('__staticProp0')
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('does not touch the data of components from other packages', () => {
|
|
91
|
+
const node = chartNode(
|
|
92
|
+
{ data: { type: 'dynamic', content: { referenceType: 'state', id: 'rows' } } },
|
|
93
|
+
{ type: 'package', path: 'some-other-lib', version: '1.0.0' },
|
|
94
|
+
'Bar'
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
const result = generateJSXSyntax(node, buildParams(), options) as types.JSXElement
|
|
98
|
+
|
|
99
|
+
expect(dataCode(result)).toBe('rows')
|
|
100
|
+
})
|
|
101
|
+
})
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import * as types from '@babel/types'
|
|
2
|
+
import generator from '@babel/generator'
|
|
3
|
+
import { elementNode, staticNode } from '@teleporthq/teleport-uidl-builders'
|
|
4
|
+
import { UIDLNode } from '@teleporthq/teleport-types'
|
|
5
|
+
import generateJSXSyntax from '../../../src/node-handlers/node-to-jsx'
|
|
6
|
+
import {
|
|
7
|
+
JSXGenerationParams,
|
|
8
|
+
JSXGenerationOptions,
|
|
9
|
+
} from '../../../src/node-handlers/node-to-jsx/types'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A repeater over the `ecommerce` global carries no array path — the editor's
|
|
13
|
+
* mapper keeps only the global's id — so the generator picks the array from the
|
|
14
|
+
* repeater's render-prop identifier. These pin that table: each e-commerce list
|
|
15
|
+
* the editor builds must land on the context array it was bound to, and an
|
|
16
|
+
* identifier the table does not know still means the cart's items.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const params = (): JSXGenerationParams => ({
|
|
20
|
+
dependencies: {},
|
|
21
|
+
propDefinitions: {},
|
|
22
|
+
stateDefinitions: {},
|
|
23
|
+
globalStateDefinitions: {},
|
|
24
|
+
nodesLookup: {},
|
|
25
|
+
windowImports: {},
|
|
26
|
+
localeReferences: [],
|
|
27
|
+
localeAttributeReferences: [],
|
|
28
|
+
globalReferences: [],
|
|
29
|
+
globalStateReferences: [],
|
|
30
|
+
hoistedConstants: [],
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
const options: JSXGenerationOptions = {
|
|
34
|
+
dynamicReferencePrefixMap: {
|
|
35
|
+
prop: 'props',
|
|
36
|
+
state: '',
|
|
37
|
+
local: '',
|
|
38
|
+
},
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const generate = (renderPropIdentifier: string): string => {
|
|
42
|
+
const repeater = {
|
|
43
|
+
type: 'cms-list-repeater',
|
|
44
|
+
content: {
|
|
45
|
+
elementType: 'Repeater',
|
|
46
|
+
name: 'repeater',
|
|
47
|
+
key: 'repeater',
|
|
48
|
+
renderPropIdentifier,
|
|
49
|
+
source: 'ecommerce',
|
|
50
|
+
nodes: {
|
|
51
|
+
list: elementNode('container', {}, [staticNode('row')]),
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
} as unknown as UIDLNode
|
|
55
|
+
const tree = elementNode('container', {}, [repeater])
|
|
56
|
+
return generator(generateJSXSyntax(tree, params(), options) as types.JSXElement).code
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
describe('cms-list-repeater over the ecommerce global', () => {
|
|
60
|
+
it('lists the shipping methods a checkout binds, not the cart items', () => {
|
|
61
|
+
expect(generate('shippingMethod')).toContain('items={ecommerce?.Cart?.shippingOptions || []}')
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('lists the discount sets a checkout offers, in both fulfilment shapes', () => {
|
|
65
|
+
expect(generate('discountOption')).toContain('items={ecommerce?.Cart?.discountOptions || []}')
|
|
66
|
+
expect(generate('discountOptionPickup')).toContain(
|
|
67
|
+
'items={ecommerce?.Cart?.discountOptionsPickup || []}'
|
|
68
|
+
)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it('keeps the payment-provider and store-location lists on their arrays', () => {
|
|
72
|
+
expect(generate('paymentProvider')).toContain('items={ecommerce?.paymentProviders || []}')
|
|
73
|
+
expect(generate('storeLocation')).toContain('items={ecommerce?.storeLocations || []}')
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('reads an identifier it does not know as the cart items', () => {
|
|
77
|
+
expect(generate('orderItem')).toContain('items={ecommerce?.Cart?.items || []}')
|
|
78
|
+
})
|
|
79
|
+
})
|