Skip to main content
Create new
Introduction
Reactivity
Props
Logic
Events
Bindings
Lifecycle
Stores
Motion
Transitions
Animations
Easing
SVG
Actions
Classes
Component composition
Context API
Special elements
Module context
Debugging
7GUIs
Miscellaneous
App.svelte
<script>
let count = 1;
$: if (count) count++;
</script>

<h1>Count is {count}!</h1>
<button on:click={() => count++}>
clicking won't cause an infinite loop
</button>
import 'svelte/internal/disclose-version';
import 'svelte/internal/flags/legacy';
import * as $ from 'svelte/internal/client';

var root = $.template(`<h1> </h1> <button>clicking won't cause an infinite loop</button>`, 1);

export default function App($$anchor, $$props) {
$.push($$props, false);

let count = $.mutable_state(1);

$.legacy_pre_effect(() => ($.get(count)), () => {
if ($.get(count)) $.update(count);
});

$.legacy_pre_effect_reset();

var fragment = root();
var h1 = $.first_child(fragment);
var text = $.child(h1);

$.reset(h1);

var button = $.sibling(h1, 2);

$.template_effect(() => $.set_text(text, `Count is ${$.get(count) ?? ''}!`));
$.event('click', button, () => $.update(count));
$.append($$anchor, fragment);
$.pop();
}
result = svelte.compile(source, {
generate: ,
});
/* Add a <style> tag to see the CSS output */
		
			
				
  • Root {
    • css: null
    • js: []
    • start: 60
    • end: 170
    • type: "Root"
    • fragment: Fragment {...}
      • type: "Fragment"
      • nodes: [...] (4)
        • Text {...}
          • type: "Text"
          • start: 58
          • end: 60
          • raw: "\n\n"
          • data: "\n\n"
          }
        • RegularElement {...}
          • type: "RegularElement"
          • start: 60
          • end: 86
          • name: "h1"
          • attributes: []
          • fragment: Fragment {...}
            • type: "Fragment"
            • nodes: [...] (3)
              • Text {...}
                • type: "Text"
                • start: 64
                • end: 73
                • raw: "Count is "
                • data: "Count is "
                }
              • ExpressionTag {...}
                • type: "ExpressionTag"
                • start: 73
                • end: 80
                • expression: Identifier {...}
                  • type: "Identifier"
                  • start: 74
                  • end: 79
                  • loc: {...}
                    • start: {...}
                      • line: 6
                      • column: 14
                      }
                    • end: {...}
                      • line: 6
                      • column: 19
                      }
                    }
                  • name: "count"
                  }
                }
              • Text {...}
                • type: "Text"
                • start: 80
                • end: 81
                • raw: "!"
                • data: "!"
                }
              ]
            }
          }
        • Text {...}
          • type: "Text"
          • start: 86
          • end: 87
          • raw: " "
          • data: " "
          }
        • RegularElement {...}
          • type: "RegularElement"
          • start: 87
          • end: 170
          • name: "button"
          • attributes: [...] (1)
            • OnDirective {...}
              • start: 95
              • end: 119
              • type: "OnDirective"
              • name: "click"
              • expression: ArrowFunctionExpression {...}
                • type: "ArrowFunctionExpression"
                • start: 105
                • end: 118
                • loc: {...}
                  • start: {...}
                    • line: 7
                    • column: 18
                    }
                  • end: {...}
                    • line: 7
                    • column: 31
                    }
                  }
                • id: null
                • expression: true
                • generator: false
                • async: false
                • params: []
                • body: UpdateExpression {...}
                  • type: "UpdateExpression"
                  • start: 111
                  • end: 118
                  • loc: {...}
                    • start: {...}
                      • line: 7
                      • column: 24
                      }
                    • end: {...}
                      • line: 7
                      • column: 31
                      }
                    }
                  • operator: "++"
                  • prefix: false
                  • argument: Identifier {...}
                    • type: "Identifier"
                    • start: 111
                    • end: 116
                    • loc: {...}
                      • start: {...}
                        • line: 7
                        • column: 24
                        }
                      • end: {...}
                        • line: 7
                        • column: 29
                        }
                      }
                    • name: "count"
                    }
                  }
                }
              • modifiers: []
              }
            ]
          • fragment: Fragment {...}
            • type: "Fragment"
            • nodes: [...] (1)
              • Text {...}
                • type: "Text"
                • start: 120
                • end: 161
                • raw: "clicking won't cause an infinite loop"
                • data: "clicking won't cause an infinite loop"
                }
              ]
            }
          }
        ]
      }
    • options: null
    • instance: Script {...}
      • type: "Script"
      • start: 0
      • end: 58
      • context: "default"
      • content: Program {...}
        • type: "Program"
        • start: 8
        • end: 49
        • loc: {...}
          • start: {...}
            • line: 1
            • column: 0
            }
          • end: {...}
            • line: 4
            • column: 0
            }
          }
        • body: [...] (2)
          • VariableDeclaration {...}
            • type: "VariableDeclaration"
            • start: 10
            • end: 24
            • loc: {...}
              • start: {...}
                • line: 2
                • column: 1
                }
              • end: {...}
                • line: 2
                • column: 15
                }
              }
            • declarations: [...] (1)
              • VariableDeclarator {...}
                • type: "VariableDeclarator"
                • start: 14
                • end: 23
                • loc: {...}
                  • start: {...}
                    • line: 2
                    • column: 5
                    }
                  • end: {...}
                    • line: 2
                    • column: 14
                    }
                  }
                • id: Identifier {...}
                  • type: "Identifier"
                  • start: 14
                  • end: 19
                  • loc: {...}
                    • start: {...}
                      • line: 2
                      • column: 5
                      }
                    • end: {...}
                      • line: 2
                      • column: 10
                      }
                    }
                  • name: "count"
                  }
                • init: Literal {...}
                  • type: "Literal"
                  • start: 22
                  • end: 23
                  • loc: {...}
                    • start: {...}
                      • line: 2
                      • column: 13
                      }
                    • end: {...}
                      • line: 2
                      • column: 14
                      }
                    }
                  • value: 1
                  • raw: "1"
                  }
                }
              ]
            • kind: "let"
            }
          • LabeledStatement {...}
            • type: "LabeledStatement"
            • start: 26
            • end: 48
            • loc: {...}
              • start: {...}
                • line: 3
                • column: 1
                }
              • end: {...}
                • line: 3
                • column: 23
                }
              }
            • body: IfStatement {...}
              • type: "IfStatement"
              • start: 29
              • end: 48
              • loc: {...}
                • start: {...}
                  • line: 3
                  • column: 4
                  }
                • end: {...}
                  • line: 3
                  • column: 23
                  }
                }
              • test: Identifier {...}
                • type: "Identifier"
                • start: 33
                • end: 38
                • loc: {...}
                  • start: {...}
                    • line: 3
                    • column: 8
                    }
                  • end: {...}
                    • line: 3
                    • column: 13
                    }
                  }
                • name: "count"
                }
              • consequent: ExpressionStatement {...}
                • type: "ExpressionStatement"
                • start: 40
                • end: 48
                • loc: {...}
                  • start: {...}
                    • line: 3
                    • column: 15
                    }
                  • end: {...}
                    • line: 3
                    • column: 23
                    }
                  }
                • expression: UpdateExpression {...}
                  • type: "UpdateExpression"
                  • start: 40
                  • end: 47
                  • loc: {...}
                    • start: {...}
                      • line: 3
                      • column: 15
                      }
                    • end: {...}
                      • line: 3
                      • column: 22
                      }
                    }
                  • operator: "++"
                  • prefix: false
                  • argument: Identifier {...}
                    • type: "Identifier"
                    • start: 40
                    • end: 45
                    • loc: {...}
                      • start: {...}
                        • line: 3
                        • column: 15
                        }
                      • end: {...}
                        • line: 3
                        • column: 20
                        }
                      }
                    • name: "count"
                    }
                  }
                }
              • alternate: null
              }
            • label: Identifier {...}
              • type: "Identifier"
              • start: 26
              • end: 27
              • loc: {...}
                • start: {...}
                  • line: 3
                  • column: 1
                  }
                • end: {...}
                  • line: 3
                  • column: 2
                  }
                }
              • name: "$"
              }
            }
          ]
        • sourceType: "module"
        }
      • attributes: []
      }
    }
The AST is not public API and may change at any point in time
protection from infinite loops • Playground • Svelte