Binding
contains information about bindings (declared variables).
const { traverse } = require('estree-toolkit');
const { parseModule } = require('meriyah');
const ast = parseModule(`
const a = 10;
let b = 20;
const [c, d] = [30, 40];
`);
traverse(ast, {
$: { scope: true },
Program(path) {
path.scope.bindings
/* =>
{
a: Binding { ... },
b: Binding { ... },
c: Binding { ... },
d: Binding { ... }
}
*/
}
});
kind
string
The way the binding has been declared. Can be any one of these - var
, let
, const
, param
, hoisted
, local
, module
.
var x = 0;
// Kind of `x` is 'var'
const x = 0;
// Kind of `x` is 'const'
let x = 0;
// Kind of `x` is 'let'
try {} catch (x) {
// Kind of `x` is 'let'
}
function a(x) {
// Kind of `x` is 'param'
}
(function (x) {
// Kind of `x` is 'param'
});
((x) => {
// Kind of `x` is 'param'
});
function x() {}
// Kind of `x` is 'hoisted'
class x() { constructor() {} }
// Kind of `x` is 'hoisted'
addEventListener(function x() {
// Kind of `x` is 'local'
});
submitClass(class x() {
// Kind of `x` is 'local'
})
// Kind 'local' only appears in FunctionExpression and ClassExpression
import { x, a as y } from 'mod';
// Kind of `x` and `y` is 'module'
import x from 'mod';
// Kind of `x` is 'module'
import * as x from 'mod';
// Kind of `x` is 'module'
name
string
The name of the binding.
scope
Scope
The scope that contains the binding.
identifierPath
NodePath
The binding's identifier's path.
// Take this as an example
function Func() {}
// ─┬──
// │
// └── This would be the binding's `identifierPath`
// In the binding generated for the above function,
// the binding's `identifierPath` would be the path to `Func`
// identifier
path
NodePath
The whole path for the binding.
// Take this as an example
function Func() {}
// ──┬───────────────
// │
// └── This would be the binding's `path`
// In the binding generated for the above function,
// the binding's `path` would be the path to the whole function
references
NodePath[]
All paths that references this binding.
constantViolations
NodePaths[]
All paths that reassigns the value of this binding.
const x = 0;
// This is a constant violation
x = 0;
const y = { a: 0 };
// This is a constant violation
y = { b: 0 }
// This is not a constant violation
y.c = 0;
constant
boolean
If the binding is constant. A binding is constant when it has no constant violations.