illegal-store-subscription part 2

pull/10714/head
Simon Holthausen 3 years ago
parent 0b0d468ed1
commit cd2ee2519d

@ -442,11 +442,6 @@ const errors = {
// message
// };
// },
// contextual_store: {
// code: 'contextual-store',
// message:
// 'Stores must be declared at the top level of the component (this may change in a future version of Svelte)'
// },
// default_export: {
// code: 'default-export',
// message: 'A component cannot have a default export'

@ -300,21 +300,28 @@ export function analyze_component(root, options) {
declaration.initial.source.value === 'svelte/store'
))
) {
if (options.runes !== false) {
if (declaration === null && /[a-z]/.test(store_name[0])) {
let is_nested_store_subscription = false;
for (let i = references[0].path.length - 1; i >= 0; i--) {
const scope = scopes.get(references[0].path[i]);
if (scope && scope !== module.scope && scope.declarations.has(store_name)) {
is_nested_store_subscription = true;
for (const reference of references) {
for (let i = reference.path.length - 1; i >= 0; i--) {
const scope =
scopes.get(reference.path[i]) ||
module.scopes.get(reference.path[i]) ||
instance.scopes.get(reference.path[i]);
if (scope) {
const owner = scope?.owner(store_name);
is_nested_store_subscription =
!!owner && owner !== module.scope && owner !== instance.scope;
break;
}
}
}
if (is_nested_store_subscription) {
error(references[0].node, 'illegal-store-subscription');
} else {
error(references[0].node, 'illegal-global', name);
}
if (options.runes !== false) {
if (declaration === null && /[a-z]/.test(store_name[0])) {
error(references[0].node, 'illegal-global', name);
} else if (declaration !== null && Runes.includes(/** @type {any} */ (name))) {
for (const { node, path } of references) {
if (path.at(-1)?.type === 'CallExpression') {

@ -1,10 +1,8 @@
import { test } from '../../test';
export default test({
skip: true,
error: {
code: '',
message:
'Stores must be declared at the top level of the component (this may change in a future version of Svelte)'
code: 'illegal-store-subscription',
message: 'Cannot subscribe to stores that are not declared at the top level of the component'
}
});

@ -1,10 +1,8 @@
import { test } from '../../test';
export default test({
skip: true,
error: {
code: '',
message:
'Stores must be declared at the top level of the component (this may change in a future version of Svelte)'
code: 'illegal-store-subscription',
message: 'Cannot subscribe to stores that are not declared at the top level of the component'
}
});

Loading…
Cancel
Save