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

[api-extractor] Fix an InternalError when a declaration refers to itself using "typeof" #2239

Merged
merged 3 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions apps/api-extractor/src/analyzer/ExportAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,17 @@ export class ExportAnalyzer {
symbol: ts.Symbol,
referringModuleIsExternal: boolean
): AstEntity | undefined {
// eslint-disable-next-line no-bitwise
if ((symbol.flags & ts.SymbolFlags.FunctionScopedVariable) !== 0) {
// If a symbol refers back to part of its own definition, don't follow that rabbit hole
// Example:
//
// function f(x: number): typeof x {
// return 123;
// }
return undefined;
}

let current: ts.Symbol = symbol;

if (referringModuleIsExternal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"preapproved",
"typeOf",
"typeOf2",
"typeOf3",
"typeParameters"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
{
"metadata": {
"toolPackage": "@microsoft/api-extractor",
"toolVersion": "[test mode]",
"schemaVersion": 1003,
"oldestForwardsCompatibleVersion": 1001
},
"kind": "Package",
"canonicalReference": "api-extractor-scenarios!",
"docComment": "",
"name": "api-extractor-scenarios",
"members": [
{
"kind": "EntryPoint",
"canonicalReference": "api-extractor-scenarios!",
"name": "",
"members": [
{
"kind": "Function",
"canonicalReference": "api-extractor-scenarios!f1:function(1)",
"docComment": "/**\n * A function that references its own parameter type.\n */\n",
"excerptTokens": [
{
"kind": "Content",
"text": "export declare function f1(x: "
},
{
"kind": "Content",
"text": "number"
},
{
"kind": "Content",
"text": "): "
},
{
"kind": "Content",
"text": "typeof "
},
{
"kind": "Reference",
"text": "x",
"canonicalReference": "api-extractor-scenarios!~x:var"
},
{
"kind": "Content",
"text": ";"
}
],
"returnTypeTokenRange": {
"startIndex": 3,
"endIndex": 5
},
"releaseTag": "Public",
"overloadIndex": 1,
"parameters": [
{
"parameterName": "x",
"parameterTypeTokenRange": {
"startIndex": 1,
"endIndex": 2
}
}
],
"name": "f1"
},
{
"kind": "Function",
"canonicalReference": "api-extractor-scenarios!f2:function(1)",
"docComment": "/**\n * A function that indirectly references its own parameter type.\n */\n",
"excerptTokens": [
{
"kind": "Content",
"text": "export declare function f2(x: "
},
{
"kind": "Content",
"text": "number"
},
{
"kind": "Content",
"text": "): "
},
{
"kind": "Content",
"text": "keyof typeof "
},
{
"kind": "Reference",
"text": "x",
"canonicalReference": "api-extractor-scenarios!~x:var"
},
{
"kind": "Content",
"text": ";"
}
],
"returnTypeTokenRange": {
"startIndex": 3,
"endIndex": 5
},
"releaseTag": "Public",
"overloadIndex": 1,
"parameters": [
{
"parameterName": "x",
"parameterTypeTokenRange": {
"startIndex": 1,
"endIndex": 2
}
}
],
"name": "f2"
},
{
"kind": "Function",
"canonicalReference": "api-extractor-scenarios!f3:function(1)",
"docComment": "/**\n * A function that references its own type.\n */\n",
"excerptTokens": [
{
"kind": "Content",
"text": "export declare function f3(): "
},
{
"kind": "Content",
"text": "typeof "
},
{
"kind": "Reference",
"text": "f3",
"canonicalReference": "api-extractor-scenarios!f3:function"
},
{
"kind": "Content",
"text": " | undefined"
},
{
"kind": "Content",
"text": ";"
}
],
"returnTypeTokenRange": {
"startIndex": 1,
"endIndex": 4
},
"releaseTag": "Public",
"overloadIndex": 1,
"parameters": [],
"name": "f3"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## API Report File for "api-extractor-scenarios"

> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).

```ts

// @public
export function f1(x: number): typeof x;

// @public
export function f2(x: number): keyof typeof x;

// @public
export function f3(): typeof f3 | undefined;


// (No @packageDocumentation comment for this package)

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

/**
* A function that references its own parameter type.
*/
export declare function f1(x: number): typeof x;

/**
* A function that indirectly references its own parameter type.
*/
export declare function f2(x: number): keyof typeof x;

/**
* A function that references its own type.
*/
export declare function f3(): typeof f3 | undefined;

export { }
26 changes: 26 additions & 0 deletions build-tests/api-extractor-scenarios/src/typeOf3/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

/**
* A function that references its own parameter type.
* @public
*/
export function f1(x: number): typeof x {
return x;
}

/**
* A function that indirectly references its own parameter type.
* @public
*/
export function f2(x: number): keyof typeof x {
return 'valueOf';
}

/**
* A function that references its own type.
* @public
*/
export function f3(): typeof f3 | undefined {
return undefined;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@microsoft/api-extractor",
"comment": "Fix an InternalError reported when a declaration referred to itself using \"tyepof\"",
"type": "patch"
}
],
"packageName": "@microsoft/api-extractor",
"email": "[email protected]"
}