Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

Commit d756f1e

Browse files
authored
feat: Add sourceType:commonjs support (#81)
* feat: Add sourceType:commonjs support * Update sourceType references
1 parent bc86b15 commit d756f1e

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function defaultOptions() {
6666
directive: false,
6767
nodejsScope: false,
6868
impliedStrict: false,
69-
sourceType: "script", // one of ['script', 'module']
69+
sourceType: "script", // one of ['script', 'module', 'commonjs']
7070
ecmaVersion: 5,
7171
childVisitorKeys: null,
7272
fallback: "iteration"
@@ -122,7 +122,7 @@ function updateDeeply(target, override) {
122122
* a function scope immediately following the global scope.
123123
* @param {boolean} [providedOptions.impliedStrict=false] implied strict mode
124124
* (if ecmaVersion >= 5).
125-
* @param {string} [providedOptions.sourceType='script'] the source type of the script. one of 'script' and 'module'
125+
* @param {string} [providedOptions.sourceType='script'] the source type of the script. one of 'script', 'module', and 'commonjs'
126126
* @param {number} [providedOptions.ecmaVersion=5] which ECMAScript version is considered
127127
* @param {Object} [providedOptions.childVisitorKeys=null] Additional known visitor keys. See [esrecurse](https://github.com/estools/esrecurse)'s the `childVisitorKeys` option.
128128
* @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.

lib/scope-manager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class ScopeManager {
6666
}
6767

6868
__isNodejsScope() {
69-
return this.__options.nodejsScope;
69+
return this.__options.nodejsScope || this.__options.sourceType === "commonjs";
7070
}
7171

7272
isModule() {

tests/nodejs-scope.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import espree from "./util/espree.js";
2828
import { analyze } from "../lib/index.js";
2929

3030
describe("nodejsScope option", () => {
31-
it("creates a function scope following the global scope immediately", () => {
31+
32+
it("creates a function scope following the global scope immediately when nodejscope: true", () => {
3233
const ast = espree(`
3334
"use strict";
3435
var hello = 20;
@@ -54,6 +55,32 @@ describe("nodejsScope option", () => {
5455
expect(scope.variables[1].name).to.be.equal("hello");
5556
});
5657

58+
it("creates a function scope following the global scope immediately when sourceType:commonjs", () => {
59+
const ast = espree(`
60+
"use strict";
61+
var hello = 20;
62+
`);
63+
64+
const scopeManager = analyze(ast, { ecmaVersion: 6, sourceType: "commonjs" });
65+
66+
expect(scopeManager.scopes).to.have.length(2);
67+
68+
let scope = scopeManager.scopes[0];
69+
70+
expect(scope.type).to.be.equal("global");
71+
expect(scope.block.type).to.be.equal("Program");
72+
expect(scope.isStrict).to.be.false;
73+
expect(scope.variables).to.have.length(0);
74+
75+
scope = scopeManager.scopes[1];
76+
expect(scope.type).to.be.equal("function");
77+
expect(scope.block.type).to.be.equal("Program");
78+
expect(scope.isStrict).to.be.true;
79+
expect(scope.variables).to.have.length(2);
80+
expect(scope.variables[0].name).to.be.equal("arguments");
81+
expect(scope.variables[1].name).to.be.equal("hello");
82+
});
83+
5784
it("creates a function scope following the global scope immediately and creates module scope", () => {
5885
const ast = espree("import {x as v} from 'mod';");
5986

0 commit comments

Comments
 (0)