@barefootjs/jsx 0.23.0 → 0.24.1
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/dist/analyzer.d.ts.map +1 -1
- package/dist/errors.d.ts +1 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/format-date-lowering.d.ts +6 -3
- package/dist/format-date-lowering.d.ts.map +1 -1
- package/dist/index.js +475 -57
- package/dist/ir-to-client-js/emit-reactive.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/to-locale-date-lowering.d.ts +62 -23
- package/dist/to-locale-date-lowering.d.ts.map +1 -1
- package/dist/types.d.ts +27 -3
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/format-date-lowering.test.ts +22 -6
- package/src/__tests__/reactive-factory-cross-file.test.ts +263 -0
- package/src/__tests__/to-locale-date-lowering.test.ts +203 -2
- package/src/analyzer.ts +350 -52
- package/src/errors.ts +6 -0
- package/src/format-date-lowering.ts +9 -5
- package/src/ir-to-client-js/emit-reactive.ts +14 -4
- package/src/jsx-to-ir.ts +15 -4
- package/src/to-locale-date-lowering.ts +437 -49
- package/src/types.ts +28 -3
|
@@ -70,6 +70,7 @@ describe('matchToLocaleDateStringCall accept/decline table', () => {
|
|
|
70
70
|
{ kind: 'identifier', name: 'createdAt' },
|
|
71
71
|
{ kind: 'literal', value: 'M/D/YYYY', literalType: 'string' },
|
|
72
72
|
{ kind: 'literal', value: 'UTC', literalType: 'string' },
|
|
73
|
+
{ kind: 'array-literal', elements: [], raw: '[]' },
|
|
73
74
|
],
|
|
74
75
|
})
|
|
75
76
|
})
|
|
@@ -82,6 +83,7 @@ describe('matchToLocaleDateStringCall accept/decline table', () => {
|
|
|
82
83
|
{ kind: 'identifier', name: 'createdAt' },
|
|
83
84
|
{ kind: 'literal', value: 'YYYY/M/D' },
|
|
84
85
|
{ kind: 'literal', value: '+09:00' },
|
|
86
|
+
{ kind: 'array-literal', elements: [] },
|
|
85
87
|
],
|
|
86
88
|
})
|
|
87
89
|
})
|
|
@@ -101,9 +103,10 @@ describe('matchToLocaleDateStringCall accept/decline table', () => {
|
|
|
101
103
|
expect(match(`createdAt.toLocaleDateString('ja-JP', { timeZone: '+25:00' })`)).toBeNull()
|
|
102
104
|
expect(match(`createdAt.toLocaleDateString('ja-JP', { timeZone: '+99:99' })`)).toBeNull()
|
|
103
105
|
expect(match(`createdAt.toLocaleDateString('ja-JP', { timeZone: '-12:60' })`)).toBeNull()
|
|
104
|
-
// options
|
|
105
|
-
expect(match(`createdAt.toLocaleDateString('ja-JP', { timeZone: 'UTC', month: 'long' })`)).toBeNull()
|
|
106
|
+
// an options bag WITHOUT timeZone still declines (host-TZ read)
|
|
106
107
|
expect(match(`createdAt.toLocaleDateString('ja-JP', { dateStyle: 'long' })`)).toBeNull()
|
|
108
|
+
// a non-literal option value declines (unprobeable)
|
|
109
|
+
expect(match(`createdAt.toLocaleDateString('en-US', { timeZone: 'UTC', dateStyle: style })`)).toBeNull()
|
|
107
110
|
// unrepresentable locale default
|
|
108
111
|
expect(match(`createdAt.toLocaleDateString('ar-SA', { timeZone: 'UTC' })`)).toBeNull()
|
|
109
112
|
})
|
|
@@ -118,6 +121,204 @@ describe('matchToLocaleDateStringCall accept/decline table', () => {
|
|
|
118
121
|
})
|
|
119
122
|
})
|
|
120
123
|
|
|
124
|
+
describe('name tokens via the options bag (#2334)', () => {
|
|
125
|
+
const md = metadata(DATE_PROP_SRC)
|
|
126
|
+
|
|
127
|
+
function match(expr: string) {
|
|
128
|
+
const { callee, args } = callParts(expr)
|
|
129
|
+
return matchToLocaleDateStringCall(callee, args, md)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
test('dateStyle long resolves a named pattern plus the 38-slot table', () => {
|
|
133
|
+
const node = match(`createdAt.toLocaleDateString('en-US', { dateStyle: 'long', timeZone: 'UTC' })`)
|
|
134
|
+
expect(node).toMatchObject({
|
|
135
|
+
helper: 'format_date',
|
|
136
|
+
args: [
|
|
137
|
+
{ kind: 'identifier', name: 'createdAt' },
|
|
138
|
+
{ kind: 'literal', value: 'MMMM D, YYYY' },
|
|
139
|
+
{ kind: 'literal', value: 'UTC' },
|
|
140
|
+
{ kind: 'array-literal' },
|
|
141
|
+
],
|
|
142
|
+
})
|
|
143
|
+
const names = (node as { args: ParsedExpr[] }).args[3] as Extract<ParsedExpr, { kind: 'array-literal' }>
|
|
144
|
+
expect(names.elements).toHaveLength(38)
|
|
145
|
+
expect(names.elements[2]).toEqual({ kind: 'literal', value: 'March', literalType: 'string' })
|
|
146
|
+
expect(names.elements[24]).toEqual({ kind: 'literal', value: 'Sunday', literalType: 'string' })
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
test('dateStyle full adds the weekday token; medium picks abbreviated names', () => {
|
|
150
|
+
expect(match(`createdAt.toLocaleDateString('en-US', { dateStyle: 'full', timeZone: 'UTC' })`)).toMatchObject({
|
|
151
|
+
args: [expect.anything(), { kind: 'literal', value: 'dddd, MMMM D, YYYY' }, expect.anything(), expect.anything()],
|
|
152
|
+
})
|
|
153
|
+
expect(match(`createdAt.toLocaleDateString('en-US', { dateStyle: 'medium', timeZone: 'UTC' })`)).toMatchObject({
|
|
154
|
+
args: [expect.anything(), { kind: 'literal', value: 'MMM D, YYYY' }, expect.anything(), expect.anything()],
|
|
155
|
+
})
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
test("ja-JP's long form is numeric — the probe ships no table", () => {
|
|
159
|
+
expect(match(`createdAt.toLocaleDateString('ja-JP', { dateStyle: 'long', timeZone: 'UTC' })`)).toMatchObject({
|
|
160
|
+
args: [
|
|
161
|
+
expect.anything(),
|
|
162
|
+
{ kind: 'literal', value: 'YYYY年M月D日' },
|
|
163
|
+
{ kind: 'literal', value: 'UTC' },
|
|
164
|
+
{ kind: 'array-literal', elements: [] },
|
|
165
|
+
],
|
|
166
|
+
})
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
test('context-inflected month names pick the form the format actually uses (Copilot, #2336)', () => {
|
|
170
|
+
// Russian inflects month names by date context: dateStyle 'long'
|
|
171
|
+
// renders genitive `марта`, а month-only format nominative `март`.
|
|
172
|
+
// Each call site ships the table whose form ICU uses THERE, verified
|
|
173
|
+
// at a second instant so a probe-index coincidence can't slip through.
|
|
174
|
+
const long = match(`createdAt.toLocaleDateString('ru-RU', { dateStyle: 'long', timeZone: 'UTC' })`)
|
|
175
|
+
// NOTE: read args BEFORE any toMatchObject with expect.anything() —
|
|
176
|
+
// bun 1.3.11's toMatchObject MUTATES the received object, emptying
|
|
177
|
+
// entries an expect.anything() matched (minimal repro pinned in this
|
|
178
|
+
// PR's description; upstream bun bug).
|
|
179
|
+
const longNames = (long as { args: ParsedExpr[] }).args[3] as Extract<ParsedExpr, { kind: 'array-literal' }>
|
|
180
|
+
const longPattern = (long as { args: ParsedExpr[] }).args[1]
|
|
181
|
+
expect(longPattern).toEqual({ kind: 'literal', value: 'D MMMM YYYY г.', literalType: 'string' })
|
|
182
|
+
expect(longNames.elements[2]).toEqual({ kind: 'literal', value: 'марта', literalType: 'string' })
|
|
183
|
+
|
|
184
|
+
const monthOnly = match(`createdAt.toLocaleDateString('ru-RU', { month: 'long', timeZone: 'UTC' })`)
|
|
185
|
+
const standaloneNames = (monthOnly as { args: ParsedExpr[] }).args[3] as Extract<ParsedExpr, { kind: 'array-literal' }>
|
|
186
|
+
expect(standaloneNames.elements[2]).toEqual({ kind: 'literal', value: 'март', literalType: 'string' })
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
test('unreproducible forms decline loudly: 2-digit year, era', () => {
|
|
190
|
+
expect(match(`createdAt.toLocaleDateString('en-US', { dateStyle: 'short', timeZone: 'UTC' })`)).toBeNull()
|
|
191
|
+
expect(match(`createdAt.toLocaleDateString('en-US', { era: 'short', year: 'numeric', timeZone: 'UTC' })`)).toBeNull()
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
test('client rewrite carries the names table', () => {
|
|
195
|
+
const result = compile(`
|
|
196
|
+
export function Foo({ createdAt }: { createdAt: Date }) {
|
|
197
|
+
return <div>{createdAt.toLocaleDateString('en-US', { dateStyle: 'long', timeZone: 'UTC' })}</div>
|
|
198
|
+
}
|
|
199
|
+
`)
|
|
200
|
+
expect(result.errors.filter((e) => e.code === ErrorCodes.UNSUPPORTED_JSX_PATTERN)).toEqual([])
|
|
201
|
+
const js = result.files.find((f) => f.type === 'clientJs')!.content
|
|
202
|
+
expect(js).toContain('formatDate(_p.createdAt, "MMMM D, YYYY", "UTC", ["January","February"')
|
|
203
|
+
expect(js).not.toContain('toLocaleDateString')
|
|
204
|
+
})
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
describe('union-typed locale (#2324 union stage)', () => {
|
|
208
|
+
const UNION_SRC = `
|
|
209
|
+
function Foo({ createdAt, locale }: { createdAt: Date; locale: 'en-US' | 'ja-JP' }) {
|
|
210
|
+
return <div>{createdAt.toLocaleDateString(locale, { timeZone: 'UTC' })}</div>
|
|
211
|
+
}
|
|
212
|
+
export { Foo }
|
|
213
|
+
`
|
|
214
|
+
|
|
215
|
+
test('a required closed string-literal union lowers to a ternary pattern', () => {
|
|
216
|
+
const md = metadata(UNION_SRC)
|
|
217
|
+
const { callee, args } = callParts(`createdAt.toLocaleDateString(locale, { timeZone: 'UTC' })`)
|
|
218
|
+
expect(matchToLocaleDateStringCall(callee, args, md)).toEqual({
|
|
219
|
+
kind: 'helper-call',
|
|
220
|
+
helper: 'format_date',
|
|
221
|
+
args: [
|
|
222
|
+
{ kind: 'identifier', name: 'createdAt' },
|
|
223
|
+
{
|
|
224
|
+
kind: 'conditional',
|
|
225
|
+
test: {
|
|
226
|
+
kind: 'binary',
|
|
227
|
+
op: '===',
|
|
228
|
+
left: { kind: 'identifier', name: 'locale' },
|
|
229
|
+
right: { kind: 'literal', value: 'en-US', literalType: 'string' },
|
|
230
|
+
},
|
|
231
|
+
consequent: { kind: 'literal', value: 'M/D/YYYY', literalType: 'string' },
|
|
232
|
+
alternate: { kind: 'literal', value: 'YYYY/M/D', literalType: 'string' },
|
|
233
|
+
},
|
|
234
|
+
{ kind: 'literal', value: 'UTC', literalType: 'string' },
|
|
235
|
+
// both members are numeric-only, so the names tables are equal ([])
|
|
236
|
+
// and collapse to a single empty leaf
|
|
237
|
+
{ kind: 'array-literal', elements: [], raw: '[]' },
|
|
238
|
+
],
|
|
239
|
+
})
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
test('members sharing one pattern collapse the ternary to a literal', () => {
|
|
243
|
+
const md = metadata(`
|
|
244
|
+
function Foo({ createdAt, locale }: { createdAt: Date; locale: 'en-US' | 'en' }) {
|
|
245
|
+
return <div>{createdAt.toLocaleDateString(locale, { timeZone: 'UTC' })}</div>
|
|
246
|
+
}
|
|
247
|
+
export { Foo }
|
|
248
|
+
`)
|
|
249
|
+
const { callee, args } = callParts(`createdAt.toLocaleDateString(locale, { timeZone: 'UTC' })`)
|
|
250
|
+
expect(matchToLocaleDateStringCall(callee, args, md)).toMatchObject({
|
|
251
|
+
helper: 'format_date',
|
|
252
|
+
args: [
|
|
253
|
+
{ kind: 'identifier', name: 'createdAt' },
|
|
254
|
+
{ kind: 'literal', value: 'M/D/YYYY' },
|
|
255
|
+
{ kind: 'literal', value: 'UTC' },
|
|
256
|
+
{ kind: 'array-literal', elements: [] },
|
|
257
|
+
],
|
|
258
|
+
})
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
test('object-props mode: accepts props.<name>, declines a bare identifier (never a prop there)', () => {
|
|
262
|
+
const md = metadata(`
|
|
263
|
+
export function Foo(props: { createdAt: Date; locale: 'en-US' | 'ja-JP' }) {
|
|
264
|
+
return <div>{props.createdAt.toLocaleDateString(props.locale, { timeZone: 'UTC' })}</div>
|
|
265
|
+
}
|
|
266
|
+
`)
|
|
267
|
+
const member = callParts(`props.createdAt.toLocaleDateString(props.locale, { timeZone: 'UTC' })`)
|
|
268
|
+
expect(matchToLocaleDateStringCall(member.callee, member.args, md)).toMatchObject({
|
|
269
|
+
helper: 'format_date',
|
|
270
|
+
args: [
|
|
271
|
+
expect.anything(),
|
|
272
|
+
{ kind: 'conditional' },
|
|
273
|
+
{ kind: 'literal', value: 'UTC' },
|
|
274
|
+
{ kind: 'array-literal', elements: [] },
|
|
275
|
+
],
|
|
276
|
+
})
|
|
277
|
+
// A bare `locale` identifier in object-props mode is a LOCAL binding,
|
|
278
|
+
// not the prop — even when a same-named prop exists (Copilot, #2331).
|
|
279
|
+
const bare = callParts(`props.createdAt.toLocaleDateString(locale, { timeZone: 'UTC' })`)
|
|
280
|
+
expect(matchToLocaleDateStringCall(bare.callee, bare.args, md)).toBeNull()
|
|
281
|
+
})
|
|
282
|
+
|
|
283
|
+
test('destructured mode: declines a props.<name> member (no props object exists)', () => {
|
|
284
|
+
const md = metadata(UNION_SRC)
|
|
285
|
+
const { callee, args } = callParts(`createdAt.toLocaleDateString(props.locale, { timeZone: 'UTC' })`)
|
|
286
|
+
expect(matchToLocaleDateStringCall(callee, args, md)).toBeNull()
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
test('declines an OPTIONAL union prop (undefined would read the host locale)', () => {
|
|
290
|
+
const md = metadata(`
|
|
291
|
+
function Foo({ createdAt, locale }: { createdAt: Date; locale?: 'en-US' | 'ja-JP' }) {
|
|
292
|
+
return <div>{/* @client */ createdAt.toLocaleDateString(locale ?? 'en-US', { timeZone: 'UTC' })}</div>
|
|
293
|
+
}
|
|
294
|
+
export { Foo }
|
|
295
|
+
`)
|
|
296
|
+
const { callee, args } = callParts(`createdAt.toLocaleDateString(locale, { timeZone: 'UTC' })`)
|
|
297
|
+
expect(matchToLocaleDateStringCall(callee, args, md)).toBeNull()
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
test('declines a union containing an unrepresentable member', () => {
|
|
301
|
+
const md = metadata(`
|
|
302
|
+
function Foo({ createdAt, locale }: { createdAt: Date; locale: 'en-US' | 'ar-SA' }) {
|
|
303
|
+
return <div>{/* @client */ createdAt.toLocaleDateString(locale, { timeZone: 'UTC' })}</div>
|
|
304
|
+
}
|
|
305
|
+
export { Foo }
|
|
306
|
+
`)
|
|
307
|
+
const { callee, args } = callParts(`createdAt.toLocaleDateString(locale, { timeZone: 'UTC' })`)
|
|
308
|
+
expect(matchToLocaleDateStringCall(callee, args, md)).toBeNull()
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
test('compiles clean (no BF021) and rewrites client JS to a ternary formatDate', () => {
|
|
312
|
+
const result = compile(UNION_SRC)
|
|
313
|
+
expect(result.errors.filter((e) => e.code === ErrorCodes.UNSUPPORTED_JSX_PATTERN)).toEqual([])
|
|
314
|
+
const js = result.files.find((f) => f.type === 'clientJs')!.content
|
|
315
|
+
expect(js).toContain(
|
|
316
|
+
'formatDate(_p.createdAt, _p.locale === "en-US" ? "M/D/YYYY" : "YYYY/M/D", "UTC")',
|
|
317
|
+
)
|
|
318
|
+
expect(js).not.toContain('toLocaleDateString')
|
|
319
|
+
})
|
|
320
|
+
})
|
|
321
|
+
|
|
121
322
|
describe('BF021 exemption round trip (#2273 seam)', () => {
|
|
122
323
|
test('the claimed literal shape compiles clean; the runtime-locale shape still fires BF021', () => {
|
|
123
324
|
const clean = compile(DATE_PROP_SRC)
|