Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Commit

Permalink
feat: Add sourceType:commonjs support (#81)
Browse files Browse the repository at this point in the history
* feat: Add sourceType:commonjs support

* Update sourceType references
  • Loading branch information
nzakas authored Nov 21, 2021
1 parent bc86b15 commit d756f1e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function defaultOptions() {
directive: false,
nodejsScope: false,
impliedStrict: false,
sourceType: "script", // one of ['script', 'module']
sourceType: "script", // one of ['script', 'module', 'commonjs']
ecmaVersion: 5,
childVisitorKeys: null,
fallback: "iteration"
Expand Down Expand Up @@ -122,7 +122,7 @@ function updateDeeply(target, override) {
* 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' and 'module'
* @param {string} [providedOptions.sourceType='script'] the source type of the script. one of 'script', 'module', and 'commonjs'
* @param {number} [providedOptions.ecmaVersion=5] which ECMAScript version is considered
* @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.
Expand Down
2 changes: 1 addition & 1 deletion lib/scope-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class ScopeManager {
}

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

isModule() {
Expand Down
29 changes: 28 additions & 1 deletion tests/nodejs-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import espree from "./util/espree.js";
import { analyze } from "../lib/index.js";

describe("nodejsScope option", () => {
it("creates a function scope following the global scope immediately", () => {

it("creates a function scope following the global scope immediately when nodejscope: true", () => {
const ast = espree(`
"use strict";
var hello = 20;
Expand All @@ -54,6 +55,32 @@ 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", () => {
const ast = espree(`
"use strict";
var hello = 20;
`);

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

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

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");
});

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

Expand Down

0 comments on commit d756f1e

Please sign in to comment.