microsoft / microsoft/TypeScript
Function expressions in a property assignment of a prototype object should be methods, not nested classes
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
/** @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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la riproduzione nell’issue ed esamina bindThisPropertyAssignment, in particolare il modo in cui vengono gestite le espressioni di funzione all’interno delle assegnazioni di proprietà di oggetti prototype. Verifica il comportamento con l’esempio mostrato usando noImplicitThis, incluso se sia c.a sia c.b funzionano e se la diagnostica segnalata è scomparsa.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 45/100