Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRY createOrderedSrc and Tests #121

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions src/modules/utils/createOrderedSrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@ import {parseCode} from 'flast';

const largeNumber = 999e8;
const sortByNodeId = (a, b) => a.nodeId > b.nodeId ? 1 : b.nodeId > a.nodeId ? -1 : 0;
const funcStartRegexp = new RegExp('function[^(]*');

/**
* Add a name to a FunctionExpression.
* @param {ASTNode} n The target node
* @param {string} [name] The new name. Defaults to 'func + n.nodeId'.
* @return {ASTNode} The new node with the name set
*/
function addNameToFE(n, name) {
name = name || 'func' + n.nodeId;
const funcSrc = '(' + n.src.replace(funcStartRegexp, 'function ' + name) + ');';
const newNode = parseCode(funcSrc);
if (newNode) {
newNode.nodeId = n.nodeId;
newNode.src = funcSrc;
return newNode;
}
}

/**
* Return the source code of the ordered nodes.
Expand All @@ -23,30 +41,13 @@ function createOrderedSrc(nodes, preserveOrder = false) {
}
} else if (n.callee.type === 'FunctionExpression') {
if (!preserveOrder) {
const altFuncName = (n.parentNode.type === 'VariableDeclarator' ? n.parentNode.id.name : 'func' + n.nodeId);
const funcStartRegexp = new RegExp('function[^(]*');
const funcSrc = n.callee?.id ? n.src : n.src.replace(funcStartRegexp, 'function ' + altFuncName);
const src = `(${funcSrc});`;
const newNode = parseCode(src);
if (newNode) {
newNode.nodeId = n.nodeId + largeNumber;
newNode.src = src;
nodes[i] = newNode;
}
const newNode = addNameToFE(n, n.parentNode?.id?.name);
newNode.nodeId = newNode.nodeId + largeNumber;
nodes[i] = newNode;
} else nodes[i] = n;
}
} else if (n.type === 'FunctionExpression' && !n.id) {
if (n.parentNode.type === 'VariableDeclarator') {
const funcStartRegexp = new RegExp('function[^(]*');
const funcSrc = n.src.replace(funcStartRegexp, 'function ' + n.parentNode.id.name);
const src = `(${funcSrc});`;
const parsedNode = parseCode(src);
if (parsedNode) {
parsedNode.nodeId = n.nodeId;
parsedNode.src = src;
nodes[i] = parsedNode;
}
}
} else if (n.type === 'FunctionExpression' && !n.id) {
nodes[i] = addNameToFE(n, n.parentNode?.id?.name);
}
n = nodes[i]; // In case the node was replaced
if (!parsedNodes.includes(n)) parsedNodes.push(n);
Expand Down
44 changes: 0 additions & 44 deletions src/modules/utils/logger.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/processors/augmentedArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ function replaceArrayWithStaticAugmentedVersion(arb) {
return arb;
}

export const preprocessors = [replaceArrayWithStaticAugmentedVersion, resolveFunctionToArray];
export const preprocessors = [replaceArrayWithStaticAugmentedVersion, resolveFunctionToArray.default];
export const postprocessors = [];
80 changes: 80 additions & 0 deletions tests/modules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1057,3 +1057,83 @@ describe('UTILS: createNewNode', async () => {
});

});
describe('UTILS: createOrderedSrc', async () => {
const targetModule = (await import('../src/modules/utils/createOrderedSrc.js')).createOrderedSrc;
it('TP-1: Re-order nodes', () => {
const code = 'a; b;';
const expected = `a\nb\n`;
const ast = generateFlatAST(code);
const targetNodes = [
4, // b()
2, // a()
];
const result = targetModule(targetNodes.map(n => ast[n]));
assert.deepStrictEqual(result, expected);
});
it('TP-2: Wrap calls in expressions', () => {
const code = 'a();';
const expected = `a();\n`;
const ast = generateFlatAST(code);const targetNodes = [
2, // a()
];
const result = targetModule(targetNodes.map(n => ast[n]));
assert.deepStrictEqual(result, expected);
});
it('TP-3: Push IIFEs to the end in order', () => {
const code = '(function(a){})(); a(); (function(b){})(); b();';
const expected = `a();\nb();\n(function(a){})();\n(function(b){})();\n`;
const ast = generateFlatAST(code);
const targetNodes = [
10, // (function(b){})()
15, // b()
7, // a()
2, // (function(a){})()
];
const result = targetModule(targetNodes.map(n => ast[n]));
assert.deepStrictEqual(result, expected);
});
it('TP-4: Add dynamic name to IIFEs', () => {
const code = '!function(a){}(); a();';
const expected = `a();\n(function func3(a){}());\n`;
const ast = generateFlatAST(code);const targetNodes = [
3, // function(a){}()
8, // a()
];
const result = targetModule(targetNodes.map(n => ast[n]));
assert.deepStrictEqual(result, expected);
});
it('TP-5: Add variable name to IIFEs', () => {
const code = 'const b = function(a){}(); a();';
const expected = `a();\n(function b(a){}());\n`;
const ast = generateFlatAST(code);const targetNodes = [
4, // function(a){}()
9, // a()
];
const result = targetModule(targetNodes.map(n => ast[n]));
assert.deepStrictEqual(result, expected);
});
it(`TP-6: Preserve node order`, () => {
const code = '(function(a){})(); a(); (function(b){})(); b();';
const expected = `(function(a){})();\na();\n(function(b){})();\nb();\n`;
const ast = generateFlatAST(code);
const targetNodes = [
10, // (function(b){})()
7, // a()
15, // b()
2, // (function(a){})()
];
const result = targetModule(targetNodes.map(n => ast[n]), true);
assert.deepStrictEqual(result, expected);
});
it(`TP-7: Standalone FEs`, () => {
const code = '~function(iife1){}();~function(iife2){}();';
const expected = `(function func4(iife1){});\n(function func10(iife2){});\n`;
const ast = generateFlatAST(code);
const targetNodes = [
10, // function(iife2){}
4, // function(iife1){}
];
const result = targetModule(targetNodes.map(n => ast[n]), true);
assert.deepStrictEqual(result, expected);
});
});
Loading