microsoft / microsoft/TypeScript

Function expressions in a property assignment of a prototype object should be methods, not nested classes

Offen
#37,720 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: Binder
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

/** @class */
module.exports.C = function() {
  this.x = 1
}
module.exports.C.prototype = {
  m1() {
    this.a = 1
  },
  m2: function() {
    this.b = 2
  }
}
var c = new module.exports.C()
c.a
c.b

Expected behavior:
c.a and c.b both work.

Actual behavior:
Only c.a works; c.b says that 'd' does not exist on type 'C'. There's also an error on this.b = 2 when noImplicitThis: true.

bindThisPropertyAssignment needs to understand that function expressions might be part of a property assignment in an object literal. The code to handle this will probably look like this:

                    // For `{ x: function() }` should modify the object literal's members, not behave like a fresh class (as long as it doesn't have @class on it)
                    if (isPropertyAssignment(thisContainer.parent)) {
                        // wow I hope those parent pointers are set!
                        const containingClass = thisContainer.parent.parent;
                        const symbolTable = containingClass.symbol.members!;
                        if (hasDynamicName(node)) {
                            bindDynamicallyNamedThisPropertyAssignment(node, containingClass.symbol);
                        }
                        else {
                            declareSymbol(symbolTable, containingClass.symbol, node, SymbolFlags.Property | SymbolFlags.Assignment, SymbolFlags.None, /*isReplaceableByMethod*/ true);
                        }
                        break;
                    }

I expect this to break a lot of tests, but it didn't. However, I haven't tried it on the user tests yet.

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 der Reproduktion im Issue und untersuche bindThisPropertyAssignment, insbesondere, wie Function Expressions innerhalb von Eigenschaftszuweisungen von Prototype-Objekten behandelt werden. Überprüfe das Verhalten mit dem gezeigten Beispiel unter noImplicitThis, einschließlich der Frage, ob sowohl c.a als auch c.b funktionieren und die gemeldete Diagnose verschwunden ist.

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
Klar beschrieben
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

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