Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add sourceType:commonjs support #81

Merged
merged 2 commits into from Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/index.js
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
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
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