thesayyn / protoc-gen-ts

Compile protocol buffer messages to TypeScript.
MIT License
360 stars 74 forks source link

Imported package name / namespace only partially removed #194

Closed plachta11b closed 1 year ago

plachta11b commented 1 year ago

Importing a package into a package with a shorter package name that is simultaneously a prefix caused namespace removal to fail.

Original function:

if (config.no_namespace) {
    return removeRootParentName(name, packages.find(p => name.startsWith(p))).replace(/\./g, '');
}

Proposed fix:

if (config.no_namespace) {
    // Create a copy of the original array. Sort the array copy by length, from longest to shortest.
    const sortedPackages = packages.map(p => p).sort((a, b) => b.length - a.length);
    // Return the new package name with longest possible match removed from the name.
    return removeRootParentName(name, sortedPackages.find(p => name.startsWith(p))).replace(/\./g, '');
}