Skip to content

Add isDefinition to references #9148

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

Merged
merged 5 commits into from
Jun 14, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/server/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ namespace ts.server {
fileName,
textSpan: ts.createTextSpanFromBounds(start, end),
isWriteAccess: entry.isWriteAccess,
isDefinition: false
};
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/protocol.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ declare namespace ts.server.protocol {
/**
* True if reference is a definition, false otherwise.
Copy link
Contributor

Choose a reason for hiding this comment

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

undefined otherwise?

Copy link
Contributor

Choose a reason for hiding this comment

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

sorry looking at the code below, it is always set, so why make it optional?

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought it was necessary for API additions. I'll make it required if that's not the case.

*/
isDefinition?: boolean;
isDefinition: boolean;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,7 @@ namespace ts {
textSpan: TextSpan;
fileName: string;
isWriteAccess: boolean;
isDefinition?: boolean;
isDefinition: boolean;
}

export interface DocumentHighlights {
Expand Down Expand Up @@ -5750,7 +5750,8 @@ namespace ts {
result.push({
fileName: entry.fileName,
textSpan: highlightSpan.textSpan,
isWriteAccess: highlightSpan.kind === HighlightSpanKind.writtenReference
isWriteAccess: highlightSpan.kind === HighlightSpanKind.writtenReference,
isDefinition: false
});
}
}
Expand Down Expand Up @@ -6740,7 +6741,7 @@ namespace ts {
fileName: node.getSourceFile().fileName,
textSpan: createTextSpanFromBounds(start, end),
isWriteAccess: isWriteAccess(node),
isDefinition: isDeclarationName(node)
isDefinition: isDeclarationName(node) || node.parent.kind === SyntaxKind.ComputedPropertyName
Copy link
Contributor

Choose a reason for hiding this comment

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

why not push this into isDeclarationName ?

Copy link
Member Author

Choose a reason for hiding this comment

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

it's not true all the time -- isDeclarationName additionally checks that node.parent.name === node which needs to be generally true for something to be a valid declaration. Computed property names don't meet this requirement because often we can't calculate the name at compile time. In this code path, however, we know that the computed property name already, so I added the special case here only .

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried pushing it further, but computed property's symbols started disappearing from our symbol baselines. I did discover isLiteralCompuatedPropertyDeclarationName, so I switched to that instead of the direct check of SyntaxKind.

};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path='fourslash.ts' />
////const { [|{| "isDefinition": true |}x|], y } = { x: 1, y: 2 };
////const z = [|{| "isDefinition": false |}x|];
var firstRange = test.ranges()[0];
goTo.position(firstRange.start, firstRange.fileName);
test.ranges().forEach(range => {
verify.referencesAtPositionContains(range, undefined, range.marker.data.isDefinition);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference path='fourslash.ts' />
////let o = { ["[|{| "isDefinition": true |}foo|]"]: 12 };
////let y = o.[|{| "isDefinition": false |}foo|];
////let z = o['[|{| "isDefinition": false |}foo|]'];
var firstRange = test.ranges()[0];
goTo.position(firstRange.start, firstRange.fileName);
test.ranges().forEach(range => {
verify.referencesAtPositionContains(range, undefined, range.marker.data.isDefinition);
});
11 changes: 11 additions & 0 deletions tests/cases/fourslash/getOccurrencesIsDefinitionOfExport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />
// @Filename: m.ts
////export var [|{| "isDefinition": true |}x|] = 12;
// @Filename: main.ts
////import { [|{| "isDefinition": true |}x|] } from "./m";
////const y = [|{| "isDefinition": false |}x|];
var firstRange = test.ranges()[0];
goTo.position(firstRange.start, firstRange.fileName);
test.ranges().forEach(range => {
verify.referencesAtPositionContains(range, undefined, range.marker.data.isDefinition);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path='fourslash.ts' />
////let o = { [|{| "isDefinition": true |}1|]: 12 };
////let y = o[[|{| "isDefinition": false |}1|]];
var firstRange = test.ranges()[0];
goTo.position(firstRange.start, firstRange.fileName);
test.ranges().forEach(range => {
verify.referencesAtPositionContains(range, undefined, range.marker.data.isDefinition);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path='fourslash.ts' />
////let o = { "[|{| "isDefinition": true |}x|]": 12 };
////let y = o.[|{| "isDefinition": false |}x|];
var firstRange = test.ranges()[0];
goTo.position(firstRange.start, firstRange.fileName);
test.ranges().forEach(range => {
verify.referencesAtPositionContains(range, undefined, range.marker.data.isDefinition);
});