microsoft / microsoft/TypeScript

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

未关闭
#37,720 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Bug Domain: Binder
主要语言
Go
星标
111k
派生
14.4k
平均合并
1 天 19 小时
30 天内合并 PR
117

描述

/** @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.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 issue 中的复现开始,检查 bindThisPropertyAssignment,尤其要关注 prototype 对象属性赋值中的函数表达式是如何处理的。在 noImplicitThis 下使用所示示例验证行为,包括 c.a 和 c.b 是否都能正常工作,以及报告的诊断是否已消失。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, typescript
领域
compilers
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。