Collection of useful helper functions when trying to determine module type (CommonJS or AMD) properties of an AST node.
AST checks are based on the Esprima (Spidermonkey) format
npm install ast-module-types
Each of these takes in a single AST node argument and returns a boolean.
isDefineAMD
: if node matches any form of an AMDdefine
function callisRequire
: if node matches arequire
function all (declaring a dependency)isTopLevelRequire
: if node matches arequire
at the very top of the file.isAMDDriverScriptRequire
: if node matches an AMD driver script's require callrequire([deps], function)
isExports
: if the node matches CommonJSmodule.exports
orexports
(defining a module)
Detecting the various forms of defining an AMD module
isNamedForm
: if the node is a define call of the form:define('name', [deps], func)
isDependencyForm
: if the node is a define call of the form:define([deps], func)
isFactoryForm
: if the node is a define call of the form:define(func(require))
isNoDependencyForm
: if the node is a define call of the form:define({})
isREMForm
: if the node matches the form:define(function(require, exports, module){});
ES6 Types
All types abide by the EStree spec
isES6Import
: if the node is any of the es6 import formsisES6Export
: if the node is of any es6 export forms
const types = require('ast-module-types');
// Assume node is some node of an AST that you parsed using esprima or esprima-fb
// ...
console.log(types.isDefineAMD(node));