Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zelliott committed Jun 25, 2022
1 parent 67963a1 commit 33ad56e
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 23 deletions.
6 changes: 4 additions & 2 deletions .vscode/launch.json
Expand Up @@ -5,10 +5,12 @@
"name": "Rush Debug",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/apps/rush/lib/start-dev.js",
"program": "${workspaceRoot}/libraries/rush-lib/lib/start.js",
"stopOnEntry": true,
"args": [
"start"
"rebuild",
"-o",
"api-extractor-scenarios"
],
"cwd": "${workspaceRoot}",
"runtimeExecutable": null,
Expand Down
28 changes: 20 additions & 8 deletions apps/api-extractor/src/generators/ApiModelGenerator.ts
Expand Up @@ -829,25 +829,29 @@ export class ApiModelGenerator {
let apiProperty: ApiProperty | undefined = parentApiItem.tryGetMemberByKey(containerKey) as ApiProperty;

if (apiProperty === undefined) {
const declaration: ts.Declaration = astDeclaration.declaration;
const nodesToCapture: IExcerptBuilderNodeToCapture[] = [];

const propertyTypeTokenRange: IExcerptTokenRange = ExcerptBuilder.createEmptyTokenRange();
let propertyTypeNode: ts.TypeNode | undefined;

if (
ts.isPropertyDeclaration(astDeclaration.declaration) ||
ts.isGetAccessorDeclaration(astDeclaration.declaration)
) {
propertyTypeNode = astDeclaration.declaration.type;
if (ts.isPropertyDeclaration(declaration) || ts.isGetAccessorDeclaration(declaration)) {
propertyTypeNode = declaration.type;
}

if (ts.isSetAccessorDeclaration(astDeclaration.declaration)) {
if (ts.isSetAccessorDeclaration(declaration)) {
// Note that TypeScript always reports an error if a setter does not have exactly one parameter.
propertyTypeNode = astDeclaration.declaration.parameters[0].type;
propertyTypeNode = declaration.parameters[0].type;
}

nodesToCapture.push({ node: propertyTypeNode, tokenRange: propertyTypeTokenRange });

let initializerTokenRange: IExcerptTokenRange | undefined = undefined;
if (ts.isPropertyDeclaration(declaration) && declaration.initializer) {
initializerTokenRange = ExcerptBuilder.createEmptyTokenRange();
nodesToCapture.push({ node: declaration.initializer, tokenRange: initializerTokenRange });
}

const excerptTokens: IExcerptToken[] = this._buildExcerptTokens(astDeclaration, nodesToCapture);
const apiItemMetadata: ApiItemMetadata = this._collector.fetchApiItemMetadata(astDeclaration);
const docComment: tsdoc.DocComment | undefined = apiItemMetadata.tsdocComment;
Expand All @@ -866,7 +870,8 @@ export class ApiModelGenerator {
isOptional,
isReadonly,
excerptTokens,
propertyTypeTokenRange
propertyTypeTokenRange,
initializerTokenRange
});
parentApiItem.addMember(apiProperty);
} else {
Expand Down Expand Up @@ -985,6 +990,12 @@ export class ApiModelGenerator {
const variableTypeTokenRange: IExcerptTokenRange = ExcerptBuilder.createEmptyTokenRange();
nodesToCapture.push({ node: variableDeclaration.type, tokenRange: variableTypeTokenRange });

let initializerTokenRange: IExcerptTokenRange | undefined = undefined;
if (variableDeclaration.initializer) {
initializerTokenRange = ExcerptBuilder.createEmptyTokenRange();
nodesToCapture.push({ node: variableDeclaration.initializer, tokenRange: initializerTokenRange });
}

const excerptTokens: IExcerptToken[] = this._buildExcerptTokens(astDeclaration, nodesToCapture);
const apiItemMetadata: ApiItemMetadata = this._collector.fetchApiItemMetadata(astDeclaration);
const docComment: tsdoc.DocComment | undefined = apiItemMetadata.tsdocComment;
Expand All @@ -997,6 +1008,7 @@ export class ApiModelGenerator {
releaseTag,
excerptTokens,
variableTypeTokenRange,
initializerTokenRange,
isReadonly
});

Expand Down
Expand Up @@ -322,6 +322,28 @@
],
"implementsTokenRanges": []
},
{
"kind": "Variable",
"canonicalReference": "api-extractor-scenarios!CONST_VARIABLE:var",
"docComment": "/**\n * @public\n */\n",
"excerptTokens": [
{
"kind": "Content",
"text": "CONST_VARIABLE: "
},
{
"kind": "Content",
"text": "string"
}
],
"isReadonly": true,
"releaseTag": "Public",
"name": "CONST_VARIABLE",
"variableTypeTokenRange": {
"startIndex": 1,
"endIndex": 2
}
},
{
"kind": "Enum",
"canonicalReference": "api-extractor-scenarios!ConstEnum:enum",
Expand Down Expand Up @@ -505,6 +527,28 @@
}
]
},
{
"kind": "Variable",
"canonicalReference": "api-extractor-scenarios!nonConstVariable:var",
"docComment": "/**\n * @public\n */\n",
"excerptTokens": [
{
"kind": "Content",
"text": "nonConstVariable: "
},
{
"kind": "Content",
"text": "string"
}
],
"isReadonly": false,
"releaseTag": "Public",
"name": "nonConstVariable",
"variableTypeTokenRange": {
"startIndex": 1,
"endIndex": 2
}
},
{
"kind": "Enum",
"canonicalReference": "api-extractor-scenarios!RegularEnum:enum",
Expand Down Expand Up @@ -711,7 +755,15 @@
"excerptTokens": [
{
"kind": "Content",
"text": "readonly someReadonlyProp = 5;"
"text": "readonly someReadonlyProp = "
},
{
"kind": "Content",
"text": "5"
},
{
"kind": "Content",
"text": ";"
}
],
"isReadonly": true,
Expand All @@ -723,7 +775,11 @@
"endIndex": 0
},
"isStatic": false,
"isProtected": false
"isProtected": false,
"initializerTokenRange": {
"startIndex": 1,
"endIndex": 2
}
},
{
"kind": "Property",
Expand Down Expand Up @@ -792,22 +848,26 @@
},
{
"kind": "Variable",
"canonicalReference": "api-extractor-scenarios!VARIABLE:var",
"canonicalReference": "api-extractor-scenarios!VARIABLE_WITHOUT_EXPLICIT_TYPE:var",
"docComment": "/**\n * @public\n */\n",
"excerptTokens": [
{
"kind": "Content",
"text": "VARIABLE: "
"text": "VARIABLE_WITHOUT_EXPLICIT_TYPE = "
},
{
"kind": "Content",
"text": "string"
"text": "\"hello\""
}
],
"isReadonly": true,
"releaseTag": "Public",
"name": "VARIABLE",
"name": "VARIABLE_WITHOUT_EXPLICIT_TYPE",
"variableTypeTokenRange": {
"startIndex": 0,
"endIndex": 0
},
"initializerTokenRange": {
"startIndex": 1,
"endIndex": 2
}
Expand Down
Expand Up @@ -22,6 +22,9 @@ export class ClassWithTypeLiterals {
} | undefined;
}

// @public (undocumented)
export const CONST_VARIABLE: string;

// @public (undocumented)
export const enum ConstEnum {
// (undocumented)
Expand All @@ -46,6 +49,9 @@ export namespace NamespaceContainingVariable {
constVariable: object[];
}

// @public (undocumented)
export let nonConstVariable: string;

// @public (undocumented)
export enum RegularEnum {
One = 1,
Expand All @@ -71,7 +77,7 @@ export class SimpleClass {
}

// @public (undocumented)
export const VARIABLE: string;
export const VARIABLE_WITHOUT_EXPLICIT_TYPE = "hello";

// (No @packageDocumentation comment for this package)

Expand Down
Expand Up @@ -17,6 +17,9 @@ export declare class ClassWithTypeLiterals {
} | undefined;
}

/** @public */
export declare const CONST_VARIABLE: string;

/** @public */
export declare const enum ConstEnum {
Zero = 0,
Expand All @@ -35,6 +38,9 @@ export declare namespace NamespaceContainingVariable {
let constVariable: object[];
}

/** @public */
export declare let nonConstVariable: string;

/** @public */
export declare enum RegularEnum {
/**
Expand Down Expand Up @@ -63,6 +69,6 @@ export declare class SimpleClass {
}

/** @public */
export declare const VARIABLE: string;
export declare const VARIABLE_WITHOUT_EXPLICIT_TYPE = "hello";

export { }
Expand Up @@ -223,7 +223,11 @@
"excerptTokens": [
{
"kind": "Content",
"text": "FOO = \"foo\""
"text": "FOO = "
},
{
"kind": "Content",
"text": "\"foo\""
}
],
"isReadonly": true,
Expand All @@ -232,6 +236,10 @@
"variableTypeTokenRange": {
"startIndex": 0,
"endIndex": 0
},
"initializerTokenRange": {
"startIndex": 1,
"endIndex": 2
}
},
{
Expand Down
Expand Up @@ -2,7 +2,13 @@
// See LICENSE in the project root for license information.

/** @public */
export const VARIABLE: string = 'hello';
export const CONST_VARIABLE: string = 'hello';

/** @public */
export let nonConstVariable: string = 'hello';

/** @public */
export const VARIABLE_WITHOUT_EXPLICIT_TYPE = 'hello';

/** @public */
export namespace NamespaceContainingVariable {
Expand Down
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/api-extractor-model",
"comment": "Add a new initializerTokenRange field to ApiProperty and ApiVariable items.",
"type": "minor"
}
],
"packageName": "@microsoft/api-extractor-model"
}
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/api-extractor",
"comment": "API Extractor now populates an initializerTokenRange field for ApiProperty and ApiVariable items.",
"type": "minor"
}
],
"packageName": "@microsoft/api-extractor"
}
12 changes: 12 additions & 0 deletions common/reviews/api/api-extractor-model.api.md
Expand Up @@ -476,8 +476,15 @@ export class ApiProperty extends ApiProperty_base {
get containerKey(): string;
// (undocumented)
static getContainerKey(name: string, isStatic: boolean): string;
readonly initializerExcerpt: Excerpt | undefined;
// @override (undocumented)
get kind(): ApiItemKind;
// Warning: (ae-forgotten-export) The symbol "IApiPropertyJson" needs to be exported by the entry point index.d.ts
//
// @override (undocumented)
static onDeserializeInto(options: Partial<IApiPropertyOptions>, context: DeserializerContext, jsonObject: IApiPropertyJson): void;
// @override (undocumented)
serializeInto(jsonObject: Partial<IApiPropertyJson>): void;
}

// Warning: (ae-forgotten-export) The symbol "ApiPropertyItem_base" needs to be exported by the entry point index.d.ts
Expand Down Expand Up @@ -633,6 +640,7 @@ export class ApiVariable extends ApiVariable_base {
get containerKey(): string;
// (undocumented)
static getContainerKey(name: string): string;
readonly initializerExcerpt?: Excerpt;
// @override (undocumented)
get kind(): ApiItemKind;
// Warning: (ae-forgotten-export) The symbol "IApiVariableJson" needs to be exported by the entry point index.d.ts
Expand Down Expand Up @@ -822,6 +830,8 @@ export interface IApiPropertyItemOptions extends IApiNameMixinOptions, IApiRelea

// @public
export interface IApiPropertyOptions extends IApiPropertyItemOptions, IApiProtectedMixinOptions, IApiStaticMixinOptions {
// (undocumented)
initializerTokenRange?: IExcerptTokenRange;
}

// @public
Expand Down Expand Up @@ -882,6 +892,8 @@ export interface IApiTypeParameterOptions {

// @public
export interface IApiVariableOptions extends IApiNameMixinOptions, IApiReleaseTagMixinOptions, IApiReadonlyMixinOptions, IApiDeclaredItemOptions {
// (undocumented)
initializerTokenRange?: IExcerptTokenRange;
// (undocumented)
variableTypeTokenRange: IExcerptTokenRange;
}
Expand Down

0 comments on commit 33ad56e

Please sign in to comment.