Skip to content

Commit 09af24c

Browse files
Merge pull request #21091 from crazylogic03/fix/on-modifier-error-message
[BUGFIX beta] Fix 'on' modifier error message regression
2 parents 8759295 + 5977946 commit 09af24c

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,4 +390,4 @@
390390
}
391391
},
392392
"packageManager": "pnpm@10.5.0"
393-
}
393+
}

packages/@ember/-internals/glimmer/tests/integration/modifiers/on-test.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { moduleFor, RenderingTestCase, runTask } from 'internal-test-helpers';
22
import { getInternalModifierManager } from '@glimmer/manager';
33
import { on } from '@glimmer/runtime';
44

5+
import { DEBUG } from '@glimmer/env';
6+
57
import { Component } from '../../utils/helpers';
68

79
moduleFor(
@@ -31,7 +33,7 @@ moduleFor(
3133
);
3234
}
3335

34-
['@test it adds an event listener'](assert) {
36+
[`@test it adds an event listener`](assert) {
3537
let count = 0;
3638

3739
this.render('<button {{on "click" this.callback}}>Click Me</button>', {
@@ -242,6 +244,30 @@ moduleFor(
242244

243245
this.assertCounts({ adds: 1, removes: 1 });
244246
}
247+
248+
[`@test it throws a helpful error when callback is undefined`](assert) {
249+
if (DEBUG) {
250+
let expectedMessage =
251+
/You must pass a function as the second argument to the `on` modifier/;
252+
assert.throws(() => {
253+
this.render('<button {{on "click" undefined}}>Click Me</button>');
254+
}, expectedMessage);
255+
} else {
256+
assert.expect(0);
257+
}
258+
}
259+
260+
[`@test it throws a helpful error when callback is null`](assert) {
261+
if (DEBUG) {
262+
let expectedMessage =
263+
/You must pass a function as the second argument to the `on` modifier/;
264+
assert.throws(() => {
265+
this.render('<button {{on "click" null}}>Click Me</button>');
266+
}, expectedMessage);
267+
} else {
268+
assert.expect(0);
269+
}
270+
}
245271
}
246272
);
247273

packages/@glimmer/runtime/lib/modifiers/on.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,24 @@ export class OnModifierState {
6868
() => 'You must pass a valid DOM event name as the first argument to the `on` modifier'
6969
);
7070

71+
let arg1 = args.positional[1];
72+
let userProvidedCallback = check(
73+
arg1 ? valueForRef(arg1) : undefined,
74+
CheckFunction,
75+
(actual) => {
76+
return `You must pass a function as the second argument to the \`on\` modifier; you passed ${
77+
actual === null ? 'null' : typeof actual
78+
}. While rendering:\n\n${args.positional[1]?.debugLabel ?? `{unlabeled value}`}`;
79+
}
80+
) as EventListener;
81+
7182
localAssert(
72-
args.positional[1],
73-
'You must pass a function as the second argument to the `on` modifier'
83+
typeof userProvidedCallback === 'function',
84+
`You must pass a function as the second argument to the \`on\` modifier; you passed ${
85+
userProvidedCallback === null ? 'null' : typeof userProvidedCallback
86+
}. While rendering:\n\n${args.positional[1]?.debugLabel ?? `{unlabeled value}`}`
7487
);
7588

76-
let userProvidedCallback = check(valueForRef(args.positional[1]), CheckFunction, (actual) => {
77-
return `You must pass a function as the second argument to the \`on\` modifier; you passed ${
78-
actual === null ? 'null' : typeof actual
79-
}. While rendering:\n\n${args.positional[1]?.debugLabel ?? `{unlabeled value}`}`;
80-
}) as EventListener;
81-
8289
if (DEBUG && args.positional.length !== 2) {
8390
throw new Error(
8491
`You can only pass two positional arguments (event name and callback) to the \`on\` modifier, but you provided ${args.positional.length}. Consider using the \`fn\` helper to provide additional arguments to the \`on\` callback.`

0 commit comments

Comments
 (0)