Comparing version 2.0.0 to 2.0.2
{ | ||
"name": "flast", | ||
"version": "2.0.0", | ||
"version": "2.0.2", | ||
"description": "Flatten JS AST", | ||
@@ -22,3 +22,3 @@ "main": "src/index.js", | ||
], | ||
"author": "ben.baryo@perimeterx.com", | ||
"author": "Ben Baryo (ben.baryo@humansecurity.com)", | ||
"license": "MIT", | ||
@@ -25,0 +25,0 @@ "bugs": { |
@@ -29,10 +29,11 @@ import {generateCode, generateFlatAST} from './flast.js'; | ||
_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; | ||
@@ -89,8 +90,9 @@ } | ||
rootNode = rootNodeReplacement[1]; | ||
if (leadingComments.length) rootNode.leadingComments = (rootNode.leadingComments || []).concat(leadingComments); | ||
if (trailingComments.length) rootNode.trailingComments = (rootNode.trailingComments || []).concat(trailingComments); | ||
if (leadingComments.length && rootNode.leadingComments !== leadingComments) rootNode.leadingComments = (rootNode.leadingComments || []).concat(leadingComments); | ||
if (trailingComments.length && rootNode.trailingComments !== trailingComments) rootNode.trailingComments = (rootNode.trailingComments || []).concat(trailingComments); | ||
} 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) { | ||
@@ -97,0 +99,0 @@ const parent = targetNode.parentNode; |
@@ -1,5 +0,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'; |
@@ -114,2 +114,23 @@ import assert from 'node:assert'; | ||
}); | ||
it(`Verify comments aren't duplicated when replacing the root node`, () => { | ||
const code = `//comment1\nconst a = 1, b = 2;`; | ||
const expected = `//comment1\nconst a = 1;\nconst b = 2;`; | ||
const arb = new Arborist(code); | ||
const decls = []; | ||
arb.ast.forEach(n => { | ||
if (n.type === 'VariableDeclarator') { | ||
decls.push({ | ||
type: 'VariableDeclaration', | ||
kind: 'const', | ||
declarations: [n], | ||
}); | ||
} | ||
}); | ||
arb.markNode(arb.ast[0], { | ||
...arb.ast[0], | ||
body: decls, | ||
}); | ||
arb.applyChanges(); | ||
assert.equal(arb.script, expected); | ||
}); | ||
}); |
@@ -37,3 +37,5 @@ import path from 'node:path'; | ||
'parseCode', | ||
'utils', | ||
'applyIteratively', | ||
'logger', | ||
'treeModifier', | ||
]; | ||
@@ -40,0 +42,0 @@ const flast = await import(path.resolve(__dirname + '/../src/index.js')); |
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'; | ||
@@ -8,3 +8,3 @@ describe('Utils tests: treeModifier', () => { | ||
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`); | ||
@@ -14,3 +14,3 @@ }); | ||
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`); | ||
@@ -25,4 +25,4 @@ }); | ||
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]); | ||
@@ -34,3 +34,3 @@ assert.equal(result, expectedOutput, `Result does not match expected output`); | ||
// 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`); | ||
@@ -51,4 +51,4 @@ }); | ||
}); | ||
const generatedFunc = utils.treeModifier(f, m); | ||
result = utils.applyIteratively(result, [generatedFunc]); | ||
const generatedFunc = treeModifier(f, m); | ||
result = applyIteratively(result, [generatedFunc]); | ||
@@ -60,29 +60,29 @@ assert.equal(result, expectedOutput, `Result does not match expected output`); | ||
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`); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
56504
1065