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

[TypeScript Parser] support release tags #6047

Merged
merged 3 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 25 additions & 2 deletions tools/apiview/parsers/js-api-parser/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {
ApiItem,
ApiItemKind,
ApiDeclaredItem,
ExcerptTokenKind
ExcerptTokenKind,
ApiPropertyItem,
ReleaseTag
} from '@microsoft/api-extractor-model';

import { writeFile } from 'fs';
Expand All @@ -15,6 +17,15 @@ function appendMembers(builder: TokensBuilder, navigation: IApiViewNavItem[], it
{
builder.lineId(item.canonicalReference.toString());
builder.indent();
const releaseTag = getReleaseTag(item);
const parentReleaseTag = getReleaseTag(item.parent);
if(releaseTag && releaseTag !== parentReleaseTag) {
if(item.parent.kind === ApiItemKind.EntryPoint) {
builder.newline();
}
builder.annotate(releaseTag);
}

if (item instanceof ApiDeclaredItem) {
if ( item.kind === ApiItemKind.Namespace) {
builder.splitAppend(`declare namespace ${item.displayName} `, item.canonicalReference.toString(), item.displayName);
Expand Down Expand Up @@ -63,6 +74,7 @@ function appendMembers(builder: TokensBuilder, navigation: IApiViewNavItem[], it
item.kind === ApiItemKind.Class ||
item.kind === ApiItemKind.Namespace)
{

Copy link
Member

Choose a reason for hiding this comment

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

nit: empty line

if (item.members.length > 0)
{
builder
Expand Down Expand Up @@ -95,6 +107,17 @@ function appendMembers(builder: TokensBuilder, navigation: IApiViewNavItem[], it
}
}

function getReleaseTag(item: ApiItem & {releaseTag?: ReleaseTag}): "alpha" | "beta" | undefined {
switch(item.releaseTag) {
case ReleaseTag.Beta:
return "beta";
case ReleaseTag.Alpha:
return "alpha";
default:
return undefined;
}
}

const apiModel = new ApiModel();
const fileName = process.argv[2];
var versionString = "";
Expand Down Expand Up @@ -125,7 +148,7 @@ var apiViewFile: IApiViewFile = {
Navigation: navigation,
Tokens: builder.tokens,
PackageName: apiModel.packages[0].name,
VersionString: "1.0.3",
VersionString: "1.0.4",
Language: "JavaScript"
}

Expand Down
121 changes: 55 additions & 66 deletions tools/apiview/parsers/js-api-parser/tokensBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

import {ApiViewTokenKind, IApiViewToken} from './models';
import { ApiViewTokenKind, IApiViewToken } from "./models";

const jsTokens = require("js-tokens");

export class TokensBuilder
{
const ANNOTATION_TOKEN = "@";

export class TokensBuilder {
tokens: IApiViewToken[] = [];
indentString: string = "";
keywords: string[] = [
Expand Down Expand Up @@ -70,140 +70,129 @@ export class TokensBuilder
"from",
"of",
"keyof",
"readonly"];
"readonly",
];

indent(): TokensBuilder
{
annotate(value: string): TokensBuilder {
this.tokens.push({
Kind: ApiViewTokenKind.StringLiteral,
Value: `${ANNOTATION_TOKEN}${value}`,
});

this.newline().indent()
return this;
}

indent(): TokensBuilder {
this.tokens.push({
Kind: ApiViewTokenKind.Whitespace,
Value: this.indentString
Value: this.indentString,
});
return this;
}

incIndent(): TokensBuilder
{
this.indentString = ' '.repeat(this.indentString.length + 4);
incIndent(): TokensBuilder {
this.indentString = " ".repeat(this.indentString.length + 4);
return this;
}

decIndent(): TokensBuilder
{
this.indentString = ' '.repeat(this.indentString.length - 4);
decIndent(): TokensBuilder {
this.indentString = " ".repeat(this.indentString.length - 4);
return this;
}

newline(): TokensBuilder
{
newline(): TokensBuilder {
this.tokens.push({
Kind: ApiViewTokenKind.Newline
Kind: ApiViewTokenKind.Newline,
});
return this;
}

lineId(id: string): TokensBuilder
{
lineId(id: string): TokensBuilder {
this.tokens.push({
Kind: ApiViewTokenKind.LineIdMarker,
DefinitionId: id
DefinitionId: id,
});
return this;
}

typeReference(id: string, name: string): TokensBuilder
{
typeReference(id: string, name: string): TokensBuilder {
this.tokens.push({
Kind: ApiViewTokenKind.TypeName,
NavigateToId: id,
Value: name
Value: name,
});
return this;
}

space(s: string = " "): TokensBuilder
{
space(s: string = " "): TokensBuilder {
this.tokens.push({
Kind: ApiViewTokenKind.Whitespace,
Value: s
Value: s,
});
return this;
}

punct(s: string): TokensBuilder
{
punct(s: string): TokensBuilder {
this.tokens.push({
Kind: ApiViewTokenKind.Punctuation,
Value: s
Value: s,
});
return this;
}

string(s: string): TokensBuilder
{
string(s: string): TokensBuilder {
this.tokens.push({
Kind: ApiViewTokenKind.StringLiteral,
Value: s
Value: s,
});
return this;
}

text(s: string): TokensBuilder
{
text(s: string): TokensBuilder {
this.tokens.push({
Kind: ApiViewTokenKind.Text,
Value: s
Value: s,
});
return this;
}

keyword(s: string): TokensBuilder
{
keyword(s: string): TokensBuilder {
this.tokens.push({
Kind: ApiViewTokenKind.Keyword,
Value: s
Value: s,
});
return this;
}

splitAppend(s: string, currentTypeId: string, currentTypeName: string)
{
s.split("\n").forEach((line, index, array) => {
if (index > 0)
{
splitAppend(s: string, currentTypeId: string, currentTypeName: string) {
s.split("\n").forEach((line, index, array) => {
if (index > 0) {
this.indent();
}

var tokens: any[] = Array.from(jsTokens(line));
tokens.forEach(token =>
{
if (this.keywords.indexOf(token.value) > 0)
{
tokens.forEach((token) => {
if (this.keywords.indexOf(token.value) > 0) {
this.keyword(token.value);
}
else if (token.value === currentTypeName)
{
this.tokens.push({ Kind: ApiViewTokenKind.TypeName, DefinitionId: currentTypeId, Value: token.value });
}
else if (token.type === "StringLiteral")
{
} else if (token.value === currentTypeName) {
this.tokens.push({
Kind: ApiViewTokenKind.TypeName,
DefinitionId: currentTypeId,
Value: token.value,
});
} else if (token.type === "StringLiteral") {
this.string(token.value);
}
else if (token.type === "Punctuator")
{
} else if (token.type === "Punctuator") {
this.punct(token.value);
}
else if (token.type === "WhiteSpace")
{
} else if (token.type === "WhiteSpace") {
this.space(token.value);
}
else
{
} else {
this.text(token.value);
}
});

if (index < array.length - 1)
{

if (index < array.length - 1) {
this.newline();
}
});
Expand Down