Skip to content

Commit 33ad56e

Browse files
committed
Initial commit
1 parent 67963a1 commit 33ad56e

File tree

13 files changed

+218
-23
lines changed

13 files changed

+218
-23
lines changed

.vscode/launch.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
"name": "Rush Debug",
66
"type": "node",
77
"request": "launch",
8-
"program": "${workspaceRoot}/apps/rush/lib/start-dev.js",
8+
"program": "${workspaceRoot}/libraries/rush-lib/lib/start.js",
99
"stopOnEntry": true,
1010
"args": [
11-
"start"
11+
"rebuild",
12+
"-o",
13+
"api-extractor-scenarios"
1214
],
1315
"cwd": "${workspaceRoot}",
1416
"runtimeExecutable": null,

apps/api-extractor/src/generators/ApiModelGenerator.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -829,25 +829,29 @@ export class ApiModelGenerator {
829829
let apiProperty: ApiProperty | undefined = parentApiItem.tryGetMemberByKey(containerKey) as ApiProperty;
830830

831831
if (apiProperty === undefined) {
832+
const declaration: ts.Declaration = astDeclaration.declaration;
832833
const nodesToCapture: IExcerptBuilderNodeToCapture[] = [];
833834

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

837-
if (
838-
ts.isPropertyDeclaration(astDeclaration.declaration) ||
839-
ts.isGetAccessorDeclaration(astDeclaration.declaration)
840-
) {
841-
propertyTypeNode = astDeclaration.declaration.type;
838+
if (ts.isPropertyDeclaration(declaration) || ts.isGetAccessorDeclaration(declaration)) {
839+
propertyTypeNode = declaration.type;
842840
}
843841

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

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

849+
let initializerTokenRange: IExcerptTokenRange | undefined = undefined;
850+
if (ts.isPropertyDeclaration(declaration) && declaration.initializer) {
851+
initializerTokenRange = ExcerptBuilder.createEmptyTokenRange();
852+
nodesToCapture.push({ node: declaration.initializer, tokenRange: initializerTokenRange });
853+
}
854+
851855
const excerptTokens: IExcerptToken[] = this._buildExcerptTokens(astDeclaration, nodesToCapture);
852856
const apiItemMetadata: ApiItemMetadata = this._collector.fetchApiItemMetadata(astDeclaration);
853857
const docComment: tsdoc.DocComment | undefined = apiItemMetadata.tsdocComment;
@@ -866,7 +870,8 @@ export class ApiModelGenerator {
866870
isOptional,
867871
isReadonly,
868872
excerptTokens,
869-
propertyTypeTokenRange
873+
propertyTypeTokenRange,
874+
initializerTokenRange
870875
});
871876
parentApiItem.addMember(apiProperty);
872877
} else {
@@ -985,6 +990,12 @@ export class ApiModelGenerator {
985990
const variableTypeTokenRange: IExcerptTokenRange = ExcerptBuilder.createEmptyTokenRange();
986991
nodesToCapture.push({ node: variableDeclaration.type, tokenRange: variableTypeTokenRange });
987992

993+
let initializerTokenRange: IExcerptTokenRange | undefined = undefined;
994+
if (variableDeclaration.initializer) {
995+
initializerTokenRange = ExcerptBuilder.createEmptyTokenRange();
996+
nodesToCapture.push({ node: variableDeclaration.initializer, tokenRange: initializerTokenRange });
997+
}
998+
988999
const excerptTokens: IExcerptToken[] = this._buildExcerptTokens(astDeclaration, nodesToCapture);
9891000
const apiItemMetadata: ApiItemMetadata = this._collector.fetchApiItemMetadata(astDeclaration);
9901001
const docComment: tsdoc.DocComment | undefined = apiItemMetadata.tsdocComment;
@@ -997,6 +1008,7 @@ export class ApiModelGenerator {
9971008
releaseTag,
9981009
excerptTokens,
9991010
variableTypeTokenRange,
1011+
initializerTokenRange,
10001012
isReadonly
10011013
});
10021014

build-tests/api-extractor-scenarios/etc/test-outputs/apiItemKinds/api-extractor-scenarios.api.json

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,28 @@
322322
],
323323
"implementsTokenRanges": []
324324
},
325+
{
326+
"kind": "Variable",
327+
"canonicalReference": "api-extractor-scenarios!CONST_VARIABLE:var",
328+
"docComment": "/**\n * @public\n */\n",
329+
"excerptTokens": [
330+
{
331+
"kind": "Content",
332+
"text": "CONST_VARIABLE: "
333+
},
334+
{
335+
"kind": "Content",
336+
"text": "string"
337+
}
338+
],
339+
"isReadonly": true,
340+
"releaseTag": "Public",
341+
"name": "CONST_VARIABLE",
342+
"variableTypeTokenRange": {
343+
"startIndex": 1,
344+
"endIndex": 2
345+
}
346+
},
325347
{
326348
"kind": "Enum",
327349
"canonicalReference": "api-extractor-scenarios!ConstEnum:enum",
@@ -505,6 +527,28 @@
505527
}
506528
]
507529
},
530+
{
531+
"kind": "Variable",
532+
"canonicalReference": "api-extractor-scenarios!nonConstVariable:var",
533+
"docComment": "/**\n * @public\n */\n",
534+
"excerptTokens": [
535+
{
536+
"kind": "Content",
537+
"text": "nonConstVariable: "
538+
},
539+
{
540+
"kind": "Content",
541+
"text": "string"
542+
}
543+
],
544+
"isReadonly": false,
545+
"releaseTag": "Public",
546+
"name": "nonConstVariable",
547+
"variableTypeTokenRange": {
548+
"startIndex": 1,
549+
"endIndex": 2
550+
}
551+
},
508552
{
509553
"kind": "Enum",
510554
"canonicalReference": "api-extractor-scenarios!RegularEnum:enum",
@@ -711,7 +755,15 @@
711755
"excerptTokens": [
712756
{
713757
"kind": "Content",
714-
"text": "readonly someReadonlyProp = 5;"
758+
"text": "readonly someReadonlyProp = "
759+
},
760+
{
761+
"kind": "Content",
762+
"text": "5"
763+
},
764+
{
765+
"kind": "Content",
766+
"text": ";"
715767
}
716768
],
717769
"isReadonly": true,
@@ -723,7 +775,11 @@
723775
"endIndex": 0
724776
},
725777
"isStatic": false,
726-
"isProtected": false
778+
"isProtected": false,
779+
"initializerTokenRange": {
780+
"startIndex": 1,
781+
"endIndex": 2
782+
}
727783
},
728784
{
729785
"kind": "Property",
@@ -792,22 +848,26 @@
792848
},
793849
{
794850
"kind": "Variable",
795-
"canonicalReference": "api-extractor-scenarios!VARIABLE:var",
851+
"canonicalReference": "api-extractor-scenarios!VARIABLE_WITHOUT_EXPLICIT_TYPE:var",
796852
"docComment": "/**\n * @public\n */\n",
797853
"excerptTokens": [
798854
{
799855
"kind": "Content",
800-
"text": "VARIABLE: "
856+
"text": "VARIABLE_WITHOUT_EXPLICIT_TYPE = "
801857
},
802858
{
803859
"kind": "Content",
804-
"text": "string"
860+
"text": "\"hello\""
805861
}
806862
],
807863
"isReadonly": true,
808864
"releaseTag": "Public",
809-
"name": "VARIABLE",
865+
"name": "VARIABLE_WITHOUT_EXPLICIT_TYPE",
810866
"variableTypeTokenRange": {
867+
"startIndex": 0,
868+
"endIndex": 0
869+
},
870+
"initializerTokenRange": {
811871
"startIndex": 1,
812872
"endIndex": 2
813873
}

build-tests/api-extractor-scenarios/etc/test-outputs/apiItemKinds/api-extractor-scenarios.api.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export class ClassWithTypeLiterals {
2222
} | undefined;
2323
}
2424

25+
// @public (undocumented)
26+
export const CONST_VARIABLE: string;
27+
2528
// @public (undocumented)
2629
export const enum ConstEnum {
2730
// (undocumented)
@@ -46,6 +49,9 @@ export namespace NamespaceContainingVariable {
4649
constVariable: object[];
4750
}
4851

52+
// @public (undocumented)
53+
export let nonConstVariable: string;
54+
4955
// @public (undocumented)
5056
export enum RegularEnum {
5157
One = 1,
@@ -71,7 +77,7 @@ export class SimpleClass {
7177
}
7278

7379
// @public (undocumented)
74-
export const VARIABLE: string;
80+
export const VARIABLE_WITHOUT_EXPLICIT_TYPE = "hello";
7581

7682
// (No @packageDocumentation comment for this package)
7783

build-tests/api-extractor-scenarios/etc/test-outputs/apiItemKinds/rollup.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export declare class ClassWithTypeLiterals {
1717
} | undefined;
1818
}
1919

20+
/** @public */
21+
export declare const CONST_VARIABLE: string;
22+
2023
/** @public */
2124
export declare const enum ConstEnum {
2225
Zero = 0,
@@ -35,6 +38,9 @@ export declare namespace NamespaceContainingVariable {
3538
let constVariable: object[];
3639
}
3740

41+
/** @public */
42+
export declare let nonConstVariable: string;
43+
3844
/** @public */
3945
export declare enum RegularEnum {
4046
/**
@@ -63,6 +69,6 @@ export declare class SimpleClass {
6369
}
6470

6571
/** @public */
66-
export declare const VARIABLE: string;
72+
export declare const VARIABLE_WITHOUT_EXPLICIT_TYPE = "hello";
6773

6874
export { }

build-tests/api-extractor-scenarios/etc/test-outputs/readonlyDeclarations/api-extractor-scenarios.api.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,11 @@
223223
"excerptTokens": [
224224
{
225225
"kind": "Content",
226-
"text": "FOO = \"foo\""
226+
"text": "FOO = "
227+
},
228+
{
229+
"kind": "Content",
230+
"text": "\"foo\""
227231
}
228232
],
229233
"isReadonly": true,
@@ -232,6 +236,10 @@
232236
"variableTypeTokenRange": {
233237
"startIndex": 0,
234238
"endIndex": 0
239+
},
240+
"initializerTokenRange": {
241+
"startIndex": 1,
242+
"endIndex": 2
235243
}
236244
},
237245
{

build-tests/api-extractor-scenarios/src/apiItemKinds/variables.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
// See LICENSE in the project root for license information.
33

44
/** @public */
5-
export const VARIABLE: string = 'hello';
5+
export const CONST_VARIABLE: string = 'hello';
6+
7+
/** @public */
8+
export let nonConstVariable: string = 'hello';
9+
10+
/** @public */
11+
export const VARIABLE_WITHOUT_EXPLICIT_TYPE = 'hello';
612

713
/** @public */
814
export namespace NamespaceContainingVariable {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/api-extractor-model",
5+
"comment": "Add a new initializerTokenRange field to ApiProperty and ApiVariable items.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@microsoft/api-extractor-model"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/api-extractor",
5+
"comment": "API Extractor now populates an initializerTokenRange field for ApiProperty and ApiVariable items.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@microsoft/api-extractor"
10+
}

common/reviews/api/api-extractor-model.api.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,15 @@ export class ApiProperty extends ApiProperty_base {
476476
get containerKey(): string;
477477
// (undocumented)
478478
static getContainerKey(name: string, isStatic: boolean): string;
479+
readonly initializerExcerpt: Excerpt | undefined;
479480
// @override (undocumented)
480481
get kind(): ApiItemKind;
482+
// Warning: (ae-forgotten-export) The symbol "IApiPropertyJson" needs to be exported by the entry point index.d.ts
483+
//
484+
// @override (undocumented)
485+
static onDeserializeInto(options: Partial<IApiPropertyOptions>, context: DeserializerContext, jsonObject: IApiPropertyJson): void;
486+
// @override (undocumented)
487+
serializeInto(jsonObject: Partial<IApiPropertyJson>): void;
481488
}
482489

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

823831
// @public
824832
export interface IApiPropertyOptions extends IApiPropertyItemOptions, IApiProtectedMixinOptions, IApiStaticMixinOptions {
833+
// (undocumented)
834+
initializerTokenRange?: IExcerptTokenRange;
825835
}
826836

827837
// @public
@@ -882,6 +892,8 @@ export interface IApiTypeParameterOptions {
882892

883893
// @public
884894
export interface IApiVariableOptions extends IApiNameMixinOptions, IApiReleaseTagMixinOptions, IApiReadonlyMixinOptions, IApiDeclaredItemOptions {
895+
// (undocumented)
896+
initializerTokenRange?: IExcerptTokenRange;
885897
// (undocumented)
886898
variableTypeTokenRange: IExcerptTokenRange;
887899
}

0 commit comments

Comments
 (0)