Skip to content

Commit 71601ba

Browse files
fix: warn against accidental global event referenced (#10442)
* fix: warn against accidental global event referenced closes #10393 * remove list * remove else * tweak * update test --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 49ad7f9 commit 71601ba

File tree

7 files changed

+70
-7
lines changed

7 files changed

+70
-7
lines changed

.changeset/chilly-snakes-scream.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: warn against accidental global event referenced

packages/svelte/src/compiler/phases/2-analyze/validation.js

+22-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { error } from '../../errors.js';
2-
import { extract_identifiers, get_parent, is_text_attribute, object } from '../../utils/ast.js';
2+
import {
3+
extract_identifiers,
4+
get_parent,
5+
is_expression_attribute,
6+
is_text_attribute,
7+
object
8+
} from '../../utils/ast.js';
39
import { warn } from '../../warnings.js';
410
import fuzzymatch from '../1-parse/utils/fuzzymatch.js';
511
import { disallowed_parapgraph_contents, interactive_elements } from '../1-parse/utils/html.js';
@@ -66,12 +72,23 @@ function validate_element(node, context) {
6672
}
6773

6874
if (attribute.name.startsWith('on') && attribute.name.length > 2) {
75+
if (!is_expression_attribute(attribute)) {
76+
error(attribute, 'invalid-event-attribute-value');
77+
}
78+
79+
const value = attribute.value[0].expression;
6980
if (
70-
attribute.value === true ||
71-
is_text_attribute(attribute) ||
72-
attribute.value.length > 1
81+
value.type === 'Identifier' &&
82+
value.name === attribute.name &&
83+
!context.state.scope.get(value.name)
7384
) {
74-
error(attribute, 'invalid-event-attribute-value');
85+
warn(
86+
context.state.analysis.warnings,
87+
attribute,
88+
context.path,
89+
'global-event-reference',
90+
attribute.name
91+
);
7592
}
7693
}
7794

packages/svelte/src/compiler/utils/ast.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function is_text_attribute(attribute) {
3636
* @param {import('#compiler').Attribute} attribute
3737
* @returns {attribute is import('#compiler').Attribute & { value: [import('#compiler').ExpressionTag] }}
3838
*/
39-
function is_expression_attribute(attribute) {
39+
export function is_expression_attribute(attribute) {
4040
return (
4141
attribute.value !== true &&
4242
attribute.value.length === 1 &&

packages/svelte/src/compiler/warnings.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ const css = {
1212

1313
/** @satisfies {Warnings} */
1414
const attributes = {
15-
'avoid-is': () => 'The "is" attribute is not supported cross-browser and should be avoided'
15+
'avoid-is': () => 'The "is" attribute is not supported cross-browser and should be avoided',
16+
/** @param {string} name */
17+
'global-event-reference': (name) =>
18+
`You are referencing globalThis.${name}. Did you forget to declare a variable with that name?`
1619
};
1720

1821
/** @satisfies {Warnings} */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from '../../test';
2+
3+
export default test({});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
let onclick;
3+
</script>
4+
5+
<button {onclick}></button>
6+
<button onclick={onclick}></button>
7+
8+
<button {onkeydown}></button>
9+
<button onkeydown={onkeydown}></button>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[
2+
{
3+
"code": "global-event-reference",
4+
"message": "You are referencing globalThis.onkeydown. Did you forget to declare a variable with that name?",
5+
"start": {
6+
"column": 8,
7+
"line": 8
8+
},
9+
"end": {
10+
"column": 19,
11+
"line": 8
12+
}
13+
},
14+
{
15+
"code": "global-event-reference",
16+
"message": "You are referencing globalThis.onkeydown. Did you forget to declare a variable with that name?",
17+
"start": {
18+
"column": 8,
19+
"line": 9
20+
},
21+
"end": {
22+
"column": 29,
23+
"line": 9
24+
}
25+
}
26+
]

0 commit comments

Comments
 (0)