Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vdiez opened this issue Feb 27, 2025 · 3 comments
Assignees
Labels
Bug A bug in TypeScript

Comments

@vdiez
Copy link

vdiez commented Feb 27, 2025

🔎 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

@RyanCavanaugh RyanCavanaugh added the Needs More Info The issue still hasn't been fully clarified label Feb 27, 2025
@RyanCavanaugh
Copy link
Member

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.

vdiez added a commit to vdiez/tsc_repro that referenced this issue Feb 27, 2025
vdiez added a commit to vdiez/tsc_repro that referenced this issue Feb 27, 2025
@vdiez
Copy link
Author

vdiez commented Feb 27, 2025

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

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript and removed Needs More Info The issue still hasn't been fully clarified labels Feb 27, 2025
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 5.9.0 milestone Feb 27, 2025
@vdiez
Copy link
Author

vdiez commented Feb 27, 2025

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;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants