Skip to content

Commit

Permalink
feat: ignore namespace collision of file deleted files (#2948)
Browse files Browse the repository at this point in the history
* feat: ignore namespace collision of the file reserving the namespace was deleted
  • Loading branch information
barak007 authored Mar 19, 2024
1 parent b76baa6 commit 530e627
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
14 changes: 5 additions & 9 deletions packages/core/src/helpers/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface CreateNamespaceOptions {
strict: boolean,
namespace: string,
filePath: string,
usedBy?: string
usedByMap: Map<string, string>
) => string;
hashSeparator?: string;
strict?: boolean;
Expand Down Expand Up @@ -75,8 +75,9 @@ export function defaultNoMatchHandler(
strict: boolean,
ns: string,
stylesheetPath: string,
usedBy?: string
usedByMap: Map<string, string>
): string {
const usedBy = usedByMap.get(ns);
throw new Error(
`Could not create namespace for:\n${stylesheetPath}\nthe last valid namespace tried was ${JSON.stringify(
ns
Expand Down Expand Up @@ -146,16 +147,11 @@ export function createNamespaceStrategy(options: CreateNamespaceOptions) {
if (usedBy === stylesheetPath) {
return finalNamespace;
} else if (strict) {
return handleNoMatch(strict, finalNamespace, stylesheetPath, usedBy);
return handleNoMatch(strict, finalNamespace, stylesheetPath, usedNamespaces);
}
i++;
}

return handleNoMatch(
strict,
finalNamespace,
stylesheetPath,
usedNamespaces.get(finalNamespace)
);
return handleNoMatch(strict, finalNamespace, stylesheetPath, usedNamespaces);
};
}
14 changes: 12 additions & 2 deletions packages/core/test/helpers/namespace.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ describe('createNamespaceStrategy', () => {
it('should throw when no unique namespace can be generated and hash slice size is larger then hash length', () => {
function getErrorMessage() {
try {
defaultNoMatchHandler(false, 'x-1', '/package/x2.st.css', '/package/x1.st.css');
defaultNoMatchHandler(
false,
'x-1',
'/package/x2.st.css',
new Map([['x-1', '/package/x1.st.css']])
);
} catch (e) {
return (e as Error).message;
}
Expand All @@ -140,7 +145,12 @@ describe('createNamespaceStrategy', () => {
it('should throw when no unique namespace can be generated in strict mode', () => {
function getErrorMessage() {
try {
defaultNoMatchHandler(true, 'x', '/package/x1.st.css', '/package/x.st.css');
defaultNoMatchHandler(
true,
'x',
'/package/x1.st.css',
new Map([['x', '/package/x.st.css']])
);
} catch (e) {
return (e as Error).message;
}
Expand Down
19 changes: 16 additions & 3 deletions packages/node/src/resolve-namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
defaultNoMatchHandler,
} from '@stylable/core';
import { dirname, relative } from 'path';
import { existsSync } from 'node:fs';
import { findPackageJson } from './find-package-json';

export function resolveNamespaceFactory(
Expand All @@ -16,7 +17,10 @@ export function resolveNamespaceFactory(

export const packageJsonLookupCache = new Map<string, string>();

export function createNamespaceStrategyNode(options: Partial<CreateNamespaceOptions> = {}) {
export function createNamespaceStrategyNode(
options: Partial<CreateNamespaceOptions> = {},
devMode?: boolean
) {
return createNamespaceStrategy({
normalizePath(packageRoot: string, stylesheetPath: string) {
return relative(packageRoot, stylesheetPath).replace(/\\/g, '/');
Expand All @@ -36,8 +40,17 @@ export function createNamespaceStrategyNode(options: Partial<CreateNamespaceOpti
dirPath: dirname(configPath),
};
},
handleNoMatch(strict, ns, stylesheetPath, usedBy) {
return strict ? defaultNoMatchHandler(strict, ns, stylesheetPath, usedBy) : ns;
handleNoMatch(strict, ns, stylesheetPath, usedByMap) {
if (strict && devMode) {
const usedBy = usedByMap.get(ns);
if (usedBy) {
if (!existsSync(usedBy)) {
usedByMap.set(ns, stylesheetPath);
return ns;
}
}
}
return strict ? defaultNoMatchHandler(strict, ns, stylesheetPath, usedByMap) : ns;
},
hashSalt: '',
hashFragment: 'full',
Expand Down

0 comments on commit 530e627

Please sign in to comment.