mirror of https://github.com/sveltejs/svelte
fix: tighten up `$` prefix validation (#13261)
Our current validation was both too lax and too strict: - too strict, because you can define a `$` variable in Svelte 4 if it's not at the top level - too strict, because you can define `$`-prefixed function parameters, but not give the parameter the name `$` - too lax, because you can define a `$`-prefixed variable if you're at least one level deep into a function. In runes mode, this should be an error This PR aligns the behavior, ensures this isn't a breaking change in legacy anymore, and makes the validation in runes mode more strictpull/13265/head
parent
355730cc2c
commit
d9369d8e30
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'svelte': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: tighten up `$` prefix validation
|
@ -1,11 +1,16 @@
|
|||||||
/** @import { FunctionDeclaration } from 'estree' */
|
/** @import { FunctionDeclaration } from 'estree' */
|
||||||
/** @import { Context } from '../types' */
|
/** @import { Context } from '../types' */
|
||||||
import { visit_function } from './shared/function.js';
|
import { visit_function } from './shared/function.js';
|
||||||
|
import { validate_identifier_name } from './shared/utils.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {FunctionDeclaration} node
|
* @param {FunctionDeclaration} node
|
||||||
* @param {Context} context
|
* @param {Context} context
|
||||||
*/
|
*/
|
||||||
export function FunctionDeclaration(node, context) {
|
export function FunctionDeclaration(node, context) {
|
||||||
|
if (context.state.analysis.runes) {
|
||||||
|
validate_identifier_name(context.state.scope.get(node.id.name));
|
||||||
|
}
|
||||||
|
|
||||||
visit_function(node, context);
|
visit_function(node, context);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
error: {
|
||||||
|
code: 'dollar_binding_invalid',
|
||||||
|
message: 'The $ name is reserved, and cannot be used for variables and imports',
|
||||||
|
position: [108, 109]
|
||||||
|
}
|
||||||
|
});
|
@ -0,0 +1,11 @@
|
|||||||
|
<svelte:options runes={false} />
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function ok($) {}
|
||||||
|
function ok2() {
|
||||||
|
let $;
|
||||||
|
}
|
||||||
|
|
||||||
|
// error
|
||||||
|
let $;
|
||||||
|
</script>
|
@ -0,0 +1,8 @@
|
|||||||
|
<svelte:options runes />
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function ok($) {}
|
||||||
|
function error() {
|
||||||
|
let $;
|
||||||
|
}
|
||||||
|
</script>
|
@ -0,0 +1,8 @@
|
|||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
error: {
|
||||||
|
code: 'dollar_binding_invalid',
|
||||||
|
message: 'The $ name is reserved, and cannot be used for variables and imports'
|
||||||
|
}
|
||||||
|
});
|
@ -1,3 +1,5 @@
|
|||||||
|
<svelte:options runes />
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
let $;
|
let $;
|
||||||
</script>
|
</script>
|
Loading…
Reference in new issue