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

Commit

Permalink
Breaking: Switch to ESM
Browse files Browse the repository at this point in the history
Also adds eslint-plugin-jsdoc for linter
  • Loading branch information
brettz9 committed Jun 24, 2021
1 parent 6a4e7cb commit 73f8dca
Show file tree
Hide file tree
Showing 49 changed files with 261 additions and 235 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; EditorConfig file: https://EditorConfig.org
; Install the "EditorConfig" plugin into your editor to use

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[package.json]
indent_size = 2
25 changes: 25 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";

module.exports = {
root: true,
extends: [
"eslint",
"plugin:node/recommended-module"
],
parserOptions: {
sourceType: "module",
ecmaVersion: "2020"
},
overrides: [
{
files: ".eslintrc.js",
extends: ["eslint", "plugin:node/recommended-script"]
},
{
files: ["tests/**/*"],
env: {
mocha: true
}
}
]
};
16 changes: 0 additions & 16 deletions .eslintrc.js

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ npm-debug.log
/.vscode
.sublimelinterrc
.eslint-release-info.json
dist
26 changes: 16 additions & 10 deletions Makefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
*/
/* global echo, exec, exit, set, target */

"use strict";

/* eslint no-console: 0*/
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

require("shelljs/make");
set("+e");
import path from "path";
import { fileURLToPath } from "url";

import "shelljs/make.js";
import checker from "npm-license";

const checker = require("npm-license");
const dirname = path.dirname(fileURLToPath(import.meta.url));

set("+e");

//------------------------------------------------------------------------------
// Settings
Expand All @@ -31,12 +34,15 @@ const OPEN_SOURCE_LICENSES = [
//------------------------------------------------------------------------------

const NODE = "node",
NODE_MODULES = "./node_modules/",
NODE_MODULES = "./node_modules",

// Utilities - intentional extra space at the end of each string
MOCHA = `${NODE_MODULES}mocha/bin/_mocha `,
MOCHA = `${NODE_MODULES}/mocha/bin/_mocha `,
ESLINT = `${NODE} ${NODE_MODULES}/eslint/bin/eslint `,
ISTANBUL = `${NODE} ${NODE_MODULES}/istanbul/lib/cli.js `,

// If switching back to Istanbul when may be working with ESM
// ISTANBUL = `${NODE} ${NODE_MODULES}/istanbul/lib/cli.js `,
C8 = `${NODE} ${NODE_MODULES}/c8/bin/c8.js`,

// Files
MAKEFILE = "./Makefile.js",
Expand Down Expand Up @@ -80,7 +86,7 @@ target.lint = function() {

target.test = function() {
let errors = 0;
const lastReturn = exec(`${ISTANBUL} cover ${MOCHA} -- -R progress -c ${TEST_FILES}`);
const lastReturn = exec(`${C8} ${MOCHA} -- -R progress -c ${TEST_FILES}`);

if (lastReturn.code !== 0) {
errors++;
Expand Down Expand Up @@ -116,7 +122,7 @@ target.checkLicenses = function() {
echo("Validating licenses");

checker.init({
start: __dirname
start: dirname
}, deps => {
const impermissible = Object.keys(deps).map(dependency => ({
name: dependency,
Expand Down
34 changes: 23 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,47 @@

ESLint Scope is the [ECMAScript](http://www.ecma-international.org/publications/standards/Ecma-262.htm) scope analyzer used in ESLint. It is a fork of [escope](http://github.com/estools/escope).

## Usage

Install:
## Install

```
npm i eslint-scope --save
```

## 📖 Usage

To use in an ESM file:

```js
import eslintScope from 'eslint-scope';
```

To use in a CommonJS file:

```js
const eslintScope = require('eslint-scope');
```

Example:

```js
var eslintScope = require('eslint-scope');
var espree = require('espree');
var estraverse = require('estraverse');
import eslintScope from 'eslint-scope';
import espree from 'espree';
import estraverse from 'estraverse';

var ast = espree.parse(code, { range: true });
var scopeManager = eslintScope.analyze(ast);
const ast = espree.parse(code, { range: true });
const scopeManager = eslintScope.analyze(ast);

var currentScope = scopeManager.acquire(ast); // global scope
const currentScope = scopeManager.acquire(ast); // global scope

estraverse.traverse(ast, {
enter: function(node, parent) {
enter (node, parent) {
// do stuff

if (/Function/.test(node.type)) {
currentScope = scopeManager.acquire(node); // get current function scope
}
},
leave: function(node, parent) {
leave (node, parent) {
if (/Function/.test(node.type)) {
currentScope = currentScope.upper; // set to parent scope
}
Expand Down
5 changes: 2 additions & 3 deletions lib/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";

const Variable = require("./variable");
import Variable from "./variable.js";

/**
* @constructor Definition
Expand Down Expand Up @@ -78,7 +77,7 @@ class ParameterDefinition extends Definition {
}
}

module.exports = {
export {
ParameterDefinition,
Definition
};
Expand Down
27 changes: 14 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,19 @@
* The main interface is the {@link analyze} function.
* @module escope
*/
"use strict";

/* eslint no-underscore-dangle: ["error", { "allow": ["__currentScope"] }] */

const assert = require("assert");
import { readFileSync } from "fs";
import assert from "assert";

import ScopeManager from "./scope-manager.js";
import Referencer from "./referencer.js";
import Reference from "./reference.js";
import Variable from "./variable.js";

const ScopeManager = require("./scope-manager");
const Referencer = require("./referencer");
const Reference = require("./reference");
const Variable = require("./variable");
const Scope = require("./scope").Scope;
const version = require("../package.json").version;
const { version } = JSON.parse(
readFileSync(new URL("../package.json", import.meta.url))
);

/**
* Set the default options
Expand Down Expand Up @@ -142,7 +143,7 @@ function analyze(tree, providedOptions) {
return scopeManager;
}

module.exports = {
export {

/** @name module:escope.version */
version,
Expand All @@ -153,13 +154,13 @@ module.exports = {
/** @name module:escope.Variable */
Variable,

/** @name module:escope.Scope */
Scope,

/** @name module:escope.ScopeManager */
ScopeManager,
analyze
};

/** @name module:escope.Scope */
export * from "./scope.js";


/* vim: set sw=4 ts=4 et tw=80 : */
7 changes: 3 additions & 4 deletions lib/pattern-visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";

/* eslint-disable no-undefined */

const Syntax = require("estraverse").Syntax;
const esrecurse = require("esrecurse");
import { Syntax } from "estraverse";
import esrecurse from "esrecurse";

/**
* Get last array element
Expand Down Expand Up @@ -147,6 +146,6 @@ class PatternVisitor extends esrecurse.Visitor {
}
}

module.exports = PatternVisitor;
export default PatternVisitor;

/* vim: set sw=4 ts=4 et tw=80 : */
3 changes: 1 addition & 2 deletions lib/reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";

const READ = 0x1;
const WRITE = 0x2;
Expand Down Expand Up @@ -162,6 +161,6 @@ Reference.WRITE = WRITE;
*/
Reference.RW = RW;

module.exports = Reference;
export default Reference;

/* vim: set sw=4 ts=4 et tw=80 : */
20 changes: 8 additions & 12 deletions lib/referencer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,17 @@
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";

/* eslint-disable no-underscore-dangle */
/* eslint-disable no-undefined */

const Syntax = require("estraverse").Syntax;
const esrecurse = require("esrecurse");
const Reference = require("./reference");
const Variable = require("./variable");
const PatternVisitor = require("./pattern-visitor");
const definition = require("./definition");
const assert = require("assert");

const ParameterDefinition = definition.ParameterDefinition;
const Definition = definition.Definition;
import { Syntax } from "estraverse";
import esrecurse from "esrecurse";
import Reference from "./reference.js";
import Variable from "./variable.js";
import PatternVisitor from "./pattern-visitor.js";
import { Definition, ParameterDefinition } from "./definition.js";
import assert from "assert";

/**
* Traverse identifier in pattern
Expand Down Expand Up @@ -624,6 +620,6 @@ class Referencer extends esrecurse.Visitor {
}
}

module.exports = Referencer;
export default Referencer;

/* vim: set sw=4 ts=4 et tw=80 : */
29 changes: 14 additions & 15 deletions lib/scope-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,22 @@
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";

/* eslint-disable no-underscore-dangle */

const Scope = require("./scope");
const assert = require("assert");

const GlobalScope = Scope.GlobalScope;
const CatchScope = Scope.CatchScope;
const WithScope = Scope.WithScope;
const ModuleScope = Scope.ModuleScope;
const ClassScope = Scope.ClassScope;
const SwitchScope = Scope.SwitchScope;
const FunctionScope = Scope.FunctionScope;
const ForScope = Scope.ForScope;
const FunctionExpressionNameScope = Scope.FunctionExpressionNameScope;
const BlockScope = Scope.BlockScope;
import {
GlobalScope,
CatchScope,
WithScope,
ModuleScope,
ClassScope,
SwitchScope,
FunctionScope,
ForScope,
FunctionExpressionNameScope,
BlockScope
} from "./scope.js";
import assert from "assert";

/**
* @constructor ScopeManager
Expand Down Expand Up @@ -241,6 +240,6 @@ class ScopeManager {
}
}

module.exports = ScopeManager;
export default ScopeManager;

/* vim: set sw=4 ts=4 et tw=80 : */
Loading

0 comments on commit 73f8dca

Please sign in to comment.