Fable: Negating negative literal values produces invalid JS

Created on 16 Oct 2017  路  4Comments  路  Source: fable-compiler/Fable

Description

Webpack issue after compilation:

```f#
ERROR in ./src/InterScreen.fs
Module parse failed: C:UserswhitetigleDocumentsworkspacecapmetier2017gamenode_modulesfable-loaderindex.js??ref--0!C:UserswhitetigleDocumentsworkspacecapmetier2017gamesrcInterScreen.fs Assigning to rvalue (1128:65)
You may need an appropriate loader to handle this file type.
| var pipe2 = function (tupledArg_34) {
| return addTo(rightScreen_3, 2, tupledArg_34[0], tupledArg_34[1]);
| }(["hydro_start_p", [rightScreen_3.width * 0.56, --345]]);
| } else if (kind.data[0].tag === 3) {} else {
| var elec1 = function (tupledArg_35) {
@ ./src/App.fs 2:0-42
@ ./src/game.fsproj
@ multi (webpack)-dev-server/client?http://localhost:8080 ./src/game.fsproj

Here's the extracted wrong output: 
```js
(["hydro_start_p", [rightScreen_3.width * 0.56, --345]]);

This is caused by this piece of code:

            let pipeMargin = -345.

            //ok
            let pipe1 =
              ("hydro_start_p",(leftScreen.width*0.56,            pipeMargin             ))
              |> addTo leftScreen 1


            //not ok
           let pipe2 =
              ("hydro_start_p",(rightScreen.width*0.56,           -pipeMargin         ))
              |> addTo rightScreen 2

-pipeMargin output is --345.

Expected and actual results

it should output 345.

this works:

```f#
let pipeMargin = 345.

        let pipe1 =
          ("hydro_start_p",(leftScreen.width*0.56,-pipeMargin))
          |> addTo leftScreen 1

        let pipe2 =
          ("hydro_start_p",(rightScreen.width*0.56,pipeMargin))
          |> addTo rightScreen 2

```

Related information

  • Fable version (dotnet fable --version): 1.2.4
  • Webpack version: 3.5.6

All 4 comments

Ah, interesting. This is due to a Fable optimization where an immutable variable is replaced by the bound value to prevent having too many assignments (the F# compiler generates many assignments in pattern matching or piping for example) but it seems it also happens with literals or inline functions:

let [<Literal>] pipeMargin = -345.
//let inline pipeMargin<'T> = -345.

let test() =
    ("hydro_start_p",-pipeMargin)
    |> addTo 1

I'd expect that Babel would automatically surround this with parens but it doesn't seem to be the case. In any case, it should be easy to fix it in Fable though I hope it doesn't affect anything else beyond the minus sign :pray:

Hmm, this is trickier than I hoped for 馃槙

BTW Thanks for changing the name of the issue, it's way clearer now 馃憤

alfonsogarciacaro closed this in 7430b0e 7 hours ago

Bravo 馃憤 馃槃

Was this page helpful?
0 / 5 - 0 ratings

Related issues

et1975 picture et1975  路  3Comments

funlambda picture funlambda  路  4Comments

tomcl picture tomcl  路  4Comments

alfonsogarciacaro picture alfonsogarciacaro  路  3Comments

et1975 picture et1975  路  3Comments