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

JS: Separate JSDoc qualified names into individual identifiers #19077

Merged
merged 15 commits into from
Mar 27, 2025

Conversation

asgerf
Copy link
Contributor

@asgerf asgerf commented Mar 20, 2025

Changes the underlying representation of JSDocNamedTypeExpr so each identifier in a qualified name has its own node. This aligns with how such types are represented in TypeScript type annotations.

Previously Foo.Bar was a single node, where as now Foo and Bar are now identifier nodes wrapped in a qualified type name node.

Before:

  • (JSDocNamedTypeExpr) Foo.Bar

After:

  • (JSDocQualifiedTypeExpr)
    • getBase()
      • (JSDocLocalTypeAccess) Foo
    • getNameNode()
      • (JSDocIdentifierTypeExpr) Bar

The names JSDocQualifiedTypeAccess and JSDocLocalTypeAccess were chosen to align with the QualifiedTypeAccess and LocalTypeAccess classes, which were themselves chosen to align with VarAccess.

For context, the reason for this change is that we're moving name/type resolution out of the TypeScript extractor into plain QL. We already have a very basic name resolution for JSDoc types in QL and I'd like to ensure that the new implementation can replace the JSDoc name resolution so we have a shared implementation for JSDoc and TypeScript. This is easier to achieve if the representations are similar.

@github-actions github-actions bot added the JS label Mar 20, 2025
@asgerf asgerf force-pushed the js/jsdoc-name-tokens branch 2 times, most recently from cabc847 to b140e02 Compare March 21, 2025 08:48
@asgerf asgerf added the no-change-note-required This PR does not need a change note label Mar 25, 2025
@asgerf asgerf marked this pull request as ready for review March 25, 2025 08:39
@Copilot Copilot bot review requested due to automatic review settings March 25, 2025 08:39
@asgerf asgerf requested a review from a team as a code owner March 25, 2025 08:39
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request refactors the representation of JSDoc qualified names so that each identifier in a qualified name is represented as its own node instead of a single aggregated node. Key changes include:

  • Introduction of a new QualifiedNameExpression class to represent qualified names.
  • Replacement of the old NameExpression with Identifier and QualifiedNameExpression throughout the parser and extractor.
  • Update of the extractor version to reflect these changes.

Reviewed Changes

Copilot reviewed 10 out of 21 changed files in this pull request and generated no comments.

Show a summary per file
File Description
javascript/extractor/src/com/semmle/js/ast/jsdoc/QualifiedNameExpression.java New class to represent qualified names by splitting identifiers
javascript/extractor/src/com/semmle/js/extractor/Main.java Updated extractor version
javascript/extractor/src/com/semmle/js/parser/JSDocParser.java Replaced NameExpression with Identifier and added support for qualified names
javascript/extractor/src/com/semmle/js/ast/jsdoc/Identifier.java Renamed NameExpression to Identifier and updated documentation accordingly
javascript/extractor/src/com/semmle/js/extractor/JSDocExtractor.java Updated type mappings and visitor methods to work with new Identifier and QualifiedNameExpression types
javascript/extractor/src/com/semmle/js/ast/jsdoc/Visitor.java Updated visitor interface to reflect the new type names
Files not reviewed (11)
  • javascript/downgrades/ccefb5e2d49318eea4aeafd4c6ae2af9f94ac72a/upgrade.properties: Language not supported
  • javascript/extractor/tests/comments/output/trap/jsdoc.js.trap: Language not supported
  • javascript/ql/lib/semmle/javascript/DOM.qll: Language not supported
  • javascript/ql/lib/semmle/javascript/Externs.qll: Language not supported
  • javascript/ql/lib/semmle/javascript/JSDoc.qll: Language not supported
  • javascript/ql/lib/semmlecode.javascript.dbscheme: Language not supported
  • javascript/ql/lib/semmlecode.javascript.dbscheme.stats: Language not supported
  • javascript/ql/lib/upgrades/5b5db607d20c7b449cef2d1c926b24d77c69bebb/upgrade.properties: Language not supported
  • javascript/ql/test/library-tests/JSDoc/NameResolution/test.expected: Language not supported
  • javascript/ql/test/library-tests/JSDoc/Nodes/tests.expected: Language not supported
  • javascript/ql/test/library-tests/TypeAnnotations/JSDoc/JSDocTypeAnnotations.expected: Language not supported

Tip: Copilot only keeps its highest confidence comments to reduce noise and keep you focused. Learn more

erik-krogh
erik-krogh previously approved these changes Mar 25, 2025
Comment on lines 1 to 2
description: split up qualified names in jsdoc type exprs
compatibility: backwards
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So every existing jsdoc_named_type_expr gets converted to a jsdoc_identifier_type_expr?

Sounds fine to me.
And I suppose an upgrade script to split the identifiers is impossible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the compatibility to partial to reflect that it won't be the same as a freshly built database but will mostly work

Comment on lines +1 to +2
description: split up qualified names in jsdoc type exprs
compatibility: backwards
Copy link
Contributor

@erik-krogh erik-krogh Mar 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here I suppose all jsdoc_qualified_type_expr are discarded?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's not good as nodes in the middle of the tree will effectively disappear. I've pushed a proper downgrade script and tested it.

asgerf added 15 commits March 27, 2025 11:51
Test updates look OK. Some intermediate results are omitted but the
qualified name of the final type names are still present.
The call to expect() below here updates 'token' and 'value' to that of the NEXT token (not the name).

The code happened to work because the 'value' field is only updated if a token with a relevant value is found. E.g. if a name token could be followed by another name, then we would have seen the wrong name here.
@asgerf
Copy link
Contributor Author

asgerf commented Mar 27, 2025

Sorry about the force-push; I had to rebase the downgrade script earlier in history to test it, and after I put it back to its original place the SHA of all the commits changed even though their contents are the same 🤷

@asgerf asgerf merged commit ed50343 into github:main Mar 27, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JS no-change-note-required This PR does not need a change note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants