microsoft / microsoft/TypeScript

Make 'new.target' emit more precautions

Offen
#15,474 11 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

I noticed that the polyfill for new.target ...

class Foo {
    constructor() {
        if (new.target)
            alert("Good.");
        else
            alert("Bad!");
    }
}

... outputs to this:

var Foo = (function () {
    function Foo() {
        var _newTarget = this.constructor;
        if (_newTarget)
            alert("Good.");
        else
            alert("Bad!");
    }
    return Foo;
}());

That seems dangerous to me, since this is always a true condition for older browsers that don't yet support new.target. Does it not make more sense to create an output such as this?:

var Foo = (function () {
    function Foo() {
        var _newTarget = this && this.constructor !== Window ? this.constructor : void 0;
                         /* (have to check 'this' also, in case of strict mode) */
        if (_newTarget)
            alert("Good.");
        else
            alert("Bad!");
    }
    return Foo;
}());

The following code fails to work as expected in Chrome v56.0.2924.87 (output is "Good." in call cases):

class $Foo {
    constructor() {
        if (new.target)
            alert("Good.");
        else
            alert("Bad!");
    }
}

type FooConstructor = typeof $Foo;

interface CallableFoo extends FooConstructor { (): $Foo; }

new $Foo(); // ok
var Foo: CallableFoo = <any>Foo;
Foo(); // ok? :/

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem verknüpften TypeScript Playground-Repro und vergleiche das ausgegebene JavaScript für new $Foo() und Foo(). Verfolge die Absenkung von new.target im Compiler; fertig ist die Arbeit, wenn die generierte Ausgabe die unterschiedlichen Ergebnisse für Konstruktor- und gewöhnliche Aufrufe beibehält, ohne beide als truthy zu behandeln.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.