scala-js/scala-js-website

vite Scala3 code section doesn't compile

Closed this issue · 1 comments

Code as it appears on scala-js.org: https://www.scala-js.org/doc/tutorial/scalajs-vite.html#

package livechart

import scala.scalajs.js
import scala.scalajs.js.annotation.*

import org.scalajs.dom

// import javascriptLogo from "/javascript.svg"
@js.native @JSImport("/javascript.svg", JSImport.Default)
val javascriptLogo: String = js.native

@main
def LiveChart(): Unit =
  dom.document.querySelector("#app").innerHTML = s"""
    <div>
      <a href="https://vitejs.dev" target="_blank">
        <img src="/vite.svg" class="logo" alt="Vite logo" />
      </a>
      <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript" target="_blank">
        <img src="$javascriptLogo" class="logo vanilla" alt="JavaScript logo" />
      </a>
      <h1>Hello Scala.js!</h1>
      <div class="card">
        <button id="counter" type="button"></button>
      </div>
      <p class="read-the-docs">
        Click on the Vite logo to learn more
      </p>
    </div>
  """

  setupCounter(dom.document.getElementById("counter"))
end LiveChart

def setupCounter(element: dom.Element): Unit =
  var counter = 0

  def setCounter(count: Int): Unit =
    counter = count
    element.innerHTML = s"count is $counter"

  element.addEventListener("click", e => setCounter(counter + 1))
  setCounter(0)
end setupCounter

Error I receive:

[error] -- [E103] Syntax Error: /Users/chris/dev/livechart/src/main/scala/livechart/LiveChart.scala:32:0                                                                                                   
[error] 32 |setupCounter(dom.document.getElementById("counter"))                                                                                                                                           
[error]    |^^^^^^^^^^^^                                                                                                                                                                                   
[error]    |Illegal start of toplevel definition                                                                                                                                                           
[error]    |                                                                                                                                                                                               
[error]    | longer explanation available when compiling with `-explain`                                                                                                                                   
[error] -- [E018] Syntax Error: /Users/chris/dev/livechart/src/main/scala/livechart/LiveChart.scala:35:46                                                                                                  
[error] 35 |def setupCounter(element: dom.Element): Unit =                                                                                                                                                 
[error]    |                                              ^                                                                                                                                                
[error]    |                                       expression expected but var found                                                                                                                       
[error]    |                                                                                                                                                                                               
[error]    | longer explanation available when compiling with `-explain`                                                                                                                                   
[error] -- [E103] Syntax Error: /Users/chris/dev/livechart/src/main/scala/livechart/LiveChart.scala:40:0                                                                                                   
[error] 40 |element.innerHTML = s"count is $counter"                                                                                                                                                       
[error]    |^^^^^^^                                                                                                                                                                                        
[error]    |Illegal start of toplevel definition                                                                                                                                                           
[error]    |                                                                                                                                                                                               
[error]    | longer explanation available when compiling with `-explain`                                                                                                                                   
[error] -- Error: /Users/chris/dev/livechart/src/main/scala/livechart/LiveChart.scala:44:0                                                                                                                 
[error] 44 |end setupCounter                                                                                                                                                                               
[error]    |^^^^^^^^^^^^^^^^                                                                                                                                                                               
[error]    |misaligned end marker                                                                                                                                                                          
[error] -- [E006] Not Found Error: /Users/chris/dev/livechart/src/main/scala/livechart/LiveChart.scala:39:2                                                                                                
[error] 39 |  counter = count                                                                                                                                                                              
[error]    |  ^^^^^^^                                                                                                                                                                                      
[error]    |  Not found: counter - did you mean count?                                                                                                                                                     
[error]    |                                                                                         
[error]    | longer explanation available when compiling with `-explain`
[error] 5 errors found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 2 s, completed Jun 2, 2024, 12:40:13 PM
sjrd commented

The first error suggests that there was a copy-paste error that removed the indentation of the line. The setupCounter(...) line is supposed to start with 2 spaces, but the error suggests it has no leading space.

Since I just went over the whole tutorial to do #641 and experienced no such error, I'm going to consider this closed.