-
Notifications
You must be signed in to change notification settings - Fork 680
[api-extractor] Add handling of file reference directives #1955
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
Open
willmtemple
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
willmtemple:ae-path-reference-directives
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| import * as path from 'path'; | ||
|
|
||
| import * as ts from 'typescript'; | ||
| import * as tsdoc from '@microsoft/tsdoc'; | ||
| import { PackageJsonLookup, Sort, InternalError } from '@rushstack/node-core-library'; | ||
|
|
@@ -43,6 +45,29 @@ export interface ICollectorOptions { | |
| extractorConfig: ExtractorConfig; | ||
| } | ||
|
|
||
| /** | ||
| * Resolve the name of a file to an absolute path relative to a TypeScript SourceFile node. | ||
| */ | ||
| function resolveNameRelativeToSourceFile(name: string, source: ts.SourceFile): string { | ||
| const output: string[] = source.fileName.split(path.sep).slice(0, -1); | ||
|
|
||
| // Process the file name, treating special selectors such as '..' and '.' and '' correctly | ||
| for (const fragment of name.split('/')) { | ||
| switch (fragment) { | ||
| case '..': // parent selector | ||
| output.pop(); | ||
| break; | ||
| case '.': // same-dir selectors | ||
| case '': | ||
| break; | ||
| default: | ||
| output.push(fragment); | ||
| } | ||
| } | ||
|
|
||
| return output.join(path.sep); | ||
| } | ||
|
|
||
| /** | ||
| * The `Collector` manages the overall data set that is used by `ApiModelGenerator`, | ||
| * `DtsRollupGenerator`, and `ApiReportGenerator`. Starting from the working package's entry point, | ||
|
|
@@ -84,6 +109,7 @@ export class Collector { | |
|
|
||
| private readonly _dtsTypeReferenceDirectives: Set<string> = new Set<string>(); | ||
| private readonly _dtsLibReferenceDirectives: Set<string> = new Set<string>(); | ||
| private readonly _dtsFileReferenceDirectives: Set<string> = new Set<string>(); | ||
|
|
||
| // Used by getOverloadIndex() | ||
| private readonly _cachedOverloadIndexesByDeclaration: Map<AstDeclaration, number>; | ||
|
|
@@ -158,6 +184,17 @@ export class Collector { | |
| return this._dtsLibReferenceDirectives; | ||
| } | ||
|
|
||
| /** | ||
| * A list of names (e.g. "runtime-library") that should appear in a path-based reference like this: | ||
| * | ||
| * ``` | ||
| * /// <reference path="path/to/example.d.ts" /> | ||
| * ``` | ||
| */ | ||
| public get dtsFileReferenceDirectives(): ReadonlySet<string> { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The /**
* A list of paths (e.g. "path/to/file.d.ts") that should appear in a reference like this:
*
* ```
* /// <reference path="path/to/file.d.ts" />
* ```
*
* The paths are resolved relative to the source file containing the reference.
*/
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah yeah just missed this when copying the doc string 👍 |
||
| return this._dtsFileReferenceDirectives; | ||
| } | ||
|
|
||
| public get entities(): ReadonlyArray<CollectorEntity> { | ||
| return this._entities; | ||
| } | ||
|
|
@@ -262,6 +299,7 @@ export class Collector { | |
| Sort.sortBy(this._entities, (x) => x.getSortKey()); | ||
| Sort.sortSet(this._dtsTypeReferenceDirectives); | ||
| Sort.sortSet(this._dtsLibReferenceDirectives); | ||
| Sort.sortSet(this._dtsFileReferenceDirectives); | ||
| this._starExportedExternalModulePaths.sort(); | ||
| } | ||
|
|
||
|
|
@@ -875,6 +913,11 @@ export class Collector { | |
| ); | ||
| this._dtsLibReferenceDirectives.add(name); | ||
| } | ||
|
|
||
| for (const referencedFile of sourceFile.referencedFiles) { | ||
| const name: string = sourceFile.text.substring(referencedFile.pos, referencedFile.end); | ||
| this._dtsFileReferenceDirectives.add(resolveNameRelativeToSourceFile(name, sourceFile)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
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
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
43 changes: 43 additions & 0 deletions
43
...actor-scenarios/etc/test-outputs/fileReferenceDirectives/api-extractor-scenarios.api.json
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,43 @@ | ||
| { | ||
| "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": "Variable", | ||
| "canonicalReference": "api-extractor-scenarios!X:var", | ||
| "docComment": "/**\n * @public\n */\n", | ||
| "excerptTokens": [ | ||
| { | ||
| "kind": "Content", | ||
| "text": "X: " | ||
| }, | ||
| { | ||
| "kind": "Reference", | ||
| "text": "Foo", | ||
| "canonicalReference": "!Foo:interface" | ||
| } | ||
| ], | ||
| "releaseTag": "Public", | ||
| "name": "X", | ||
| "variableTypeTokenRange": { | ||
| "startIndex": 1, | ||
| "endIndex": 2 | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } |
13 changes: 13 additions & 0 deletions
13
...enarios/etc/test-outputs/fileReferenceDirectives/api-extractor-scenarios.api.md
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,13 @@ | ||
| ## 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 (undocumented) | ||
| export const X: Foo; | ||
|
|
||
|
|
||
| // (No @packageDocumentation comment for this package) | ||
|
|
||
| ``` |
8 changes: 8 additions & 0 deletions
8
build-tests/api-extractor-scenarios/etc/test-outputs/fileReferenceDirectives/rollup.d.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,8 @@ | ||
| /// <reference path="../../../src/fileReferenceDirectives/shim.d.ts" /> | ||
|
|
||
| /** | ||
| * @public | ||
| */ | ||
| export declare const X: Foo; | ||
|
|
||
| export { } |
4 changes: 4 additions & 0 deletions
4
build-tests/api-extractor-scenarios/src/fileReferenceDirectives/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 |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| export { X } from './nested/module'; |
9 changes: 9 additions & 0 deletions
9
build-tests/api-extractor-scenarios/src/fileReferenceDirectives/nested/module.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,9 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| /// <reference path="../shim.d.ts" /> | ||
|
|
||
| /** | ||
| * @public | ||
| */ | ||
| export const X: Foo = {}; |
4 changes: 4 additions & 0 deletions
4
build-tests/api-extractor-scenarios/src/fileReferenceDirectives/shim.d.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,4 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| declare interface Foo {} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you simply use path.relative()?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this one, I don't think so. I need to boot into Windows and check it, but I believe that the reference directive in text has to follow the POSIX convention and use forward slash as its path separator just like a node module, but the file name of the source file could be in Windows convention, so relying on
pathto process the reference path may not be safe. Need to do some testing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the input and output paths are POSIX format, then you can use
path.posix.relative().If the input path is Windows style and you want to convert to POSIX, there is no theoretically correct API for that, but
Text.replaceAll(thePath, '\\', '/')works reasonably well with Node.js