https://github.com/dependents/node-ast-module-types
AST helpers for recognizing CommonJS, AMD, and ES6 module types
https://github.com/dependents/node-ast-module-types
amd ast commonjs es6
Last synced: 3 months ago
JSON representation
AST helpers for recognizing CommonJS, AMD, and ES6 module types
- Host: GitHub
- URL: https://github.com/dependents/node-ast-module-types
- Owner: dependents
- License: mit
- Created: 2014-03-28T17:13:17.000Z (over 11 years ago)
- Default Branch: main
- Last Pushed: 2025-02-01T13:31:48.000Z (5 months ago)
- Last Synced: 2025-03-29T21:09:08.205Z (4 months ago)
- Topics: amd, ast, commonjs, es6
- Language: JavaScript
- Homepage:
- Size: 934 KB
- Stars: 12
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ast-module-types
[](https://github.com/dependents/node-ast-module-types/actions/workflows/ci.yml?query=branch%3Amain)
[](https://www.npmjs.com/package/ast-module-types)
[](https://www.npmjs.com/package/ast-module-types)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**
```sh
npm install ast-module-types
```## API
Each of these takes in a single AST node argument
and returns a boolean.* `isDefineAMD`: if node matches any form of an AMD `define` function call
* `isRequire`: if node matches a `require` function all (declaring a dependency)
* `isTopLevelRequire`: if node matches a `require` at the very top of the file.
* `isAMDDriverScriptRequire`: if node matches an AMD driver script's require call `require([deps], function)`
* `isExports`: if the node matches CommonJS `module.exports` or `exports` (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](https://github.com/estree/estree/blob/master/es2015.md)*
* `isES6Import`: if the node is any of the es6 import forms
* `isES6Export`: if the node is of any es6 export forms## Usage
```js
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));
```## License
[MIT](LICENSE)