From b5f53b05cef9c70315a42ebae58ac94d1412751b Mon Sep 17 00:00:00 2001 From: Ben Baryo Date: Sun, 13 Oct 2024 12:23:56 +0300 Subject: [PATCH] Initial ESM porting effort --- .eslintignore | 2 - .eslintrc.js | 39 -------- .github/workflows/node.js.yml | 4 +- eslint.config.js | 4 +- package.json | 3 +- src/arborist.js | 4 +- src/flast.js | 10 +-- src/index.js | 10 +-- src/types.js | 4 +- src/utils/applyIteratively.js | 10 +-- src/utils/index.js | 8 +- src/utils/logger.js | 4 +- src/utils/treeModifier.js | 2 +- tests/aboristTests.js | 155 -------------------------------- tests/arborist.test.js | 114 ++++++++++++++++++++++++ tests/functionality.test.js | 118 +++++++++++++++++++++++++ tests/functionalityTests.js | 161 ---------------------------------- tests/parsing.test.js | 48 ++++++++++ tests/parsingTests.js | 68 -------------- tests/tester.js | 31 ------- tests/utils.test.js | 82 +++++++++++++++++ tests/utilsTests.js | 32 ------- 22 files changed, 393 insertions(+), 520 deletions(-) delete mode 100644 .eslintignore delete mode 100644 .eslintrc.js delete mode 100644 tests/aboristTests.js create mode 100644 tests/arborist.test.js create mode 100644 tests/functionality.test.js delete mode 100644 tests/functionalityTests.js create mode 100644 tests/parsing.test.js delete mode 100644 tests/parsingTests.js delete mode 100644 tests/tester.js create mode 100644 tests/utils.test.js delete mode 100644 tests/utilsTests.js diff --git a/.eslintignore b/.eslintignore deleted file mode 100644 index 48fe356..0000000 --- a/.eslintignore +++ /dev/null @@ -1,2 +0,0 @@ -*tmp*/ -*tmp*.* \ No newline at end of file diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 2b0d438..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1,39 +0,0 @@ -module.exports = { - env: { - browser: true, - node: true, - commonjs: true, - es2021: true, - }, - extends: 'eslint:recommended', - parserOptions: { - ecmaVersion: 'latest', - }, - rules: { - indent: [ - 'error', - 'tab', - { - SwitchCase: 1 - }, - ], - 'linebreak-style': [ - 'error', - 'unix', - ], - quotes: [ - 'error', - 'single', - { - allowTemplateLiterals: true - }, - ], - semi: [ - 'error', - 'always', - ], - 'no-empty': [ - 'off', - ], - }, -}; diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 8a1bb63..d22d731 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -16,7 +16,7 @@ jobs: strategy: matrix: - node-version: [16.x, 18.x, 20.x] + node-version: [18.x, 20.x] # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ steps: @@ -27,4 +27,4 @@ jobs: node-version: ${{ matrix.node-version }} cache: 'npm' - run: npm install - - run: npm test + - run: npm test:coverage diff --git a/eslint.config.js b/eslint.config.js index 6eb6227..39fdd00 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -13,7 +13,7 @@ const compat = new FlatCompat({ }); export default [{ - ignores: ["**/*tmp*/", "**/*tmp*.*"], + ignores: ["**/*tmp*/", "**/*tmp*.*", "eslint.config.js", "node_modules/"], }, ...compat.extends("eslint:recommended"), { languageOptions: { globals: { @@ -23,7 +23,7 @@ export default [{ }, ecmaVersion: "latest", - sourceType: "commonjs", + sourceType: "module", }, rules: { diff --git a/package.json b/package.json index 9cbbb4b..7eeb823 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "scripts": { "lint": "eslint .", "prepare": "husky", - "test": "node tests/tester.js" + "test": "node --test", + "test:coverage": "node --test --experimental-test-coverage" }, "repository": { "type": "git", diff --git a/src/arborist.js b/src/arborist.js index ff0e808..ef07b3d 100644 --- a/src/arborist.js +++ b/src/arborist.js @@ -1,4 +1,4 @@ -const {generateCode, generateFlatAST,} = require('./flast'); +import {generateCode, generateFlatAST} from './flast.js'; const Arborist = class { /** @@ -169,6 +169,6 @@ const Arborist = class { } }; -module.exports = { +export { Arborist, }; \ No newline at end of file diff --git a/src/flast.js b/src/flast.js index c6ccc05..bd43896 100644 --- a/src/flast.js +++ b/src/flast.js @@ -1,7 +1,7 @@ -const {parse} = require('espree'); -const {generate, attachComments} = require('escodegen'); -const estraverse = require('estraverse'); -const {analyze} = require('eslint-scope'); +import {parse} from 'espree'; +import {generate, attachComments} from 'escodegen'; +import estraverse from 'estraverse'; +import {analyze} from 'eslint-scope'; const ecmaVersion = 'latest'; const sourceType = 'module'; @@ -288,7 +288,7 @@ async function generateFlatASTAsync(inputCode, opts = {}) { return Promise.all(promises).then(() => tree); } -module.exports = { +export { estraverse, extractNodesFromRoot, generateCode, diff --git a/src/index.js b/src/index.js index f9f17c5..160d602 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,4 @@ -module.exports = { - ...require('./flast'), - ...require('./arborist'), - ...require('./types'), - utils: require('./utils'), -}; \ No newline at end of file +export * from './flast.js'; +export * from './arborist.js'; +export * from './types.js'; +export * from './utils/index.js'; \ No newline at end of file diff --git a/src/types.js b/src/types.js index d1b3fa8..7836095 100644 --- a/src/types.js +++ b/src/types.js @@ -1,4 +1,4 @@ -const {Scope} = require('eslint-scope'); +import {Scope} from 'eslint-scope'; /** * @typedef ASTNode @@ -88,7 +88,7 @@ class ASTNode {} */ class ASTScope extends Scope {} -module.exports = { +export { ASTNode, ASTScope, }; \ No newline at end of file diff --git a/src/utils/applyIteratively.js b/src/utils/applyIteratively.js index e8ae15e..e6e16bb 100644 --- a/src/utils/applyIteratively.js +++ b/src/utils/applyIteratively.js @@ -1,6 +1,6 @@ -const {Arborist} = require('../arborist'); -const logger = require('./logger'); -const {createHash} = require('node:crypto'); +import {Arborist} from '../arborist.js'; +import {logger} from './logger.js'; +import {createHash} from 'node:crypto'; const generateHash = str => createHash('sha256').update(str).digest('hex'); @@ -12,7 +12,7 @@ const generateHash = str => createHash('sha256').update(str).digest('hex'); * @param {number?} maxIterations (optional) Stop the loop after this many iterations at most. * @return {string} The possibly modified script. */ -function runLoop(script, funcs, maxIterations = 500) { +function applyIteratively(script, funcs, maxIterations = 500) { let scriptSnapshot = ''; let currentIteration = 0; let changesCounter = 0; @@ -62,4 +62,4 @@ function runLoop(script, funcs, maxIterations = 500) { return script; } -module.exports = runLoop; \ No newline at end of file +export {applyIteratively}; \ No newline at end of file diff --git a/src/utils/index.js b/src/utils/index.js index 7b85392..737a7da 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1,5 +1,5 @@ -module.exports = { - applyIteratively: require('./applyIteratively'), - logger: require('./logger'), - treeModifier: require('./treeModifier'), +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 diff --git a/src/utils/logger.js b/src/utils/logger.js index d76f894..4e9a995 100644 --- a/src/utils/logger.js +++ b/src/utils/logger.js @@ -35,10 +35,10 @@ const logger = { setLogLevelDebug() {this.setLogLevel(this.logLevels.DEBUG);}, setLogLevelLog() {this.setLogLevel(this.logLevels.LOG);}, setLogLevelError() {this.setLogLevel(this.logLevels.ERROR);}, - + setLogFunc(newLogfunc) { this.logFunc = newLogfunc; }, }; -module.exports = logger; \ No newline at end of file +export {logger}; \ No newline at end of file diff --git a/src/utils/treeModifier.js b/src/utils/treeModifier.js index 4a6cc94..c8271fe 100644 --- a/src/utils/treeModifier.js +++ b/src/utils/treeModifier.js @@ -20,4 +20,4 @@ function treeModifier(filterFunc, modFunc, funcName) { return func; } -module.exports = treeModifier; \ No newline at end of file +export {treeModifier}; \ No newline at end of file diff --git a/tests/aboristTests.js b/tests/aboristTests.js deleted file mode 100644 index a5de576..0000000 --- a/tests/aboristTests.js +++ /dev/null @@ -1,155 +0,0 @@ -const assert = require('node:assert'); -const {Arborist, generateFlatAST} = require(__dirname + '/../src/index'); -module.exports = [ - { - enabled: true, - name: 'Node replacement', - description: 'Verify node replacement works as expected.', - run() { - const code = `console.log('Hello' + ' ' + 'there!');`; - const expectedOutput = `console.log('General' + ' ' + 'Kenobi');`; - const replacements = { - 'Hello': 'General', - 'there!': 'Kenobi', - }; - const arborist = new Arborist(code); - arborist.ast.filter(n => n.type === 'Literal' && replacements[n.value]) - .forEach(n => arborist.markNode(n, { - type: 'Literal', - value: replacements[n.value], - raw: `'${replacements[n.value]}'`, - })); - const numberOfChangesMade = arborist.applyChanges(); - const result = arborist.script; - - assert.equal(result, expectedOutput, - `Result does not match expected output.`); - assert.equal(numberOfChangesMade, Object.keys(replacements).length, - `The number of actual replacements does not match expectations.`); - return true; - }, - }, - { - enabled: true, - name: 'Root node replacement', - description: 'Verify the root node replacement works as expected.', - run() { - const code = `a;`; - const expectedOutput = `b`; - const arborist = new Arborist(code); - arborist.markNode(arborist.ast[0], { - type: 'Identifier', - name: 'b', - }); - arborist.applyChanges(); - const result = arborist.script; - - assert.equal(result, expectedOutput, - `Result does not match expected output.`); - return true; - }, - }, - { - enabled: true, - name: 'Root node replacement - TN', - description: 'Verify only the root node is replaced .', - run() { - const code = `a;b;`; - const expectedOutput = `c`; - const arborist = new Arborist(code); - arborist.markNode(arborist.ast[4], { - type: 'Identifier', - name: 'v', - }); - arborist.markNode(arborist.ast[0], { - type: 'Identifier', - name: 'c', - }); - arborist.applyChanges(); - const result = arborist.script; - - assert.equal(result, expectedOutput, - `Result does not match expected output.`); - return true; - }, - }, - { - enabled: true, - name: 'Node deletion', - description: 'Verify node deletion works as expected.', - run() { - const code = `const a = ['There', 'can', 'be', 'only', 'one'];`; - const expectedOutput = `const a = ['one'];`; - const literalToSave = 'one'; - const arborist = new Arborist(code); - arborist.ast.filter(n => n.type === 'Literal' && n.value !== literalToSave).forEach(n => arborist.markNode(n)); - const numberOfChangesMade = arborist.applyChanges(); - const expectedNumberOfChanges = 4; - const result = arborist.script; - - assert.equal(result, expectedOutput, - `Result does not match expected output.`); - assert.equal(numberOfChangesMade, expectedNumberOfChanges, - `The number of actual changes does not match expectations.`); - }, - }, - { - enabled: true, - name: 'Arborist accepts a valid script on instantiation', - description: `Verify a valid script can be used to initialize an arborist instance.`, - run() { - const code = `console.log('test');`; - let error = ''; - let arborist; - const expectedArraySize = generateFlatAST(code).length; - try { - arborist = new Arborist(code); - } catch (e) { - error = e.message; - } - assert.ok(arborist?.script, - `Arborist failed to instantiate. ${error ? 'Error: ' + error : ''}`); - assert(!error, - `Arborist instantiated with an error: ${error}`); - assert.equal(arborist.script, code, - `Arborist script did not match initialization argument.`); - assert.equal(arborist.ast.length, expectedArraySize, - `Arborist did not generate a flat AST array.`); - }, - }, - { - enabled: true, - name: 'Arborist accepts a valid flat AST array on instantiation', - description: `Verify a valid AST array can be used to initialize an arborist instance.`, - run() { - const code = `console.log('test');`; - const ast = generateFlatAST(code); - let error = ''; - let arborist; - try { - arborist = new Arborist(ast); - } catch (e) { - error = e.message; - } - assert.ok(arborist?.ast?.length, - `Arborist failed to instantiate. ${error ? 'Error: ' + error : ''}`); - assert.equal(error, '', - `Arborist instantiated with an error: ${error}`); - assert.deepEqual(arborist.ast, ast, - `Arborist ast array did not match initialization argument.`); - }, - }, - { - enabled: true, - name: `Invalid changes are not applied`, - description: `Verify a valid AST array can be used to initialize an arborist instance.`, - run() { - const code = `console.log('test');`; - const arborist = new Arborist(code); - arborist.markNode(arborist.ast.find(n => n.type === 'Literal'), {type: 'EmptyStatement'}); - arborist.markNode(arborist.ast.find(n => n.name === 'log'), {type: 'EmptyStatement'}); - arborist.applyChanges(); - assert.equal(arborist.script, code); - }, - }, -]; \ No newline at end of file diff --git a/tests/arborist.test.js b/tests/arborist.test.js new file mode 100644 index 0000000..75a40a9 --- /dev/null +++ b/tests/arborist.test.js @@ -0,0 +1,114 @@ +import assert from 'node:assert'; +import {describe, it} from 'node:test'; +import {Arborist, generateFlatAST} from '../src/index.js'; + +describe('Arborist tests', () => { + it('Verify node replacement works as expected', () => { + const code = `console.log('Hello' + ' ' + 'there!');`; + const expectedOutput = `console.log('General' + ' ' + 'Kenobi');`; + const replacements = { + 'Hello': 'General', + 'there!': 'Kenobi', + }; + const arborist = new Arborist(code); + arborist.ast.filter(n => n.type === 'Literal' && replacements[n.value]) + .forEach(n => arborist.markNode(n, { + type: 'Literal', + value: replacements[n.value], + raw: `'${replacements[n.value]}'`, + })); + const numberOfChangesMade = arborist.applyChanges(); + const result = arborist.script; + + assert.equal(result, expectedOutput, `Result does not match expected output.`); + assert.equal(numberOfChangesMade, Object.keys(replacements).length, `The number of actual replacements does not match expectations.`); + }); + it('Verify the root node replacement works as expected', () => { + const code = `a;`; + const expectedOutput = `b`; + const arborist = new Arborist(code); + arborist.markNode(arborist.ast[0], { + type: 'Identifier', + name: 'b', + }); + arborist.applyChanges(); + const result = arborist.script; + + assert.equal(result, expectedOutput, `Result does not match expected output.`); + }); + it('Verify only the root node is replaced', () => { + const code = `a;b;`; + const expectedOutput = `c`; + const arborist = new Arborist(code); + arborist.markNode(arborist.ast[4], { + type: 'Identifier', + name: 'v', + }); + arborist.markNode(arborist.ast[0], { + type: 'Identifier', + name: 'c', + }); + arborist.applyChanges(); + const result = arborist.script; + + assert.equal(result, expectedOutput, `Result does not match expected output.`); + }); + it('Verify node deletion works as expected', () => { + const code = `const a = ['There', 'can', 'be', 'only', 'one'];`; + const expectedOutput = `const a = ['one'];`; + const literalToSave = 'one'; + const arborist = new Arborist(code); + arborist.ast.filter(n => n.type === 'Literal' && n.value !== literalToSave).forEach(n => arborist.markNode(n)); + const numberOfChangesMade = arborist.applyChanges(); + const expectedNumberOfChanges = 4; + const result = arborist.script; + + assert.equal(result, expectedOutput, `Result does not match expected output.`); + assert.equal(numberOfChangesMade, expectedNumberOfChanges, `The number of actual changes does not match expectations.`); + }); + it('Verify the correct node is targeted for deletion', () => { + const code = `var a = 1;`; + const expectedResult = ``; + const arborist = new Arborist(code); + arborist.markNode(arborist.ast.find(n => n.type === 'VariableDeclarator')); + arborist.applyChanges(); + assert.equal(arborist.script, expectedResult, 'An incorrect node was targeted for deletion.'); + }); + it('Verify a valid script can be used to initialize an arborist instance', () => { + const code = `console.log('test');`; + let error = ''; + let arborist; + const expectedArraySize = generateFlatAST(code).length; + try { + arborist = new Arborist(code); + } catch (e) { + error = e.message; + } + assert.ok(arborist?.script, `Arborist failed to instantiate. ${error ? 'Error: ' + error : ''}`); + assert.ok(!error, `Arborist instantiated with an error: ${error}`); + assert.equal(arborist.script, code, `Arborist script did not match initialization argument.`); + assert.equal(arborist.ast.length, expectedArraySize, `Arborist did not generate a flat AST array.`); + }); + it('Verify a valid AST array can be used to initialize an arborist instance', () => { + const code = `console.log('test');`; + const ast = generateFlatAST(code); + let error = ''; + let arborist; + try { + arborist = new Arborist(ast); + } catch (e) { + error = e.message; + } + assert.ok(arborist?.ast?.length, `Arborist failed to instantiate. ${error ? 'Error: ' + error : ''}`); + assert.equal(error, '', `Arborist instantiated with an error: ${error}`); + assert.deepEqual(arborist.ast, ast, `Arborist ast array did not match initialization argument.`); + }); + it('Verify invalid changes are not applied', () => { + const code = `console.log('test');`; + const arborist = new Arborist(code); + arborist.markNode(arborist.ast.find(n => n.type === 'Literal'), {type: 'EmptyStatement'}); + arborist.markNode(arborist.ast.find(n => n.name === 'log'), {type: 'EmptyStatement'}); + arborist.applyChanges(); + assert.equal(arborist.script, code, 'Invalid changes were applied.'); + }); +}); \ No newline at end of file diff --git a/tests/functionality.test.js b/tests/functionality.test.js new file mode 100644 index 0000000..2690adf --- /dev/null +++ b/tests/functionality.test.js @@ -0,0 +1,118 @@ +import path from 'node:path'; +import assert from 'node:assert'; +import {describe, it} from 'node:test'; +import {fileURLToPath} from 'node:url'; +import {generateFlatAST, generateCode} from '../src/index.js'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +describe('Functionality tests', () => { + it('Verify the code breakdown generates the expected nodes by checking the properties of the generated ASTNodes', () => { + const code = `a=3`; + const ast = generateFlatAST(code); + const expectedBreakdown = [ + {nodeId: 0, type: 'Program', start: 0, end: 3, src: 'a=3', parentNode: null, parentKey: ''}, + {nodeId: 1, type: 'ExpressionStatement', start: 0, end: 3, src: 'a=3', parentKey: 'body'}, + {nodeId: 2, type: 'AssignmentExpression', start: 0, end: 3, src: 'a=3', operator: '=', parentKey: 'expression'}, + {nodeId: 3, type: 'Identifier', start: 0, end: 1, src: 'a', parentKey: 'left'}, + {nodeId: 4, type: 'Literal', start: 2, end: 3, src: '3', value: 3, raw: '3', parentKey: 'right'}, + ]; + expectedBreakdown.forEach(node => { + const parsedNode = ast[node.nodeId]; + for (const [k, v] of Object.entries(node)) { + assert.equal(v, parsedNode[k], `Node #${parsedNode[k]} parsed wrong on key '${k}'`); + } + }); + }); + it('Verify the expected functions and classes can be imported', async () => { + const availableImports = [ + 'Arborist', + 'ASTNode', + 'ASTScope', + 'estraverse', + 'generateCode', + 'generateFlatAST', + 'parseCode', + 'utils', + ]; + const flast = await import(path.resolve(__dirname + '/../src/index.js')); + for (const importName of availableImports) { + assert.ok(importName in flast, `Failed to import "${importName}"`); + } + }); + it('Verify the code breakdown generates the expected nodes by checking the number of nodes for each expected type', () => { + const code = `console.log('hello' + ' ' + 'there');`; + const ast = generateFlatAST(code); + const expectedBreakdown = { + Program: 1, + ExpressionStatement: 1, + CallExpression: 1, + MemberExpression: 1, + Identifier: 2, + BinaryExpression: 2, + Literal: 3, + }; + const expectedNumberOfNodes = 11; + assert.equal(ast.length, expectedNumberOfNodes, `Unexpected number of nodes`); + for (const nodeType of Object.keys(expectedBreakdown)) { + const numberOfNodes = ast.filter(n => n.type === nodeType).length; + assert.equal(numberOfNodes, expectedBreakdown[nodeType], `Wrong number of nodes for '${nodeType}' node type`); + } + }); + it('Verify the AST can be parsed and regenerated into the same code', () => { + const code = `console.log('hello' + ' ' + 'there');`; + const ast = generateFlatAST(code); + const regeneratedCode = generateCode(ast[0]); + assert.equal(regeneratedCode, code, `Original code did not regenerate back to the same source.`); + }); + it(`Verify generateFlatAST's detailed option works as expected`, () => { + const code = `var a = [1]; a[0];`; + const noDetailsAst = generateFlatAST(code, {detailed: false}); + const [noDetailsVarDec, noDetailsVarRef] = noDetailsAst.filter(n => n.type === 'Identifier'); + assert.equal(noDetailsVarDec.references || noDetailsVarRef.declNode || noDetailsVarRef.scope, undefined, + `Flat AST generated with details despite 'detailed' option set to false.`); + + const noSrcAst = generateFlatAST(code, {includeSrc: false}); + assert.equal(noSrcAst.find(n => n.src !== undefined), null, `Flat AST generated with src despite 'includeSrc' option set to false.`); + + const detailedAst = generateFlatAST(code, {detailed: true}); + const [detailedVarDec, detailedVarRef] = detailedAst.filter(n => n.type === 'Identifier'); + assert.ok(detailedVarDec.parentNode && detailedVarDec.childNodes && detailedVarDec.references && + detailedVarRef.declNode && detailedVarRef.nodeId && detailedVarRef.scope && detailedVarRef.src, + `Flat AST missing details despite 'detailed' option set to true.`); + + const detailedNoSrcAst = generateFlatAST(code, {detailed: true, includeSrc: false}); + assert.equal(detailedNoSrcAst[0].src, undefined, `Flat AST includes details despite 'detailed' option set to true and 'includeSrc' option set to false.`); + }); + it(`Verify a script is parsed in "sloppy mode" if strict mode is restricting parsing`, () => { + const code = `let a; delete a;`; + let ast = []; + let error = ''; + try { + ast = generateFlatAST(code); + } catch (e) { + error = e.message; + } + assert.ok(ast.length, `Script was not parsed. Got the error "${error}"`); + }); + it(`Verify a script is only parsed in its selected sourceType`, () => { + const code = `let a; delete a;`; + let unparsedAst = []; + let parsedAst = []; + let unparsedError = ''; + let parsedError = ''; + try { + unparsedAst = generateFlatAST(code, {alernateSourceTypeOnFailure: false}); + } catch (e) { + unparsedError = e.message; + } + try { + parsedAst = generateFlatAST(code, {alernateSourceTypeOnFailure: true}); + } catch (e) { + parsedError = e.message; + } + assert.equal(unparsedAst.length, 0, `Script was not parsed.${unparsedError ? 'Error: ' + unparsedError : ''}`); + assert.ok(parsedAst.length, `Script was not parsed.${parsedError ? 'Error: ' + parsedError : ''}`); + }); +}); \ No newline at end of file diff --git a/tests/functionalityTests.js b/tests/functionalityTests.js deleted file mode 100644 index b0014ad..0000000 --- a/tests/functionalityTests.js +++ /dev/null @@ -1,161 +0,0 @@ -const assert = require('node:assert'); -const {generateFlatAST, generateCode} = require(__dirname + '/../src/index'); -module.exports = [ - { - enabled: true, - name: 'ASTNode structure integrity', - description: 'Verify the code breakdown generates the expected nodes by checking the properties of the generated ASTNodes.', - run() { - const code = `a=3`; - const ast = generateFlatAST(code); - const expectedBreakdown = [ - {nodeId: 0, type: 'Program', start: 0, end: 3, src: 'a=3', parentNode: null, parentKey: ''}, - {nodeId: 1, type: 'ExpressionStatement', start: 0, end: 3, src: 'a=3', parentKey: 'body'}, - {nodeId: 2, type: 'AssignmentExpression', start: 0, end: 3, src: 'a=3', operator: '=', parentKey: 'expression'}, - {nodeId: 3, type: 'Identifier', start: 0, end: 1, src: 'a', parentKey: 'left'}, - {nodeId: 4, type: 'Literal', start: 2, end: 3, src: '3', value: 3, raw: '3', parentKey: 'right'}, - ]; - expectedBreakdown.forEach(node => { - const parsedNode = ast[node.nodeId]; - for (const [k, v] of Object.entries(node)) { - assert.equal(v, parsedNode[k], - `Node #${parsedNode[k]} parsed wrong on key '${k}'`); - } - }); - }, - }, - { - enabled: true, - name: 'Verify Available Imports', - description: 'Verify the expected functions and classes can be imported.', - run() { - const {resolve} = require('node:path'); - const availableImports = [ - 'Arborist', - 'ASTNode', - 'ASTScope', - 'estraverse', - 'generateCode', - 'generateFlatAST', - 'parseCode', - 'utils', - ]; - function tryImporting(importName) { - const {[importName]: tempImport} = require(importSource); - return tempImport; - } - const importSource = resolve(__dirname + '/../src/index'); - for (const importName of availableImports) { - assert.ok(tryImporting(importName), `Failed to import "${importName}" from ${importSource}`); - } - }, - }, - { - enabled: true, - name: 'Number of nodes', - description: 'Verify the code breakdown generates the expected nodes by checking the number of nodes for each expected type.', - run() { - const code = `console.log('hello' + ' ' + 'there');`; - const ast = generateFlatAST(code); - const expectedBreakdown = { - Program: 1, - ExpressionStatement: 1, - CallExpression: 1, - MemberExpression: 1, - Identifier: 2, - BinaryExpression: 2, - Literal: 3, - }; - const expectedNumberOfNodes = 11; - assert.equal(ast.length, expectedNumberOfNodes, - `Unexpected number of nodes`); - for (const nodeType of Object.keys(expectedBreakdown)) { - const numberOfNodes = ast.filter(n => n.type === nodeType).length; - assert.equal(numberOfNodes, expectedBreakdown[nodeType], - `Wrong number of nodes for '${nodeType}' node type`); - } - }, - }, - { - enabled: true, - name: 'Parse and generate', - description: 'Verify the AST can be parsed and regenerated into the same code.', - run() { - const code = `console.log('hello' + ' ' + 'there');`; - const ast = generateFlatAST(code); - const regeneratedCode = generateCode(ast[0]); - assert.equal(regeneratedCode, code, - `Original code did not regenerate back to the same source.`); - }, - }, - { - enabled: true, - name: 'Options: detailed', - description: `Verify generateFlatAST's detailed option works as expected.`, - source: `var a = [1]; a[0];`, - run() { - const code = `var a = [1]; a[0];`; - const noDetailsAst = generateFlatAST(code, {detailed: false}); - const [noDetailsVarDec, noDetailsVarRef] = noDetailsAst.filter(n => n.type === 'Identifier'); - assert.equal(noDetailsVarDec.references || noDetailsVarRef.declNode || noDetailsVarRef.scope, undefined, - `Flat AST generated with details despite 'detailed' option set to false.`); - - const noSrcAst = generateFlatAST(code, {includeSrc: false}); - assert.equal(noSrcAst.find(n => n.src !== undefined), null, - `Flat AST generated with src despite 'includeSrc' option set to false.`); - - const detailedAst = generateFlatAST(code, {detailed: true}); - const [detailedVarDec, detailedVarRef] = detailedAst.filter(n => n.type === 'Identifier'); - assert.ok(detailedVarDec.parentNode && detailedVarDec.childNodes && detailedVarDec.references && - detailedVarRef.declNode && detailedVarRef.nodeId && detailedVarRef.scope && detailedVarRef.src, - `Flat AST missing details despite 'detailed' option set to true.`); - - const detailedNoSrcAst = generateFlatAST(code, {detailed: true, includeSrc: false}); - assert.equal(detailedNoSrcAst[0].src, undefined, - `Flat AST includes details despite 'detailed' option set to true and 'includeSrc' option set to false.`); - }, - }, - { - enabled: true, - name: 'Dynamic sourceType switching', - description: `Verify a script is parsed in "sloppy mode" if strict mode is restricting parsing.`, - run() { - const code = `let a; delete a;`; - let ast = []; - let error = ''; - try { - ast = generateFlatAST(code); - } catch (e) { - error = e.message; - } - assert.ok(ast.length, - `Script was not parsed. Got the error "${error}"`); - }, - }, - { - enabled: true, - name: 'Disable dynamic sourceType switching', - description: `Verify a script is only parsed in its selected sourceType.`, - run() { - const code = `let a; delete a;`; - let unparsedAst = []; - let parsedAst = []; - let unparsedError = ''; - let parsedError = ''; - try { - unparsedAst = generateFlatAST(code, {alernateSourceTypeOnFailure: false}); - } catch (e) { - unparsedError = e.message; - } - try { - parsedAst = generateFlatAST(code, {alernateSourceTypeOnFailure: true}); - } catch (e) { - parsedError = e.message; - } - assert.equal(unparsedAst.length, 0, - `Script was not parsed.${unparsedError ? 'Error: ' + unparsedError : ''}`); - assert.ok(parsedAst.length, - `Script was not parsed.${parsedError ? 'Error: ' + parsedError : ''}`); - }, - }, -]; \ No newline at end of file diff --git a/tests/parsing.test.js b/tests/parsing.test.js new file mode 100644 index 0000000..9d696fd --- /dev/null +++ b/tests/parsing.test.js @@ -0,0 +1,48 @@ +import assert from 'node:assert'; +import {describe, it} from 'node:test'; +import {generateFlatAST} from '../src/index.js'; + +describe('Parsing tests', () => { + it('Verify the function-expression-name scope is always replaced with its child scope', () => { + const code = ` +(function test(p) { + let i = 1; + i; +})();`; + const ast = generateFlatAST(code); + const testedScope = ast[0].allScopes[Object.keys(ast[0].allScopes).slice(-1)[0]]; + const expectedParentScopeType = 'function-expression-name'; + const expectedScopeType = 'function'; + // ast.slice(-1)[0].type is the last identifier in the code and should have the expected scope type + assert.equal(ast.slice(-1)[0].scope.type, expectedScopeType, `Unexpected scope`); + assert.equal(testedScope.type, expectedParentScopeType, `Tested scope is not the child of the correct scope`); + }); + it('Verify declNode references the local declaration correctly', () => { + const innerScopeVal = 'inner'; + const outerScopeVal = 'outer'; + const code = `var a = '${outerScopeVal}'; + if (true) { + let a = '${innerScopeVal}'; + console.log(a); + } + console.log(a);`; + const ast = generateFlatAST(code); + const [innerIdentifier, outerIdentifier] = ast.filter(n => n.type === 'Identifier' && n.parentNode.type === 'CallExpression'); + const innerValResult = innerIdentifier.declNode.parentNode.init.value; + const outerValResult = outerIdentifier.declNode.parentNode.init.value; + assert.equal(innerValResult, innerScopeVal, `Declaration node (inner scope) is incorrectly referenced.`); + assert.equal(outerValResult, outerScopeVal, `Declaration node (outer scope) is incorrectly referenced.`); + }); + it(`Verify a function's identifier isn't treated as a reference`, () => { + const code = `function a() { + var a; + }`; + const ast = generateFlatAST(code); + const funcId = ast.find(n => n.name ==='a' && n.parentNode.type === 'FunctionDeclaration'); + const varId = ast.find(n =>n.name ==='a' && n.parentNode.type === 'VariableDeclarator'); + const functionReferencesFound = !!funcId.references?.length; + const variableReferencesFound = !!varId.references?.length; + assert.ok(!functionReferencesFound, `References to a function were incorrectly found`); + assert.ok(!variableReferencesFound, `References to a variable were incorrectly found`); + }); +}); \ No newline at end of file diff --git a/tests/parsingTests.js b/tests/parsingTests.js deleted file mode 100644 index a46e3d6..0000000 --- a/tests/parsingTests.js +++ /dev/null @@ -1,68 +0,0 @@ -const assert = require('node:assert'); -const {generateFlatAST} = require('../src/index'); - -module.exports = [ - { - enabled: true, - name: 'Function expression namespace scope is exchanged for child scope.', - description: 'Verify the function-expression-name scope is always replaced with its child scope.', - run() { - const code = ` -(function test(p) { - let i = 1; - i; -})();`; - const ast = generateFlatAST(code); - const testedScope = ast[0].allScopes[Object.keys(ast[0].allScopes).slice(-1)[0]]; - const expectedParentScopeType = 'function-expression-name'; - const expectedScopeType = 'function'; - // ast.slice(-1)[0].type is the last identifier in the code and should have the expected scope type - assert.equal(ast.slice(-1)[0].scope.type, expectedScopeType, - `Unexpected scope`); - assert.equal(testedScope.type, expectedParentScopeType, - `Tested scope is not the child of the correct scope`); - }, - }, - { - enabled: true, - name: 'Local variable declaration supersedes outer scope declaration', - description: 'Verify declNode references the local declaration correctly.', - run() { - const innerScopeVal = 'inner'; - const outerScopeVal = 'outer'; - const code = `var a = '${outerScopeVal}'; - if (true) { - let a = '${innerScopeVal}'; - console.log(a); - } - console.log(a);`; - const ast = generateFlatAST(code); - const [innerIdentifier, outerIdentifier] = ast.filter(n => n.type === 'Identifier' && n.parentNode.type === 'CallExpression'); - const innerValResult = innerIdentifier.declNode.parentNode.init.value; - const outerValResult = outerIdentifier.declNode.parentNode.init.value; - assert.equal(innerValResult, innerScopeVal, - `Declaration node (inner scope) is incorrectly referenced.`); - assert.equal(outerValResult, outerScopeVal, - `Declaration node (outer scope) is incorrectly referenced.`); - }, - }, - { - enabled: true, - name: 'Variable references are not confused with functions of the same name', - description: `Verify a function's identifier isn't treated as a reference.`, - run() { - const code = `function a() { - var a; - }`; - const ast = generateFlatAST(code); - const funcId = ast.find(n => n.name ==='a' && n.parentNode.type === 'FunctionDeclaration'); - const varId = ast.find(n =>n.name ==='a' && n.parentNode.type === 'VariableDeclarator'); - const functionReferencesFound = !!funcId.references?.length; - const variableReferencesFound = !!varId.references?.length; - assert.ok(!functionReferencesFound, - `References to a function were incorrectly found`); - assert.ok(!variableReferencesFound, - `References to a variable were incorrectly found`); - }, - }, -]; \ No newline at end of file diff --git a/tests/tester.js b/tests/tester.js deleted file mode 100644 index f073246..0000000 --- a/tests/tester.js +++ /dev/null @@ -1,31 +0,0 @@ -const tests = { - Parsing: './parsingTests', - Functionality: './functionalityTests', - Arborist: './aboristTests', - Utils: './utilsTests', -}; - -let allTests = 0; -let skippedTests = 0; -console.time('tests in'); -for (const [moduleName, moduleTests] of Object.entries(tests)) { - const loadedTests = require(moduleTests); - for (const test of loadedTests) { - allTests++; - if (test.enabled) { - process.stdout.write(`[${moduleName}] ${test.name}`.padEnd(90, '.')); - console.time('PASS'); - test.run(); - console.timeEnd('PASS'); - } else { - skippedTests++; - console.log(`Testing [${moduleName}] ${test.name}...`.padEnd(101, '.') + ` SKIPPED: ${test.reason}`); - } - } -} -if (skippedTests > 0) { - process.stdout.write(`Completed ${allTests - skippedTests}/${allTests} (${skippedTests} skipped) `); -} else process.stdout.write(`Completed ${allTests} `); -console.timeEnd('tests in'); - -module.exports = tests; \ No newline at end of file diff --git a/tests/utils.test.js b/tests/utils.test.js new file mode 100644 index 0000000..d251229 --- /dev/null +++ b/tests/utils.test.js @@ -0,0 +1,82 @@ +import assert from 'node:assert'; +import {utils} from '../src/index.js'; +import {describe, it} from 'node:test'; + +describe('Utils tests: treeModifier', () => { + it(`Verify treeModifier sets a generic function name`, () => { + const expectedFuncName = 'func'; + const generatedFunc = utils.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); + assert.equal(generatedFunc.name, expectedFuncName, `The name of the generated function does not match`); + }); +}); +describe('Utils tests: applyIteratively', () => { + it('Verify applyIteratively cannot remove the root node without replacing it', () => { + const code = `a`; + 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]); + + 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}); + assert.equal(result, code, `Result does not match expected output`); + }); + it('Verify applyIteratively works as expected', () => { + const code = `console.log('Hello' + ' ' + 'there');`; + const expectedOutput = `console.log('General' + ' ' + 'Kenobi');`; + const replacements = { + Hello: 'General', + there: 'Kenobi', + }; + let result = code; + const f = n => n.type === 'Literal' && replacements[n.value]; + const m = (n, arb) => arb.markNode(n, { + type: 'Literal', + value: replacements[n.value], + }); + const generatedFunc = utils.treeModifier(f, m); + result = utils.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`); + }); + 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`); + }); + 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`); + }); + 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`); + }); + 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`); + }); + 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`); + }); +}); \ No newline at end of file diff --git a/tests/utilsTests.js b/tests/utilsTests.js deleted file mode 100644 index d3d5692..0000000 --- a/tests/utilsTests.js +++ /dev/null @@ -1,32 +0,0 @@ -const {utils} = require(__dirname + '/../src/index'); -const assert = require('node:assert'); -module.exports = [ - { - enabled: true, - name: 'treeModifier + applyIteratively', - description: '', - run() { - const code = `console.log('Hello' + ' ' + 'there');`; - const expectedOutput = `console.log('General' + ' ' + 'Kenobi');`; - const expectedFuncName = 'StarWarsDialog'; - const replacements = { - Hello: 'General', - there: 'Kenobi', - }; - let result = code; - const f = n => n.type === 'Literal' && replacements[n.value]; - const m = (n, arb) => arb.markNode(n, { - type: 'Literal', - value: replacements[n.value], - }); - const generatedFunc = utils.treeModifier(f, m, expectedFuncName); - result = utils.applyIteratively(result, [generatedFunc]); - - assert.equal(result, expectedOutput, - `Result does not match expected output.`); - assert.equal(generatedFunc.name, expectedFuncName, - `The name of the generated function does not match.`); - return true; - }, - }, -]; \ No newline at end of file