From 56246fdff033e268ae87d2cfad7be7e2c68ad666 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Fri, 17 Nov 2023 17:04:36 -0500 Subject: [PATCH] docs: Update README with analyze() options Fixes #110 --- README.md | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 110baaf..2451649 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,18 @@ To use in a CommonJS file: const eslintScope = require('eslint-scope'); ``` +In order to analyze scope, you'll need to have an [ESTree](https://github.com/estree/estree) compliant AST structure to run it on. The primary method is `eslintScope.analyze()`, which takes two arguments: + +1. `ast` - the ESTree-compliant AST structure to analyze. +2. `options` (optional) - Options to adjust how the scope is analyzed, including: + * `ignoreEval` (default: `false`) - Set to `true` to ignore all `eval()` calls (which would normally create scopes). + * `nodejsScope` (default: `false`) - Set to `true` to create a top-level function scope needed for CommonJS evaluation. + * `impliedStrict` (default: `false`) - Set to `true` to evaluate the code in strict mode even outside of modules and without `"use strict"`. + * `ecmaVersion` (default: `5`) - The version of ECMAScript to use to evaluate the code. + * `sourceType` (default: `"script"`) - The type of JavaScript file to evaluate. Change to `"module"` for ECMAScript module code. + * `childVisitorKeys` (default: `null`) - An object with visitor key information (like [`eslint-visitor-keys`](https://github.com/eslint/eslint-visitor-keys)). Without this, `eslint-scope` finds child nodes to visit algorithmically. Providing this option is a performance enhancement. + * `fallback` (default: `"iteration"`) - The strategy to use when `childVisitorKeys` is not specified. May be a function. + Example: ```js @@ -33,8 +45,13 @@ import * as eslintScope from 'eslint-scope'; import * as espree from 'espree'; import estraverse from 'estraverse'; -const ast = espree.parse(code, { range: true }); -const scopeManager = eslintScope.analyze(ast); +const options = { + ecmaVersion: 2022, + sourceType: "module" +}; + +const ast = espree.parse(code, { range: true, ...options }); +const scopeManager = eslintScope.analyze(ast, options); const currentScope = scopeManager.acquire(ast); // global scope