-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Report.isOneLineRequire should be true if single line LogicalExp…
…ression assignment (#205) --------- Co-authored-by: Thomas.G <gentilhomme.thomas@gmail.com>
- Loading branch information
1 parent
9d85fa2
commit f04cfb1
Showing
3 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
test/issues/170-isOneLineRequire-logicalExpression-CJS-export.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Import Node.js Dependencies | ||
import { test } from "node:test"; | ||
import assert from "node:assert"; | ||
|
||
// Import Internal Dependencies | ||
import { runASTAnalysis } from "../../index.js"; | ||
|
||
const validTestCases = [ | ||
["module.exports = require('fs') || require('constants');", ["fs", "constants"]], | ||
["module.exports = require('constants') ? require('fs') : require('foo');", ["constants", "fs", "foo"]], | ||
|
||
// should have at least one branch has a `require` callee | ||
["module.exports = require('constants') || {};", ["constants"]], | ||
["module.exports = {} || require('constants');", ["constants"]], | ||
["module.exports = require('constants') ? require('fs') : {};", ["constants", "fs"]], | ||
["module.exports = require('constants') ? {} : require('fs');", ["constants", "fs"]], | ||
|
||
// should apply to nested conditions | ||
["module.exports = (require('constants') || {}) || (require('foo') || {});", ["constants", "foo"]], | ||
["module.exports = require('constants') ? (require('fs') || {}) : ({} || require('foo'));", ["constants", "fs", "foo"]], | ||
["module.exports = require('constants') ? ({} || require('fs')) : (require('foo') || {});", ["constants", "fs", "foo"]], | ||
["module.exports = require('constants') ? (require('fs') ? {} : require('bar')) : {};", ["constants", "fs", "bar"]], | ||
["module.exports = require('constants') ? {} : (require('fs') ? {} : require('bar'));", ["constants", "fs", "bar"]], | ||
|
||
// test condition that are not `require` callees, here `notRequire('someModule')`, are ignored | ||
["module.exports = notRequire('someModule') ? require('constants') : require('foo');", | ||
["constants", "foo"] | ||
], | ||
["module.exports = ok ? (notRequire('someModule') ? require('constants') : require('foo')) : {};", | ||
["constants", "foo"] | ||
], | ||
["module.exports = ok ? {} : (notRequire('someModule') ? require('constants') : require('foo'));", | ||
["constants", "foo"] | ||
] | ||
]; | ||
|
||
test("it should return isOneLineRequire true given a single line CJS export with a valid assignment", () => { | ||
validTestCases.forEach((test) => { | ||
const [source, modules] = test; | ||
const { dependencies, isOneLineRequire } = runASTAnalysis(source); | ||
|
||
assert.ok(isOneLineRequire); | ||
assert.deepEqual([...dependencies.keys()], modules); | ||
}); | ||
}); | ||
|
||
const invalidTestCases = [ | ||
// should have at least one `require` callee | ||
["module.exports = notRequire('foo') || {};", []], | ||
["module.exports = {} || notRequire('foo');", []], | ||
["module.exports = require('constants') ? {} : {};", ["constants"]], | ||
|
||
// same behavior should apply to nested conditions | ||
["module.exports = (notRequire('foo') || {}) || (notRequire('foo') || {});", []], | ||
["module.exports = require('constants') ? (notRequire('foo') || {}) : (notRequire('foo') || {});", ["constants"]], | ||
["module.exports = require('constants') ? (notRequire('foo') || {}) : (notRequire('foo') || {});", ["constants"]], | ||
["module.exports = require('constants') ? (require('constants') ? {} : {}) : (require('constants') ? {} : {});", ["constants"]] | ||
]; | ||
|
||
test("it should return isOneLineRequire false given a single line CJS export with illegal callees", () => { | ||
invalidTestCases.forEach((test) => { | ||
const [source, modules] = test; | ||
const { dependencies, isOneLineRequire } = runASTAnalysis(source); | ||
|
||
assert.ok(isOneLineRequire === false); | ||
assert.deepEqual([...dependencies.keys()], modules); | ||
}); | ||
}); |