diff --git a/src/arborist.js b/src/arborist.js index dfd2a06..f71db97 100644 --- a/src/arborist.js +++ b/src/arborist.js @@ -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; } @@ -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) { diff --git a/src/utils/index.js b/src/utils/index.js index 737a7da..1338921 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -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, -}; \ No newline at end of file +export * from './applyIteratively.js'; +export * from './logger.js'; +export * from './treeModifier.js'; \ No newline at end of file diff --git a/tests/functionality.test.js b/tests/functionality.test.js index 2690adf..43e485c 100644 --- a/tests/functionality.test.js +++ b/tests/functionality.test.js @@ -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) { diff --git a/tests/utils.test.js b/tests/utils.test.js index d251229..928ddda 100644 --- a/tests/utils.test.js +++ b/tests/utils.test.js @@ -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`); }); }); @@ -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', () => { @@ -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`); }); }); \ No newline at end of file