diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..9fe93ed --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/.eslintrc.cjs b/.eslintrc.cjs new file mode 100644 index 0000000..3b591b1 --- /dev/null +++ b/.eslintrc.cjs @@ -0,0 +1,30 @@ +"use strict"; + +module.exports = { + root: true, + extends: [ + "eslint", + "plugin:node/recommended-module" + ], + parserOptions: { + sourceType: "module", + ecmaVersion: "2020" + }, + overrides: [ + { + files: ["tests/**/*"], + env: { + mocha: true + } + }, + { + files: ["*.cjs"], + extends: ["eslint", "plugin:node/recommended-script"], + parserOptions: { + sourceType: "script", + ecmaVersion: "2020" + } + } + ], + ignorePatterns: ["/dist", "/coverage"] +}; diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index bc0737e..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1,16 +0,0 @@ -"use strict"; - -module.exports = { - root: true, - extends: [ - "eslint" - ], - overrides: [ - { - files: ["tests/**/*"], - env: { - mocha: true - } - } - ] -}; diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d0a5f6f..7ed8d02 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,7 +25,7 @@ jobs: strategy: matrix: os: [ubuntu-latest] - node: [14.x, 13.x, 12.x, 10.x, 8.x] + node: [16.x, 14.x, 12.x, "12.22.0"] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v2 diff --git a/.gitignore b/.gitignore index 3222b4e..f191c4c 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ npm-debug.log /.vscode .sublimelinterrc .eslint-release-info.json +dist diff --git a/Makefile.js b/Makefile.js index 952691c..31b2c95 100644 --- a/Makefile.js +++ b/Makefile.js @@ -6,24 +6,29 @@ */ /* 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 dirname = path.dirname(fileURLToPath(import.meta.url)); -const checker = require("npm-license"); +// `shelljs/make.js` global command to unset any `set('-e')` (to exit upon +// first error) +set("+e"); //------------------------------------------------------------------------------ // Settings //------------------------------------------------------------------------------ const OPEN_SOURCE_LICENSES = [ - /MIT/, /BSD/, /Apache/, /ISC/, /WTF/, /Public Domain/ + /MIT/u, /BSD/u, /Apache/u, /ISC/u, /WTF/u, /Public Domain/u ]; //------------------------------------------------------------------------------ @@ -31,17 +36,21 @@ 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", JS_FILES = "lib/**/*.js", - TEST_FILES = "tests/*.js"; + TEST_FILES = "tests/*.js", + CJS_TEST_FILES = "tests/*.cjs"; //------------------------------------------------------------------------------ // Tasks @@ -73,6 +82,12 @@ target.lint = function() { errors++; } + echo("Validating CJS JavaScript test files"); + lastReturn = exec(ESLINT + CJS_TEST_FILES); + if (lastReturn.code !== 0) { + errors++; + } + if (errors) { exit(1); } @@ -80,7 +95,13 @@ target.lint = function() { target.test = function() { let errors = 0; - const lastReturn = exec(`${ISTANBUL} cover ${MOCHA} -- -R progress -c ${TEST_FILES}`); + let lastReturn = exec(`${NODE} ${MOCHA} -- -R progress -c ${CJS_TEST_FILES}`); + + if (lastReturn.code !== 0) { + errors++; + } + + lastReturn = exec(`${C8} ${MOCHA} -- -R progress -c ${TEST_FILES}`); if (lastReturn.code !== 0) { errors++; @@ -116,7 +137,7 @@ target.checkLicenses = function() { echo("Validating licenses"); checker.init({ - start: __dirname + start: dirname }, deps => { const impermissible = Object.keys(deps).map(dependency => ({ name: dependency, diff --git a/README.md b/README.md index e824b42..4ad41d5 100644 --- a/README.md +++ b/README.md @@ -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 * as 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 * as eslintScope from 'eslint-scope'; +import * as 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 } diff --git a/lib/definition.js b/lib/definition.js index 172bfe2..9744ef4 100644 --- a/lib/definition.js +++ b/lib/definition.js @@ -21,18 +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"; -const Variable = require("./variable"); +import Variable from "./variable.js"; /** - * @class Definition + * @constructor Definition */ class Definition { constructor(type, name, node, parent, index, kind) { /** - * @member {String} Definition#type - type of the occurrence (e.g. "Parameter", "Variable", ...). + * @member {string} Definition#type - type of the occurrence (e.g. "Parameter", "Variable", ...). */ this.type = type; @@ -52,19 +51,19 @@ class Definition { this.parent = parent; /** - * @member {Number?} Definition#index - the index in the declaration statement. + * @member {number?} Definition#index - the index in the declaration statement. */ this.index = index; /** - * @member {String?} Definition#kind - the kind of the declaration statement. + * @member {string?} Definition#kind - the kind of the declaration statement. */ this.kind = kind; } } /** - * @class ParameterDefinition + * @constructor ParameterDefinition */ class ParameterDefinition extends Definition { constructor(name, node, index, rest) { @@ -78,7 +77,7 @@ class ParameterDefinition extends Definition { } } -module.exports = { +export { ParameterDefinition, Definition }; diff --git a/lib/index.js b/lib/index.js index 0f16fa4..ac386ad 100644 --- a/lib/index.js +++ b/lib/index.js @@ -45,18 +45,16 @@ * The main interface is the {@link analyze} function. * @module escope */ -"use strict"; - /* eslint no-underscore-dangle: ["error", { "allow": ["__currentScope"] }] */ -const assert = require("assert"); +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; +import eslintScopeVersion from "./version.js"; /** * Set the default options @@ -77,15 +75,15 @@ function defaultOptions() { /** * Preform deep update on option object - * @param {Object} target - Options - * @param {Object} override - Updates + * @param {Object} target Options + * @param {Object} override Updates * @returns {Object} Updated options */ function updateDeeply(target, override) { /** * Is hash object - * @param {Object} value - Test value + * @param {Object} value Test value * @returns {boolean} Result */ function isHashObject(value) { @@ -114,20 +112,20 @@ function updateDeeply(target, override) { * Main interface function. Takes an Espree syntax tree and returns the * analyzed scopes. * @function analyze - * @param {espree.Tree} tree - Abstract Syntax Tree - * @param {Object} providedOptions - Options that tailor the scope analysis - * @param {boolean} [providedOptions.optimistic=false] - the optimistic flag - * @param {boolean} [providedOptions.directive=false]- the directive flag - * @param {boolean} [providedOptions.ignoreEval=false]- whether to check 'eval()' calls - * @param {boolean} [providedOptions.nodejsScope=false]- whether the whole + * @param {espree.Tree} tree Abstract Syntax Tree + * @param {Object} providedOptions Options that tailor the scope analysis + * @param {boolean} [providedOptions.optimistic=false] the optimistic flag + * @param {boolean} [providedOptions.directive=false] the directive 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 + * @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 {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. + * @param {string} [providedOptions.sourceType='script'] the source type of the script. one of 'script' and 'module' + * @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. * @returns {ScopeManager} ScopeManager */ function analyze(tree, providedOptions) { @@ -142,10 +140,10 @@ function analyze(tree, providedOptions) { return scopeManager; } -module.exports = { +export { /** @name module:escope.version */ - version, + eslintScopeVersion as version, /** @name module:escope.Reference */ Reference, @@ -153,13 +151,22 @@ module.exports = { /** @name module:escope.Variable */ Variable, - /** @name module:escope.Scope */ - Scope, - /** @name module:escope.ScopeManager */ ScopeManager, + + /** @name module:escope.Referencer */ + Referencer, + analyze }; +/** @name module:escope.Definition */ +export { Definition } from "./definition.js"; + +/** @name module:escope.PatternVisitor */ +export { default as PatternVisitor } from "./pattern-visitor.js"; + +/** @name module:escope.Scope */ +export { Scope } from "./scope.js"; /* vim: set sw=4 ts=4 et tw=80 : */ diff --git a/lib/pattern-visitor.js b/lib/pattern-visitor.js index afa6291..a9ff48e 100644 --- a/lib/pattern-visitor.js +++ b/lib/pattern-visitor.js @@ -21,16 +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-undefined */ -const Syntax = require("estraverse").Syntax; -const esrecurse = require("esrecurse"); +import estraverse from "estraverse"; +import esrecurse from "esrecurse"; + +const { Syntax } = estraverse; /** * Get last array element - * @param {array} xs - array + * @param {Array} xs array * @returns {any} Last elment */ function getLast(xs) { @@ -147,6 +148,6 @@ class PatternVisitor extends esrecurse.Visitor { } } -module.exports = PatternVisitor; +export default PatternVisitor; /* vim: set sw=4 ts=4 et tw=80 : */ diff --git a/lib/reference.js b/lib/reference.js index 9529827..e657d62 100644 --- a/lib/reference.js +++ b/lib/reference.js @@ -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; @@ -29,7 +28,7 @@ const RW = READ | WRITE; /** * A Reference represents a single occurrence of an identifier in code. - * @class Reference + * @constructor Reference */ class Reference { constructor(ident, scope, flag, writeExpr, maybeImplicitGlobal, partial, init) { @@ -91,7 +90,7 @@ class Reference { /** * Whether the reference is static. - * @method Reference#isStatic + * @function Reference#isStatic * @returns {boolean} static */ isStatic() { @@ -100,7 +99,7 @@ class Reference { /** * Whether the reference is writeable. - * @method Reference#isWrite + * @function Reference#isWrite * @returns {boolean} write */ isWrite() { @@ -109,7 +108,7 @@ class Reference { /** * Whether the reference is readable. - * @method Reference#isRead + * @function Reference#isRead * @returns {boolean} read */ isRead() { @@ -118,7 +117,7 @@ class Reference { /** * Whether the reference is read-only. - * @method Reference#isReadOnly + * @function Reference#isReadOnly * @returns {boolean} read only */ isReadOnly() { @@ -127,7 +126,7 @@ class Reference { /** * Whether the reference is write-only. - * @method Reference#isWriteOnly + * @function Reference#isWriteOnly * @returns {boolean} write only */ isWriteOnly() { @@ -136,7 +135,7 @@ class Reference { /** * Whether the reference is read-write. - * @method Reference#isReadWrite + * @function Reference#isReadWrite * @returns {boolean} read write */ isReadWrite() { @@ -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 : */ diff --git a/lib/referencer.js b/lib/referencer.js index 7a79118..0b90ec8 100644 --- a/lib/referencer.js +++ b/lib/referencer.js @@ -21,28 +21,26 @@ (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"); +import estraverse 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"; -const ParameterDefinition = definition.ParameterDefinition; -const Definition = definition.Definition; +const { Syntax } = estraverse; /** * Traverse identifier in pattern - * @param {Object} options - options - * @param {pattern} rootPattern - root pattern - * @param {Refencer} referencer - referencer - * @param {callback} callback - callback + * @param {Object} options options + * @param {pattern} rootPattern root pattern + * @param {Refencer} referencer referencer + * @param {callback} callback callback * @returns {void} */ function traverseIdentifierInPattern(options, rootPattern, referencer, callback) { @@ -209,8 +207,8 @@ class Referencer extends esrecurse.Visitor { /** * Visit pattern callback - * @param {pattern} pattern - pattern - * @param {Object} info - info + * @param {pattern} pattern pattern + * @param {Object} info info * @returns {void} */ function visitPatternCallback(pattern, info) { @@ -643,6 +641,6 @@ class Referencer extends esrecurse.Visitor { } } -module.exports = Referencer; +export default Referencer; /* vim: set sw=4 ts=4 et tw=80 : */ diff --git a/lib/scope-manager.js b/lib/scope-manager.js index ef56170..5a12314 100644 --- a/lib/scope-manager.js +++ b/lib/scope-manager.js @@ -21,11 +21,10 @@ (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 { +import { BlockScope, CatchScope, ClassFieldInitializerScope, @@ -37,11 +36,11 @@ const { ModuleScope, SwitchScope, WithScope -} = require("./scope"); -const assert = require("assert"); +} from "./scope.js"; +import assert from "assert"; /** - * @class ScopeManager + * @constructor ScopeManager */ class ScopeManager { constructor(options) { @@ -92,8 +91,7 @@ class ScopeManager { * "are declared by the node" means the node is same as `Variable.defs[].node` or `Variable.defs[].parent`. * If the node declares nothing, this method returns an empty array. * CAUTION: This API is experimental. See https://github.com/estools/escope/pull/69 for more details. - * - * @param {Espree.Node} node - a node to get. + * @param {Espree.Node} node a node to get. * @returns {Variable[]} variables that declared by the node. */ getDeclaredVariables(node) { @@ -102,16 +100,16 @@ class ScopeManager { /** * acquire scope from node. - * @method ScopeManager#acquire - * @param {Espree.Node} node - node for the acquired scope. - * @param {boolean=} inner - look up the most inner scope, default value is false. + * @function ScopeManager#acquire + * @param {Espree.Node} node node for the acquired scope. + * @param {?boolean} [inner=false] look up the most inner scope, default value is false. * @returns {Scope?} Scope from node */ acquire(node, inner) { /** * predicate - * @param {Scope} testScope - scope to test + * @param {Scope} testScope scope to test * @returns {boolean} predicate */ function predicate(testScope) { @@ -156,8 +154,8 @@ class ScopeManager { /** * acquire all scopes from node. - * @method ScopeManager#acquireAll - * @param {Espree.Node} node - node for the acquired scope. + * @function ScopeManager#acquireAll + * @param {Espree.Node} node node for the acquired scope. * @returns {Scopes?} Scope array */ acquireAll(node) { @@ -166,9 +164,9 @@ class ScopeManager { /** * release the node. - * @method ScopeManager#release - * @param {Espree.Node} node - releasing node. - * @param {boolean=} inner - look up the most inner scope, default value is false. + * @function ScopeManager#release + * @param {Espree.Node} node releasing node. + * @param {?boolean} [inner=false] look up the most inner scope, default value is false. * @returns {Scope?} upper scope for the node. */ release(node, inner) { @@ -247,6 +245,6 @@ class ScopeManager { } } -module.exports = ScopeManager; +export default ScopeManager; /* vim: set sw=4 ts=4 et tw=80 : */ diff --git a/lib/scope.js b/lib/scope.js index 651f56a..73386e6 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -21,24 +21,25 @@ (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; +import estraverse from "estraverse"; -const Reference = require("./reference"); -const Variable = require("./variable"); -const Definition = require("./definition").Definition; -const assert = require("assert"); +import Reference from "./reference.js"; +import Variable from "./variable.js"; +import { Definition } from "./definition.js"; +import assert from "assert"; + +const { Syntax } = estraverse; /** * Test if scope is struct - * @param {Scope} scope - scope - * @param {Block} block - block - * @param {boolean} isMethodDefinition - is method definition - * @param {boolean} useDirective - use directive + * @param {Scope} scope scope + * @param {Block} block block + * @param {boolean} isMethodDefinition is method definition + * @param {boolean} useDirective use directive * @returns {boolean} is strict scope */ function isStrictScope(scope, block, isMethodDefinition, useDirective) { @@ -121,8 +122,8 @@ function isStrictScope(scope, block, isMethodDefinition, useDirective) { /** * Register scope - * @param {ScopeManager} scopeManager - scope manager - * @param {Scope} scope - scope + * @param {ScopeManager} scopeManager scope manager + * @param {Scope} scope scope * @returns {void} */ function registerScope(scopeManager, scope) { @@ -139,7 +140,7 @@ function registerScope(scopeManager, scope) { /** * Should be statically - * @param {Object} def - def + * @param {Object} def def * @returns {boolean} should be statically */ function shouldBeStatically(def) { @@ -150,14 +151,14 @@ function shouldBeStatically(def) { } /** - * @class Scope + * @constructor Scope */ class Scope { constructor(scopeManager, type, upperScope, block, isMethodDefinition) { /** * One of 'module', 'block', 'switch', 'function', 'catch', 'with', 'function', 'class', 'global'. - * @member {String} Scope#type + * @member {string} Scope#type */ this.type = type; @@ -461,8 +462,8 @@ class Scope { /** * returns resolved {Reference} - * @method Scope#resolve - * @param {Espree.Identifier} ident - identifier to be resolved. + * @function Scope#resolve + * @param {Espree.Identifier} ident identifier to be resolved. * @returns {Reference} reference */ resolve(ident) { @@ -481,7 +482,7 @@ class Scope { /** * returns this scope is static - * @method Scope#isStatic + * @function Scope#isStatic * @returns {boolean} static */ isStatic() { @@ -490,7 +491,7 @@ class Scope { /** * returns this scope has materialized arguments - * @method Scope#isArgumentsMaterialized + * @function Scope#isArgumentsMaterialized * @returns {boolean} arguemnts materialized */ isArgumentsMaterialized() { // eslint-disable-line class-methods-use-this @@ -499,7 +500,7 @@ class Scope { /** * returns this scope has materialized `this` reference - * @method Scope#isThisMaterialized + * @function Scope#isThisMaterialized * @returns {boolean} this materialized */ isThisMaterialized() { // eslint-disable-line class-methods-use-this @@ -527,10 +528,10 @@ class GlobalScope extends Scope { variables: [], /** - * List of {@link Reference}s that are left to be resolved (i.e. which - * need to be linked to the variable they refer to). - * @member {Reference[]} Scope#implicit#left - */ + * List of {@link Reference}s that are left to be resolved (i.e. which + * need to be linked to the variable they refer to). + * @member {Reference[]} Scope#implicit#left + */ left: [] }; } @@ -737,7 +738,7 @@ class ClassFieldInitializerScope extends Scope { } } -module.exports = { +export { Scope, GlobalScope, ModuleScope, diff --git a/lib/variable.js b/lib/variable.js index 702c478..286202f 100644 --- a/lib/variable.js +++ b/lib/variable.js @@ -21,19 +21,18 @@ (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"; /** * A Variable represents a locally scoped identifier. These include arguments to * functions. - * @class Variable + * @constructor Variable */ class Variable { constructor(name, scope) { /** * The variable name, as given in the source code. - * @member {String} Variable#name + * @member {string} Variable#name */ this.name = name; @@ -83,6 +82,6 @@ Variable.Variable = "Variable"; Variable.ImportBinding = "ImportBinding"; Variable.ImplicitGlobalVariable = "ImplicitGlobalVariable"; -module.exports = Variable; +export default Variable; /* vim: set sw=4 ts=4 et tw=80 : */ diff --git a/lib/version.js b/lib/version.js new file mode 100644 index 0000000..aa53e2a --- /dev/null +++ b/lib/version.js @@ -0,0 +1,3 @@ +const version = "5.1.1"; + +export default version; diff --git a/package.json b/package.json index b1739df..65c3ddb 100644 --- a/package.json +++ b/package.json @@ -2,10 +2,18 @@ "name": "eslint-scope", "description": "ECMAScript scope analyzer for ESLint", "homepage": "http://github.com/eslint/eslint-scope", - "main": "lib/index.js", + "main": "./dist/eslint-scope.cjs", + "type": "module", + "exports": { + ".": { + "import": "./lib/index.js", + "require": "./dist/eslint-scope.cjs" + }, + "./package.json": "./package.json" + }, "version": "5.1.1", "engines": { - "node": ">=8.0.0" + "node": "^12.22.0 || ^14.17.0 || >=16.0.0" }, "repository": "eslint/eslint-scope", "bugs": { @@ -13,6 +21,9 @@ }, "license": "BSD-2-Clause", "scripts": { + "prepare": "npm run update-version && npm run build", + "build": "rollup -c", + "update-version": "node tools/update-version.js", "test": "node Makefile.js test", "lint": "node Makefile.js lint", "generate-release": "eslint-generate-release", @@ -24,25 +35,28 @@ "files": [ "LICENSE", "README.md", - "lib" + "lib", + "dist/eslint-scope.cjs" ], "dependencies": { "esrecurse": "^4.3.0", "estraverse": "^5.2.0" }, "devDependencies": { - "@typescript-eslint/parser": "^1.11.0", - "chai": "^4.2.0", - "eslint": "^6.0.1", - "eslint-config-eslint": "^5.0.1", - "eslint-plugin-node": "^9.1.0", - "eslint-release": "^1.0.0", + "@typescript-eslint/parser": "^4.28.1", + "c8": "^7.7.3", + "chai": "^4.3.4", + "eslint": "^7.29.0", + "eslint-config-eslint": "^7.0.0", + "eslint-plugin-jsdoc": "^35.4.1", + "eslint-plugin-node": "^11.1.0", + "eslint-release": "^3.1.2", "eslint-visitor-keys": "^3.0.0", "espree": "^8.0.0", - "istanbul": "^0.4.5", - "mocha": "^6.1.4", + "mocha": "^9.0.1", "npm-license": "^0.3.3", - "shelljs": "^0.8.3", - "typescript": "^3.5.2" + "rollup": "^2.52.7", + "shelljs": "^0.8.4", + "typescript": "^4.3.5" } } diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..135e147 --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,10 @@ +export default { + input: "./lib/index.js", + external: ["assert", "estraverse", "esrecurse"], + treeshake: false, + output: { + format: "cjs", + file: "dist/eslint-scope.cjs", + sourcemap: true + } +}; diff --git a/tests/arguments.js b/tests/arguments.js index 60038d7..268f210 100644 --- a/tests/arguments.js +++ b/tests/arguments.js @@ -20,13 +20,12 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("arguments", () => { it("arguments are correctly materialized", () => { diff --git a/tests/catch-scope.js b/tests/catch-scope.js index 62c52d1..e27f7f6 100644 --- a/tests/catch-scope.js +++ b/tests/catch-scope.js @@ -20,13 +20,12 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("catch", () => { it("creates scope", () => { diff --git a/tests/child-visitor-keys.js b/tests/child-visitor-keys.js index 8ff0be5..d700424 100644 --- a/tests/child-visitor-keys.js +++ b/tests/child-visitor-keys.js @@ -20,11 +20,10 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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 expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("childVisitorKeys option", () => { it("should handle as a known node if the childVisitorKeys option was given.", () => { diff --git a/tests/class-fields.js b/tests/class-fields.js index a8739aa..57714d8 100644 --- a/tests/class-fields.js +++ b/tests/class-fields.js @@ -3,12 +3,10 @@ * @author Toru Nagashima */ -"use strict"; - -const assert = require("assert"); -const espree = require("espree"); -const { KEYS } = require("eslint-visitor-keys"); -const { analyze } = require("../lib/index"); +import assert from "assert"; +import * as espree from "espree"; +import { KEYS } from "eslint-visitor-keys"; +import { analyze } from "../lib/index.js"; describe("Class fields", () => { describe("class C { f = g }", () => { diff --git a/tests/commonjs.cjs b/tests/commonjs.cjs new file mode 100644 index 0000000..23f17df --- /dev/null +++ b/tests/commonjs.cjs @@ -0,0 +1,41 @@ +/** + * @fileoverview Tests for checking that the commonjs entry points are still accessible + * @author Mike Reinstein + */ + +"use strict"; + +//------------------------------------------------------------------------------ +// Requirements +//------------------------------------------------------------------------------ + +const assert = require("assert"); +const eslintScope = require("../dist/eslint-scope.cjs"); + + +//------------------------------------------------------------------------------ +// Tests +//------------------------------------------------------------------------------ + +describe("commonjs", () => { + it("is an object", () => { + assert.strictEqual(typeof eslintScope, "object"); + }); + + it("has exports", () => { + assert.strictEqual(typeof eslintScope.version, "string"); + + [ + "analyze", + "Definition", + "PatternVisitor", + "Reference", + "Referencer", + "Scope", + "ScopeManager", + "Variable" + ].forEach(prop => { + assert.strictEqual(typeof eslintScope[prop], "function"); + }); + }); +}); diff --git a/tests/es6-arrow-function-expression.js b/tests/es6-arrow-function-expression.js index 998ef73..87beafc 100644 --- a/tests/es6-arrow-function-expression.js +++ b/tests/es6-arrow-function-expression.js @@ -20,13 +20,12 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("ES6 arrow function expression", () => { it("materialize scope for arrow function expression", () => { diff --git a/tests/es6-block-scope.js b/tests/es6-block-scope.js index 1e5a21e..8ddf5bc 100644 --- a/tests/es6-block-scope.js +++ b/tests/es6-block-scope.js @@ -20,13 +20,12 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("ES6 block scope", () => { it("let is materialized in ES6 block scope#1", () => { diff --git a/tests/es6-catch.js b/tests/es6-catch.js index afc24e9..26419f3 100644 --- a/tests/es6-catch.js +++ b/tests/es6-catch.js @@ -20,13 +20,12 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("ES6 catch", () => { it("takes binding pattern", () => { diff --git a/tests/es6-class.js b/tests/es6-class.js index 93c5620..0816458 100644 --- a/tests/es6-class.js +++ b/tests/es6-class.js @@ -20,13 +20,12 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("ES6 class", () => { it("declaration name creates class scope", () => { diff --git a/tests/es6-default-parameters.js b/tests/es6-default-parameters.js index d88adc2..ef7f28d 100644 --- a/tests/es6-default-parameters.js +++ b/tests/es6-default-parameters.js @@ -20,14 +20,13 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ /* eslint-disable guard-for-in */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("ES6 default parameters:", () => { describe("a default parameter creates a writable reference for its initialization:", () => { diff --git a/tests/es6-destructuring-assignments.js b/tests/es6-destructuring-assignments.js index 89fa664..9a85808 100644 --- a/tests/es6-destructuring-assignments.js +++ b/tests/es6-destructuring-assignments.js @@ -20,13 +20,12 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("ES6 destructuring assignments", () => { it("Pattern in var in ForInStatement", () => { diff --git a/tests/es6-export.js b/tests/es6-export.js index d015b42..b683f62 100644 --- a/tests/es6-export.js +++ b/tests/es6-export.js @@ -20,11 +20,10 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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 expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("export declaration", () => { diff --git a/tests/es6-import.js b/tests/es6-import.js index 6a03886..0cd8e9d 100644 --- a/tests/es6-import.js +++ b/tests/es6-import.js @@ -20,13 +20,12 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("import declaration", () => { diff --git a/tests/es6-iteration-scope.js b/tests/es6-iteration-scope.js index fb478e6..2ed630f 100644 --- a/tests/es6-iteration-scope.js +++ b/tests/es6-iteration-scope.js @@ -20,13 +20,12 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("ES6 iteration scope", () => { it("let materialize iteration scope for ForInStatement#1", () => { diff --git a/tests/es6-new-target.js b/tests/es6-new-target.js index 16d5cdd..dc10c58 100644 --- a/tests/es6-new-target.js +++ b/tests/es6-new-target.js @@ -20,13 +20,12 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("ES6 new.target", () => { it("should not make references of new.target", () => { diff --git a/tests/es6-object.js b/tests/es6-object.js index 8f9e700..e94967d 100644 --- a/tests/es6-object.js +++ b/tests/es6-object.js @@ -20,13 +20,12 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("ES6 object", () => { it("method definition", () => { diff --git a/tests/es6-rest-args.js b/tests/es6-rest-args.js index 637f1f2..36dc61e 100644 --- a/tests/es6-rest-args.js +++ b/tests/es6-rest-args.js @@ -20,13 +20,12 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("ES6 rest arguments", () => { it("materialize rest argument in scope", () => { diff --git a/tests/es6-super.js b/tests/es6-super.js index 7a74edf..dc42e2e 100644 --- a/tests/es6-super.js +++ b/tests/es6-super.js @@ -20,11 +20,10 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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 expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("ES6 super", () => { it("is not handled as reference", () => { diff --git a/tests/es6-switch.js b/tests/es6-switch.js index 780a6e1..05be10c 100644 --- a/tests/es6-switch.js +++ b/tests/es6-switch.js @@ -20,13 +20,12 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("ES6 switch", () => { it("materialize scope", () => { diff --git a/tests/es6-template-literal.js b/tests/es6-template-literal.js index 8b1f4ec..d72a5bb 100644 --- a/tests/es6-template-literal.js +++ b/tests/es6-template-literal.js @@ -20,13 +20,12 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("ES6 template literal", () => { it("refer variables", () => { diff --git a/tests/export-star-as-ns-from-source.js b/tests/export-star-as-ns-from-source.js index 49d4257..85c98d8 100644 --- a/tests/export-star-as-ns-from-source.js +++ b/tests/export-star-as-ns-from-source.js @@ -1,9 +1,7 @@ -"use strict"; - -const assert = require("assert"); -const espree = require("espree"); -const { KEYS } = require("eslint-visitor-keys"); -const { analyze } = require("../lib/index"); +import assert from "assert"; +import * as espree from "espree"; +import { KEYS } from "eslint-visitor-keys"; +import { analyze } from "../lib/index.js"; describe("export * as ns from 'source'", () => { let scopes; diff --git a/tests/fallback.js b/tests/fallback.js index 6c7dde5..cfbc6a3 100644 --- a/tests/fallback.js +++ b/tests/fallback.js @@ -20,12 +20,10 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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 expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("fallback option", () => { it("should raise an error when it encountered an unknown node if no fallback.", () => { diff --git a/tests/function-expression-name.js b/tests/function-expression-name.js index f982b83..ef238d8 100644 --- a/tests/function-expression-name.js +++ b/tests/function-expression-name.js @@ -20,13 +20,12 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("function name", () => { it("should create its special scope", () => { diff --git a/tests/get-declared-variables.js b/tests/get-declared-variables.js index ee42289..48b54eb 100644 --- a/tests/get-declared-variables.js +++ b/tests/get-declared-variables.js @@ -20,15 +20,16 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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 expect = require("chai").expect; -const visit = require("esrecurse").visit; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import esrecurse from "esrecurse"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; + +const { visit } = esrecurse; describe("ScopeManager.prototype.getDeclaredVariables", () => { - /* eslint-disable require-jsdoc */ + /* eslint-disable jsdoc/require-jsdoc */ function verify(ast, type, expectedNamesList) { const scopeManager = analyze(ast, { ecmaVersion: 6, diff --git a/tests/global-increment.js b/tests/global-increment.js index df48b94..53517d9 100644 --- a/tests/global-increment.js +++ b/tests/global-increment.js @@ -20,13 +20,12 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("global increment", () => { it("becomes read/write", () => { diff --git a/tests/implicit-global-reference.js b/tests/implicit-global-reference.js index 011a5f8..4c869ff 100644 --- a/tests/implicit-global-reference.js +++ b/tests/implicit-global-reference.js @@ -19,11 +19,10 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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 expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("implicit global reference", () => { it("assignments global scope", () => { diff --git a/tests/implied-strict.js b/tests/implied-strict.js index 3896941..2d07869 100644 --- a/tests/implied-strict.js +++ b/tests/implied-strict.js @@ -20,13 +20,12 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("impliedStrict option", () => { it("ensures all user scopes are strict if ecmaVersion >= 5", () => { diff --git a/tests/label.js b/tests/label.js index 1605a23..f3330b3 100644 --- a/tests/label.js +++ b/tests/label.js @@ -20,13 +20,12 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("label", () => { it("should not create variables", () => { diff --git a/tests/nodejs-scope.js b/tests/nodejs-scope.js index 2ce8cf7..c3a90ca 100644 --- a/tests/nodejs-scope.js +++ b/tests/nodejs-scope.js @@ -20,13 +20,12 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +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", () => { diff --git a/tests/object-expression.js b/tests/object-expression.js index 81b4a9d..f3440d7 100644 --- a/tests/object-expression.js +++ b/tests/object-expression.js @@ -1,7 +1,5 @@ -"use strict"; - -const expect = require("chai").expect; -const analyze = require("..").analyze; +import { expect } from "chai"; +import { analyze } from "../lib/index.js"; describe("object expression", () => { it("doesn't require property type", () => { diff --git a/tests/optimistic.js b/tests/optimistic.js index ddf0d06..941e724 100644 --- a/tests/optimistic.js +++ b/tests/optimistic.js @@ -19,11 +19,10 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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 expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("optimistic", () => { it("direct call to eval", () => { diff --git a/tests/references.js b/tests/references.js index 6de0a1b..b9b4213 100644 --- a/tests/references.js +++ b/tests/references.js @@ -20,13 +20,12 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("References:", () => { describe("When there is a `let` declaration on global,", () => { diff --git a/tests/typescript.js b/tests/typescript.js index 0b312b3..8ac53c2 100644 --- a/tests/typescript.js +++ b/tests/typescript.js @@ -2,7 +2,6 @@ * @fileoverview Typescript scope tests * @author Reyad Attiyat */ -"use strict"; /* eslint-disable no-unused-expressions */ @@ -10,9 +9,11 @@ // Requirements //------------------------------------------------------------------------------ -const expect = require("chai").expect, - parse = require("@typescript-eslint/parser").parse, - analyze = require("..").analyze; +import { expect } from "chai"; +import typescriptEslintParser from "@typescript-eslint/parser"; +import { analyze } from "../lib/index.js"; + +const { parse } = typescriptEslintParser; //------------------------------------------------------------------------------ // Tests @@ -27,7 +28,9 @@ describe("typescript", () => { function foo(bar: string | number): string | number { return bar; } - `); + `, { + range: true + }); const scopeManager = analyze(ast); diff --git a/tests/util/espree.js b/tests/util/espree.js index 6804513..3e3de7a 100644 --- a/tests/util/espree.js +++ b/tests/util/espree.js @@ -21,18 +21,21 @@ (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 espree = require("espree"); - -module.exports = function(code, sourceType) { - sourceType = sourceType || "module"; +import * as espree from "espree"; +/** + * Parse into Espree AST. + * @param {string} code The code + * @param {"module"|"script"} [sourceType="module"] The source type + * @returns {Object} The parsed Espree AST + */ +export default function(code, sourceType = "module") { return espree.parse(code, { range: true, ecmaVersion: 7, sourceType }); -}; +} /* vim: set sw=4 ts=4 et tw=80 : */ diff --git a/tests/with-scope.js b/tests/with-scope.js index 2d33e3d..f90bd33 100644 --- a/tests/with-scope.js +++ b/tests/with-scope.js @@ -20,13 +20,12 @@ // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (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-unused-expressions */ -const expect = require("chai").expect; -const espree = require("./util/espree"); -const analyze = require("..").analyze; +import { expect } from "chai"; +import espree from "./util/espree.js"; +import { analyze } from "../lib/index.js"; describe("with", () => { it("creates scope", () => { diff --git a/tools/update-version.js b/tools/update-version.js new file mode 100644 index 0000000..977bcaf --- /dev/null +++ b/tools/update-version.js @@ -0,0 +1,18 @@ +/** + * @fileoverview Script to update lib/version.js to the value in package.json + * @author Nicholas C. Zakas + */ + +import fs from "fs"; + +/* + * IMPORTANT: This must be run *before* Rollup so the built package will have + * the correct version number exported. + * + * This is necessary because ESM can't import JSON files directly and we want + * this value to be available in the browser as well as in Node.js. + */ + +const pkg = JSON.parse(fs.readFileSync("./package.json", "utf8")); + +fs.writeFileSync("lib/version.js", `const version = "${pkg.version}";\n\nexport default version;\n`);