Skip to content

Commit

Permalink
Add deprecated related feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingwl committed May 13, 2020
1 parent 5ef2228 commit 9185734
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 50 deletions.
4 changes: 4 additions & 0 deletions src/compiler/factoryPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2595,6 +2595,10 @@ namespace ts {
return createJSDocTag(SyntaxKind.JSDocAuthorTag, "author", comment);
}

export function createJSDocDeprecatedTag(comment?: string) {
return createJSDocTag(SyntaxKind.JSDocDeprecatedTag, "deprecated", comment);
}

export function createJSDocPublicTag() {
return createJSDocTag(SyntaxKind.JSDocPublicTag, "public");
}
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7065,6 +7065,9 @@ namespace ts {
case "readonly":
tag = parseSimpleTag(start, SyntaxKind.JSDocReadonlyTag, tagName);
break;
case "deprecated":
tag = parseSimpleTag(start, SyntaxKind.JSDocDeprecatedTag, tagName);
break;
case "this":
tag = parseThisTag(start, tagName);
break;
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ namespace ts {
JSDocAugmentsTag,
JSDocImplementsTag,
JSDocAuthorTag,
JSDocDeprecatedTag,
JSDocClassTag,
JSDocPublicTag,
JSDocPrivateTag,
Expand Down Expand Up @@ -2676,6 +2677,10 @@ namespace ts {
kind: SyntaxKind.JSDocAuthorTag;
}

export interface JSDocDeprecatedTag extends JSDocTag {
kind: SyntaxKind.JSDocDeprecatedTag;
}

export interface JSDocClassTag extends JSDocTag {
kind: SyntaxKind.JSDocClassTag;
}
Expand Down
9 changes: 9 additions & 0 deletions src/compiler/utilitiesPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,11 @@ namespace ts {
return getFirstJSDocTag(node, isJSDocTemplateTag);
}

/** Gets the JSDoc deprecated tag for the node if present */
export function getJSDocDeprecatedTag(node: Node): JSDocDeprecatedTag | undefined {
return getFirstJSDocTag(node, isJSDocDeprecatedTag);
}

/** Gets the JSDoc type tag for the node if present and valid */
export function getJSDocTypeTag(node: Node): JSDocTypeTag | undefined {
// We should have already issued an error if there were multiple type jsdocs, so just use the first one.
Expand Down Expand Up @@ -1692,6 +1697,10 @@ namespace ts {
return node.kind === SyntaxKind.JSDocTemplateTag;
}

export function isJSDocDeprecatedTag(node: Node): node is JSDocDeprecatedTag {
return node.kind === SyntaxKind.JSDocDeprecatedTag;
}

export function isJSDocTypedefTag(node: Node): node is JSDocTypedefTag {
return node.kind === SyntaxKind.JSDocTypedefTag;
}
Expand Down
1 change: 1 addition & 0 deletions src/harness/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ namespace ts.server {
isCaseSensitive: entry.isCaseSensitive,
fileName: entry.file,
textSpan: this.decodeSpan(entry),
isDeprecated: !!entry.tags?.includes(protocol.SymbolTag.Deprecated)
}));
}

Expand Down
1 change: 1 addition & 0 deletions src/harness/fourslashImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2982,6 +2982,7 @@ namespace FourSlash {
textSpan: ts.createTextSpanFromRange(e.range),
containerName: e.containerName || "",
containerKind: e.containerKind || ts.ScriptElementKind.unknown,
isDeprecated: !!e.isDeprecated
})));
}
}
Expand Down
1 change: 1 addition & 0 deletions src/harness/fourslashInterfaceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,7 @@ namespace FourSlashInterface {
readonly range: FourSlash.Range;
readonly containerName?: string;
readonly containerKind?: ts.ScriptElementKind;
readonly isDeprecated?: boolean
}

export type ArrayOrSingle<T> = T | readonly T[];
Expand Down
9 changes: 9 additions & 0 deletions src/server/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2818,6 +2818,10 @@ namespace ts.server.protocol {
arguments: NavtoRequestArgs;
}

export enum SymbolTag {
Deprecated = 1
}

/**
* An item found in a navto response.
*/
Expand Down Expand Up @@ -2857,6 +2861,11 @@ namespace ts.server.protocol {
* Kind of symbol's container symbol (if any).
*/
containerKind?: ScriptElementKind;

/**
* The symbol's tag.
*/
tags?: SymbolTag[]
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1886,7 +1886,8 @@ namespace ts.server {
matchKind: navItem.matchKind,
file: navItem.fileName,
start: scriptInfo.positionToLineOffset(navItem.textSpan.start),
end: scriptInfo.positionToLineOffset(textSpanEnd(navItem.textSpan))
end: scriptInfo.positionToLineOffset(textSpanEnd(navItem.textSpan)),
tags: navItem.isDeprecated ? [protocol.SymbolTag.Deprecated] : undefined
};
if (navItem.kindModifiers && (navItem.kindModifiers !== "")) {
bakedItem.kindModifiers = navItem.kindModifiers;
Expand Down
1 change: 1 addition & 0 deletions src/services/navigateTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ namespace ts.NavigateTo {
// TODO(jfreeman): What should be the containerName when the container has a computed name?
containerName: containerName ? (<Identifier>containerName).text : "",
containerKind: containerName ? getNodeKind(container!) : ScriptElementKind.unknown, // TODO: GH#18217 Just use `container ? ...`
isDeprecated: !!getJSDocDeprecatedTag(rawItem.declaration)
};
}
}
1 change: 1 addition & 0 deletions src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ namespace ts {
textSpan: TextSpan;
containerName: string;
containerKind: ScriptElementKind;
isDeprecated: boolean
}

export enum IndentStyle {
Expand Down
7 changes: 7 additions & 0 deletions src/testRunner/unittests/tsserver/declarationFileMaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ namespace ts.projectSystem {
isCaseSensitive: true,
kind: ScriptElementKind.functionElement,
kindModifiers: "export,declare",
tags: undefined
},
{
...protocolFileSpanFromSubstring({
Expand All @@ -307,6 +308,7 @@ namespace ts.projectSystem {
isCaseSensitive: true,
kind: ScriptElementKind.functionElement,
kindModifiers: "export",
tags: undefined
},
{
...protocolFileSpanFromSubstring({
Expand All @@ -318,6 +320,7 @@ namespace ts.projectSystem {
isCaseSensitive: true,
kind: ScriptElementKind.functionElement,
kindModifiers: "export",
tags: undefined
},
]);

Expand All @@ -338,6 +341,7 @@ namespace ts.projectSystem {
isCaseSensitive: true,
kind: ScriptElementKind.functionElement,
kindModifiers: "export",
tags: undefined
},
{
...protocolFileSpanFromSubstring({
Expand All @@ -349,6 +353,7 @@ namespace ts.projectSystem {
isCaseSensitive: true,
kind: ScriptElementKind.functionElement,
kindModifiers: "export",
tags: undefined
},
{
...protocolFileSpanFromSubstring({
Expand All @@ -360,6 +365,7 @@ namespace ts.projectSystem {
isCaseSensitive: true,
kind: ScriptElementKind.functionElement,
kindModifiers: "export",
tags: undefined
}
]);
});
Expand All @@ -378,6 +384,7 @@ namespace ts.projectSystem {
isCaseSensitive: true,
kind: ScriptElementKind.functionElement,
kindModifiers: "export",
tags: undefined
}
]);
});
Expand Down
27 changes: 26 additions & 1 deletion src/testRunner/unittests/tsserver/navTo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
namespace ts.projectSystem {
describe("unittests:: tsserver:: navigate-to for javascript project", () => {
function findNavToItem(items: protocol.NavtoItem[], itemName: string, itemKind: string) {
return find(items, item => item.name === itemName && item.kind === itemKind);
}

function containsNavToItem(items: protocol.NavtoItem[], itemName: string, itemKind: string) {
return find(items, item => item.name === itemName && item.kind === itemKind) !== undefined;
return findNavToItem(items, itemName, itemKind) !== undefined;
}

it("should not include type symbols", () => {
Expand Down Expand Up @@ -67,5 +71,26 @@ export const ghijkl = a.abcdef;`
assert.strictEqual(item.name, "abcdef");
assert.strictEqual(item.file, file1.path);
});

it("should work with Deprecated", () => {
const file1: File = {
path: "/a/b/file1.js",
content: "/** @deprecated */\nfunction foo () {}"
};
const configFile: File = {
path: "/a/b/jsconfig.json",
content: "{}"
};
const host = createServerHost([file1, configFile, libFile]);
const session = createSession(host);
openFilesForSession([file1], session);

// Try to find some interface type defined in lib.d.ts
const libTypeNavToRequest = makeSessionRequest<protocol.NavtoRequestArgs>(CommandNames.Navto, { searchValue: "foo", file: file1.path, projectFileName: configFile.path });
const items = session.executeCommand(libTypeNavToRequest).response as protocol.NavtoItem[];
const fooItem = findNavToItem(items, "foo", "function");
assert.isNotNull(fooItem, `Cannot find function symbol "foo".`);
assert.isTrue(fooItem!.tags?.some(x => x === protocol.SymbolTag.Deprecated), "Cannot find deprecated tag");
});
});
}
64 changes: 40 additions & 24 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,28 +385,29 @@ declare namespace ts {
JSDocAugmentsTag = 307,
JSDocImplementsTag = 308,
JSDocAuthorTag = 309,
JSDocClassTag = 310,
JSDocPublicTag = 311,
JSDocPrivateTag = 312,
JSDocProtectedTag = 313,
JSDocReadonlyTag = 314,
JSDocCallbackTag = 315,
JSDocEnumTag = 316,
JSDocParameterTag = 317,
JSDocReturnTag = 318,
JSDocThisTag = 319,
JSDocTypeTag = 320,
JSDocTemplateTag = 321,
JSDocTypedefTag = 322,
JSDocPropertyTag = 323,
SyntaxList = 324,
NotEmittedStatement = 325,
PartiallyEmittedExpression = 326,
CommaListExpression = 327,
MergeDeclarationMarker = 328,
EndOfDeclarationMarker = 329,
SyntheticReferenceExpression = 330,
Count = 331,
JSDocDeprecatedTag = 310,
JSDocClassTag = 311,
JSDocPublicTag = 312,
JSDocPrivateTag = 313,
JSDocProtectedTag = 314,
JSDocReadonlyTag = 315,
JSDocCallbackTag = 316,
JSDocEnumTag = 317,
JSDocParameterTag = 318,
JSDocReturnTag = 319,
JSDocThisTag = 320,
JSDocTypeTag = 321,
JSDocTemplateTag = 322,
JSDocTypedefTag = 323,
JSDocPropertyTag = 324,
SyntaxList = 325,
NotEmittedStatement = 326,
PartiallyEmittedExpression = 327,
CommaListExpression = 328,
MergeDeclarationMarker = 329,
EndOfDeclarationMarker = 330,
SyntheticReferenceExpression = 331,
Count = 332,
FirstAssignment = 62,
LastAssignment = 74,
FirstCompoundAssignment = 63,
Expand Down Expand Up @@ -435,9 +436,9 @@ declare namespace ts {
LastStatement = 241,
FirstNode = 153,
FirstJSDocNode = 294,
LastJSDocNode = 323,
LastJSDocNode = 324,
FirstJSDocTagNode = 306,
LastJSDocTagNode = 323,
LastJSDocTagNode = 324,
}
export enum NodeFlags {
None = 0,
Expand Down Expand Up @@ -1649,6 +1650,9 @@ declare namespace ts {
export interface JSDocAuthorTag extends JSDocTag {
kind: SyntaxKind.JSDocAuthorTag;
}
export interface JSDocDeprecatedTag extends JSDocTag {
kind: SyntaxKind.JSDocDeprecatedTag;
}
export interface JSDocClassTag extends JSDocTag {
kind: SyntaxKind.JSDocClassTag;
}
Expand Down Expand Up @@ -3546,6 +3550,8 @@ declare namespace ts {
function getJSDocReturnTag(node: Node): JSDocReturnTag | undefined;
/** Gets the JSDoc template tag for the node if present */
function getJSDocTemplateTag(node: Node): JSDocTemplateTag | undefined;
/** Gets the JSDoc deprecated tag for the node if present */
function getJSDocDeprecatedTag(node: Node): JSDocDeprecatedTag | undefined;
/** Gets the JSDoc type tag for the node if present and valid */
function getJSDocTypeTag(node: Node): JSDocTypeTag | undefined;
/**
Expand Down Expand Up @@ -3760,6 +3766,7 @@ declare namespace ts {
function isJSDocReturnTag(node: Node): node is JSDocReturnTag;
function isJSDocTypeTag(node: Node): node is JSDocTypeTag;
function isJSDocTemplateTag(node: Node): node is JSDocTemplateTag;
function isJSDocDeprecatedTag(node: Node): node is JSDocDeprecatedTag;
function isJSDocTypedefTag(node: Node): node is JSDocTypedefTag;
function isJSDocPropertyTag(node: Node): node is JSDocPropertyTag;
function isJSDocPropertyLikeTag(node: Node): node is JSDocPropertyLikeTag;
Expand Down Expand Up @@ -4270,6 +4277,7 @@ declare namespace ts {
function createJSDocTypeLiteral(jsDocPropertyTags?: readonly JSDocPropertyLikeTag[], isArrayType?: boolean): JSDocTypeLiteral;
function createJSDocImplementsTag(classExpression: JSDocImplementsTag["class"], comment?: string): JSDocImplementsTag;
function createJSDocAuthorTag(comment?: string): JSDocTag;
function createJSDocDeprecatedTag(comment?: string): JSDocTag;
function createJSDocPublicTag(): JSDocTag;
function createJSDocPrivateTag(): JSDocTag;
function createJSDocProtectedTag(): JSDocTag;
Expand Down Expand Up @@ -5585,6 +5593,7 @@ declare namespace ts {
textSpan: TextSpan;
containerName: string;
containerKind: ScriptElementKind;
isDeprecated: boolean;
}
enum IndentStyle {
None = 0,
Expand Down Expand Up @@ -8380,6 +8389,9 @@ declare namespace ts.server.protocol {
command: CommandTypes.Navto;
arguments: NavtoRequestArgs;
}
enum SymbolTag {
Deprecated = 1
}
/**
* An item found in a navto response.
*/
Expand Down Expand Up @@ -8413,6 +8425,10 @@ declare namespace ts.server.protocol {
* Kind of symbol's container symbol (if any).
*/
containerKind?: ScriptElementKind;
/**
* The symbol's tag.
*/
tags?: SymbolTag[];
}
/**
* Navto response message. Body is an array of navto items. Each
Expand Down
Loading

0 comments on commit 9185734

Please sign in to comment.