TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

checker.getSuggestionDiagnostics fails with `TypeError: Cannot read properties of undefined (reading 'parent')`

Open vdiez opened this issue 10 months ago • 4 comments

🔎 Search Terms

tsc: "Cannot read properties of undefined (reading 'parent')" getThisType getSuggestionDiagnostics

🕗 Version & Regression Information

  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

No response

💻 Code

I have an ESLint rule where I'm getting the TS diagnostics to report deprecations.

While linting a project with JS code, the rule crashes. I managed to get the very minimum reproducer with a TS program with this file:

//test.js
/**
 * @param {String}
 * @return {this}
 */
Foo.prototype.on = function(type) {
  return this;
};

and a tsconfig.json with the following contents:

{
	"compilerOptions": {
		"allowJs": true,
		"noImplicitAny": true
	},
	"files": ["test.js]
}
const sourceFile = program.getSourceFile('test.js');
const diagnostics: ts.DiagnosticWithLocation[] = checker.getSuggestionDiagnostics(sourceFile);

🙁 Actual behavior

It crashes with error:

TypeError: Cannot read properties of undefined (reading 'parent')
    at eval (eval at getThisType (C:\projects\test\node_modules\typescript\lib\typescript.js:66507:7), <anonymous>:1:35)
    at getThisType (C:\projects\test\node_modules\typescript\lib\typescript.js:66507:7)
    at getTypeFromThisTypeNode (C:\projects\test\node_modules\typescript\lib\typescript.js:66518:28)
    at getTypeFromTypeNodeWorker (C:\projects\test\node_modules\typescript\lib\typescript.js:66586:16)
    at getTypeFromTypeNode (C:\projects\test\node_modules\typescript\lib\typescript.js:66552:41)
    at getReturnTypeFromAnnotation (C:\projects\test\node_modules\typescript\lib\typescript.js:63200:14)
    at checkFunctionExpressionOrObjectLiteralMethodDeferred (C:\projects\test\node_modules\typescript\lib\typescript.js:82410:24)
    at checkDeferredNode (C:\projects\test\node_modules\typescript\lib\typescript.js:89929:9)
    at Set.forEach (<anonymous>)
    at checkDeferredNodes (C:\projects\test\node_modules\typescript\lib\typescript.js:89907:27)

🙂 Expected behavior

When I remove the JSDoc or I extend the prototype of a known object, this does not crash. I would expect checker.getSuggestionDiagnostics(sourceFile) to return an empty array.

Additional information about the issue

No response

vdiez avatar Feb 27 '25 15:02 vdiez

Unfortunately it's pretty easy to hold the compiler API the wrong way and cause weird exceptions. We need a complete runnable standalone repro in order to investigate.

RyanCavanaugh avatar Feb 27 '25 17:02 RyanCavanaugh

Thanks for the quick answer @RyanCavanaugh,

I prepared a reproducer here

Instructions:

git clone https://github.com/vdiez/tsc_repro.git
cd tsc_repro
npm install
node repro.js

vdiez avatar Feb 27 '25 18:02 vdiez

for info, it does not crash if we change the test.js as such:

  • we add the Foo declaration:
//test.js
function Foo() {}

/**
 * @param {String}
 * @return {this}
 */
Foo.prototype.on = function(type) {
  return this;
};
  • we just remove the JSDoc:
Foo.prototype.on = function(type) {
  return this;
};

vdiez avatar Feb 27 '25 18:02 vdiez

Closing language service bugs related to the 6.0 implementation. For more information, see #62827

RyanCavanaugh avatar Dec 09 '25 23:12 RyanCavanaugh