-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compartment-mapper): Reenable CommonJS
This reverts commit 8d7fb04.
- Loading branch information
Showing
11 changed files
with
205 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import parser from '@babel/parser'; | ||
import traverse from '@babel/traverse'; | ||
|
||
/* parseRequires | ||
* Analyzes a CommonJS module's obvious static shallow dependencies, returning | ||
* an array of every module specifier that the module requires with a string | ||
* literal. | ||
* | ||
* Does not differentiate conditional dependencies, which will likely cause | ||
* errors in the loading phase. For example, `if (isNode) { require('fs') }` | ||
* will reveal an unconditional dependency on a possibly unloadable `fs` | ||
* module, even though the module has a contingency when that module is not | ||
* available. | ||
* | ||
* Does not discover dynamic dependencies, which will likely cause errors in | ||
* the module execution phase. | ||
* For example, `require(path)` will not be discovered by this parser, the | ||
* module will successfully load, but will likely be unable to synchronously | ||
* require the module with the given path. | ||
* | ||
* @typedef ImportSpecifier string | ||
* @param {string} source | ||
* @param {string} location | ||
* @return {Array<ImportSpecifier>} | ||
*/ | ||
export const parseRequires = (source, location, packageLocation) => { | ||
try { | ||
const ast = parser.parse(source); | ||
const required = new Set(); | ||
traverse.default(ast, { | ||
CallExpression(path) { | ||
const { node, scope } = path; | ||
const { callee, arguments: args } = node; | ||
if (callee.name !== 'require') { | ||
return; | ||
} | ||
// We do not recognize `require()` or `require("id", true)`. | ||
if (args.length !== 1) { | ||
return; | ||
} | ||
// We only consider the form `require("id")`, not `require(expression)`. | ||
const [specifier] = args; | ||
if (specifier.type !== 'StringLiteral') { | ||
return; | ||
} | ||
// The existence of a require variable in any parent scope indicates that | ||
// this is not a free-variable use of the term `require`, so it does not | ||
// likely refer to the module's given `require`. | ||
if (scope.hasBinding('require')) { | ||
return; | ||
} | ||
required.add(specifier.value); | ||
}, | ||
}); | ||
return Array.from(required).sort(); | ||
} catch (error) { | ||
if (/import/.exec(error.message) !== null) { | ||
throw new Error( | ||
`Cannot parse CommonJS module at ${location}, consider adding "type": "module" to package.json in ${packageLocation}: ${error}`, | ||
); | ||
} | ||
throw new Error(`Cannot parse CommonJS module at ${location}: ${error}`); | ||
} | ||
}; |
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
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
7 changes: 6 additions & 1 deletion
7
packages/compartment-mapper/test/node_modules/app/package.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
packages/compartment-mapper/test/node_modules/danny/package.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import test from 'ava'; | ||
import { parseRequires } from '../src/parse-requires.js'; | ||
|
||
test('parse unique require calls', t => { | ||
t.plan(1); | ||
const code = ` | ||
require("b"); // sorted later | ||
// require("bogus"); // not discovered | ||
function square(require) { | ||
require("shadowed"); // over-shadowed by argument | ||
} | ||
require("a"); | ||
function c() { | ||
require("c"); // found despite inner scope | ||
} | ||
require("a"); // de-duplicated | ||
require( "d" ); // such space | ||
require("bogus", "bogus", "bogus"); | ||
require("id\\""); // found, despite inner quote | ||
`; | ||
const requires = parseRequires(code); | ||
t.deepEqual(requires, ['a', 'b', 'c', 'd', 'id"']); | ||
}); |