Skip to content

Bug in build-optimizer or uglify-es (AOT output bug) #12082

Closed
@fbernhard

Description

@fbernhard

As per @trotyl on angular/angular#25800 I post this here:

Bug Report or Feature Request (mark with an x)

- [ X] bug report -> please search issues before submitting
- [ ] feature request

Command (mark with an x)

- [ ] new
- [ X] build
- [ ] serve
- [ ] test
- [ ] e2e
- [ ] generate
- [ ] add
- [ ] update
- [ ] lint
- [ ] xi18n
- [ ] run
- [ ] config
- [ ] help
- [ ] version
- [ ] doc

Versions

Angular CLI: 6.0.8
Node: 10.4.0
OS: darwin x64
Angular: 6.1.6

Repro steps

Current behavior

The following Typescript code:

let table1 = [null, "hello"];
let table2 = [null, "world"];

let i = -1;
for (const row of table1) {
  i++;
  if (!row) {
    continue;
  }
  console.log(row, table2[i])
}

when compiled with AOT in an Angular project (ng build --prod --build-optimizer --vendor-chunk=true --aot), produces the following "compiled" code:

for (var l = [null, "world"], n = -1, u = 0, a = [null, "hello"]; u < a.length; u++) {
   var o = a[u];
   o && console.log(o, l[++n])
}

The compiled code checks for null first before it increments i, the written Typescript codes increments i before the null check.

When run in a browser the AOT code prints hello null

Expected behavior

The AOT compiled code in a browser should print hello world

Minimal reproduction of the problem with instructions

Copy above Typescript snipped into a newly created Angular project and build it with AOT.

What is the motivation / use case for changing the behavior?

We expect AOT compiled code to do exactly what the code intends to do

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions