Skip to content

Expand assert-pure.ql to cover common and be a path-problem query#2455

Merged
robertbrignull merged 3 commits intomainfrom
robertbrignull/import-path-problem
May 31, 2023
Merged

Expand assert-pure.ql to cover common and be a path-problem query#2455
robertbrignull merged 3 commits intomainfrom
robertbrignull/import-path-problem

Conversation

@robertbrignull
Copy link
Copy Markdown
Contributor

Expands the assert-pure.ql query in a couple of ways:

  • Covers stuff in the src/common directory as well as src/pure. Stuff in this directory should also be independent of vscode, unless it is in the src/common/vscode directory.
  • Convert the query to a path-problem query. My aim here is to make this query easier to use, by explaining where an import comes from. It can otherwise be a bit hard to understand when the import chain gets more than a couple of files.

The query now has 10 alerts 🎉 😱

Screenshot 2023-05-26 at 16 44 37

All of these alerts are because of logging, either from importing extLogger or OutputChannelLogger or showAndLogErrorMessage.

The reason there's 6 alerts for selection-commands.ts is because there's 6 separate vscode imports that it transitively imports, via the giant helpers.ts file. I believe there's not a good way to compress these into a single alert without losing the information of which import it's connected to.

Checklist

  • CHANGELOG.md has been updated to incorporate all user visible changes made by this pull request.
  • Issues have been created for any UI or other user-facing changes made by this pull request.
  • [Maintainers only] If this pull request makes user-facing changes that require documentation changes, open a corresponding docs pull request in the github/codeql repo and add the ready-for-doc-review label there.

@robertbrignull robertbrignull requested a review from a team May 26, 2023 15:45
@robertbrignull robertbrignull requested a review from a team as a code owner May 26, 2023 15:45
Copy link
Copy Markdown
Contributor

@aeisenberg aeisenberg left a comment

Choose a reason for hiding this comment

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

This is really neat. Thanks for implementing.

Comment thread .github/codeql/queries/assert-pure.ql
Copy link
Copy Markdown
Contributor

@adityasharad adityasharad left a comment

Choose a reason for hiding this comment

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

Neat! A couple of passing suggestions.

Comment thread .github/codeql/queries/assert-pure.ql Outdated
Comment on lines +12 to +14
class VSCodeImport extends AstNode {
VSCodeImport() { this.(Import).getImportedPath().getValue() = "vscode" }
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since you know the type you need, might as well declare it rather than casting.

Suggested change
class VSCodeImport extends AstNode {
VSCodeImport() { this.(Import).getImportedPath().getValue() = "vscode" }
}
class VSCodeImport extends Import {
VSCodeImport() { this.getImportedPath().getValue() = "vscode" }
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Changing it to Import gets some errors because Import is an abstract class.
Screenshot 2023-05-30 at 11 35 57

I assume this was the reason it was like this originally. However, I should be able to change it to ImportDeclaration because all of the imports in our codebase are import rather than require. My understanding is that means we can use ImportDeclaration and not miss anything. At least right now it doesn't change the results.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oof I didn't realise Import was abstract.
Changing it to extends ImportDeclaration is a good specific solution.
If you want something more generic, try the (relatively new) instanceof keyword: class VSCodeImport extends AstNode instanceof Import. instanceof says we are creating a subtype of Import that inherits all its methods, but does not add to its set of values as an abstract class.

Comment thread .github/codeql/queries/assert-pure.ql Outdated
m.getAnImportedModule*().getAnImport() = v
select m, "This module is not pure: it has a transitive dependency on the vscode API imported $@", v, "here"
m.getFile() instanceof PureFile and
getANonTypeOnlyImport(getANonTypeOnlyImportedModule*(m)) = v
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor: this looks correct, but it is less confusing if you can easily see the relationship between edges and the select clause. Right now I think each getANonTypeOnlyImportedModule step actually contains two edges(importing module -> import -> imported module) and the getANonTypeOnlyImport step is one edge (importing module -> import).

I think you could even write this as edges+(m, v) and it would work because of the typing of v, but verify that to be sure!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ooh, good point. Thanks for saying this. I agree it's less confusing if it's clear the relation to edges, and we can just do edges+(m, v) because of the typing of v.

@robertbrignull robertbrignull merged commit 81fb126 into main May 31, 2023
@robertbrignull robertbrignull deleted the robertbrignull/import-path-problem branch May 31, 2023 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants