Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilly-snakes-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: warn against accidental global event referenced
27 changes: 22 additions & 5 deletions packages/svelte/src/compiler/phases/2-analyze/validation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { error } from '../../errors.js';
import { extract_identifiers, get_parent, is_text_attribute, object } from '../../utils/ast.js';
import {
extract_identifiers,
get_parent,
is_expression_attribute,
is_text_attribute,
object
} from '../../utils/ast.js';
import { warn } from '../../warnings.js';
import fuzzymatch from '../1-parse/utils/fuzzymatch.js';
import { disallowed_parapgraph_contents, interactive_elements } from '../1-parse/utils/html.js';
Expand Down Expand Up @@ -66,12 +72,23 @@ function validate_element(node, context) {
}

if (attribute.name.startsWith('on') && attribute.name.length > 2) {
if (!is_expression_attribute(attribute)) {
error(attribute, 'invalid-event-attribute-value');
}

const value = attribute.value[0].expression;
if (
attribute.value === true ||
is_text_attribute(attribute) ||
attribute.value.length > 1
value.type === 'Identifier' &&
value.name === attribute.name &&
!context.state.scope.get(value.name)
) {
error(attribute, 'invalid-event-attribute-value');
warn(
context.state.analysis.warnings,
attribute,
context.path,
'global-event-reference',
attribute.name
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/compiler/utils/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function is_text_attribute(attribute) {
* @param {import('#compiler').Attribute} attribute
* @returns {attribute is import('#compiler').Attribute & { value: [import('#compiler').ExpressionTag] }}
*/
function is_expression_attribute(attribute) {
export function is_expression_attribute(attribute) {
return (
attribute.value !== true &&
attribute.value.length === 1 &&
Expand Down
5 changes: 4 additions & 1 deletion packages/svelte/src/compiler/warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ const css = {

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

/** @satisfies {Warnings} */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { test } from '../../test';

export default test({});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
let onclick;
</script>

<button {onclick}></button>
<button onclick={onclick}></button>

<button {onkeydown}></button>
<button onkeydown={onkeydown}></button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"code": "global-event-reference",
"message": "You are referencing globalThis.onkeydown. Did you forget to declare a variable with that name?",
"start": {
"column": 8,
"line": 8
},
"end": {
"column": 19,
"line": 8
}
},
{
"code": "global-event-reference",
"message": "You are referencing globalThis.onkeydown. Did you forget to declare a variable with that name?",
"start": {
"column": 8,
"line": 9
},
"end": {
"column": 29,
"line": 9
}
}
]