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

fix: md names handle spaces, colons, and both #1297

Merged
merged 1 commit into from
May 10, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/collections/componentSetBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,16 @@ export const entryToTypeAndName =
(reg: RegistryAccess) =>
(rawEntry: string): MetadataTypeAndMetadataName => {
// split on the first colon, and then join the rest back together to support names that include colons
const [typeName, ...name] = rawEntry.split(':').map((entry) => entry.trim());
const type = reg.getTypeByName(typeName);
const parent = reg.getParentType(typeName);
const [typeName, ...name] = rawEntry.split(':');
const type = reg.getTypeByName(typeName.trim());
const parent = reg.getParentType(type.name);
// If a user is requesting a child type that is unaddressable (more common with custom registries to create proper behavior)
// throw an error letting them know to use the entire parent instead
// or if they're requesting a COFT, unadressable without parent, don't throw because the parent could be requested - we don't know at this point
if (type.isAddressable === false && parent !== undefined && !type.unaddressableWithoutParent) {
throw new Error(`Cannot use this type, instead use ${parent.name}`);
}
return { type, metadataName: name.length ? name.join(':') : '*' };
return { type, metadataName: name.length ? name.join(':').trim() : '*' };
};

const typeAndNameToMetadataComponents =
Expand Down
30 changes: 30 additions & 0 deletions test/collections/componentSetBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,4 +546,34 @@ describe('entryToTypeAndName', () => {
metadataName: '*',
});
});
it('leading spaces in name are trimmed', () => {
expect(entryToTypeAndName(reg)('Layout: My Layout')).to.deep.equal({
type: reg.getTypeByName('Layout'),
metadataName: 'My Layout',
});
});
it('trailing spaces in name are trimmed', () => {
expect(entryToTypeAndName(reg)('Layout:My Layout ')).to.deep.equal({
type: reg.getTypeByName('Layout'),
metadataName: 'My Layout',
});
});
it('spaces in name', () => {
expect(entryToTypeAndName(reg)('Layout:My Layout')).to.deep.equal({
type: reg.getTypeByName('Layout'),
metadataName: 'My Layout',
});
});
it('colons in name', () => {
expect(entryToTypeAndName(reg)('Layout:My:Colon:Layout')).to.deep.equal({
type: reg.getTypeByName('Layout'),
metadataName: 'My:Colon:Layout',
});
});
it('colons and spaces in name', () => {
expect(entryToTypeAndName(reg)('Layout:My : Colon : Layout')).to.deep.equal({
type: reg.getTypeByName('Layout'),
metadataName: 'My : Colon : Layout',
});
});
});