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 temperature = 50;
let previous;
let direction = null;
// if this block ran more than once per tick it would always show "cooler"
$: {
if (previous !== undefined) {
direction = temperature > previous
? 'warmer'
: 'cooler';
}
previous = temperature;
}
</script>

<input type="range" bind:value={temperature}>

{#if direction}
<p>the temperature is getting {direction}</p>
{/if}
import 'svelte/internal/disclose-version';
import 'svelte/internal/flags/legacy';
import * as $ from 'svelte/internal/client';

var root_1 = $.template(`<p> </p>`);
var root = $.template(`<input type="range"> <!>`, 1);

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

let temperature = $.mutable_state(50);
let previous = $.mutable_state();
let direction = $.mutable_state(null);

$.legacy_pre_effect(() => ($.get(previous), $.get(temperature)), () => {
if ($.get(previous) !== undefined) {
$.set(direction, $.get(temperature) > $.get(previous) ? 'warmer' : 'cooler');
}

$.set(previous, $.get(temperature));
});

$.legacy_pre_effect_reset();

var fragment = root();
var input = $.first_child(fragment);

$.remove_input_defaults(input);

var node = $.sibling(input, 2);

{
var consequent = ($$anchor) => {
var p = root_1();
var text = $.child(p);

result = svelte.compile(source, {
generate: ,
});
/* Add a <style> tag to see the CSS output */
		
			
				
  • Root {
    • css: null
    • js: []
    • start: 328
    • end: 444
    • type: "Root"
    • fragment: Fragment {...}
      • type: "Fragment"
      • nodes: [...] (4)
        • Text {...}
          • type: "Text"
          • start: 326
          • end: 328
          • raw: "\n\n"
          • data: "\n\n"
          }
        • RegularElement {...}
          • type: "RegularElement"
          • start: 328
          • end: 373
          • name: "input"
          • attributes: [...] (2)
            • Attribute {...}
              • type: "Attribute"
              • start: 335
              • end: 347
              • name: "type"
              • value: [...] (1)
                • Text {...}
                  • start: 341
                  • end: 346
                  • type: "Text"
                  • raw: "range"
                  • data: "range"
                  }
                ]
              }
            • BindDirective {...}
              • start: 348
              • end: 372
              • type: "BindDirective"
              • name: "value"
              • expression: Identifier {...}
                • type: "Identifier"
                • start: 360
                • end: 371
                • loc: {...}
                  • start: {...}
                    • line: 18
                    • column: 32
                    }
                  • end: {...}
                    • line: 18
                    • column: 43
                    }
                  }
                • name: "temperature"
                }
              • modifiers: []
              }
            ]
          • fragment: Fragment {...}
            • type: "Fragment"
            • nodes: []
            }
          }
        • Text {...}
          • type: "Text"
          • start: 373
          • end: 375
          • raw: " "
          • data: " "
          }
        • IfBlock {...}
          • type: "IfBlock"
          • elseif: false
          • start: 375
          • end: 444
          • test: Identifier {...}
            • type: "Identifier"
            • start: 380
            • end: 389
            • loc: {...}
              • start: {...}
                • line: 20
                • column: 5
                }
              • end: {...}
                • line: 20
                • column: 14
                }
              }
            • name: "direction"
            }
          • consequent: Fragment {...}
            • type: "Fragment"
            • nodes: [...] (3)
              • Text {...}
                • type: "Text"
                • start: 390
                • end: 393
                • raw: "\n "
                • data: "\n "
                }
              • RegularElement {...}
                • type: "RegularElement"
                • start: 393
                • end: 438
                • name: "p"
                • attributes: []
                • fragment: Fragment {...}
                  • type: "Fragment"
                  • nodes: [...] (2)
                    • Text {...}
                      • type: "Text"
                      • start: 396
                      • end: 423
                      • raw: "the temperature is getting "
                      • data: "the temperature is getting "
                      }
                    • ExpressionTag {...}
                      • type: "ExpressionTag"
                      • start: 423
                      • end: 434
                      • expression: Identifier {...}
                        • type: "Identifier"
                        • start: 424
                        • end: 433
                        • loc: {...}
                          • start: {...}
                            • line: 21
                            • column: 33
                            }
                          • end: {...}
                            • line: 21
                            • column: 42
                            }
                          }
                        • name: "direction"
                        }
                      }
                    ]
                  }
                }
              • Text {...}
                • type: "Text"
                • start: 438
                • end: 439
                • raw: "\n"
                • data: "\n"
                }
              ]
            }
          • alternate: null
          }
        ]
      }
    • options: null
    • instance: Script {...}
      • type: "Script"
      • start: 0
      • end: 326
      • context: "default"
      • content: Program {...}
        • type: "Program"
        • start: 8
        • end: 317
        • loc: {...}
          • start: {...}
            • line: 1
            • column: 0
            }
          • end: {...}
            • line: 16
            • column: 0
            }
          }
        • body: [...] (4)
          • VariableDeclaration {...}
            • type: "VariableDeclaration"
            • start: 11
            • end: 32
            • loc: {...}
              • start: {...}
                • line: 2
                • column: 2
                }
              • end: {...}
                • line: 2
                • column: 23
                }
              }
            • declarations: [...] (1)
              • VariableDeclarator {...}
                • type: "VariableDeclarator"
                • start: 15
                • end: 31
                • loc: {...}
                  • start: {...}
                    • line: 2
                    • column: 6
                    }
                  • end: {...}
                    • line: 2
                    • column: 22
                    }
                  }
                • id: Identifier {...}
                  • type: "Identifier"
                  • start: 15
                  • end: 26
                  • loc: {...}
                    • start: {...}
                      • line: 2
                      • column: 6
                      }
                    • end: {...}
                      • line: 2
                      • column: 17
                      }
                    }
                  • name: "temperature"
                  }
                • init: Literal {...}
                  • type: "Literal"
                  • start: 29
                  • end: 31
                  • loc: {...}
                    • start: {...}
                      • line: 2
                      • column: 20
                      }
                    • end: {...}
                      • line: 2
                      • column: 22
                      }
                    }
                  • value: 50
                  • raw: "50"
                  }
                }
              ]
            • kind: "let"
            }
          • VariableDeclaration {...}
            • type: "VariableDeclaration"
            • start: 35
            • end: 48
            • loc: {...}
              • start: {...}
                • line: 3
                • column: 2
                }
              • end: {...}
                • line: 3
                • column: 15
                }
              }
            • declarations: [...] (1)
              • VariableDeclarator {...}
                • type: "VariableDeclarator"
                • start: 39
                • end: 47
                • loc: {...}
                  • start: {...}
                    • line: 3
                    • column: 6
                    }
                  • end: {...}
                    • line: 3
                    • column: 14
                    }
                  }
                • id: Identifier {...}
                  • type: "Identifier"
                  • start: 39
                  • end: 47
                  • loc: {...}
                    • start: {...}
                      • line: 3
                      • column: 6
                      }
                    • end: {...}
                      • line: 3
                      • column: 14
                      }
                    }
                  • name: "previous"
                  }
                • init: null
                }
              ]
            • kind: "let"
            }
          • VariableDeclaration {...}
            • type: "VariableDeclaration"
            • start: 54
            • end: 75
            • loc: {...}
              • start: {...}
                • line: 5
                • column: 2
                }
              • end: {...}
                • line: 5
                • column: 23
                }
              }
            • declarations: [...] (1)
              • VariableDeclarator {...}
                • type: "VariableDeclarator"
                • start: 58
                • end: 74
                • loc: {...}
                  • start: {...}
                    • line: 5
                    • column: 6
                    }
                  • end: {...}
                    • line: 5
                    • column: 22
                    }
                  }
                • id: Identifier {...}
                  • type: "Identifier"
                  • start: 58
                  • end: 67
                  • loc: {...}
                    • start: {...}
                      • line: 5
                      • column: 6
                      }
                    • end: {...}
                      • line: 5
                      • column: 15
                      }
                    }
                  • name: "direction"
                  }
                • init: Literal {...}
                  • type: "Literal"
                  • start: 70
                  • end: 74
                  • loc: {...}
                    • start: {...}
                      • line: 5
                      • column: 18
                      }
                    • end: {...}
                      • line: 5
                      • column: 22
                      }
                    }
                  • value: null
                  • raw: "null"
                  }
                }
              ]
            • kind: "let"
            }
          • LabeledStatement {...}
            • type: "LabeledStatement"
            • start: 155
            • end: 316
            • loc: {...}
              • start: {...}
                • line: 7
                • column: 2
                }
              • end: {...}
                • line: 15
                • column: 3
                }
              }
            • body: BlockStatement {...}
              • type: "BlockStatement"
              • start: 158
              • end: 316
              • loc: {...}
                • start: {...}
                  • line: 7
                  • column: 5
                  }
                • end: {...}
                  • line: 15
                  • column: 3
                  }
                }
              • body: [...] (2)
                • IfStatement {...}
                  • type: "IfStatement"
                  • start: 164
                  • end: 279
                  • loc: {...}
                    • start: {...}
                      • line: 8
                      • column: 4
                      }
                    • end: {...}
                      • line: 12
                      • column: 5
                      }
                    }
                  • test: BinaryExpression {...}
                    • type: "BinaryExpression"
                    • start: 168
                    • end: 190
                    • loc: {...}
                      • start: {...}
                        • line: 8
                        • column: 8
                        }
                      • end: {...}
                        • line: 8
                        • column: 30
                        }
                      }
                    • left: Identifier {...}
                      • type: "Identifier"
                      • start: 168
                      • end: 176
                      • loc: {...}
                        • start: {...}
                          • line: 8
                          • column: 8
                          }
                        • end: {...}
                          • line: 8
                          • column: 16
                          }
                        }
                      • name: "previous"
                      }
                    • operator: "!=="
                    • right: Identifier {...}
                      • type: "Identifier"
                      • start: 181
                      • end: 190
                      • loc: {...}
                        • start: {...}
                          • line: 8
                          • column: 21
                          }
                        • end: {...}
                          • line: 8
                          • column: 30
                          }
                        }
                      • name: "undefined"
                      }
                    }
                  • consequent: BlockStatement {...}
                    • type: "BlockStatement"
                    • start: 192
                    • end: 279
                    • loc: {...}
                      • start: {...}
                        • line: 8
                        • column: 32
                        }
                      • end: {...}
                        • line: 12
                        • column: 5
                        }
                      }
                    • body: [...] (1)
                      • ExpressionStatement {...}
                        • type: "ExpressionStatement"
                        • start: 200
                        • end: 273
                        • loc: {...}
                          • start: {...}
                            • line: 9
                            • column: 6
                            }
                          • end: {...}
                            • line: 11
                            • column: 19
                            }
                          }
                        • expression: AssignmentExpression {...}
                          • type: "AssignmentExpression"
                          • start: 200
                          • end: 272
                          • loc: {...}
                            • start: {...}
                              • line: 9
                              • column: 6
                              }
                            • end: {...}
                              • line: 11
                              • column: 18
                              }
                            }
                          • operator: "="
                          • left: Identifier {...}
                            • type: "Identifier"
                            • start: 200
                            • end: 209
                            • loc: {...}
                              • start: {...}
                                • line: 9
                                • column: 6
                                }
                              • end: {...}
                                • line: 9
                                • column: 15
                                }
                              }
                            • name: "direction"
                            }
                          • right: ConditionalExpression {...}
                            • type: "ConditionalExpression"
                            • start: 212
                            • end: 272
                            • loc: {...}
                              • start: {...}
                                • line: 9
                                • column: 18
                                }
                              • end: {...}
                                • line: 11
                                • column: 18
                                }
                              }
                            • test: BinaryExpression {...}
                              • type: "BinaryExpression"
                              • start: 212
                              • end: 234
                              • loc: {...}
                                • start: {...}
                                  • line: 9
                                  • column: 18
                                  }
                                • end: {...}
                                  • line: 9
                                  • column: 40
                                  }
                                }
                              • left: Identifier {...}
                                • type: "Identifier"
                                • start: 212
                                • end: 223
                                • loc: {...}
                                  • start: {...}
                                    • line: 9
                                    • column: 18
                                    }
                                  • end: {...}
                                    • line: 9
                                    • column: 29
                                    }
                                  }
                                • name: "temperature"
                                }
                              • operator: ">"
                              • right: Identifier {...}
                                • type: "Identifier"
                                • start: 226
                                • end: 234
                                • loc: {...}
                                  • start: {...}
                                    • line: 9
                                    • column: 32
                                    }
                                  • end: {...}
                                    • line: 9
                                    • column: 40
                                    }
                                  }
                                • name: "previous"
                                }
                              }
                            • consequent: Literal {...}
                              • type: "Literal"
                              • start: 245
                              • end: 253
                              • loc: {...}
                                • start: {...}
                                  • line: 10
                                  • column: 10
                                  }
                                • end: {...}
                                  • line: 10
                                  • column: 18
                                  }
                                }
                              • value: "warmer"
                              • raw: "'warmer'"
                              }
                            • alternate: Literal {...}
                              • type: "Literal"
                              • start: 264
                              • end: 272
                              • loc: {...}
                                • start: {...}
                                  • line: 11
                                  • column: 10
                                  }
                                • end: {...}
                                  • line: 11
                                  • column: 18
                                  }
                                }
                              • value: "cooler"
                              • raw: "'cooler'"
                              }
                            }
                          }
                        }
                      ]
                    }
                  • alternate: null
                  }
                • ExpressionStatement {...}
                  • type: "ExpressionStatement"
                  • start: 289
                  • end: 312
                  • loc: {...}
                    • start: {...}
                      • line: 14
                      • column: 4
                      }
                    • end: {...}
                      • line: 14
                      • column: 27
                      }
                    }
                  • expression: AssignmentExpression {...}
                    • type: "AssignmentExpression"
                    • start: 289
                    • end: 311
                    • loc: {...}
                      • start: {...}
                        • line: 14
                        • column: 4
                        }
                      • end: {...}
                        • line: 14
                        • column: 26
                        }
                      }
                    • operator: "="
                    • left: Identifier {...}
                      • type: "Identifier"
                      • start: 289
                      • end: 297
                      • loc: {...}
                        • start: {...}
                          • line: 14
                          • column: 4
                          }
                        • end: {...}
                          • line: 14
                          • column: 12
                          }
                        }
                      • name: "previous"
                      }
                    • right: Identifier {...}
                      • type: "Identifier"
                      • start: 300
                      • end: 311
                      • loc: {...}
                        • start: {...}
                          • line: 14
                          • column: 15
                          }
                        • end: {...}
                          • line: 14
                          • column: 26
                          }
                        }
                      • name: "temperature"
                      }
                    }
                  }
                ]
              }
            • label: Identifier {...}
              • type: "Identifier"
              • start: 155
              • end: 156
              • loc: {...}
                • start: {...}
                  • line: 7
                  • column: 2
                  }
                • end: {...}
                  • line: 7
                  • column: 3
                  }
                }
              • name: "$"
              }
            • leadingComments: [...] (1)
              • Line {...}
                • type: "Line"
                • value: " if this block ran more than once per tick it would always show \"cooler\""
                • start: 78
                • end: 152
                }
              ]
            }
          ]
        • sourceType: "module"
        }
      • attributes: []
      }
    }
The AST is not public API and may change at any point in time
logic relies on once per tick evaluation (courtesy of Rich Harris) • Playground • Svelte