Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error on expression scope store #5079

Merged
merged 4 commits into from
Jul 7, 2020

Conversation

tanhauhau
Copy link
Member

Fixes #5048

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR relates to an outstanding issue, so please reference it in your PR, or create an explanatory one for discussion. In many cases, features are absent for a reason.
  • This message body should clearly illustrate what problems it solves. If there are related issues, remember to reference them.
  • Ideally, include a test that fails without this PR but passes with it. PRs will only be merged once they pass CI. (Remember to npm run lint!)

Tests

  • Run the tests with npm test or yarn test)

@Conduitry
Copy link
Member

It looks like this would also prevent using $store in a scope where the top-level store had been shadowed, correct? I don't really have a strong opinion about whether that's something we want. Using $store where store has been shadowed is confusing, but it currently works fine. It's only assignments to $store - which get rewritten to attempt a direct reference to store - that cause a problem.

I guess this partially comes down to whether we ever plan to let you autosubscribe to stores that aren't declared on the top level. Making $store refer to the non-top-level store would technically be a breaking change, although the current behavior here might not be officially documented anywhere, and might not be desirable. If we do want to eventually change this behavior, it's probably better to have an intervening period where the input causes a compiler error, rather than continuing to permit this and then suddenly changing its behavior sometime in the future.

As I've been writing this, I've sort of talked myself into thinking that disallowing all uses of $store when store is shadowed is the better way forward, but I wanted to make sure everyone's okay with that, even though the compiler would then reject some currently working (albeit confusing) components.

@tanhauhau
Copy link
Member Author

I would think that any variable name that begins with $ refers to the store value of the variable name without the $. ie: $store refers to store value of store, and store should be the one in the current scope.

@Conduitry
Copy link
Member

Looking at this PR now, I think we want to also prevent usage of $store in the <script> block when the top-level store has been shadowed. For example,

<script>
	import { writable } from 'svelte/store';
	const store = writable();
	{
		let store;
		$store = 42;
		console.log($store);
	}
</script>

produces a runtime error, but should also be a compile-time error.

Similarly to the previous comment, this would also have the effect of making the following code:

<script>
	import { writable } from 'svelte/store';
	const store = writable(42);
	{
		let store;
		console.log($store);
	}
</script>

stop printing '42' and start being a compile-time error, which seems like a reasonable enough slight change if we're also okay with that happening in the template.

@tanhauhau
Copy link
Member Author

That make sense to me.

@Conduitry Conduitry merged commit f739b47 into sveltejs:master Jul 7, 2020
hontas added a commit to hontas/svelte that referenced this pull request Jul 18, 2020
* upstream/master: (190 commits)
  invalidate $$props and $$restProps only when there are changes (sveltejs#5123)
  site: use https in link in blog (sveltejs#5148)
  Simplify each block bindings example (sveltejs#5094)
  fix $$props reactive for slots (sveltejs#5125)
  site: add FAQ entry for how to document a svelte component (sveltejs#5131)
  site: remove an obsolete TODO in blog post (sveltejs#5135)
  Increase timeout for unit build
  Increase timeout for unit tests
  -> v3.24.0
  spread condition for input element (sveltejs#5004)
  update changelog
  fix(5018): compare wholeText instead of data (sveltejs#5028)
  html anchor in head (sveltejs#5071)
  error on expression scope store (sveltejs#5079)
  update changelog
  preprocess self-closing script and style tags (sveltejs#5082)
  update changelog
  fix: Parameters with default values are optional (sveltejs#5083)
  make builds time out after a reasonable period (sveltejs#5100)
  site: fix blog typo (sveltejs#5090)
  ...
hontas added a commit to hontas/svelte that referenced this pull request Jul 18, 2020
* master: (67 commits)
  add updating guard to binding callback (sveltejs#5126)
  Bump lodash from 4.17.15 to 4.17.19 (sveltejs#5152)
  Bump lodash from 4.17.15 to 4.17.19 in /site (sveltejs#5155)
  Fixes sveltejs#5153 (sveltejs#5154)
  invalidate $$props and $$restProps only when there are changes (sveltejs#5123)
  site: use https in link in blog (sveltejs#5148)
  Simplify each block bindings example (sveltejs#5094)
  fix $$props reactive for slots (sveltejs#5125)
  site: add FAQ entry for how to document a svelte component (sveltejs#5131)
  site: remove an obsolete TODO in blog post (sveltejs#5135)
  Increase timeout for unit build
  Increase timeout for unit tests
  -> v3.24.0
  spread condition for input element (sveltejs#5004)
  update changelog
  fix(5018): compare wholeText instead of data (sveltejs#5028)
  html anchor in head (sveltejs#5071)
  error on expression scope store (sveltejs#5079)
  update changelog
  preprocess self-closing script and style tags (sveltejs#5082)
  ...
taylorzane pushed a commit to taylorzane/svelte that referenced this pull request Dec 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

$store =doesn't check whether store is different in current scope
2 participants