Search code examples
functional-programmingpurescript

Variable within `do` block not found PureScript


This does not work:

itAsync "subscribeEventedOn should receive any attached data" $ \done -> do      
  w <- getWindow
  subscribeEventedOn "foo" (\event -> do
    fprint event
    expect (unwrapDetail event) `toDeepEqual` d'
    itIs done
  ) w
  emitOn sampleEvent w
  expect true `toEqual` true

Unknown value 'w'

but this does work:

w <- getWindow
itAsync "subscribeEventedOn should receive any attached data" $ \done -> do      
  subscribeEventedOn "foo" (\event -> do
    fprint event
    expect (unwrapDetail event) `toDeepEqual` d'
    itIs done
  ) w
  emitOn sampleEvent w
  expect true `toEqual` true

why? This does not make sense to me. (Btw I have nested do blocks in my unit tests)


Solution

  • I was able to reproduce this by guessing the types of the names in your example code, and it looks like this is caused by a peculiarity of the PureScript lexer. You can fix it by moving the closing paren up to the line above:

    itAsync "subscribeEventedOn should receive any attached data" $ \done -> do      
      w <- getWindow
      subscribeEventedOn "foo" (\event -> do
        fprint event
        expect (unwrapDetail event) `toDeepEqual` d'
        itIs done) w
      emitOn sampleEvent w
      expect true `toEqual` true