-
-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(commonjs): auto-detect conditional requires (#1038)
- Loading branch information
1 parent
38a3aa4
commit 55e7e2d
Showing
18 changed files
with
364 additions
and
65 deletions.
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
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
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
3 changes: 3 additions & 0 deletions
3
packages/commonjs/test/fixtures/function/conditional-require-chain/_config.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,3 @@ | ||
module.exports = { | ||
description: 'makes requires in conditionally required modules conditional as well' | ||
}; |
1 change: 1 addition & 0 deletions
1
packages/commonjs/test/fixtures/function/conditional-require-chain/dep.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 @@ | ||
require('./throws.js'); |
6 changes: 6 additions & 0 deletions
6
packages/commonjs/test/fixtures/function/conditional-require-chain/main.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,6 @@ | ||
global.false = false; | ||
|
||
if (global.false) { | ||
// eslint-disable-next-line global-require | ||
require('./dep.js'); | ||
} |
1 change: 1 addition & 0 deletions
1
packages/commonjs/test/fixtures/function/conditional-require-chain/throws.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 @@ | ||
throw new Error('This should not be executed'); |
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
3 changes: 3 additions & 0 deletions
3
packages/commonjs/test/fixtures/function/strict-requires-detect-conditionals/_config.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,3 @@ | ||
module.exports = { | ||
description: 'automatically detects requires nested in conditionals' | ||
}; |
1 change: 1 addition & 0 deletions
1
packages/commonjs/test/fixtures/function/strict-requires-detect-conditionals/hoisted.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 @@ | ||
module.exports = 'this should be top-level'; |
44 changes: 44 additions & 0 deletions
44
packages/commonjs/test/fixtures/function/strict-requires-detect-conditionals/main.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,44 @@ | ||
/* eslint-disable global-require */ | ||
global.false = false; | ||
global.true = true; | ||
|
||
if (global.false) { | ||
require('./throws.js'); | ||
} | ||
|
||
if (global.true) { | ||
/* do nothing */ | ||
} else { | ||
require('./throws.js'); | ||
} | ||
|
||
const conditionalFalse = global.false ? require('./throws.js') : null; | ||
const conditionalTrue = global.true ? null : require('./throws.js'); | ||
|
||
const logicalAnd = global.false && require('./throws.js'); | ||
const logicalOr = global.true || require('./throws.js'); | ||
|
||
function requireFunctionDeclaration() { | ||
require('./throws.js'); | ||
} | ||
|
||
const requireFunctionExpression = function () { | ||
require('./throws.js'); | ||
}; | ||
|
||
const requireArrowFunction = () => require('./throws.js'); | ||
|
||
if (global.false) { | ||
requireFunctionDeclaration(); | ||
requireFunctionExpression(); | ||
requireArrowFunction(); | ||
} | ||
|
||
// These should not cause wrapping | ||
t.is( | ||
(function () { | ||
return require('./hoisted.js'); | ||
})(), | ||
'this should be top-level' | ||
); | ||
t.is((() => require('./hoisted.js'))(), 'this should be top-level'); |
1 change: 1 addition & 0 deletions
1
packages/commonjs/test/fixtures/function/strict-requires-detect-conditionals/throws.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 @@ | ||
throw new Error('This should never be executed or imported'); |
4 changes: 3 additions & 1 deletion
4
packages/commonjs/test/fixtures/function/strict-requires-file-without-module-type/main.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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
global.null = 0; | ||
|
||
// eslint-disable-next-line global-require | ||
t.is(0 && require('./error.js'), 0); | ||
t.is(global.null && require('./error.js'), 0); |
Oops, something went wrong.