Skip to content

Use unique declaration references #192

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

Closed
wants to merge 4 commits into from

Conversation

valerybugakov
Copy link
Member

Fixes #152

The too many references issue was caused by the fact that ts has multiple definitions in the same file, and we used this information to create a reference for each namespace ts block.

Screenshot 2022-10-24 at 18 18 52

This PR removes redundant references by filtering out duplicated SCIP symbol values.
This PR also fixes SCIP symbol generation for namespace imports. Covered by the updated snapshot test.

Test plan

See the updated snapshots/input/syntax/src/namespace.d.ts spanshot test.

@varungandhi-src
Copy link
Contributor

@valerybugakov I think you accidentally committed generated files. I've opened a PR to fix the .gitignore. #193

@valerybugakov
Copy link
Member Author

@varungandhi-src, approved your PR 👍

@olafurpg
Copy link
Member

Please wait with merging until I've reviewed.

Copy link
Member

@olafurpg olafurpg left a comment

Choose a reason for hiding this comment

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

@valerybugakov Thank you for this contribution! There are several independent changes in this PR that make it harder to review than necessary. I opened a separate PR #194 to demonstrate how the issue can be fixed with a smaller diff. In the future, I encourage you to try and open separate PRs for independent changes. It's especially important that functional changes like the new ts.isNamespaceImport(node) condition are documented with a minimized diff in the git history.

@@ -11,8 +11,8 @@
"tslint": "tslint -p tsconfig.json --format stylish",
"eslint": "eslint --cache '**/*.ts?(x)'",
"build": "node ./node_modules/typescript/bin/tsc -b .",
"test": "uvu -r ts-node/register --ignore dist",
"update-snapshots": "uvu -r ts-node/register --ignore dist --update-snapshots",
"test": "TS_NODE_TRANSPILE_ONLY=true uvu -r ts-node/register --ignore dist",
Copy link
Member

Choose a reason for hiding this comment

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

I added new test-fast and update-snapshots-fast scripts. I prefer to get the type errors in the terminal because typechecking is reasonably fast in this codebase and debugging runtime exceptions is slower than debugging type errors.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good! To clarify the reasoning behind this: while iterating on changes locally, I often have type errors because I want to test some logic but don't want to invest time into making Typescript happy about it because there's no intent to commit it. Type-checking slows down the process of trying different ideas on existing tests.

Copy link
Member

Choose a reason for hiding this comment

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

I'm +1 to adding these scripts to package.json as long as we keep the typechecking variants as well. For sg/sg, I appreciate that sg start doesn't typecheck because it's a big codebase slowing down the edit/reload/debug feedback loop. In scip-typescript, typechecking has been fast enough for my needs.

if (
ts.isImportSpecifier(node) ||
ts.isImportClause(node) ||
ts.isNamespaceImport(node)
Copy link
Member

Choose a reason for hiding this comment

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

Is this change related to de-duplication? It would be nice to isolate this change and have a diff in the snapshot tests to better understand the exact motivation for this change.

Copy link
Member Author

Choose a reason for hiding this comment

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

return []
}

return Array.from(
Copy link
Member

Choose a reason for hiding this comment

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

This expression is fairly expensive considering it's in a very hot loop and that we can achieve a similar results with a single if condition, as is done in
#194

It's very important that we try to keep performance parity with tsc -b and the fewer redundant collections we allocate the better.

const isDefinition = this.declarationName(node.parent) === node
if (isDefinition) {
role |= scip.scip.SymbolRole.Definition
private visitDefinition(node: ts.Node, sym: ts.Symbol): void {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not convinced this refactoring is an improvement in maintainability. In the future, I want to add more symbol roles such as import and write and then the symbol_roles bitmask will need to be updated in two different places.

@@ -65,7 +65,21 @@ export class FileIndexer {
if (ts.isIdentifier(node) || ts.isStringLiteralLike(node)) {
const sym = this.getTSSymbolAtLocation(node)
if (sym) {
this.visitSymbolOccurrence(node, sym)
/**
Copy link
Member

Choose a reason for hiding this comment

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

I agree it's not super clear why this.declarationName(node.parent) === node identifies a definition. To improve readability, I'd prefer to add a function private isDefinition(...): boolean with a docstring than adding an inline comment like this.

Copy link
Member Author

Choose a reason for hiding this comment

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

@olafurpg olafurpg closed this Oct 27, 2022
@valerybugakov valerybugakov deleted the vb/unique-definition-references branch October 28, 2022 02:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Snapshot output seems noisy; are we emitting needlessly redundant references?
3 participants