Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/eslint-scope/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ In order to analyze scope, you'll need to have an [ESTree](https://github.com/es
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.
* `sourceType` (default: `"script"`) - The type of JavaScript file to evaluate. Change to `"module"` for ECMAScript module code, or `"commonjs"` for CommonJS module code.
* `childVisitorKeys` (default: `null`) - An object with visitor key information (like [`eslint-visitor-keys`](https://github.com/eslint/js/tree/main/packages/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.
* `jsx` (default: `false`) - Enables the tracking of JSX components as variable references.
Expand Down
10 changes: 6 additions & 4 deletions packages/eslint-scope/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ import eslintScopeVersion from "./version.js";
function defaultOptions() {
return {
optimistic: false,
nodejsScope: false,
impliedStrict: false,
sourceType: "script", // one of ['script', 'module', 'commonjs']
ecmaVersion: 5,
Expand Down Expand Up @@ -114,9 +113,6 @@ function updateDeeply(target, override) {
* @param {Object} providedOptions Options that tailor the scope analysis
* @param {boolean} [providedOptions.optimistic=false] the optimistic flag
* @param {boolean} [providedOptions.ignoreEval=false] whether to check 'eval()' calls
* @param {boolean} [providedOptions.nodejsScope=false] whether the whole
* script is executed under node.js environment. When enabled, escope adds
* a function scope immediately following the global scope.
* @param {boolean} [providedOptions.impliedStrict=false] implied strict mode
* (if ecmaVersion >= 5).
* @param {string} [providedOptions.sourceType='script'] the source type of the script. one of 'script', 'module', and 'commonjs'
Expand All @@ -125,9 +121,15 @@ function updateDeeply(target, override) {
* @param {Object} [providedOptions.childVisitorKeys=null] Additional known visitor keys. See [esrecurse](https://github.com/estools/esrecurse)'s the `childVisitorKeys` option.
* @param {string} [providedOptions.fallback='iteration'] A kind of the fallback in order to encounter with unknown node. See [esrecurse](https://github.com/estools/esrecurse)'s the `fallback` option.
* @returns {ScopeManager} ScopeManager
* @throws {Error} When `nodejsScope` option is passed.
*/
function analyze(tree, providedOptions) {
const options = updateDeeply(defaultOptions(), providedOptions);

if (options.nodejsScope) {
throw new Error("`nodejsScope` option has been removed. To create a top-level function scope for CommonJS evaluation, set `sourceType` to 'commonjs'.");
}

const scopeManager = new ScopeManager(options);
const referencer = new Referencer(options, scopeManager);

Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-scope/lib/scope-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ScopeManager {
}

isGlobalReturn() {
return this.__options.nodejsScope || this.__options.sourceType === "commonjs";
return this.__options.sourceType === "commonjs";
}

isModule() {
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-scope/lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ class FunctionScope extends Scope {
// }
__isValidResolution(ref, variable) {

// If `options.nodejsScope` is true, `this.block` becomes a Program node.
// If `options.sourceType` is "commonjs", `this.block` becomes a Program node.
if (this.block.type === "Program") {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ import { expect } from "chai";
import espree from "./util/espree.js";
import { analyze } from "../lib/index.js";

describe("nodejsScope option", () => {
describe("commonjs scope", () => {

it("creates a function scope following the global scope immediately when nodejscope: true", () => {
it("creates a function scope following the global scope immediately when sourceType:commonjs", () => {
const ast = espree(`
"use strict";
var hello = 20;
`);

const scopeManager = analyze(ast, { ecmaVersion: 6, nodejsScope: true });
const scopeManager = analyze(ast, { ecmaVersion: 6, sourceType: "commonjs" });

expect(scopeManager.scopes).to.have.length(2);
expect(scopeManager.isGlobalReturn()).to.be.true;
Expand All @@ -54,62 +54,17 @@ describe("nodejsScope option", () => {
expect(scope.variables[1].name).to.be.equal("hello");
});

it("creates a function scope following the global scope immediately when sourceType:commonjs", () => {
it("analyze() throws an error when `nodejsScope:true` option is passed", () => {
const ast = espree(`
"use strict";
var hello = 20;
`);

const scopeManager = analyze(ast, { ecmaVersion: 6, sourceType: "commonjs" });

expect(scopeManager.scopes).to.have.length(2);
expect(scopeManager.isGlobalReturn()).to.be.true;

let scope = scopeManager.scopes[0];

expect(scope.type).to.be.equal("global");
expect(scope.block.type).to.be.equal("Program");
expect(scope.isStrict).to.be.false;
expect(scope.variables).to.have.length(0);

scope = scopeManager.scopes[1];
expect(scope.type).to.be.equal("function");
expect(scope.block.type).to.be.equal("Program");
expect(scope.isStrict).to.be.true;
expect(scope.variables).to.have.length(2);
expect(scope.variables[0].name).to.be.equal("arguments");
expect(scope.variables[1].name).to.be.equal("hello");
expect(() => analyze(ast, { ecmaVersion: 6, nodejsScope: true })).to.throw(
"`nodejsScope` option has been removed. To create a top-level function scope for CommonJS evaluation, set `sourceType` to 'commonjs'."
);
});

it("creates a function scope following the global scope immediately and creates module scope", () => {
const ast = espree("import {x as v} from 'mod';");

const scopeManager = analyze(ast, { ecmaVersion: 6, nodejsScope: true, sourceType: "module" });

expect(scopeManager.scopes).to.have.length(3);
expect(scopeManager.isGlobalReturn()).to.be.true;

let scope = scopeManager.scopes[0];

expect(scope.type).to.be.equal("global");
expect(scope.block.type).to.be.equal("Program");
expect(scope.isStrict).to.be.false;
expect(scope.variables).to.have.length(0);

scope = scopeManager.scopes[1];
expect(scope.type).to.be.equal("function");
expect(scope.block.type).to.be.equal("Program");
expect(scope.isStrict).to.be.false;
expect(scope.variables).to.have.length(1);
expect(scope.variables[0].name).to.be.equal("arguments");

scope = scopeManager.scopes[2];
expect(scope.type).to.be.equal("module");
expect(scope.variables).to.have.length(1);
expect(scope.variables[0].name).to.be.equal("v");
expect(scope.variables[0].defs[0].type).to.be.equal("ImportBinding");
expect(scope.references).to.have.length(0);
});
Comment on lines -84 to -112
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was inherited from escope. I'm not sure why nodejsScope: true, sourceType: "module" combination was supported at all, and even less why it was supported by nesting scopes as: Global > Function > Module. For this combination, I think Global > Module > Function would make more sense for practical purposes as it could represent code that is going to be wrapped in a function that is in an ES module.

});

// vim: set sw=4 ts=4 et tw=80 :
4 changes: 2 additions & 2 deletions packages/eslint-scope/tests/implied-strict.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ describe("impliedStrict option", () => {
expect(scope.isStrict).to.be.false;
});

it("omits a nodejs global scope when ensuring all user scopes are strict", () => {
it("omits a commonjs global scope when ensuring all user scopes are strict", () => {
const ast = espree(`
function foo() {}
`);

const scopeManager = analyze(ast, { ecmaVersion: 5, nodejsScope: true, impliedStrict: true });
const scopeManager = analyze(ast, { ecmaVersion: 5, sourceType: "commonjs", impliedStrict: true });

expect(scopeManager.scopes).to.have.length(3);

Expand Down