-
Notifications
You must be signed in to change notification settings - Fork 229
[js-api-parser] add interface generator #13453
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
609ed3c
add interface generator
KarishmaGhiya d4fe796
fix types
KarishmaGhiya c9d5101
add tests
KarishmaGhiya 66d45b4
add tests
KarishmaGhiya 1064558
Update tools/apiview/parsers/js-api-parser/src/tokenGenerators/interf…
KarishmaGhiya 17032c9
Update tools/apiview/parsers/js-api-parser/src/tokenGenerators/interf…
KarishmaGhiya 328255d
update tokenGenerators
KarishmaGhiya fe41b30
Merge branch 'interfaces' of https://github.com/KarishmaGhiya/azure-s…
KarishmaGhiya 7b3ebd5
token generator for default types fpr type parameter fixed
KarishmaGhiya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
tools/apiview/parsers/js-api-parser/src/tokenGenerators/helpers.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { ExcerptToken, ExcerptTokenKind } from "@microsoft/api-extractor-model"; | ||
| import { ReviewToken, TokenKind } from "../models"; | ||
|
|
||
| /** Helper to create a token with common properties */ | ||
| export function createToken( | ||
| kind: TokenKind, | ||
| value: string, | ||
| options?: { | ||
| hasSuffixSpace?: boolean; | ||
| hasPrefixSpace?: boolean; | ||
| navigateToId?: string; | ||
| deprecated?: boolean; | ||
| }, | ||
| ): ReviewToken { | ||
| return { | ||
| Kind: kind, | ||
| Value: value, | ||
| HasSuffixSpace: options?.hasSuffixSpace ?? false, | ||
| HasPrefixSpace: options?.hasPrefixSpace ?? false, | ||
| NavigateToId: options?.navigateToId, | ||
| IsDeprecated: options?.deprecated, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Determines if a token needs a leading space based on its value | ||
| * @param value The token value | ||
| * @returns true if the token needs a leading space | ||
| */ | ||
| export function needsLeadingSpace(value: string): boolean { | ||
| return value === "|" || value === "&" || value === "is" || value === "extends"; | ||
| } | ||
|
|
||
| /** | ||
| * Determines if a token needs a trailing space based on its value | ||
| * @param value The token value | ||
| * @returns true if the token needs a trailing space | ||
| */ | ||
| export function needsTrailingSpace(value: string): boolean { | ||
| return value === "|" || value === "&" || value === "is" || value === "extends"; | ||
| } | ||
|
|
||
| /** Process excerpt tokens and add them to the tokens array */ | ||
| export function processExcerptTokens( | ||
| excerptTokens: readonly ExcerptToken[], | ||
| tokens: ReviewToken[], | ||
| deprecated?: boolean, | ||
| ): void { | ||
| for (const excerpt of excerptTokens) { | ||
| const trimmedText = excerpt.text.trim(); | ||
| if (!trimmedText) continue; | ||
|
|
||
| const hasPrefixSpace = needsLeadingSpace(trimmedText); | ||
| const hasSuffixSpace = needsTrailingSpace(trimmedText); | ||
|
|
||
| if (excerpt.kind === ExcerptTokenKind.Reference && excerpt.canonicalReference) { | ||
| tokens.push( | ||
| createToken(TokenKind.TypeName, trimmedText, { | ||
| navigateToId: excerpt.canonicalReference.toString(), | ||
| hasPrefixSpace, | ||
| hasSuffixSpace, | ||
| deprecated, | ||
| }), | ||
| ); | ||
| } else { | ||
| tokens.push( | ||
| createToken(TokenKind.Text, trimmedText, { | ||
| hasPrefixSpace, | ||
| hasSuffixSpace, | ||
| deprecated, | ||
| }), | ||
| ); | ||
| } | ||
| } | ||
| } |
38 changes: 20 additions & 18 deletions
38
tools/apiview/parsers/js-api-parser/src/tokenGenerators/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,31 @@ | ||
| import { enumTokenGenerator } from "./enum"; | ||
| import { ReviewToken } from '../models'; | ||
| import { ApiItem } from '@microsoft/api-extractor-model'; | ||
| import { ReviewToken } from "../models"; | ||
| import { ApiItem } from "@microsoft/api-extractor-model"; | ||
| import { functionTokenGenerator } from "./function"; | ||
| import { interfaceTokenGenerator } from "./interfaces"; | ||
|
|
||
| /** | ||
| * Interface for token generators that create ReviewTokens from ApiItems. | ||
| */ | ||
| export interface TokenGenerator<T extends ApiItem = ApiItem> { | ||
| /** | ||
| * Validates if the given ApiItem can be processed by this token generator. | ||
| * @param item - The ApiItem to validate. | ||
| * @returns True if the item is valid; otherwise, false. | ||
| */ | ||
| isValid(item: ApiItem): item is T; | ||
| /** | ||
| * Validates if the given ApiItem can be processed by this token generator. | ||
| * @param item - The ApiItem to validate. | ||
| * @returns True if the item is valid; otherwise, false. | ||
| */ | ||
| isValid(item: ApiItem): item is T; | ||
|
|
||
| /** | ||
| * Generates ReviewTokens from the given ApiItem. | ||
| * @param item - The ApiItem to process. | ||
| * @param deprecated - Indicates if the Api is deprecated. | ||
| * @returns An array of ReviewTokens generated from the ApiItem. | ||
| */ | ||
| generate(item: T, deprecated?: boolean): ReviewToken[]; | ||
| /** | ||
| * Generates ReviewTokens from the given ApiItem. | ||
| * @param item - The ApiItem to process. | ||
| * @param deprecated - Indicates if the Api is deprecated. | ||
| * @returns An array of ReviewTokens generated from the ApiItem. | ||
| */ | ||
| generate(item: T, deprecated?: boolean): ReviewToken[]; | ||
| } | ||
|
|
||
| export const generators: TokenGenerator[] = [ | ||
| enumTokenGenerator, | ||
| functionTokenGenerator | ||
| ]; | ||
| enumTokenGenerator, | ||
| functionTokenGenerator, | ||
| interfaceTokenGenerator, | ||
| ]; |
99 changes: 99 additions & 0 deletions
99
tools/apiview/parsers/js-api-parser/src/tokenGenerators/interfaces.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import { ApiInterface, ApiItem, ApiItemKind } from "@microsoft/api-extractor-model"; | ||
| import { ReviewToken, TokenKind } from "../models"; | ||
| import { TokenGenerator } from "./index"; | ||
| import { createToken, processExcerptTokens } from "./helpers"; | ||
|
|
||
| function isValid(item: ApiItem): item is ApiInterface { | ||
| return item.kind === ApiItemKind.Interface; | ||
| } | ||
|
|
||
| function generate(item: ApiInterface, deprecated?: boolean): ReviewToken[] { | ||
| const tokens: ReviewToken[] = []; | ||
| if (item.kind !== ApiItemKind.Interface) { | ||
| throw new Error( | ||
| `Invalid item ${item.displayName} of kind ${item.kind} for Interface token generator.`, | ||
| ); | ||
| } | ||
|
|
||
| // Extract structured properties | ||
| const typeParameters = item.typeParameters; | ||
|
|
||
| // Add export and interface keywords | ||
| tokens.push(createToken(TokenKind.Keyword, "export", { hasSuffixSpace: true, deprecated })); | ||
|
|
||
| // Check for default export | ||
| const isDefaultExport = item.excerptTokens.some((t) => t.text.includes("export default")); | ||
| if (isDefaultExport) { | ||
| tokens.push(createToken(TokenKind.Keyword, "default", { hasSuffixSpace: true, deprecated })); | ||
| } | ||
|
|
||
| tokens.push(createToken(TokenKind.Keyword, "interface", { hasSuffixSpace: true, deprecated })); | ||
|
|
||
| // Create interface name token with proper metadata (matching splitAndBuild behavior) | ||
| const nameToken = createToken(TokenKind.TypeName, item.displayName, { deprecated }); | ||
| nameToken.NavigateToId = item.canonicalReference.toString(); | ||
| nameToken.NavigationDisplayName = item.displayName; | ||
| nameToken.RenderClasses = ["interface"]; | ||
| tokens.push(nameToken); | ||
|
|
||
| // Add type parameters | ||
| if (typeParameters?.length > 0) { | ||
| tokens.push(createToken(TokenKind.Text, "<", { deprecated })); | ||
| typeParameters.forEach((tp, index) => { | ||
| tokens.push(createToken(TokenKind.TypeName, tp.name, { deprecated })); | ||
|
|
||
| if (tp.constraintExcerpt?.text.trim()) { | ||
| tokens.push( | ||
| createToken(TokenKind.Keyword, "extends", { | ||
| hasPrefixSpace: true, | ||
| hasSuffixSpace: true, | ||
| deprecated, | ||
| }), | ||
| ); | ||
| processExcerptTokens(tp.constraintExcerpt.spannedTokens, tokens, deprecated); | ||
| } | ||
|
|
||
| if (tp.defaultTypeExcerpt?.text.trim()) { | ||
| tokens.push( | ||
| createToken(TokenKind.Text, "=", { | ||
| hasPrefixSpace: true, | ||
| hasSuffixSpace: true, | ||
| deprecated, | ||
| }), | ||
| ); | ||
| processExcerptTokens(tp.defaultTypeExcerpt.spannedTokens, tokens, deprecated); | ||
| } | ||
|
|
||
| if (index < typeParameters.length - 1) { | ||
KarishmaGhiya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| tokens.push(createToken(TokenKind.Text, ",", { hasSuffixSpace: true, deprecated })); | ||
| } | ||
| }); | ||
| tokens.push(createToken(TokenKind.Text, ">", { deprecated })); | ||
| } | ||
|
|
||
| // Add extends clause if interface extends other interfaces | ||
| if (item.extendsTypes && item.extendsTypes.length > 0) { | ||
| tokens.push( | ||
| createToken(TokenKind.Keyword, "extends", { | ||
| hasPrefixSpace: true, | ||
| hasSuffixSpace: true, | ||
| deprecated, | ||
| }), | ||
| ); | ||
|
|
||
| item.extendsTypes.forEach((extendsType, index) => { | ||
| processExcerptTokens(extendsType.excerpt.spannedTokens, tokens, deprecated); | ||
|
|
||
| if (index < item.extendsTypes.length - 1) { | ||
| tokens.push(createToken(TokenKind.Text, ",", { hasSuffixSpace: true, deprecated })); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| return tokens; | ||
| } | ||
|
|
||
| export const interfaceTokenGenerator: TokenGenerator<ApiInterface> = { | ||
| isValid, | ||
| generate, | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.