Skip to content

Commit 140d3cd

Browse files
committed
feat: Show the identifier of the other block if a name collision occurs.
1 parent ec338bf commit 140d3cd

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

packages/@css-blocks/core/src/BlockParser/BlockFactory.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class BlockFactory {
3939
postcssImpl: typeof postcss;
4040
importer: Importer;
4141
configuration: ResolvedConfiguration;
42-
blockNames: ObjectDictionary<number>;
42+
blockNames: ObjectDictionary<string>;
4343
parser: BlockParser;
4444
preprocessors: Preprocessors;
4545
faultTolerant: boolean;
@@ -207,11 +207,11 @@ export class BlockFactory {
207207
}
208208

209209
// Ensure this block name is unique.
210-
const uniqueName = this.getUniqueBlockName(block.name, file.type === "ImportedCompiledCssFile");
210+
const uniqueName = this.getUniqueBlockName(block.name, block.identifier, file.type === "ImportedCompiledCssFile");
211211
if (uniqueName === null) {
212212
// For ImportedCompiledCssFiles, leave the name alone and add an error.
213213
block.addError(
214-
new CssBlockError("Block uses a name that has already been used! Check dependencies for conflicting block names.", {
214+
new CssBlockError(`Block uses a name that has already been used by ${this.blockNames[block.name]}`, {
215215
filename: block.identifier,
216216
}),
217217
);
@@ -442,15 +442,21 @@ export class BlockFactory {
442442
* @return The unique block name that is now registered with the BlockFactory, or null if
443443
* the name has already been registered and should not be overridden.
444444
*/
445-
getUniqueBlockName(name: string, doNotOverride = false): string | null {
445+
getUniqueBlockName(name: string, identifier: string, doNotOverride = false): string | null {
446446
if (!this.blockNames[name]) {
447-
this.blockNames[name] = 1;
447+
this.blockNames[name] = identifier;
448448
return name;
449449
}
450450
if (doNotOverride) {
451451
return null;
452452
}
453-
return `${name}-${++this.blockNames[name]}`;
453+
let i = 2;
454+
while (this.blockNames[`${name}-${i}`]) {
455+
i++;
456+
}
457+
name = `${name}-${i}`;
458+
this.blockNames[name] = identifier;
459+
return name;
454460
}
455461

456462
preprocessor(file: ImportedFile): Preprocessor {

0 commit comments

Comments
 (0)