Skip to content

Instantly share code, notes, and snippets.

@aneurysmjs
Last active December 18, 2022 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aneurysmjs/1d8bd49d6b221ca51831255780906cdc to your computer and use it in GitHub Desktop.
Save aneurysmjs/1d8bd49d6b221ca51831255780906cdc to your computer and use it in GitHub Desktop.
Jscodeshift's scope methods
// getScope() is a method provided by jscodeshift that can be used to get the Scope object for a given Node.
// A Scope object represents the lexical scope of a node in the abstract syntax tree (AST).
// It contains information about the variables, functions, and other declarations that are in scope at a given point in the code.
// Here's an example of how getScope() can be used:
import { getScope } from 'jscodeshift';
const source = `
function foo() {
let x = 10;
console.log(x);
}
`;
const ast = parse(source);
// Get the scope for the function body
const scope = getScope(ast.program.body[0].body);
console.log(scope.variables); // [{ name: 'x' }]
// getBindings() is another method provided by jscodeshift that can be used to get the bindings (i.e., variables, functions, etc.)
// in a given Scope. It returns an object with keys for each binding type (e.g., variables, functions, etc.) and values that are arrays of binding names.
// Here's an example of how getBindings() can be used:
import { getScope, getBindings } from 'jscodeshift';
const source = `
function foo() {
let x = 10;
console.log(x);
}
`;
const ast = parse(source);
// Get the scope for the function body
const scope = getScope(ast.program.body[0].body);
console.log(getBindings(scope));
/*
{
variables: ['x']
}
*/
// withScope() is a method provided by jscodeshift that allows you to execute a callback function with a given Scope object as the current scope.
// This can be useful when you want to perform operations within the context of a specific scope.
// Here's an example of how withScope() can be used:
import { withScope } from 'jscodeshift';
const source = `
function foo() {
let x = 10;
console.log(x);
}
`;
const ast = parse(source);
// Get the scope for the function body
const scope = getScope(ast.program.body[0].body);
withScope(scope, () => {
// Within this callback function, the current scope is set to `scope`
console.log(getCurrentScope().variables); // [{ name: 'x' }]
});
// Here are some additional examples of using getScope(), getBindings(), and withScope() from jscodeshift:
import { getScope, getBindings, withScope } from 'jscodeshift';
// Example 1: Getting the scope for a function declaration
const source = `
function foo() {
let x = 10;
console.log(x);
}
`;
const ast = parse(source);
// Get the scope for the function declaration
const scope = getScope(ast.program.body[0]);
console.log(scope.variables); // [{ name: 'x' }]
// Example 2: Getting the bindings for a block statement
const source = `
{
let x = 10;
let y = 20;
const z = 30;
function foo() {}
}
`;
const ast = parse(source);
// Get the scope for the block statement
const scope = getScope(ast.program.body[0]);
console.log(getBindings(scope));
/*
{
variables: ['x', 'y'],
functions: ['foo']
}
*/
// Example 3: Using withScope to execute a callback function with a specific scope
const source = `
function foo() {
let x = 10;
console.log(x);
}
`;
const ast = parse(source);
// Get the scope for the function declaration
const scope = getScope(ast.program.body[0]);
withScope(scope, () => {
console.log(getCurrentScope().variables); // [{ name: 'x' }]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment