Skip to content

Commit

Permalink
Flatten imports (#30)
Browse files Browse the repository at this point in the history
* Minor performance improvements

* Flatten utils to be directly imported
  • Loading branch information
BenBaryoPX authored Oct 17, 2024
1 parent 2df5876 commit bf4dbfa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
12 changes: 7 additions & 5 deletions src/arborist.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ const Arborist = class {
* @return {ASTNode}
*/
_getCorrectTargetForDeletion(startNode) {
const relevantTypes = ['ExpressionStatement', 'UnaryExpression', 'UpdateExpression'];
const relevantClauses = ['consequent', 'alternate'];
let currentNode = startNode;
while (
['ExpressionStatement', 'UnaryExpression', 'UpdateExpression'].includes(currentNode?.parentNode?.type) ||
while (relevantTypes.includes(currentNode?.parentNode?.type) ||
(currentNode.parentNode.type === 'VariableDeclaration' &&
(currentNode.parentNode.declarations.length === 1 ||
!currentNode.parentNode.declarations.filter(d => d.nodeId !== currentNode.nodeId && !d.isMarked).length)
!currentNode.parentNode.declarations.filter(d => d !== currentNode && !d.isMarked).length)
)) currentNode = currentNode.parentNode;
if (['consequent', 'alternate'].includes(currentNode.parentKey)) currentNode.isEmpty = true;
if (relevantClauses.includes(currentNode.parentKey)) currentNode.isEmpty = true;
return currentNode;
}

Expand Down Expand Up @@ -90,7 +91,8 @@ const Arborist = class {
} else {
for (const targetNodeId of this.markedForDeletion) {
try {
const targetNode = this.ast.find(n => n.nodeId === targetNodeId);
let targetNode = this.ast[targetNodeId];
targetNode = targetNode.nodeId === targetNodeId ? targetNode : this.ast.find(n => n.nodeId === targetNodeId);
if (targetNode) {
const parent = targetNode.parentNode;
if (parent[targetNode.parentKey] === targetNode) {
Expand Down
8 changes: 3 additions & 5 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export const utils = {
applyIteratively: (await import('./applyIteratively.js')).applyIteratively,
logger: (await import('./logger.js')).logger,
treeModifier: (await import('./treeModifier.js')).treeModifier,
};
export * from './applyIteratively.js';
export * from './logger.js';
export * from './treeModifier.js';
4 changes: 3 additions & 1 deletion tests/functionality.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ describe('Functionality tests', () => {
'generateCode',
'generateFlatAST',
'parseCode',
'utils',
'applyIteratively',
'logger',
'treeModifier',
];
const flast = await import(path.resolve(__dirname + '/../src/index.js'));
for (const importName of availableImports) {
Expand Down
46 changes: 23 additions & 23 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import assert from 'node:assert';
import {utils} from '../src/index.js';
import {describe, it} from 'node:test';
import {treeModifier, applyIteratively, logger} from '../src/index.js';

describe('Utils tests: treeModifier', () => {
it(`Verify treeModifier sets a generic function name`, () => {
const expectedFuncName = 'func';
const generatedFunc = utils.treeModifier(() => {}, () => {});
const generatedFunc = treeModifier(() => {}, () => {});
assert.equal(generatedFunc.name, expectedFuncName, `The default name of the generated function does not match`);
});
it(`Verify treeModifier sets the function's name properly`, () => {
const expectedFuncName = 'expectedFuncName';
const generatedFunc = utils.treeModifier(() => {}, () => {}, expectedFuncName);
const generatedFunc = treeModifier(() => {}, () => {}, expectedFuncName);
assert.equal(generatedFunc.name, expectedFuncName, `The name of the generated function does not match`);
});
});
Expand All @@ -20,15 +20,15 @@ describe('Utils tests: applyIteratively', () => {
const expectedOutput = code;
const f = n => n.type === 'Program';
const m = (n, arb) => arb.markNode(n);
const generatedFunc = utils.treeModifier(f, m);
const result = utils.applyIteratively(code, [generatedFunc]);
const generatedFunc = treeModifier(f, m);
const result = applyIteratively(code, [generatedFunc]);

assert.equal(result, expectedOutput, `Result does not match expected output`);
});
it('Verify applyIteratively catches a critical exception', () => {
const code = `a`;
// noinspection JSCheckFunctionSignatures
const result = utils.applyIteratively(code, {length: 4});
const result = applyIteratively(code, {length: 4});
assert.equal(result, code, `Result does not match expected output`);
});
it('Verify applyIteratively works as expected', () => {
Expand All @@ -44,39 +44,39 @@ describe('Utils tests: applyIteratively', () => {
type: 'Literal',
value: replacements[n.value],
});
const generatedFunc = utils.treeModifier(f, m);
result = utils.applyIteratively(result, [generatedFunc]);
const generatedFunc = treeModifier(f, m);
result = applyIteratively(result, [generatedFunc]);

assert.equal(result, expectedOutput, `Result does not match expected output`);
});
});
describe('Utils tests: logger', () => {
it(`Verify logger sets the log level to DEBUG properly`, () => {
const expectedLogLevel = utils.logger.logLevels.DEBUG;
utils.logger.setLogLevelDebug();
assert.equal(utils.logger.currentLogLevel, expectedLogLevel, `The log level DEBUG was not set properly`);
const expectedLogLevel = logger.logLevels.DEBUG;
logger.setLogLevelDebug();
assert.equal(logger.currentLogLevel, expectedLogLevel, `The log level DEBUG was not set properly`);
});
it(`Verify logger sets the log level to NONE properly`, () => {
const expectedLogLevel = utils.logger.logLevels.NONE;
utils.logger.setLogLevelNone();
assert.equal(utils.logger.currentLogLevel, expectedLogLevel, `The log level NONE was not set properly`);
const expectedLogLevel = logger.logLevels.NONE;
logger.setLogLevelNone();
assert.equal(logger.currentLogLevel, expectedLogLevel, `The log level NONE was not set properly`);
});
it(`Verify logger sets the log level to LOG properly`, () => {
const expectedLogLevel = utils.logger.logLevels.LOG;
utils.logger.setLogLevelLog();
assert.equal(utils.logger.currentLogLevel, expectedLogLevel, `The log level LOG was not set properly`);
const expectedLogLevel = logger.logLevels.LOG;
logger.setLogLevelLog();
assert.equal(logger.currentLogLevel, expectedLogLevel, `The log level LOG was not set properly`);
});
it(`Verify logger sets the log level to ERROR properly`, () => {
const expectedLogLevel = utils.logger.logLevels.ERROR;
utils.logger.setLogLevelError();
assert.equal(utils.logger.currentLogLevel, expectedLogLevel, `The log level ERROR was not set properly`);
const expectedLogLevel = logger.logLevels.ERROR;
logger.setLogLevelError();
assert.equal(logger.currentLogLevel, expectedLogLevel, `The log level ERROR was not set properly`);
});
it(`Verify logger sets the log function properly`, () => {
const expectedLogFunc = () => 'test';
utils.logger.setLogFunc(expectedLogFunc);
assert.equal(utils.logger.logFunc, expectedLogFunc, `The log function was not set properly`);
logger.setLogFunc(expectedLogFunc);
assert.equal(logger.logFunc, expectedLogFunc, `The log function was not set properly`);
});
it(`Verify logger throws an error when setting an unknown log level`, () => {
assert.throws(() => utils.logger.setLogLevel(0), Error, `An error was not thrown when setting an unknown log level`);
assert.throws(() => logger.setLogLevel(0), Error, `An error was not thrown when setting an unknown log level`);
});
});

0 comments on commit bf4dbfa

Please sign in to comment.