-
Notifications
You must be signed in to change notification settings - Fork 58
fix: fix resolution of various type annotation edge cases #968
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
pimterry
wants to merge
2
commits into
nodejs:main
Choose a base branch
from
pimterry:fix-ref-parsing
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@node-core/doc-kit': patch | ||
| --- | ||
|
|
||
| Resolve unions and arrays of display-name types (`{HTTP/2 Headers Object | vm.Module}`, `{HTTP/2 Headers Object[]}`), and stop capturing prose such as `U+007B ({), and U+007D (}).` as a type annotation. |
162 changes: 162 additions & 0 deletions
162
packages/core/src/generators/metadata/utils/__tests__/resolveTypes.test.mjs
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,162 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { describe, it, mock, beforeEach } from 'node:test'; | ||
|
|
||
| const warnings = []; | ||
|
|
||
| mock.module('../../../../logger/index.mjs', { | ||
| defaultExport: { | ||
| warn: message => warnings.push(message), | ||
| }, | ||
| }); | ||
|
|
||
| const { resolveTypeAnnotations } = await import('../resolveTypes.mjs'); | ||
|
|
||
| const HTTP2_TYPE_MAP = { | ||
| 'HTTP/2 Headers Object': 'http2.html#headers-object', | ||
| }; | ||
|
|
||
| /** | ||
| * Resolves a single annotation and returns its node | ||
| * | ||
| * @param {string} value The type value | ||
| * @param {Record<string, string>} typeMap The mapping of types to links | ||
| */ | ||
| const resolve = (value, typeMap = {}) => { | ||
| const node = { type: 'typeAnnotation', value }; | ||
|
|
||
| resolveTypeAnnotations( | ||
| { type: 'root', children: [node] }, | ||
| typeMap, | ||
| 'test.md' | ||
| ); | ||
|
|
||
| return node; | ||
| }; | ||
|
|
||
| /** | ||
| * Resolves a single annotation and returns its links as `[text, href]` pairs, | ||
| * asserting that every range lines up with the text it links | ||
| * | ||
| * @param {string} value The type value | ||
| * @param {Record<string, string>} typeMap The mapping of types to links | ||
| */ | ||
| const linksIn = (value, typeMap = {}) => { | ||
| const { data } = resolve(value, typeMap); | ||
|
|
||
| for (const link of data.links) { | ||
| assert.equal(value.slice(link.start, link.end), link.text); | ||
| } | ||
|
|
||
| return data.links.map(({ text, href }) => [text, href]); | ||
| }; | ||
|
|
||
| describe('resolveTypeAnnotations', () => { | ||
| beforeEach(() => { | ||
| warnings.length = 0; | ||
| }); | ||
|
|
||
| it('links a type that is a whole map key', () => { | ||
| assert.deepEqual(linksIn('HTTP/2 Headers Object', HTTP2_TYPE_MAP), [ | ||
| ['HTTP/2 Headers Object', 'http2.html#headers-object'], | ||
| ]); | ||
| assert.deepEqual(warnings, []); | ||
| }); | ||
|
|
||
| it('links an array of a display name', () => { | ||
| assert.deepEqual(linksIn('HTTP/2 Headers Object[]', HTTP2_TYPE_MAP), [ | ||
| ['HTTP/2 Headers Object', 'http2.html#headers-object'], | ||
| ]); | ||
| assert.deepEqual(warnings, []); | ||
| }); | ||
|
|
||
| it('resolves module-qualified names alongside display names', () => { | ||
| assert.deepEqual(linksIn('Module Namespace Object | vm.Module'), [ | ||
| [ | ||
| 'Module Namespace Object', | ||
| 'https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects', | ||
| ], | ||
| ['vm.Module', 'vm.html#class-vmmodule'], | ||
| ]); | ||
| assert.deepEqual(warnings, []); | ||
| }); | ||
|
|
||
| it('keeps the resolvable parts of a partly unknown union', () => { | ||
| assert.deepEqual( | ||
| linksIn('HTTP/2 Headers Object | HTTP/2 Raw Headers', HTTP2_TYPE_MAP), | ||
| [['HTTP/2 Headers Object', 'http2.html#headers-object']] | ||
| ); | ||
| assert.deepEqual(warnings, []); | ||
| }); | ||
|
|
||
| it('warns when no part of an unparsable type resolves', () => { | ||
| const node = resolve('HTTP/2 Raw Headers', HTTP2_TYPE_MAP); | ||
|
|
||
| assert.equal(node.data.parseError, true); | ||
| assert.deepEqual(node.data.links, []); | ||
| assert.deepEqual(warnings, [ | ||
| 'Invalid type annotation: {HTTP/2 Raw Headers}', | ||
| ]); | ||
| }); | ||
|
|
||
| it('does not read `||` in default-value prose as a union', () => { | ||
| const node = resolve("req.url || '/'"); | ||
|
|
||
| assert.deepEqual(node.data.links, []); | ||
| assert.deepEqual(warnings, ["Invalid type annotation: {req.url || '/'}"]); | ||
| }); | ||
|
|
||
| it('marks which values are TypeScript, for the highlighter', () => { | ||
| const isTypeScript = (value, typeMap) => | ||
| resolve(value, typeMap).data.typescript; | ||
|
|
||
| assert.equal(isTypeScript('Promise<string> | null'), true); | ||
| assert.equal(isTypeScript('HTTP/2 Headers Object', HTTP2_TYPE_MAP), false); | ||
| assert.equal(isTypeScript('Module Namespace Object | vm.Module'), false); | ||
| assert.equal(isTypeScript('HTTP/2 Raw Headers', HTTP2_TYPE_MAP), false); | ||
| }); | ||
|
|
||
| it('parses valid TypeScript rather than splitting it', () => { | ||
| assert.deepEqual( | ||
| linksIn('Promise<string> | Buffer', { Buffer: 'buffer.html#buffer' }), | ||
| [ | ||
| [ | ||
| 'Promise', | ||
| 'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise', | ||
| ], | ||
| [ | ||
| 'string', | ||
| 'https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type', | ||
| ], | ||
| ['Buffer', 'buffer.html#buffer'], | ||
| ] | ||
| ); | ||
| assert.deepEqual(warnings, []); | ||
| }); | ||
|
|
||
| it('resolves every annotation in a nested tree', () => { | ||
| const STRING_URL = | ||
| 'https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type'; | ||
|
|
||
| // Two per paragraph: a visitor that skips siblings still reaches the first | ||
| const nodes = ['string', 'HTTP/2 Headers Object', 'string', 'string'].map( | ||
| value => ({ type: 'typeAnnotation', value }) | ||
| ); | ||
|
|
||
| resolveTypeAnnotations( | ||
| { | ||
| type: 'root', | ||
| children: [ | ||
| { type: 'paragraph', children: nodes.slice(0, 2) }, | ||
| { type: 'paragraph', children: nodes.slice(2) }, | ||
| ], | ||
| }, | ||
| HTTP2_TYPE_MAP, | ||
| 'test.md' | ||
| ); | ||
|
|
||
| assert.deepEqual( | ||
| nodes.map(({ data }) => data.links.map(({ href }) => href)), | ||
| [[STRING_URL], ['http2.html#headers-object'], [STRING_URL], [STRING_URL]] | ||
| ); | ||
| }); | ||
| }); |
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
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 |
|---|---|---|
|
|
@@ -48,6 +48,11 @@ export const lookupTypeName = (name, typeMap) => { | |
| return ''; | ||
| }; | ||
|
|
||
| // A dotted identifier path, e.g. `vm.Module` or `os.constants.dlopen`. Names | ||
| // that merely contain a dot (prose, `Object.<string, string>`) must not be | ||
| // turned into a module link. | ||
| const MODULE_QUALIFIED_NAME = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/; | ||
|
Member
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. Can you put this in
Member
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. bump |
||
|
|
||
| /** | ||
| * Resolves a type identifier to a documentation URL: map lookups first, then | ||
| * the dotted-name heuristic for Node.js types like `vm.Module` (which links | ||
|
|
@@ -65,7 +70,7 @@ export const resolveTypeReference = (name, typeMap) => { | |
| } | ||
|
|
||
| // Transform Node.js types like 'vm.Something'. | ||
| if (name.indexOf('.') >= 0) { | ||
| if (MODULE_QUALIFIED_NAME.test(name)) { | ||
| const [mod, ...pieces] = name.split('.'); | ||
| const isClass = pieces.at(-1).match(/^[A-Z][a-z]/); | ||
|
|
||
|
|
||
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
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.