@barefootjs/jsx 0.39.5 → 0.39.6
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/adapters/array-method.d.ts +93 -0
- package/dist/adapters/array-method.d.ts.map +1 -0
- package/dist/adapters/attr-plan.d.ts +135 -0
- package/dist/adapters/attr-plan.d.ts.map +1 -0
- package/dist/adapters/boolean-result.d.ts +84 -9
- package/dist/adapters/boolean-result.d.ts.map +1 -1
- package/dist/adapters/callback-method.d.ts +126 -0
- package/dist/adapters/callback-method.d.ts.map +1 -0
- package/dist/adapters/component-props.d.ts +99 -0
- package/dist/adapters/component-props.d.ts.map +1 -0
- package/dist/adapters/component-tree.d.ts +25 -0
- package/dist/adapters/component-tree.d.ts.map +1 -0
- package/dist/adapters/literal-value.d.ts +22 -0
- package/dist/adapters/literal-value.d.ts.map +1 -0
- package/dist/adapters/loop-plan.d.ts +53 -0
- package/dist/adapters/loop-plan.d.ts.map +1 -0
- package/dist/adapters/prop-classes.d.ts +37 -0
- package/dist/adapters/prop-classes.d.ts.map +1 -0
- package/dist/expression-parser.d.ts +38 -0
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/index.d.ts +17 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1765 -914
- package/dist/jsx-to-ir.d.ts +465 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +19 -0
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +1 -1
- package/src/__tests__/action-accessor.test.ts +6 -24
- package/src/__tests__/array-method-catalogue.test.ts +99 -0
- package/src/__tests__/attr-plan.test.ts +121 -0
- package/src/__tests__/binding-scope-ratchet.test.ts +12 -12
- package/src/__tests__/boolean-result-expr.test.ts +106 -0
- package/src/__tests__/boolean-result.test.ts +34 -1
- package/src/__tests__/callback-method.test.ts +189 -0
- package/src/__tests__/component-props.test.ts +148 -0
- package/src/__tests__/delegated-handler-preamble.test.ts +4 -4
- package/src/__tests__/dsl-shared-helpers.test.ts +166 -0
- package/src/__tests__/issue-3240-nested-static-map-event-delegation.test.ts +1 -1
- package/src/__tests__/loop-plan.test.ts +104 -0
- package/src/__tests__/map-call-decisions.test.ts +521 -0
- package/src/__tests__/nullable-signal-attr.test.ts +45 -0
- package/src/adapters/array-method.ts +193 -0
- package/src/adapters/attr-plan.ts +200 -0
- package/src/adapters/boolean-result.ts +208 -12
- package/src/adapters/callback-method.ts +301 -0
- package/src/adapters/component-props.ts +144 -0
- package/src/adapters/component-tree.ts +118 -0
- package/src/adapters/literal-value.ts +36 -0
- package/src/adapters/loop-plan.ts +88 -0
- package/src/adapters/prop-classes.ts +73 -0
- package/src/expression-parser.ts +537 -374
- package/src/index.ts +53 -2
- package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +9 -7
- package/src/jsx-to-ir.ts +1328 -991
- package/src/nullable-signal-attr.ts +78 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shared array / string method catalogue (#3476): what each method
|
|
3
|
+
* lowers to, independent of any DSL's spelling.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { describe, test, expect } from 'bun:test'
|
|
7
|
+
import {
|
|
8
|
+
ARRAY_METHOD_CATALOGUE,
|
|
9
|
+
emitArrayMethod,
|
|
10
|
+
lowerArrayMethod,
|
|
11
|
+
prefixedArrayMethodSpelling,
|
|
12
|
+
} from '../adapters/array-method'
|
|
13
|
+
import type { ArrayMethod } from '../adapters/parsed-expr-emitter'
|
|
14
|
+
import type { ParsedExpr } from '../expression-parser'
|
|
15
|
+
|
|
16
|
+
const id = (name: string): ParsedExpr => ({ kind: 'identifier', name })
|
|
17
|
+
const recv = id('xs')
|
|
18
|
+
const a = id('a')
|
|
19
|
+
const b = id('b')
|
|
20
|
+
const c = id('c')
|
|
21
|
+
const emit = (e: ParsedExpr) => (e.kind === 'identifier' ? e.name : '?')
|
|
22
|
+
const spelling = prefixedArrayMethodSpelling('bf.', 'UNDEF')
|
|
23
|
+
const lower = (method: ArrayMethod, args: ParsedExpr[]) =>
|
|
24
|
+
emitArrayMethod(lowerArrayMethod(method, recv, args), emit, spelling)
|
|
25
|
+
|
|
26
|
+
describe('lowerArrayMethod', () => {
|
|
27
|
+
test('fills a defaulted argument the source left out', () => {
|
|
28
|
+
expect(lower('join', [])).toBe("bf.join(xs, ',')")
|
|
29
|
+
expect(lower('at', [])).toBe('bf.at(xs, 0)')
|
|
30
|
+
expect(lower('toFixed', [])).toBe('bf.to_fixed(xs, 0)')
|
|
31
|
+
expect(lower('repeat', [])).toBe('bf.repeat(xs, 0)')
|
|
32
|
+
expect(lower('padStart', [])).toBe('bf.pad_start(xs, 0)')
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
test("slice's end defaults to the backend's undefined literal, its start to 0", () => {
|
|
36
|
+
expect(lower('slice', [])).toBe('bf.slice(xs, 0, UNDEF)')
|
|
37
|
+
expect(lower('slice', [a])).toBe('bf.slice(xs, a, UNDEF)')
|
|
38
|
+
expect(lower('slice', [a, b])).toBe('bf.slice(xs, a, b)')
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
test('an optional argument the source left out shortens the call', () => {
|
|
42
|
+
expect(lower('split', [])).toBe('bf.split(xs)')
|
|
43
|
+
expect(lower('split', [a])).toBe('bf.split(xs, a)')
|
|
44
|
+
expect(lower('split', [a, b])).toBe('bf.split(xs, a, b)')
|
|
45
|
+
expect(lower('startsWith', [a])).toBe('bf.starts_with(xs, a)')
|
|
46
|
+
expect(lower('endsWith', [a, b])).toBe('bf.ends_with(xs, a, b)')
|
|
47
|
+
expect(lower('padEnd', [a])).toBe('bf.pad_end(xs, a)')
|
|
48
|
+
expect(lower('padEnd', [a, b])).toBe('bf.pad_end(xs, a, b)')
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('arguments past the catalogued ones are dropped', () => {
|
|
52
|
+
// JS's `fromIndex` is not lowered.
|
|
53
|
+
expect(lower('indexOf', [a, b])).toBe('bf.index_of(xs, a)')
|
|
54
|
+
expect(lower('concat', [a, b])).toBe('bf.concat(xs, a)')
|
|
55
|
+
expect(lower('trim', [a])).toBe('bf.trim(xs)')
|
|
56
|
+
expect(lower('padStart', [a, b, c])).toBe('bf.pad_start(xs, a, b)')
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('concat with no argument is the receiver itself', () => {
|
|
60
|
+
expect(lowerArrayMethod('concat', recv, [])).toEqual({ kind: 'receiver', object: recv })
|
|
61
|
+
expect(lower('concat', [])).toBe('xs')
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
test('a reversed copy and the in-place reverse share one helper', () => {
|
|
65
|
+
expect(lower('reverse', [])).toBe('bf.reverse(xs)')
|
|
66
|
+
expect(lower('toReversed', [])).toBe('bf.reverse(xs)')
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test('a missing required argument throws (the parser refuses such a call)', () => {
|
|
70
|
+
expect(() => lowerArrayMethod('replace', recv, [a])).toThrow("'.replace()' needs argument 2")
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
test('emits the receiver first, then the arguments in order', () => {
|
|
74
|
+
const order: string[] = []
|
|
75
|
+
const recording = (e: ParsedExpr) => (order.push(emit(e)), emit(e))
|
|
76
|
+
emitArrayMethod(lowerArrayMethod('padStart', recv, [a, b]), recording, spelling)
|
|
77
|
+
expect(order).toEqual(['xs', 'a', 'b'])
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
describe('ARRAY_METHOD_CATALOGUE', () => {
|
|
82
|
+
test('string defaults need no escaping in any DSL', () => {
|
|
83
|
+
for (const entry of Object.values(ARRAY_METHOD_CATALOGUE)) {
|
|
84
|
+
for (const param of entry.params) {
|
|
85
|
+
if (param.kind === 'default' && param.value.kind === 'string') {
|
|
86
|
+
expect(param.value.value).not.toMatch(/['\\]/)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
test('a required argument never follows an optional one', () => {
|
|
93
|
+
for (const [method, entry] of Object.entries(ARRAY_METHOD_CATALOGUE)) {
|
|
94
|
+
const kinds = entry.params.map((p) => p.kind)
|
|
95
|
+
const firstOptional = kinds.indexOf('optional')
|
|
96
|
+
if (firstOptional !== -1) expect({ method, rest: kinds.slice(firstOptional) }).toEqual({ method, rest: kinds.slice(firstOptional).map(() => 'optional') })
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
})
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The attribute emission plan shared by the DSL adapters (#3478): which kind
|
|
3
|
+
* `planAttrExpression` picks for an `{expr}` attribute value, and what
|
|
4
|
+
* `planSpreadAttr` makes of a `{...spread}`.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, test, expect } from 'bun:test'
|
|
8
|
+
import { planAttrExpression, planSpreadAttr, unsupportedAttrExpression, type AttrPlanContext, type SpreadPlanContext } from '../adapters/attr-plan'
|
|
9
|
+
import { parseExpression } from '../expression-parser'
|
|
10
|
+
import type { ExpressionAttr, SpreadAttr } from '../types'
|
|
11
|
+
|
|
12
|
+
const expression = (expr: string, extra: Partial<ExpressionAttr> = {}): ExpressionAttr => ({
|
|
13
|
+
kind: 'expression',
|
|
14
|
+
expr,
|
|
15
|
+
parsed: parseExpression(expr),
|
|
16
|
+
...extra,
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
function context(overrides: Partial<AttrPlanContext> = {}): AttrPlanContext {
|
|
20
|
+
return {
|
|
21
|
+
propsObjectName: 'props',
|
|
22
|
+
restPropsName: 'rest',
|
|
23
|
+
nullableOptionalProps: new Set(['id', 'tag']),
|
|
24
|
+
nullableSignalGetters: new Set(['title']),
|
|
25
|
+
nullishAttrCtx: { nullableSignals: new Set(['title']), memos: new Map() },
|
|
26
|
+
isLoopBound: () => false,
|
|
27
|
+
boolStr: (expr) => expr === 'open',
|
|
28
|
+
...overrides,
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const plan = (value: ExpressionAttr, name = 'data-x', ctx = context()) => planAttrExpression(value, name, ctx)
|
|
33
|
+
|
|
34
|
+
describe('planAttrExpression', () => {
|
|
35
|
+
test('a bare read of a nullable optional prop or signal is guarded', () => {
|
|
36
|
+
expect(plan(expression('id'))).toEqual({ kind: 'omit-nullable', boolStr: false, restKey: false })
|
|
37
|
+
expect(plan(expression('props.id'))).toEqual({ kind: 'omit-nullable', boolStr: false, restKey: false })
|
|
38
|
+
expect(plan(expression('rest.tag'))).toEqual({ kind: 'omit-nullable', boolStr: false, restKey: true })
|
|
39
|
+
expect(plan(expression('title()'))).toEqual({ kind: 'omit-nullable', boolStr: false, restKey: false })
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('a loop binding that shadows the name is the row, not the prop (#2488)', () => {
|
|
43
|
+
expect(plan(expression('id'), 'data-x', context({ isLoopBound: (n) => n === 'id' }))).toEqual({ kind: 'plain', boolStr: false })
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
test('a boolean attribute is present when truthy, even over a nullable prop', () => {
|
|
47
|
+
expect(plan(expression('id'), 'disabled')).toEqual({ kind: 'boolean-attr' })
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
test('`expr || undefined` binds the value once and renders it when truthy', () => {
|
|
51
|
+
expect(plan(expression('open', { presenceOrUndefined: true }))).toEqual({ kind: 'presence', boolStr: true })
|
|
52
|
+
expect(plan(expression('id', { presenceOrUndefined: true }))).toEqual({ kind: 'presence', boolStr: false })
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('`cond ? value : undefined` carries the parsed branches', () => {
|
|
56
|
+
expect(plan(expression(`active ? 'page' : undefined`))).toMatchObject({
|
|
57
|
+
kind: 'undefined-alternate',
|
|
58
|
+
ternary: { condition: 'active', testParsed: { kind: 'identifier', name: 'active' }, consequentParsed: { kind: 'literal' } },
|
|
59
|
+
})
|
|
60
|
+
expect(plan(expression(`active ? 'page' : null`))).toMatchObject({ kind: 'undefined-alternate' })
|
|
61
|
+
expect(plan(expression(`active ? 'page' : 'none'`))).toMatchObject({ kind: 'plain' })
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
test('a value that may reach nil through a signal is bound and tested', () => {
|
|
65
|
+
expect(plan(expression(`title() ?? undefined`))).toMatchObject({ kind: 'omit-nil' })
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
test('anything else renders, through bool_str when the adapter says so', () => {
|
|
69
|
+
expect(plan(expression('open'))).toEqual({ kind: 'plain', boolStr: true })
|
|
70
|
+
expect(plan(expression('count()'))).toEqual({ kind: 'plain', boolStr: false })
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
describe('planSpreadAttr', () => {
|
|
75
|
+
const spread = (expr: string): SpreadAttr => ({ kind: 'spread', expr, parsed: parseExpression(expr) }) as SpreadAttr
|
|
76
|
+
const ctx = (overrides: Partial<SpreadPlanContext> = {}): SpreadPlanContext => ({
|
|
77
|
+
propsObjectName: 'props',
|
|
78
|
+
propsParams: [{ name: 'id' }, { name: 'tone' }],
|
|
79
|
+
localConstants: [
|
|
80
|
+
{ name: 'sizeAttrs', value: `size ? { 'data-size': size } : {}` },
|
|
81
|
+
{ name: 'alias', value: 'other' },
|
|
82
|
+
{ name: 'moduleAttrs', value: `on ? { a: 1 } : {}`, isModule: true },
|
|
83
|
+
] as SpreadPlanContext['localConstants'],
|
|
84
|
+
isBound: () => false,
|
|
85
|
+
...overrides,
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
test('{...props} enumerates the props params', () => {
|
|
89
|
+
expect(planSpreadAttr(spread(' props '), ctx())).toEqual({ kind: 'props-object', params: ['id', 'tone'] })
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
test('a function-scope const adds its initializer as a second candidate', () => {
|
|
93
|
+
const plan = planSpreadAttr(spread('sizeAttrs'), ctx())
|
|
94
|
+
expect(plan.kind === 'value' && plan.candidates.map((c) => c?.kind)).toEqual(['identifier', 'conditional'])
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
test('no second candidate for a bare-name initializer, a module const, or a shadowed name (#2489)', () => {
|
|
98
|
+
for (const [name, c] of [
|
|
99
|
+
['alias', ctx()],
|
|
100
|
+
['moduleAttrs', ctx()],
|
|
101
|
+
['sizeAttrs', ctx({ isBound: (n) => n === 'sizeAttrs' })],
|
|
102
|
+
['attrs()', ctx()],
|
|
103
|
+
] as const) {
|
|
104
|
+
const plan = planSpreadAttr(spread(name), c)
|
|
105
|
+
expect({ name, candidates: plan.kind === 'value' ? plan.candidates.length : -1 }).toEqual({ name, candidates: 1 })
|
|
106
|
+
}
|
|
107
|
+
})
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
describe('unsupportedAttrExpression', () => {
|
|
111
|
+
test('an object literal or a tagged template is refused, with the gate reason', () => {
|
|
112
|
+
expect(unsupportedAttrExpression(`{ a: 1 }`, [])).toEqual({ reason: expect.any(String) })
|
|
113
|
+
expect(unsupportedAttrExpression(`(( { a: 1 } ))`, [])).not.toBeNull()
|
|
114
|
+
expect(unsupportedAttrExpression('cn`base ${tone()}`', [])).not.toBeNull()
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
test('anything else goes on to the plan without being parsed here', () => {
|
|
118
|
+
expect(unsupportedAttrExpression('count()', [])).toBeNull()
|
|
119
|
+
expect(unsupportedAttrExpression('`plain ${x}`', [])).toBeNull()
|
|
120
|
+
})
|
|
121
|
+
})
|
|
@@ -102,12 +102,6 @@ type Pattern = (typeof PATTERNS)[number]
|
|
|
102
102
|
* ===============================================================================
|
|
103
103
|
*/
|
|
104
104
|
const ALLOWLIST: Record<string, Partial<Record<Pattern, number>>> = {
|
|
105
|
-
// FLOOR (shape 1, already guarded): `expandDynamicPropValue`'s
|
|
106
|
-
// `this.scope`-mirroring shadow guard (`erb-adapter.ts`'s own
|
|
107
|
-
// `!this.scope.isBound(trimmed)` check, #2489) precedes this `.find(` —
|
|
108
|
-
// the lookup itself is the legitimate const-resolution step once the
|
|
109
|
-
// shadow question is already answered by `scope`.
|
|
110
|
-
'packages/adapter-erb/src/adapter/erb-adapter.ts': { 'localConstants.find(': 1 },
|
|
111
105
|
// FLOOR (shape 2/3 boundary): `inlineLocalHelperCall` resolves a call's
|
|
112
106
|
// CALLEE name against module/component consts to inline a local
|
|
113
107
|
// arrow-helper's body (`sortClass(k)` → its substituted expression) — a
|
|
@@ -168,12 +162,18 @@ const ALLOWLIST: Record<string, Partial<Record<Pattern, number>>> = {
|
|
|
168
162
|
// FLOOR (shape 1): `packageModuleConst` — same `NewXxxProps` ctor-context
|
|
169
163
|
// bucket as `ctor-lowering.ts`.
|
|
170
164
|
'packages/adapter-go-template/src/adapter/memo/memo-value.ts': { 'localConstants.find(': 1 },
|
|
171
|
-
// FLOOR (shape 1, already guarded):
|
|
172
|
-
//
|
|
173
|
-
// `
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
},
|
|
165
|
+
// FLOOR (shape 1, already guarded): `planSpreadAttr`'s spread-const
|
|
166
|
+
// resolution (#3478, moved out of the ERB and Mojolicious adapters). The
|
|
167
|
+
// `ctx.isBound(name)` shadow guard (#2489, the caller's `this.scope`)
|
|
168
|
+
// precedes this `.find(` — the lookup itself is the const-resolution step
|
|
169
|
+
// once `scope` has answered the shadow question.
|
|
170
|
+
'packages/jsx/src/adapters/attr-plan.ts': { 'localConstants.find(': 1 },
|
|
171
|
+
// FLOOR (shape 1, already guarded): `planLoop`'s computed-const loop-source
|
|
172
|
+
// gate (#3473, moved out of the eight DSL adapters, which spelled it
|
|
173
|
+
// `(this.localConstants ?? []).find(` or `.some(`). The `ctx.scope.isBound`
|
|
174
|
+
// shadow check precedes this `.find(` — the lookup is the const-resolution
|
|
175
|
+
// step once the scope has answered the shadow question.
|
|
176
|
+
'packages/jsx/src/adapters/loop-plan.ts': { 'localConstants.find(': 1 },
|
|
177
177
|
// FLOOR (shape 2): `generateSignalInitializers`'s reachability analysis
|
|
178
178
|
// for SSR no-op initializer dead-code elimination — module/component-level
|
|
179
179
|
// declaration reachability, unrelated to loop-row shadowing entirely.
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { describe, test, expect } from 'bun:test'
|
|
2
|
+
import { isAriaBooleanAttr, isBooleanResultExpr } from '../adapters/boolean-result.ts'
|
|
3
|
+
|
|
4
|
+
describe('isBooleanResultExpr', () => {
|
|
5
|
+
describe('detected as boolean-result', () => {
|
|
6
|
+
test.each([
|
|
7
|
+
// Top-level comparison
|
|
8
|
+
['count() > 0'],
|
|
9
|
+
['x === y'],
|
|
10
|
+
['a !== b'],
|
|
11
|
+
['x >= 10'],
|
|
12
|
+
['a == b'],
|
|
13
|
+
// Unary logical NOT
|
|
14
|
+
['!accepted()'],
|
|
15
|
+
['!ok'],
|
|
16
|
+
// Boolean literals
|
|
17
|
+
['true'],
|
|
18
|
+
['false'],
|
|
19
|
+
// Logical combinator with both sides boolean
|
|
20
|
+
['x > 0 && y < 10'],
|
|
21
|
+
['!a || b === c'],
|
|
22
|
+
// Conditional with both branches boolean
|
|
23
|
+
['cond ? true : false'],
|
|
24
|
+
['x > 0 ? x === 1 : !y'],
|
|
25
|
+
])('"%s" is boolean-result', expr => {
|
|
26
|
+
expect(isBooleanResultExpr(expr)).toBe(true)
|
|
27
|
+
})
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
describe('not boolean-result', () => {
|
|
31
|
+
test.each([
|
|
32
|
+
// Bare identifier — could be anything; the adapter has no
|
|
33
|
+
// type info from source text. Leave unwrapped.
|
|
34
|
+
['accepted'],
|
|
35
|
+
['count'],
|
|
36
|
+
// Call expression — same reason.
|
|
37
|
+
['accepted()'],
|
|
38
|
+
['user.isAdmin()'],
|
|
39
|
+
// Member access
|
|
40
|
+
['props.checked'],
|
|
41
|
+
// Numeric / string / null literal
|
|
42
|
+
['0'],
|
|
43
|
+
['"hello"'],
|
|
44
|
+
['null'],
|
|
45
|
+
// Template literal (handled by a separate emit path)
|
|
46
|
+
['`${name}`'],
|
|
47
|
+
// Logical fallback whose right side is non-boolean — `||
|
|
48
|
+
// 'fallback'` returns a string, not a boolean
|
|
49
|
+
['x() || "fallback"'],
|
|
50
|
+
// Conditional with non-boolean branches
|
|
51
|
+
['cond ? "yes" : "no"'],
|
|
52
|
+
['ok() ? count() : 0'],
|
|
53
|
+
// Arithmetic — `+` is not a comparison
|
|
54
|
+
['a + b'],
|
|
55
|
+
])('"%s" is NOT boolean-result', expr => {
|
|
56
|
+
expect(isBooleanResultExpr(expr)).toBe(false)
|
|
57
|
+
})
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
test('returns false for unparseable input', () => {
|
|
61
|
+
// `parseExpression` always returns a `ParsedExpr` — for shapes it
|
|
62
|
+
// can't categorise it lands on `{ kind: 'unsupported' }`, which
|
|
63
|
+
// the classifier falls through to the default case and declines
|
|
64
|
+
// to wrap. The contract here is "do not throw, do not wrap".
|
|
65
|
+
expect(isBooleanResultExpr('???invalid<<')).toBe(false)
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
describe('isAriaBooleanAttr', () => {
|
|
70
|
+
test.each([
|
|
71
|
+
// Strict boolean state.
|
|
72
|
+
'aria-atomic',
|
|
73
|
+
'aria-busy',
|
|
74
|
+
'aria-disabled',
|
|
75
|
+
'aria-hidden',
|
|
76
|
+
'aria-modal',
|
|
77
|
+
'aria-multiline',
|
|
78
|
+
'aria-multiselectable',
|
|
79
|
+
'aria-readonly',
|
|
80
|
+
'aria-required',
|
|
81
|
+
// Tri-state.
|
|
82
|
+
'aria-checked',
|
|
83
|
+
'aria-pressed',
|
|
84
|
+
])('%s is recognised as ARIA boolean', (name) => {
|
|
85
|
+
expect(isAriaBooleanAttr(name)).toBe(true)
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
test.each([
|
|
89
|
+
// String-valued / token-valued ARIA attributes — wrapping with
|
|
90
|
+
// `bool_str` would coerce a user-supplied string to "true"/"false".
|
|
91
|
+
'aria-label',
|
|
92
|
+
'aria-labelledby',
|
|
93
|
+
'aria-describedby',
|
|
94
|
+
'aria-current', // page | step | location | … | true | false
|
|
95
|
+
'aria-sort', // ascending | descending | none | other
|
|
96
|
+
'aria-haspopup', // false | true | menu | listbox | tree | grid | dialog
|
|
97
|
+
'aria-invalid', // false | true | grammar | spelling
|
|
98
|
+
// Non-ARIA attributes — should also fall through.
|
|
99
|
+
'disabled',
|
|
100
|
+
'data-active',
|
|
101
|
+
'class',
|
|
102
|
+
'id',
|
|
103
|
+
])('%s is NOT recognised as ARIA boolean', (name) => {
|
|
104
|
+
expect(isAriaBooleanAttr(name)).toBe(false)
|
|
105
|
+
})
|
|
106
|
+
})
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, test, expect } from 'bun:test'
|
|
2
|
-
import { isBooleanResultParsed, stringifyBooleanTernaryBranches } from '../adapters/boolean-result'
|
|
2
|
+
import { collectBooleanValueNames, isBooleanResultParsed, stringifyBooleanTernaryBranches } from '../adapters/boolean-result'
|
|
3
3
|
import { exprToString, parseExpression } from '../expression-parser'
|
|
4
4
|
|
|
5
5
|
const rewrite = (src: string) => exprToString(stringifyBooleanTernaryBranches(parseExpression(src)!))
|
|
@@ -25,3 +25,36 @@ describe('stringifyBooleanTernaryBranches (#3349)', () => {
|
|
|
25
25
|
expect(stringifyBooleanTernaryBranches(plain)).toBe(plain)
|
|
26
26
|
})
|
|
27
27
|
})
|
|
28
|
+
|
|
29
|
+
describe('collectBooleanValueNames props (#3443)', () => {
|
|
30
|
+
const boolType = { kind: 'primitive', raw: 'boolean', primitive: 'boolean' } as const
|
|
31
|
+
const prop = (name: string, optional: boolean, defaultValue?: string) => ({
|
|
32
|
+
name,
|
|
33
|
+
type: boolType,
|
|
34
|
+
optional,
|
|
35
|
+
...(defaultValue === undefined ? {} : { defaultValue, parsed: parseExpression(defaultValue)! }),
|
|
36
|
+
})
|
|
37
|
+
const collect = (params: ReturnType<typeof prop>[]) =>
|
|
38
|
+
collectBooleanValueNames({ signals: [], memos: [], localConstants: [], propsParams: params })
|
|
39
|
+
|
|
40
|
+
test('a required or boolean-defaulted prop is boolean', () => {
|
|
41
|
+
const { names, props } = collect([prop('a', false), prop('b', true, 'false'), prop('c', true, '!x')])
|
|
42
|
+
expect([...names].sort()).toEqual(['a', 'b', 'c'])
|
|
43
|
+
expect([...props].sort()).toEqual(['a', 'b', 'c'])
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
test('a default reading an earlier proven-boolean param is boolean', () => {
|
|
47
|
+
// `{ off = false, on = off }`: `off` is in scope and boolean. A later or
|
|
48
|
+
// unproven binding (`{ a = c, c = false }`, `{ b, d = b }`) is not.
|
|
49
|
+
const { names } = collect([prop('off', true, 'false'), prop('on', true, 'off'), prop('a', true, 'c'), prop('c', true, 'false'), prop('b', true), prop('d', true, 'b')])
|
|
50
|
+
expect([...names].sort()).toEqual(['c', 'off', 'on'])
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
test('an optional prop without a boolean default is not', () => {
|
|
54
|
+
// `{ on = undefined }` still renders `undefined` when omitted; so does a
|
|
55
|
+
// default of any other non-boolean expression.
|
|
56
|
+
const { names, props } = collect([prop('a', true), prop('b', true, 'undefined'), prop('c', true, 'fallback()')])
|
|
57
|
+
expect(names.size).toBe(0)
|
|
58
|
+
expect(props.size).toBe(0)
|
|
59
|
+
})
|
|
60
|
+
})
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The higher-order callback chain shared by the DSL adapters (#3477): what
|
|
3
|
+
* each `CALLBACK_METHODS` call lowers to, independent of any DSL's spelling,
|
|
4
|
+
* and the support gate's refusal of a callback that reads its index.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, test, expect } from 'bun:test'
|
|
8
|
+
import {
|
|
9
|
+
emitCallbackEval,
|
|
10
|
+
lowerCallbackMethod,
|
|
11
|
+
type CallbackEvalSpelling,
|
|
12
|
+
type CallbackLoweringOptions,
|
|
13
|
+
} from '../adapters/callback-method'
|
|
14
|
+
import {
|
|
15
|
+
asCallbackMethodCall,
|
|
16
|
+
isSupported,
|
|
17
|
+
parseExpression,
|
|
18
|
+
unboundCallbackParamRead,
|
|
19
|
+
type ParsedExpr,
|
|
20
|
+
} from '../expression-parser'
|
|
21
|
+
|
|
22
|
+
const jinja: CallbackLoweringOptions = { adapterName: 'Jinja' }
|
|
23
|
+
const kolon: CallbackLoweringOptions = { adapterName: 'Xslate', nativePredicate: true }
|
|
24
|
+
|
|
25
|
+
function callOf(source: string) {
|
|
26
|
+
const call = asCallbackMethodCall(parseExpression(source))
|
|
27
|
+
if (!call) throw new Error(`not a callback method call: ${source}`)
|
|
28
|
+
return call
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const lower = (source: string, options = jinja) => {
|
|
32
|
+
const call = callOf(source)
|
|
33
|
+
return lowerCallbackMethod(call.method, call.arrow, call.args, options)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const spelling: CallbackEvalSpelling = {
|
|
37
|
+
call: (helper, operands) => `bf.${helper}(${operands.join(', ')})`,
|
|
38
|
+
string: (value) => `'${value.replace(/'/g, "\\'")}'`,
|
|
39
|
+
boolean: (value) => (value ? 'T' : 'F'),
|
|
40
|
+
env: (entries) => `{${entries.map(([name, value]) => `${name}=${value}`).join(', ')}}`,
|
|
41
|
+
}
|
|
42
|
+
const emitIdentifier = (e: ParsedExpr) => (e.kind === 'identifier' ? `$${e.name}` : '?')
|
|
43
|
+
|
|
44
|
+
function render(source: string, options = jinja): string {
|
|
45
|
+
const plan = lower(source, options)
|
|
46
|
+
if (plan.kind !== 'eval') throw new Error(`expected an eval plan, got ${plan.kind}`)
|
|
47
|
+
return emitCallbackEval(plan, 'RECV', emitIdentifier, spelling)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
describe('lowerCallbackMethod: predicates', () => {
|
|
51
|
+
test('a serializable predicate goes to its evaluator helper with the captured names', () => {
|
|
52
|
+
expect(render('items.filter(x => x > limit)')).toBe(
|
|
53
|
+
`bf.filter_eval(RECV, '{"kind":"binary","op":">","left":{"kind":"identifier","name":"x"},"right":{"kind":"identifier","name":"limit"}}', 'x', {limit=$limit})`,
|
|
54
|
+
)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test('the find family carries its direction', () => {
|
|
58
|
+
expect(render('items.find(x => x.done)')).toEndWith(`, 'x', T, {})`)
|
|
59
|
+
expect(render('items.findLast(x => x.done)')).toEndWith(`, 'x', F, {})`)
|
|
60
|
+
expect(render('items.findIndex(x => x.done)')).toStartWith('bf.find_index_eval(')
|
|
61
|
+
expect(render('items.findLastIndex(x => x.done)')).toEndWith(`, 'x', F, {})`)
|
|
62
|
+
expect(render('items.every(x => x.done)')).toStartWith('bf.every_eval(')
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test('a predicate the evaluator cannot model: BF101 naming the adapter, or the native form', () => {
|
|
66
|
+
const refused = lower('items.filter(x => x.name.localeCompare(q) > 0)')
|
|
67
|
+
expect(refused).toMatchObject({ kind: 'refuse' })
|
|
68
|
+
expect(refused.kind === 'refuse' && refused.message).toBe(
|
|
69
|
+
`'.filter(...)' predicate is outside the Jinja adapter's evaluable surface`,
|
|
70
|
+
)
|
|
71
|
+
expect(lower('items.filter(x => x.name.startsWith(q))', kolon)).toMatchObject({
|
|
72
|
+
kind: 'native-predicate',
|
|
73
|
+
method: 'filter',
|
|
74
|
+
param: 'x',
|
|
75
|
+
})
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
test('the native form never takes an opaque call: it would read the callee instead', () => {
|
|
79
|
+
expect(lower('items.filter(x => x.name.localeCompare(q) > 0)', kolon)).toMatchObject({ kind: 'refuse' })
|
|
80
|
+
expect(lower('items.filter(x => isOk(x))', kolon)).toMatchObject({ kind: 'refuse' })
|
|
81
|
+
// A zero-arg call is a signal getter, which the native form reads correctly.
|
|
82
|
+
expect(lower('items.filter(x => x.n > limit())', kolon)).toMatchObject({ kind: 'native-predicate' })
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
test('the native form never takes a nested callback that reads its index: it binds the element only', () => {
|
|
86
|
+
const nested = `rows.filter(row => row.join(',').startsWith('a') && row.filter((x, i) => i > 0).length > 0)`
|
|
87
|
+
expect(lower(nested, kolon)).toMatchObject({ kind: 'refuse' })
|
|
88
|
+
expect(lower(`rows.filter(row => row.join(',').startsWith('a') && row.filter(x => x > 0).length > 0)`, kolon)).toMatchObject({
|
|
89
|
+
kind: 'native-predicate',
|
|
90
|
+
})
|
|
91
|
+
// The evaluator binds the nested index, so a serializable predicate keeps it.
|
|
92
|
+
expect(lower('rows.filter(row => row.filter((x, i) => i > 0).length > 0)', kolon)).toMatchObject({ kind: 'eval' })
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
test('the identity filter goes native only on an adapter with a native form', () => {
|
|
96
|
+
expect(lower('items.filter(x => x)', kolon)).toMatchObject({ kind: 'native-predicate', method: 'filter' })
|
|
97
|
+
expect(lower('items.filter(x => x)')).toMatchObject({ kind: 'eval', helper: 'filter_eval' })
|
|
98
|
+
})
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
describe('lowerCallbackMethod: sort, reduce, projections', () => {
|
|
102
|
+
test('a sort comparator goes to the evaluator, then the structured descriptor, then BF101', () => {
|
|
103
|
+
expect(render('items.toSorted((a, b) => a.n - b.n)')).toStartWith(`bf.sort_eval(RECV, '{`)
|
|
104
|
+
expect(render('items.toSorted((a, b) => a.n - b.n)')).toEndWith(`, 'a', 'b', {})`)
|
|
105
|
+
expect(lower('items.sort((a, b) => a.name.localeCompare(b.name))')).toMatchObject({ kind: 'structured-sort' })
|
|
106
|
+
expect(lower('items.sort((a, b) => a.name.localeCompare(b.name, locale))')).toEqual({
|
|
107
|
+
kind: 'refuse',
|
|
108
|
+
message: `'.sort(...)' comparator is outside the Jinja adapter's evaluable / structured surface`,
|
|
109
|
+
suggestion: `Pre-compute the sorted array, or move this position to a '/* @client */' boundary.`,
|
|
110
|
+
})
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
test('a reducer needs a string or number literal seed', () => {
|
|
114
|
+
expect(render('items.reduce((acc, x) => acc + x.n, 0)')).toEndWith(`, 'acc', 'x', 0, 'left', {})`)
|
|
115
|
+
expect(render(`items.reduceRight((acc, x) => acc + x, '')`)).toEndWith(`, 'acc', 'x', '', 'right', {})`)
|
|
116
|
+
expect(render('items.reduce((acc, x) => acc + x, 1.5)')).toEndWith(`, 1.5, 'left', {})`)
|
|
117
|
+
for (const source of ['items.reduce((acc, x) => acc + x)', 'items.reduce((acc, x) => acc + x, start)', 'items.reduce((acc, x) => acc + x, true)']) {
|
|
118
|
+
const plan = lower(source)
|
|
119
|
+
expect(plan.kind === 'refuse' && plan.message).toBe(
|
|
120
|
+
`'.reduce(...)' is outside the Jinja adapter's evaluable surface (needs a literal initial value and an evaluable reducer body)`,
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
test('map and flatMap project through their evaluators', () => {
|
|
126
|
+
expect(render('items.map(x => x.n * k)')).toEndWith(`, 'x', {k=$k})`)
|
|
127
|
+
expect(render('items.flatMap(x => x.tags)')).toStartWith('bf.flat_map_eval(RECV, ')
|
|
128
|
+
const plan = lower('items.map(x => x.name.localeCompare(q))')
|
|
129
|
+
expect(plan.kind === 'refuse' && plan.message).toBe(`'.map(...)' projection is outside the Jinja adapter's evaluable surface`)
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
test('the env names come after the receiver and the operands, in sorted order', () => {
|
|
133
|
+
const order: string[] = []
|
|
134
|
+
const plan = lower('items.filter(x => x > b && x < a)')
|
|
135
|
+
if (plan.kind !== 'eval') throw new Error(plan.kind)
|
|
136
|
+
emitCallbackEval(plan, 'RECV', (e) => (order.push(emitIdentifier(e)), emitIdentifier(e)), spelling)
|
|
137
|
+
expect(order).toEqual(['$a', '$b'])
|
|
138
|
+
})
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
describe('unboundCallbackParamRead: the index and array params', () => {
|
|
142
|
+
const read = (source: string) => {
|
|
143
|
+
const call = callOf(source)
|
|
144
|
+
return unboundCallbackParamRead(call.method, call.arrow)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
test('a callback that reads its index or array param is found', () => {
|
|
148
|
+
expect(read('items.map((x, i) => x + i)')).toBe('i')
|
|
149
|
+
expect(read('items.filter((x, i, all) => all.length > 1)')).toBe('all')
|
|
150
|
+
expect(read('items.reduce((acc, x, i) => acc + i, 0)')).toBe('i')
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
test("'evaluator' scope binds a .map / .filter index, nothing more", () => {
|
|
154
|
+
const call = callOf('items.map((x, i, all) => i)')
|
|
155
|
+
expect(unboundCallbackParamRead(call.method, call.arrow, 'evaluator')).toBeNull()
|
|
156
|
+
const arr = callOf('items.filter((x, i, all) => all.length)')
|
|
157
|
+
expect(unboundCallbackParamRead(arr.method, arr.arrow, 'evaluator')).toBe('all')
|
|
158
|
+
const some = callOf('items.some((x, i) => i > 0)')
|
|
159
|
+
expect(unboundCallbackParamRead(some.method, some.arrow, 'evaluator')).toBe('i')
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
test('declared but unread, shadowed, or within the bound params: not found', () => {
|
|
163
|
+
expect(read('items.map((x, i) => x)')).toBeNull()
|
|
164
|
+
expect(read('items.reduce((acc, x, i) => acc + x, 0)')).toBeNull()
|
|
165
|
+
expect(read('items.toSorted((a, b) => a - b)')).toBeNull()
|
|
166
|
+
expect(read('rows.filter((r, i) => r.tags.some(i => i > 0))')).toBeNull()
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
test('the support gate refuses it, at any depth', () => {
|
|
170
|
+
expect(isSupported(parseExpression('items.map((x, i) => x + i).join(",")'))).toMatchObject({
|
|
171
|
+
supported: false,
|
|
172
|
+
reason: expect.stringContaining(`'.map()' callback reads 'i', its index or array parameter`),
|
|
173
|
+
})
|
|
174
|
+
expect(isSupported(parseExpression('rows.filter(r => r.tags.some((t, j) => j > 1)).length')).supported).toBe(false)
|
|
175
|
+
expect(isSupported(parseExpression('items.map((x, i) => x).join(",")')).supported).toBe(true)
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
test('inside an evaluator body, a nested .map / .filter binds its index', () => {
|
|
179
|
+
expect(isSupported(parseExpression(`rows.map(r => r.map((x, i) => i).join(',')).join(';')`)).supported).toBe(true)
|
|
180
|
+
expect(isSupported(parseExpression(`rows.map(r => r.filter((x, i) => i > 0).length)`)).supported).toBe(true)
|
|
181
|
+
expect(isSupported(parseExpression(`rows.some(r => r.map((x, i) => x + i).join('') === q)`)).supported).toBe(true)
|
|
182
|
+
// The evaluator binds neither a nested callback's array param nor the index of
|
|
183
|
+
// a nested callback other than .map / .filter.
|
|
184
|
+
expect(isSupported(parseExpression('rows.map(r => r.map((x, i, all) => all.length))')).supported).toBe(false)
|
|
185
|
+
expect(isSupported(parseExpression('rows.map(r => r.some((x, i) => i > 0))')).supported).toBe(false)
|
|
186
|
+
// The receiver of the outer call is still lowered by the template.
|
|
187
|
+
expect(isSupported(parseExpression('rows.map((r, i) => r.slice(i)).map(r => r.length)')).supported).toBe(false)
|
|
188
|
+
})
|
|
189
|
+
})
|