question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Svelte generates code referencing undefined variables like div_nodes

See original GitHub issue

Hi. I’m trying to use ESLint on the final IIFE .js that Svelte generates, in order to get the no-undef lint (usage of undefined variable).

.eslintrc.js
module.exports = {
  parserOptions: {
    ecmaVersion: 2019,
    sourceType: 'module',
  },
  env: {
    es6: true,
    browser: true,
    node: true,
  },
  plugins: [
    'svelte3',
    '@typescript-eslint/eslint-plugin'
  ],
  overrides: [
    {
      files: ['**/*.svelte'],
      processor: 'svelte3/svelte3',
      extends: "eslint:recommended",
    },
    {
      files: ['**/*.js'],
      rules: {
        "no-undef": "warn",
      },
    },
  ],
}

This gives me:

/path/to/bundle.js
1668:43  warning  'form_nodes' is not defined  no-undef

Offending code in bundle.js:

    		l: function claim(nodes) {
    			if (default_slot) { default_slot.l(form_nodes); }
    			throw new Error("options.hydrate only works if the component was compiled with the `hydratable: true` option");
    		},

Form.svelte is just <form><slot /></form> (I simplified it for the purpose of this report). If I add content to the default slot, or other HTML tags (children) to <form>, the issue is still there. Searching for form_nodes and _nodes in bundle.js gives me no results aside of the function above. If I change Form.svelte content to <div><slot /></form>, I get warning about undefined div_nodes.

Are <...>_nodes in the code above intended to be undefined? How can I set them? Or what else can I do to get the results I want (getting “… is not defined” errors at compile time, not runtime)?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Conduitrycommented, Oct 19, 2019

Current shot at this:

diff --git a/src/compiler/compile/render_dom/wrappers/Slot.ts b/src/compiler/compile/render_dom/wrappers/Slot.ts
index 1ab1b6ab..fb90afab 100644
--- a/src/compiler/compile/render_dom/wrappers/Slot.ts
+++ b/src/compiler/compile/render_dom/wrappers/Slot.ts
@@ -137,12 +137,12 @@ export default class SlotWrapper extends Wrapper {
 		block.render_listeners(`_${slot.name}`);
 		block.event_listeners = listeners;
 
-		if (block.chunks.create) create.push(b`if (!${slot}) { ${block.chunks.create} }`);
-		if (block.chunks.claim) claim.push(b`if (!${slot}) { ${block.chunks.claim} }`);
-		if (block.chunks.hydrate) hydrate.push(b`if (!${slot}) { ${block.chunks.hydrate} }`);
-		if (block.chunks.mount) mount.push(b`if (!${slot}) { ${block.chunks.mount} }`);
-		if (block.chunks.update) update.push(b`if (!${slot}) { ${block.chunks.update} }`);
-		if (block.chunks.destroy) destroy.push(b`if (!${slot}) { ${block.chunks.destroy} }`);
+		if (block.chunks.create.length) create.push(b`if (!${slot}) { ${block.chunks.create} }`);
+		if (block.chunks.claim.length) claim.push(b`if (!${slot}) { ${block.chunks.claim} }`);
+		if (block.chunks.hydrate.length) hydrate.push(b`if (!${slot}) { ${block.chunks.hydrate} }`);
+		if (block.chunks.mount.length) mount.push(b`if (!${slot}) { ${block.chunks.mount} }`);
+		if (block.chunks.update.length) update.push(b`if (!${slot}) { ${block.chunks.update} }`);
+		if (block.chunks.destroy.length) destroy.push(b`if (!${slot}) { ${block.chunks.destroy} }`);
 
 		block.chunks.create = create;
 		block.chunks.claim = claim;
@@ -155,9 +155,11 @@ export default class SlotWrapper extends Wrapper {
 			b`if (${slot}) ${slot}.c();`
 		);
 
-		block.chunks.claim.push(
-			b`if (${slot}) ${slot}.l(${parent_nodes});`
-		);
+		if (renderer.options.hydratable) {
+			block.chunks.claim.push(
+				b`if (${slot}) ${slot}.l(${parent_nodes});`
+			);
+		}
 
 		block.chunks.mount.push(b`
 			if (${slot}) {

Seems to address this particular issue, and doesn’t break any tests.

0reactions
Conduitrycommented, Oct 19, 2019
diff --git a/src/compiler/compile/render_dom/Block.ts b/src/compiler/compile/render_dom/Block.ts
index f73212f3..8ea90049 100644
--- a/src/compiler/compile/render_dom/Block.ts
+++ b/src/compiler/compile/render_dom/Block.ts
@@ -272,7 +272,7 @@ export default class Block {
 			}`;
 		}
 
-		if (this.renderer.options.hydratable || this.chunks.claim.length > 0) {
+		if (this.renderer.options.hydratable || this.renderer.options.dev) {
 			if (this.chunks.claim.length === 0 && this.chunks.hydrate.length === 0) {
 				properties.claim = noop;
 			} else {

still causes three js sample tests to fail (producing unexpected claim methods in blocks), and I’m not sure why. (Maybe something about claim being empty vs hydrate being empty?) You can keep poking at this if you’d like, but I don’t think I’m going to.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Docs • Svelte
Complete documentation for Svelte.
Read more >
Dynamic behavior in Svelte: working with variables and props
Objective: Learn and put into practice some basic Svelte concepts, like creating components, passing data using props, rendering JavaScript ...
Read more >
Reactive Store variables in Svelte: undefined? - Stack Overflow
The log results in undefined because. Reactive statements run after other script code and before the component markup is rendered, ...
Read more >
Compile Svelte in your head (Part 1) | Tan Li Hau
The Svelte compiler analyses the code you write and generates an optimised JavaScript output. To study how Svelte compiles the code, ...
Read more >
Binding DOM to JS variables in Svelte - YouTube
Shows how to access, manipulate, and attach listeners on DOM nodes from Svelte code. Useful for integrating 3rd party JS libraries.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found