@barefootjs/jsx 0.39.5 → 0.39.7
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 +142 -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 +153 -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/computed-const.d.ts +52 -0
- package/dist/adapters/computed-const.d.ts.map +1 -0
- package/dist/adapters/dangerous-inner-html.d.ts +10 -0
- package/dist/adapters/dangerous-inner-html.d.ts.map +1 -1
- package/dist/adapters/element-shell.d.ts +44 -0
- package/dist/adapters/element-shell.d.ts.map +1 -0
- package/dist/adapters/interface.d.ts +2 -0
- package/dist/adapters/interface.d.ts.map +1 -1
- 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/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts +7 -0
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/expression-parser.d.ts +38 -0
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/fold-render-reassignments.d.ts +43 -0
- package/dist/fold-render-reassignments.d.ts.map +1 -0
- package/dist/free-refs.d.ts +9 -0
- package/dist/free-refs.d.ts.map +1 -1
- package/dist/index.d.ts +23 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3954 -2401
- package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
- package/dist/ir-to-client-js/csr-substitute.d.ts +0 -13
- package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts +9 -10
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/index.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/declaration-emit.d.ts +5 -0
- package/dist/ir-to-client-js/plan/declaration-emit.d.ts.map +1 -1
- package/dist/ir-to-client-js/prop-handling.d.ts +11 -12
- package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +13 -0
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- 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 +22 -2
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/props-binding.d.ts +34 -5
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/relocate.d.ts +16 -0
- package/dist/relocate.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +0 -6
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/types.d.ts +54 -2
- package/dist/types.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__/block-memo-type.test.ts +51 -0
- 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 +264 -0
- package/src/__tests__/client-only-date-lowering.test.ts +2 -2
- package/src/__tests__/component-props.test.ts +148 -0
- package/src/__tests__/computed-const.test.ts +101 -0
- package/src/__tests__/delegated-handler-preamble.test.ts +4 -4
- package/src/__tests__/dsl-shared-helpers.test.ts +166 -0
- package/src/__tests__/element-shell.test.ts +78 -0
- package/src/__tests__/fold-render-reassignments.test.ts +96 -0
- package/src/__tests__/inlinable-local-method-call.test.ts +81 -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__/memo-body-expr.test.ts +120 -0
- package/src/__tests__/nullable-signal-attr.test.ts +45 -0
- package/src/__tests__/prop-handling.test.ts +35 -16
- package/src/__tests__/props-alias-const.test.ts +65 -0
- package/src/__tests__/signal-prop-seed.test.ts +90 -0
- package/src/__tests__/ssr-seed-plan.test.ts +54 -1
- package/src/__tests__/static-template-reachability.test.ts +137 -0
- package/src/adapters/array-method.ts +193 -0
- package/src/adapters/attr-plan.ts +228 -0
- package/src/adapters/boolean-result.ts +208 -12
- package/src/adapters/callback-method.ts +363 -0
- package/src/adapters/component-props.ts +144 -0
- package/src/adapters/component-tree.ts +118 -0
- package/src/adapters/computed-const.ts +185 -0
- package/src/adapters/dangerous-inner-html.ts +33 -0
- package/src/adapters/element-shell.ts +160 -0
- package/src/adapters/interface.ts +8 -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/analyzer.ts +271 -82
- package/src/augment-inherited-props.ts +12 -0
- package/src/expression-parser.ts +537 -374
- package/src/fold-render-reassignments.ts +314 -0
- package/src/free-refs.ts +49 -0
- package/src/index.ts +61 -3
- package/src/ir-to-client-js/compute-inlinability.ts +93 -17
- package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +9 -7
- package/src/ir-to-client-js/csr-substitute.ts +13 -35
- package/src/ir-to-client-js/emit-registration.ts +3 -2
- package/src/ir-to-client-js/html-template.ts +26 -70
- package/src/ir-to-client-js/index.ts +3 -2
- package/src/ir-to-client-js/init-declarations.ts +1 -1
- package/src/ir-to-client-js/plan/build-declaration-emit.ts +25 -51
- package/src/ir-to-client-js/plan/declaration-emit.ts +5 -0
- package/src/ir-to-client-js/prop-handling.ts +13 -54
- package/src/ir-to-client-js/rewrite-destructured-props.ts +6 -5
- package/src/ir-to-client-js/stringify/declaration-emit.ts +1 -1
- package/src/ir-to-client-js/utils.ts +11 -0
- package/src/jsx-to-ir.ts +1346 -1022
- package/src/nullable-signal-attr.ts +85 -4
- package/src/props-binding.ts +100 -5
- package/src/relocate.ts +40 -8
- package/src/ssr-seed-plan.ts +18 -6
- package/src/types.ts +50 -2
|
@@ -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,264 @@
|
|
|
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
|
+
isUnloweredCall,
|
|
12
|
+
unloweredCallRefusal,
|
|
13
|
+
type CallbackEvalSpelling,
|
|
14
|
+
type CallbackLoweringOptions,
|
|
15
|
+
} from '../adapters/callback-method'
|
|
16
|
+
import {
|
|
17
|
+
asCallbackMethodCall,
|
|
18
|
+
isSupported,
|
|
19
|
+
parseExpression,
|
|
20
|
+
unboundCallbackParamRead,
|
|
21
|
+
type ParsedExpr,
|
|
22
|
+
} from '../expression-parser'
|
|
23
|
+
|
|
24
|
+
const jinja: CallbackLoweringOptions = { adapterName: 'Jinja' }
|
|
25
|
+
const kolon: CallbackLoweringOptions = { adapterName: 'Xslate', nativePredicate: true }
|
|
26
|
+
|
|
27
|
+
function callOf(source: string) {
|
|
28
|
+
const call = asCallbackMethodCall(parseExpression(source))
|
|
29
|
+
if (!call) throw new Error(`not a callback method call: ${source}`)
|
|
30
|
+
return call
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const lower = (source: string, options = jinja) => {
|
|
34
|
+
const call = callOf(source)
|
|
35
|
+
return lowerCallbackMethod(call.method, call.arrow, call.args, options)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const spelling: CallbackEvalSpelling = {
|
|
39
|
+
call: (helper, operands) => `bf.${helper}(${operands.join(', ')})`,
|
|
40
|
+
string: (value) => `'${value.replace(/'/g, "\\'")}'`,
|
|
41
|
+
boolean: (value) => (value ? 'T' : 'F'),
|
|
42
|
+
env: (entries) => `{${entries.map(([name, value]) => `${name}=${value}`).join(', ')}}`,
|
|
43
|
+
}
|
|
44
|
+
const emitIdentifier = (e: ParsedExpr) => (e.kind === 'identifier' ? `$${e.name}` : '?')
|
|
45
|
+
|
|
46
|
+
function render(source: string, options = jinja): string {
|
|
47
|
+
const plan = lower(source, options)
|
|
48
|
+
if (plan.kind !== 'eval') throw new Error(`expected an eval plan, got ${plan.kind}`)
|
|
49
|
+
return emitCallbackEval(plan, 'RECV', emitIdentifier, spelling)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
describe('lowerCallbackMethod: predicates', () => {
|
|
53
|
+
test('a serializable predicate goes to its evaluator helper with the captured names', () => {
|
|
54
|
+
expect(render('items.filter(x => x > limit)')).toBe(
|
|
55
|
+
`bf.filter_eval(RECV, '{"kind":"binary","op":">","left":{"kind":"identifier","name":"x"},"right":{"kind":"identifier","name":"limit"}}', 'x', {limit=$limit})`,
|
|
56
|
+
)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('the find family carries its direction', () => {
|
|
60
|
+
expect(render('items.find(x => x.done)')).toEndWith(`, 'x', T, {})`)
|
|
61
|
+
expect(render('items.findLast(x => x.done)')).toEndWith(`, 'x', F, {})`)
|
|
62
|
+
expect(render('items.findIndex(x => x.done)')).toStartWith('bf.find_index_eval(')
|
|
63
|
+
expect(render('items.findLastIndex(x => x.done)')).toEndWith(`, 'x', F, {})`)
|
|
64
|
+
expect(render('items.every(x => x.done)')).toStartWith('bf.every_eval(')
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
test('a predicate the evaluator cannot model: BF101 naming the adapter, or the native form', () => {
|
|
68
|
+
const refused = lower('items.filter(x => x.name.localeCompare(q) > 0)')
|
|
69
|
+
expect(refused).toMatchObject({ kind: 'refuse' })
|
|
70
|
+
expect(refused.kind === 'refuse' && refused.message).toBe(
|
|
71
|
+
`'.filter(...)' predicate is outside the Jinja adapter's evaluable surface`,
|
|
72
|
+
)
|
|
73
|
+
expect(lower('items.filter(x => x.name.startsWith(q))', kolon)).toMatchObject({
|
|
74
|
+
kind: 'native-predicate',
|
|
75
|
+
method: 'filter',
|
|
76
|
+
param: 'x',
|
|
77
|
+
})
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('the native form never takes an opaque call: it would read the callee instead', () => {
|
|
81
|
+
expect(lower('items.filter(x => x.name.localeCompare(q) > 0)', kolon)).toMatchObject({ kind: 'refuse' })
|
|
82
|
+
expect(lower('items.filter(x => isOk(x))', kolon)).toMatchObject({ kind: 'refuse' })
|
|
83
|
+
// A zero-arg call is a signal getter, which the native form reads correctly.
|
|
84
|
+
expect(lower('items.filter(x => x.n > limit())', kolon)).toMatchObject({ kind: 'native-predicate' })
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test('the native form never takes a nested callback that reads its index: it binds the element only', () => {
|
|
88
|
+
const nested = `rows.filter(row => row.join(',').startsWith('a') && row.filter((x, i) => i > 0).length > 0)`
|
|
89
|
+
expect(lower(nested, kolon)).toMatchObject({ kind: 'refuse' })
|
|
90
|
+
expect(lower(`rows.filter(row => row.join(',').startsWith('a') && row.filter(x => x > 0).length > 0)`, kolon)).toMatchObject({
|
|
91
|
+
kind: 'native-predicate',
|
|
92
|
+
})
|
|
93
|
+
// The evaluator binds the nested index, so a serializable predicate keeps it.
|
|
94
|
+
expect(lower('rows.filter(row => row.filter((x, i) => i > 0).length > 0)', kolon)).toMatchObject({ kind: 'eval' })
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
test('the identity filter goes native only on an adapter with a native form', () => {
|
|
98
|
+
expect(lower('items.filter(x => x)', kolon)).toMatchObject({ kind: 'native-predicate', method: 'filter' })
|
|
99
|
+
expect(lower('items.filter(x => x)')).toMatchObject({ kind: 'eval', helper: 'filter_eval' })
|
|
100
|
+
})
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
describe('lowerCallbackMethod: sort, reduce, projections', () => {
|
|
104
|
+
test('a sort comparator goes to the evaluator, then the structured descriptor, then BF101', () => {
|
|
105
|
+
expect(render('items.toSorted((a, b) => a.n - b.n)')).toStartWith(`bf.sort_eval(RECV, '{`)
|
|
106
|
+
expect(render('items.toSorted((a, b) => a.n - b.n)')).toEndWith(`, 'a', 'b', {})`)
|
|
107
|
+
expect(lower('items.sort((a, b) => a.name.localeCompare(b.name))')).toMatchObject({ kind: 'structured-sort' })
|
|
108
|
+
expect(lower('items.sort((a, b) => a.name.localeCompare(b.name, locale))')).toEqual({
|
|
109
|
+
kind: 'refuse',
|
|
110
|
+
message: `'.sort(...)' comparator is outside the Jinja adapter's evaluable / structured surface`,
|
|
111
|
+
suggestion: `Pre-compute the sorted array, or move this position to a '/* @client */' boundary.`,
|
|
112
|
+
})
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
test('a reducer needs a string or number literal seed', () => {
|
|
116
|
+
expect(render('items.reduce((acc, x) => acc + x.n, 0)')).toEndWith(`, 'acc', 'x', 0, 'left', {})`)
|
|
117
|
+
expect(render(`items.reduceRight((acc, x) => acc + x, '')`)).toEndWith(`, 'acc', 'x', '', 'right', {})`)
|
|
118
|
+
expect(render('items.reduce((acc, x) => acc + x, 1.5)')).toEndWith(`, 1.5, 'left', {})`)
|
|
119
|
+
for (const source of ['items.reduce((acc, x) => acc + x)', 'items.reduce((acc, x) => acc + x, start)', 'items.reduce((acc, x) => acc + x, true)']) {
|
|
120
|
+
const plan = lower(source)
|
|
121
|
+
expect(plan.kind === 'refuse' && plan.message).toBe(
|
|
122
|
+
`'.reduce(...)' is outside the Jinja adapter's evaluable surface (needs a literal initial value and an evaluable reducer body)`,
|
|
123
|
+
)
|
|
124
|
+
}
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
test('map and flatMap project through their evaluators', () => {
|
|
128
|
+
expect(render('items.map(x => x.n * k)')).toEndWith(`, 'x', {k=$k})`)
|
|
129
|
+
expect(render('items.flatMap(x => x.tags)')).toStartWith('bf.flat_map_eval(RECV, ')
|
|
130
|
+
const plan = lower('items.map(x => x.name.localeCompare(q))')
|
|
131
|
+
expect(plan.kind === 'refuse' && plan.message).toBe(`'.map(...)' projection is outside the Jinja adapter's evaluable surface`)
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
test('the env names come after the receiver and the operands, in sorted order', () => {
|
|
135
|
+
const order: string[] = []
|
|
136
|
+
const plan = lower('items.filter(x => x > b && x < a)')
|
|
137
|
+
if (plan.kind !== 'eval') throw new Error(plan.kind)
|
|
138
|
+
emitCallbackEval(plan, 'RECV', (e) => (order.push(emitIdentifier(e)), emitIdentifier(e)), spelling)
|
|
139
|
+
expect(order).toEqual(['$a', '$b'])
|
|
140
|
+
})
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
describe('unboundCallbackParamRead: the index and array params', () => {
|
|
144
|
+
const read = (source: string) => {
|
|
145
|
+
const call = callOf(source)
|
|
146
|
+
return unboundCallbackParamRead(call.method, call.arrow)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
test('a callback that reads its index or array param is found', () => {
|
|
150
|
+
expect(read('items.map((x, i) => x + i)')).toBe('i')
|
|
151
|
+
expect(read('items.filter((x, i, all) => all.length > 1)')).toBe('all')
|
|
152
|
+
expect(read('items.reduce((acc, x, i) => acc + i, 0)')).toBe('i')
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
test("'evaluator' scope binds a .map / .filter index, nothing more", () => {
|
|
156
|
+
const call = callOf('items.map((x, i, all) => i)')
|
|
157
|
+
expect(unboundCallbackParamRead(call.method, call.arrow, 'evaluator')).toBeNull()
|
|
158
|
+
const arr = callOf('items.filter((x, i, all) => all.length)')
|
|
159
|
+
expect(unboundCallbackParamRead(arr.method, arr.arrow, 'evaluator')).toBe('all')
|
|
160
|
+
const some = callOf('items.some((x, i) => i > 0)')
|
|
161
|
+
expect(unboundCallbackParamRead(some.method, some.arrow, 'evaluator')).toBe('i')
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
test('declared but unread, shadowed, or within the bound params: not found', () => {
|
|
165
|
+
expect(read('items.map((x, i) => x)')).toBeNull()
|
|
166
|
+
expect(read('items.reduce((acc, x, i) => acc + x, 0)')).toBeNull()
|
|
167
|
+
expect(read('items.toSorted((a, b) => a - b)')).toBeNull()
|
|
168
|
+
expect(read('rows.filter((r, i) => r.tags.some(i => i > 0))')).toBeNull()
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
test('the support gate refuses it, at any depth', () => {
|
|
172
|
+
expect(isSupported(parseExpression('items.map((x, i) => x + i).join(",")'))).toMatchObject({
|
|
173
|
+
supported: false,
|
|
174
|
+
reason: expect.stringContaining(`'.map()' callback reads 'i', its index or array parameter`),
|
|
175
|
+
})
|
|
176
|
+
expect(isSupported(parseExpression('rows.filter(r => r.tags.some((t, j) => j > 1)).length')).supported).toBe(false)
|
|
177
|
+
expect(isSupported(parseExpression('items.map((x, i) => x).join(",")')).supported).toBe(true)
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
test('inside an evaluator body, a nested .map / .filter binds its index', () => {
|
|
181
|
+
expect(isSupported(parseExpression(`rows.map(r => r.map((x, i) => i).join(',')).join(';')`)).supported).toBe(true)
|
|
182
|
+
expect(isSupported(parseExpression(`rows.map(r => r.filter((x, i) => i > 0).length)`)).supported).toBe(true)
|
|
183
|
+
expect(isSupported(parseExpression(`rows.some(r => r.map((x, i) => x + i).join('') === q)`)).supported).toBe(true)
|
|
184
|
+
// The evaluator binds neither a nested callback's array param nor the index of
|
|
185
|
+
// a nested callback other than .map / .filter.
|
|
186
|
+
expect(isSupported(parseExpression('rows.map(r => r.map((x, i, all) => all.length))')).supported).toBe(false)
|
|
187
|
+
expect(isSupported(parseExpression('rows.map(r => r.some((x, i) => i > 0))')).supported).toBe(false)
|
|
188
|
+
// The receiver of the outer call is still lowered by the template.
|
|
189
|
+
expect(isSupported(parseExpression('rows.map((r, i) => r.slice(i)).map(r => r.length)')).supported).toBe(false)
|
|
190
|
+
})
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
describe('isUnloweredCall (#3493)', () => {
|
|
194
|
+
function call(source: string): Extract<ParsedExpr, { kind: 'call' }> {
|
|
195
|
+
const parsed = parseExpression(source)
|
|
196
|
+
if (parsed.kind !== 'call') throw new Error(`expected a call: ${source}`)
|
|
197
|
+
return parsed
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const noGlobals = () => false
|
|
201
|
+
|
|
202
|
+
test('a method call with arguments outside the catalogue has no lowering', () => {
|
|
203
|
+
for (const source of ["s().localeCompare('m')", "props.label.localeCompare('m')", 'label.codePointAt(0)']) {
|
|
204
|
+
const c = call(source)
|
|
205
|
+
expect(isUnloweredCall(c.callee, c.args, 'props', noGlobals)).toBe(true)
|
|
206
|
+
}
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
test('a getter read or a function-valued prop keeps the callee read', () => {
|
|
210
|
+
for (const source of ['props.value()', 'props.value.v()', 'props.renderNode(node)']) {
|
|
211
|
+
const c = call(source)
|
|
212
|
+
expect(isUnloweredCall(c.callee, c.args, 'props', noGlobals)).toBe(false)
|
|
213
|
+
}
|
|
214
|
+
// The props parameter's own name, whatever it is.
|
|
215
|
+
const named = call('p.renderNode(node)')
|
|
216
|
+
expect(isUnloweredCall(named.callee, named.args, 'p', noGlobals)).toBe(false)
|
|
217
|
+
expect(isUnloweredCall(named.callee, named.args, null, noGlobals)).toBe(true)
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
test('a zero-arg call rooted at a global has no lowering (#3505)', () => {
|
|
221
|
+
const isGlobal = (name: string) => ['Date', 'Math', 'window', 'location', 'globalThis'].includes(name)
|
|
222
|
+
for (const source of [
|
|
223
|
+
'Date.now()',
|
|
224
|
+
'Math.random()',
|
|
225
|
+
'window.performance.now()',
|
|
226
|
+
'location.toString()',
|
|
227
|
+
'Date.now.call()',
|
|
228
|
+
'Date[method]()',
|
|
229
|
+
'globalThis[name].now()',
|
|
230
|
+
]) {
|
|
231
|
+
const c = call(source)
|
|
232
|
+
expect(isUnloweredCall(c.callee, c.args, 'props', isGlobal)).toBe(true)
|
|
233
|
+
}
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
test('a zero-arg call on a bound name keeps the callee read, even when it is named like a global', () => {
|
|
237
|
+
// `isGlobal` is false for a prop, a local (`const navigator = useContext(Nav)`) or a loop binding.
|
|
238
|
+
for (const source of ['ctx.open()', 'navigator.current()', 'document.title()', 'item.label()', 'props.getters[key]()']) {
|
|
239
|
+
const c = call(source)
|
|
240
|
+
expect(isUnloweredCall(c.callee, c.args, 'props', noGlobals)).toBe(false)
|
|
241
|
+
}
|
|
242
|
+
})
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
describe('unloweredCallRefusal (#3493)', () => {
|
|
246
|
+
test('a zero-arg call on a global names the global, not missing arguments (#3505)', () => {
|
|
247
|
+
const call = parseExpression('Date.now()')
|
|
248
|
+
if (call.kind !== 'call') throw new Error('expected a call')
|
|
249
|
+
expect(unloweredCallRefusal(call.callee, call.args, 'Jinja').message).toBe(
|
|
250
|
+
"Call 'Date.now()' reads a JavaScript global, which has no Jinja template binding; read as a value it would drop the call",
|
|
251
|
+
)
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
test('names the whole call and the adapter, and offers both escapes', () => {
|
|
255
|
+
const call = parseExpression("s().localeCompare('m')")
|
|
256
|
+
if (call.kind !== 'call') throw new Error('expected a call')
|
|
257
|
+
const refusal = unloweredCallRefusal(call.callee, call.args, 'Jinja')
|
|
258
|
+
expect(refusal.message).toBe(
|
|
259
|
+
"Call 's().localeCompare(...)' has no Jinja template lowering; read as a value it would drop the call and its arguments",
|
|
260
|
+
)
|
|
261
|
+
expect(refusal.suggestion).toContain('/* @client */')
|
|
262
|
+
expect(refusal.suggestion).toContain('pass it as a prop')
|
|
263
|
+
})
|
|
264
|
+
})
|
|
@@ -186,8 +186,8 @@ describe('#2641 — reactive attribute bindings route catalogued calls through t
|
|
|
186
186
|
|
|
187
187
|
describe('#2645 — /* @client */ text expressions elide in the static/CSR template like SSR', () => {
|
|
188
188
|
// `irToComponentTemplateWithOpts`'s 'expression' case never checked
|
|
189
|
-
// `node.clientOnly` — a bare-destructured-prop receiver
|
|
190
|
-
//
|
|
189
|
+
// `node.clientOnly` — a bare-destructured-prop receiver
|
|
190
|
+
// (`createdAt.toISOString()` reads only a prop) routed to this static
|
|
191
191
|
// template builder, which had no clientOnly awareness and inlined the
|
|
192
192
|
// (possibly lowered) value directly, breaking SSR/CSR byte parity: SSR
|
|
193
193
|
// renders the @client region empty, this builder rendered it populated.
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The props a DSL adapter passes to a child component (#3479): which entries,
|
|
3
|
+
* spreads and captures `planComponentProps` makes of a component call, and
|
|
4
|
+
* the order `spellComponentProps` spells them in.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, test, expect } from 'bun:test'
|
|
8
|
+
import {
|
|
9
|
+
isEmptyComponentProps,
|
|
10
|
+
planComponentProps,
|
|
11
|
+
spellComponentProps,
|
|
12
|
+
type ComponentPropEntry,
|
|
13
|
+
type ComponentPropsOptions,
|
|
14
|
+
} from '../adapters/component-props'
|
|
15
|
+
import type { AttrValue, IRComponent, IRNode, IRProp } from '../types'
|
|
16
|
+
|
|
17
|
+
const loc = { file: 'x.tsx', start: { line: 1, column: 0 }, end: { line: 1, column: 0 } }
|
|
18
|
+
const text = (value: string): IRNode => ({ type: 'text', value, loc }) as IRNode
|
|
19
|
+
|
|
20
|
+
function prop(name: string, value: AttrValue): IRProp {
|
|
21
|
+
return { name, value, loc } as IRProp
|
|
22
|
+
}
|
|
23
|
+
const literal = (value: string): AttrValue => ({ kind: 'literal', value })
|
|
24
|
+
const expression = (expr: string): AttrValue => ({ kind: 'expression', expr }) as AttrValue
|
|
25
|
+
const spread = (expr: string): AttrValue => ({ kind: 'spread', expr }) as AttrValue
|
|
26
|
+
const jsx = (...children: IRNode[]): AttrValue => ({ kind: 'jsx-children', children }) as AttrValue
|
|
27
|
+
|
|
28
|
+
function component(props: IRProp[], extra: Partial<IRComponent> = {}): IRComponent {
|
|
29
|
+
return {
|
|
30
|
+
type: 'component',
|
|
31
|
+
name: 'Tag',
|
|
32
|
+
props,
|
|
33
|
+
propsType: null,
|
|
34
|
+
children: [],
|
|
35
|
+
template: 'tag',
|
|
36
|
+
slotId: 's0',
|
|
37
|
+
loc,
|
|
38
|
+
...extra,
|
|
39
|
+
} as IRComponent
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const noProps: ComponentPropsOptions = { propsObjectName: null, propsParams: [] }
|
|
43
|
+
const withProps: ComponentPropsOptions = {
|
|
44
|
+
propsObjectName: 'props',
|
|
45
|
+
propsParams: [{ name: 'tone' }, { name: 'size' }],
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The plan, spelled as `key=value` parts and `...spread`. */
|
|
49
|
+
function spell(comp: IRComponent, options = noProps): string[] {
|
|
50
|
+
const describe = (entry: ComponentPropEntry): string => {
|
|
51
|
+
switch (entry.kind) {
|
|
52
|
+
case 'value':
|
|
53
|
+
return `${entry.name}=${entry.value.kind === 'literal' ? `'${entry.value.value}'` : (entry.value as { expr: string }).expr}`
|
|
54
|
+
case 'named-slot':
|
|
55
|
+
return `${entry.name}=<slot>`
|
|
56
|
+
case 'props-param':
|
|
57
|
+
return `${entry.name}=$${entry.name}`
|
|
58
|
+
case 'slot-id':
|
|
59
|
+
return `_bf_slot='${entry.slotId}'`
|
|
60
|
+
case 'children':
|
|
61
|
+
return 'children=<children>'
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return spellComponentProps(planComponentProps(comp, options), {
|
|
65
|
+
entry: describe,
|
|
66
|
+
spread: (value) => value.expr,
|
|
67
|
+
}).map((segment) => (segment.kind === 'spread' ? `...${segment.expr}` : `{${segment.parts.join(', ')}}`))
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
describe('planComponentProps', () => {
|
|
71
|
+
test('ordinary props, then the slot id', () => {
|
|
72
|
+
expect(spell(component([prop('tone', literal('warm')), prop('size', expression('size()'))]))).toEqual([
|
|
73
|
+
`{tone='warm', size=size(), _bf_slot='s0'}`,
|
|
74
|
+
])
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('callback props and ref are client-only; a callback-named literal is kept', () => {
|
|
78
|
+
const comp = component([
|
|
79
|
+
prop('onClick', expression('handle')),
|
|
80
|
+
prop('ref', expression('el')),
|
|
81
|
+
prop('onLabel', literal('on')),
|
|
82
|
+
])
|
|
83
|
+
expect(spell(comp)).toEqual([`{onLabel='on', _bf_slot='s0'}`])
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
test('a loop row root passes no slot id', () => {
|
|
87
|
+
expect(spell(component([prop('tone', literal('warm'))], { loopItemRoot: true }))).toEqual([`{tone='warm'}`])
|
|
88
|
+
expect(spell(component([], { slotId: null }))).toEqual(['{}'])
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
test('a spread folds in at its source position', () => {
|
|
92
|
+
const comp = component([prop('tone', literal('a')), prop('', spread('rest')), prop('size', literal('b'))])
|
|
93
|
+
expect(spell(comp)).toEqual([`{tone='a'}`, '...rest', `{size='b', _bf_slot='s0'}`])
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
test('{...props} enumerates the props params', () => {
|
|
97
|
+
const comp = component([prop('', spread(' props ')), prop('label', literal('x'))])
|
|
98
|
+
expect(spell(comp, withProps)).toEqual([`{tone=$tone, size=$size, label='x', _bf_slot='s0'}`])
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
test('a repeated key keeps its first position and takes the later value', () => {
|
|
102
|
+
const later = component([prop('', spread('props')), prop('tone', literal('explicit'))])
|
|
103
|
+
expect(spell(later, withProps)).toEqual([`{tone='explicit', size=$size, _bf_slot='s0'}`])
|
|
104
|
+
const earlier = component([prop('tone', literal('first')), prop('', spread('props'))])
|
|
105
|
+
expect(spell(earlier, withProps)).toEqual([`{tone=$tone, size=$size, _bf_slot='s0'}`])
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
test('a key repeated across a spread stays twice: the spread may set it in between', () => {
|
|
109
|
+
const comp = component([prop('tone', literal('a')), prop('', spread('rest')), prop('tone', literal('b'))])
|
|
110
|
+
expect(spell(comp)).toEqual([`{tone='a'}`, '...rest', `{tone='b', _bf_slot='s0'}`])
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
test('named slots in place, the children last', () => {
|
|
114
|
+
const nested = component([prop('header', jsx(text('H'))), prop('tone', literal('x'))], { children: [text('C')] })
|
|
115
|
+
expect(spell(nested)).toEqual([`{header=<slot>, tone='x', _bf_slot='s0', children=<children>}`])
|
|
116
|
+
// `children={<jsx/>}` is the children entry, not an ordinary prop.
|
|
117
|
+
const attribute = component([prop('children', jsx(text('C'))), prop('tone', literal('x'))])
|
|
118
|
+
expect(spell(attribute)).toEqual([`{tone='x', _bf_slot='s0', children=<children>}`])
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
test('the children follow a trailing spread, so the spread cannot override them', () => {
|
|
122
|
+
const comp = component([prop('', spread('rest'))], { children: [text('C')], slotId: null })
|
|
123
|
+
expect(spell(comp)).toEqual(['{}', '...rest', '{children=<children>}'])
|
|
124
|
+
})
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
describe('spellComponentProps', () => {
|
|
128
|
+
test('spells entries in source order and drops an empty part', () => {
|
|
129
|
+
const order: string[] = []
|
|
130
|
+
const comp = component([prop('a', jsx(text('A'))), prop('b', literal('x')), prop('c', jsx(text('C')))], {
|
|
131
|
+
children: [text('D')],
|
|
132
|
+
})
|
|
133
|
+
const segments = spellComponentProps(planComponentProps(comp, noProps), {
|
|
134
|
+
entry: (entry) => {
|
|
135
|
+
order.push(entry.kind === 'slot-id' || entry.kind === 'children' ? entry.kind : entry.name)
|
|
136
|
+
return entry.kind === 'value' ? '' : 'part'
|
|
137
|
+
},
|
|
138
|
+
spread: () => '',
|
|
139
|
+
})
|
|
140
|
+
expect(order).toEqual(['a', 'b', 'c', 'slot-id', 'children'])
|
|
141
|
+
expect(segments).toEqual([{ kind: 'entries', parts: ['part', 'part', 'part', 'part'] }])
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
test('isEmptyComponentProps: no part in any segment', () => {
|
|
145
|
+
expect(isEmptyComponentProps([{ kind: 'entries', parts: [] }])).toBe(true)
|
|
146
|
+
expect(isEmptyComponentProps([{ kind: 'entries', parts: [] }, { kind: 'spread', expr: 'x' }])).toBe(false)
|
|
147
|
+
})
|
|
148
|
+
})
|