fix(jsdoc-core): extract the correct basename for scoped modules, and when a namespace is present

This commit is contained in:
Jeff Williams 2023-12-30 14:34:38 -08:00
parent 638a89a204
commit aa49b841bb
No known key found for this signature in database
2 changed files with 13 additions and 3 deletions

View File

@ -139,14 +139,16 @@ export const getTrailingScope = (name) => {
};
/**
* Get a symbol's basename, which is the first part of its full name before any punctuation (other
* than an underscore). For example, all of the following names have the basename `Foo`:
* Get a symbol's basename, which is the first part of its full name before any scope punctuation.
* For example, all of the following names have the basename `Foo`:
*
* + `Foo`
* + `Foo.bar`
* + `Foo.prototype.bar`
* + `Foo#bar`
*
* Similarly, the longname `module:@foo/bar.Baz` has the basename `module:@foo/bar`.
*
* @param {?string} [name] - The symbol's full name.
* @returns {?string} The symbol's basename.
*/
@ -155,7 +157,7 @@ export function getBasename(name) {
return null;
}
return name.replace(/^([$a-z_][$a-z_0-9]*).*?$/i, '$1');
return name.replace(/^((?:[a-z]+:)?[$a-z@_][$a-z_/0-9]*).*?$/i, '$1');
}
// TODO: docs

View File

@ -127,6 +127,14 @@ describe('@jsdoc/core.name', () => {
it('returns the basename if the original value has punctuation', () => {
expect(name.getBasename('foo.Bar#baz')).toBe('foo');
});
it('returns the basename if the original value starts with a namespace', () => {
expect(name.getBasename('module:foo.Bar')).toBe('module:foo');
});
it('returns the basename if the original value is a module identifier with `@` and `/`', () => {
expect(name.getBasename('module:@foo/bar.Baz')).toBe('module:@foo/bar');
});
});
describe('getLeadingScope', () => {