jsverse/transloco-keys-manager

Bug: keys not extracted from typescript files after an explicit coercion expression

Closed this issue · 2 comments

Is there an existing issue for this?

  • I have searched the existing issues

Is this a regression?

Yes

Current behavior

Strings passed to a translate call that follow a coercion expression or some other expression containing <> are not extracted.

import {Component} from "@angular/core";
import {translate} from "@jsverse/transloco";

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrl: './app.component.scss'
})
export class AppComponent {
  constructor() {
    translate("This first key will be extracted");
  }

  getString(): string
  {
    return "5";
  }

  extractionProblem(): void
  {
    translate("This second key will be extracted");
    const foo = <string>this.getString();
    translate("This third key will NOT be extracted");
  }
}

Expected behavior

All strings should be extracted

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/~/github.com/dalyl-zero/transloco-bug-report

Transloco Config

No response

Debug Logs

No response

Please provide the environment you discovered this bug in

Transloco: 7.1.0
Transloco Keys Manager: 4.1.0
Angular: 17.3.0
Node: 18.13.0
Package Manager: 
OS: Windows/Mac

Additional context

This does not happen on transloco-keys-manager 3.8.0 and is independent of the version of Transloco

I would like to make a pull request for this bug

No

Also stumbled upon this issue!
It is very sneaky, it ignores the remaining part of the file from key extractor perspective, with no clear errors so it can be overlooked very easily.

The issue was that the TypeScript prefix cast operator, the AST thinks this is a JSX element, and that's also the original reason why the as operator was introduced back in v1.6.
Nice catch 👍