|
| 1 | +// Import Node.js Dependencies |
| 2 | +import { test } from "node:test"; |
| 3 | +import assert from "node:assert"; |
| 4 | + |
| 5 | +// Import Internal Dependencies |
| 6 | +import { runASTAnalysis } from "../../index.js"; |
| 7 | + |
| 8 | +const validTestCases = [ |
| 9 | + ["module.exports = require('fs') || require('constants');", ["fs", "constants"]], |
| 10 | + ["module.exports = require('constants') ? require('fs') : require('foo');", ["constants", "fs", "foo"]], |
| 11 | + |
| 12 | + // should have at least one branch has a `require` callee |
| 13 | + ["module.exports = require('constants') || {};", ["constants"]], |
| 14 | + ["module.exports = {} || require('constants');", ["constants"]], |
| 15 | + ["module.exports = require('constants') ? require('fs') : {};", ["constants", "fs"]], |
| 16 | + ["module.exports = require('constants') ? {} : require('fs');", ["constants", "fs"]], |
| 17 | + |
| 18 | + // should apply to nested conditions |
| 19 | + ["module.exports = (require('constants') || {}) || (require('foo') || {});", ["constants", "foo"]], |
| 20 | + ["module.exports = require('constants') ? (require('fs') || {}) : ({} || require('foo'));", ["constants", "fs", "foo"]], |
| 21 | + ["module.exports = require('constants') ? ({} || require('fs')) : (require('foo') || {});", ["constants", "fs", "foo"]], |
| 22 | + ["module.exports = require('constants') ? (require('fs') ? {} : require('bar')) : {};", ["constants", "fs", "bar"]], |
| 23 | + ["module.exports = require('constants') ? {} : (require('fs') ? {} : require('bar'));", ["constants", "fs", "bar"]], |
| 24 | + |
| 25 | + // test condition that are not `require` callees, here `notRequire('someModule')`, are ignored |
| 26 | + ["module.exports = notRequire('someModule') ? require('constants') : require('foo');", |
| 27 | + ["constants", "foo"] |
| 28 | + ], |
| 29 | + ["module.exports = ok ? (notRequire('someModule') ? require('constants') : require('foo')) : {};", |
| 30 | + ["constants", "foo"] |
| 31 | + ], |
| 32 | + ["module.exports = ok ? {} : (notRequire('someModule') ? require('constants') : require('foo'));", |
| 33 | + ["constants", "foo"] |
| 34 | + ] |
| 35 | +]; |
| 36 | + |
| 37 | +test("it should return isOneLineRequire true given a single line CJS export with a valid assignment", () => { |
| 38 | + validTestCases.forEach((test) => { |
| 39 | + const [source, modules] = test; |
| 40 | + const { dependencies, isOneLineRequire } = runASTAnalysis(source); |
| 41 | + |
| 42 | + assert.ok(isOneLineRequire); |
| 43 | + assert.deepEqual([...dependencies.keys()], modules); |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +const invalidTestCases = [ |
| 48 | + // should have at least one `require` callee |
| 49 | + ["module.exports = notRequire('foo') || {};", []], |
| 50 | + ["module.exports = {} || notRequire('foo');", []], |
| 51 | + ["module.exports = require('constants') ? {} : {};", ["constants"]], |
| 52 | + |
| 53 | + // same behavior should apply to nested conditions |
| 54 | + ["module.exports = (notRequire('foo') || {}) || (notRequire('foo') || {});", []], |
| 55 | + ["module.exports = require('constants') ? (notRequire('foo') || {}) : (notRequire('foo') || {});", ["constants"]], |
| 56 | + ["module.exports = require('constants') ? (notRequire('foo') || {}) : (notRequire('foo') || {});", ["constants"]], |
| 57 | + ["module.exports = require('constants') ? (require('constants') ? {} : {}) : (require('constants') ? {} : {});", ["constants"]] |
| 58 | +]; |
| 59 | + |
| 60 | +test("it should return isOneLineRequire false given a single line CJS export with illegal callees", () => { |
| 61 | + invalidTestCases.forEach((test) => { |
| 62 | + const [source, modules] = test; |
| 63 | + const { dependencies, isOneLineRequire } = runASTAnalysis(source); |
| 64 | + |
| 65 | + assert.ok(isOneLineRequire === false); |
| 66 | + assert.deepEqual([...dependencies.keys()], modules); |
| 67 | + }); |
| 68 | +}); |
0 commit comments