Skip to content

Commit

Permalink
fix: module names now account for callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
gabidobo committed Sep 17, 2022
1 parent 88a09ec commit fc02b67
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 19 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

64 changes: 46 additions & 18 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,49 @@ export const getModuleNameFromLocation = (location, allowURLs) => {
return 'root';
};

/** Reduce mod1>mod1>mod2>mod2>mod2 to mod1>mod2 */
export const compactifyModules = (modules) => {
let currentComponent;
const compactModules = [];

modules.forEach((module) => {
if (currentComponent !== module) {
currentComponent = module;
compactModules.push(module);
}
});

return compactModules;
};

/** Truncate to the last segment that was started from root */
/** For ex: root>mod1>mod2>root>mod3 should translate to mod3 */
/** Also fold resulting modules to account for callback-type calls */
/** For ex: root>mod1>mod2>mod1>mod3>mod4>mod3 should translate to mod1>mod3 */
export const foldModules = (modules) => {
let foldedModules = [...modules];

const lastRootOccurrence = foldedModules.lastIndexOf('root');
if (lastRootOccurrence !== -1) {
foldedModules = foldedModules.slice(lastRootOccurrence + 1);
}

let currentIndex = 0;

while (currentIndex < foldedModules.length) {
const lastOccurrence = foldedModules.lastIndexOf(foldedModules[currentIndex]);
if (lastOccurrence !== -1) {
foldedModules = [
...foldedModules.slice(0, currentIndex),
...foldedModules.slice(lastOccurrence),
];
}
currentIndex += 1;
}

return foldedModules;
};

export const getCurrentModuleInfo = ({stack: stackInput, allowURLs = false} = {}) => {
try {
const stack = (stackInput || currentStack())
Expand Down Expand Up @@ -152,25 +195,10 @@ export const getCurrentModuleInfo = ({stack: stackInput, allowURLs = false} = {}
let isExtension = false;

if (modules.length) {
let currentComponent;
let compactModules = [];

// Reduce mod1>mod1>mod2>mod2>mod2 to mod1>mod2
modules.forEach((module) => {
if (currentComponent !== module) {
currentComponent = module;
compactModules.push(module);
}
});

// Truncate to the last segment that was started from root
// For ex: mod1>mod2>root>mod3 should translate to mod3
const lastRootOccurrence = compactModules.lastIndexOf('root');
if (lastRootOccurrence !== -1) {
compactModules = compactModules.slice(lastRootOccurrence + 1);
}
const compactModules = compactifyModules(modules);
const foldedModules = foldModules(compactModules);

name = compactModules.join('>') || 'root';
name = foldedModules.join('>') || 'root';

// Detect if any items in the stack are browser extensions
isExtension = !!modules.find(
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {SourceMapConsumer} from 'source-map-js';
import {
addSourceMap,
addTrustedModules,
compactifyModules,
foldModules,
getCurrentModuleInfo,
getModuleNameFromLocation,
getModulePermissions,
Expand Down Expand Up @@ -461,4 +463,33 @@ describe('module', () => {
).toBe('module-name');
});
});

describe('compactifyModules', () => {
test('should leave simple chain unchanged', () => {
const chain = ['modA', 'modB', 'modC'];
expect(compactifyModules(chain)).toStrictEqual(chain);
});

test('should make chain compact', () => {
const chain = ['modA', 'modB', 'modB', 'modB', 'modC', 'modC', 'modA', 'modA'];
expect(compactifyModules(chain)).toStrictEqual(['modA', 'modB', 'modC', 'modA']);
});
});

describe('foldModules', () => {
test('should only consider the latest root-based segment', () => {
const chain = ['root', 'modA', 'root', 'modB', 'modC', 'root', 'modD', 'modE'];
expect(foldModules(chain)).toStrictEqual(['modD', 'modE']);
});

test('should fold a chain', () => {
const chain = ['root', 'modA', 'root', 'modB', 'modC', 'modB', 'modD', 'modE', 'modD'];
expect(foldModules(chain)).toStrictEqual(['modB', 'modD']);
});

test('should fold a chain that starts and ends on same module', () => {
const chain = ['modA', 'modB', 'modC', 'modD', 'modA'];
expect(foldModules(chain)).toStrictEqual(['modA']);
});
});
});

0 comments on commit fc02b67

Please sign in to comment.