This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Issue with prenamer renaming to already used values (#374)
- Loading branch information
1 parent
87fc985
commit 8a8a88d
Showing
3 changed files
with
133 additions
and
111 deletions.
There are no files selected for viewing
This file contains 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 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,77 @@ | ||
import { Languages } from "@azure-tools/codemodel"; | ||
import { length, Dictionary } from "@azure-tools/linq"; | ||
import { removeSequentialDuplicates, fixLeadingNumber, deconstruct, Style, Styler } from "@azure-tools/codegen"; | ||
|
||
export function getNameOptions(typeName: string, components: Array<string>) { | ||
const result = new Set<string>(); | ||
|
||
// add a variant for each incrementally inclusive parent naming scheme. | ||
for (let i = 0; i < length(components); i++) { | ||
const subset = Style.pascal([...removeSequentialDuplicates(components.slice(-1 * i, length(components)))]); | ||
result.add(subset); | ||
} | ||
|
||
// add a second-to-last-ditch option as <typename>.<name> | ||
result.add( | ||
Style.pascal([ | ||
...removeSequentialDuplicates([...fixLeadingNumber(deconstruct(typeName)), ...deconstruct(components.last)]), | ||
]), | ||
); | ||
return [...result.values()]; | ||
} | ||
|
||
interface SetNameOptions { | ||
/** | ||
* Remove consecutive duplicate words in the name. | ||
* @example "FooBarBarSomething" -> "FooBarSomething" | ||
*/ | ||
removeDuplicates?: boolean; | ||
|
||
/** | ||
* Set containing the list of names already used in the given scope. | ||
*/ | ||
existingNames?: Set<string>; | ||
} | ||
|
||
const setNameDefaultOptions: SetNameOptions = Object.freeze({ | ||
removeDuplicates: true, | ||
}); | ||
|
||
export function setName( | ||
thing: { language: Languages }, | ||
styler: Styler, | ||
defaultValue: string, | ||
overrides: Dictionary<string>, | ||
options?: SetNameOptions, | ||
) { | ||
setNameAllowEmpty(thing, styler, defaultValue, overrides, options); | ||
if (!thing.language.default.name) { | ||
throw new Error("Name is empty!"); | ||
} | ||
} | ||
|
||
export function setNameAllowEmpty( | ||
thing: { language: Languages }, | ||
styler: Styler, | ||
defaultValue: string, | ||
overrides: Dictionary<string>, | ||
options?: SetNameOptions, | ||
) { | ||
options = { ...setNameDefaultOptions, ...options }; | ||
|
||
const newName = styler( | ||
defaultValue && isUnassigned(thing.language.default.name) ? defaultValue : thing.language.default.name, | ||
options.removeDuplicates, | ||
overrides, | ||
); | ||
|
||
// Check if the new name is not yet taken. | ||
if (newName && !options.existingNames?.has(newName)) { | ||
options.existingNames?.add(newName); | ||
thing.language.default.name = newName; | ||
} | ||
} | ||
|
||
export function isUnassigned(value: string) { | ||
return !value || value.indexOf("·") > -1; | ||
} |
This file contains 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