Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
@hishprorg/mollitia-cumque
Advanced tools
<code>@hishprorg/mollitia-cumque</code> Tools for working with ESTree AST <img alt="npm" src="https://img.shields.io/npm
@hishprorg/mollitia-cumque
npm i @hishprorg/mollitia-cumque
# or
yarn add @hishprorg/mollitia-cumque
// Supports both CommonJS and ES Modules
// ES Module
import { traverse, builders as b } from '@hishprorg/mollitia-cumque';
// CommonJS
const { traverse, builders: b } = require('@hishprorg/mollitia-cumque');
const { traverse } = require('@hishprorg/mollitia-cumque');
traverse(ast, {
Program(path) {
// Do something with the path
}
});
const { builders: b } = require('@hishprorg/mollitia-cumque');
b.identifier('x'); // => { type: 'Identifier', name: 'x' }
const { traverse, is } = require('@hishprorg/mollitia-cumque');
const { parseModule } = require('meriyah');
const ast = parseModule(`x = 0`);
traverse(ast, {
AssignmentExpression(path) {
if (is.identifier(path.node.left, { name: 'x' })) {
// `left` is an identifier with name `x`
}
}
});
const { traverse, builders: b } = require('@hishprorg/mollitia-cumque');
const { parseModule } = require('meriyah');
const ast = parseModule('a = b');
traverse(ast, {
Identifier(path) {
if (path.node.name === 'a') {
path.replaceWith(b.identifier('c'));
}
}
});
// Now the AST represents - `c = b`
const { traverse } = require('@hishprorg/mollitia-cumque');
traverse(ast, {
// Enable scope
$: { scope: true },
Program(path) {
// `path.scope` is now available in all paths
}
});
const { traverse } = require('@hishprorg/mollitia-cumque');
const { parseModule } = require('meriyah');
const ast = parseModule(`
import { a } from 'source';
const { b, c: [d, { e }] } = a;
`);
traverse(ast, {
$: { scope: true },
Program(path) {
path.scope.hasBinding('a') // => true
path.scope.hasBinding('b') // => true
path.scope.hasBinding('c') // => false
path.scope.hasBinding('d') // => true
path.scope.hasBinding('e') // => true
}
});
const { traverse } = require('@hishprorg/mollitia-cumque');
const { parseModule } = require('meriyah');
const ast = parseModule(`
import { a } from 'source';
fn(a);
s = a;
let obj = { a };
`);
traverse(ast, {
$: { scope: true },
Program(path) {
// Returns all the paths that reference the binding `a`
path.scope.getBinding('a').references // => [NodePath, NodePath, NodePath]
}
});
const { traverse } = require('@hishprorg/mollitia-cumque');
const { parseModule } = require('meriyah');
const ast = parseModule(`
const fx = require('fx-mod');
`);
traverse(ast, {
$: { scope: true },
Program(path) {
path.scope.hasGlobalBinding('require') // => true
}
});
const { traverse } = require('@hishprorg/mollitia-cumque');
const { parseModule } = require('meriyah');
const ast = parseModule(`
const a = 0
a.reload()
while (a.ok) a.run()
`);
traverse(ast, {
$: { scope: true },
Program(path) {
// `a` -> `b`
path.scope.renameBinding('a', 'b')
}
});
// Output code:
// const b = 0
//
// b.reload()
// while (b.ok) b.run()
There are several static utilities that you can use.
evaluate
const { utils: u, traverse } = require('@hishprorg/mollitia-cumque');
// We are using `meriyah` but you can use any parser (like `acorn`)
const { parseModule } = require('meriyah');
traverse(parseModule(`1 + 2`), {
BinaryExpression(path) {
u.evaluate(path) // => { value: 3 }
}
});
traverse(parseModule(`1 === 2`), {
BinaryExpression(path) {
u.evaluate(path) // => { value: false }
}
});
traverse(parseModule(`iDoNotKnowWhatThisIs === 55`), {
BinaryExpression(path) {
u.evaluate(path) // => undefined
}
});
traverse(parseModule(`
({
text: 'This is an object',
data: [1, 'two']
})
`), {
ObjectExpression(path) {
u.evaluate(path) // => { value: { text: 'This is an object', data: [1, 'two'] } }
}
});
traverse(parseModule(`1 > 5 ? 'YES' : 'NO'`), {
ConditionalExpression(path) {
u.evaluate(path) // => { value: 'NO' }
}
});
evaluateTruthy
true
, false
or undefined
depending on
evaluation result.There's more functionalities, please read the documentation.
You can find the documentation at https://@hishprorg/mollitia-cumque.netlify.app/
I know there is Babel. But there are
other tools which are faster than Babel. For example, meriyah
is 3x faster than @babel/parser
, astring
is up to 50x faster than @babel/generator
. But these tool only work with ESTree AST. I wanted to use these
faster alternatives for one of my projects but could not find any traverser with
batteries-included. So I built one myself, with awesome scope analysis, it has all the things that you would need for traversing an ESTree AST. Also, a little bit faster than Babel.
Licensed under the MIT License.
FAQs
security holding package
The npm package @hishprorg/mollitia-cumque receives a total of 1 weekly downloads. As such, @hishprorg/mollitia-cumque popularity was classified as not popular.
We found that @hishprorg/mollitia-cumque demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.