From 145ed7b5727b8d7f15c80a23a94ce31ed8064168 Mon Sep 17 00:00:00 2001 From: Reyad Attiyat Date: Fri, 10 Feb 2017 20:14:34 -0600 Subject: [PATCH 1/2] Chore: Add Makefile.js and eslint This adds a Makefile that will run the tests, lint the code, and check licenses --- .eslintrc.yml | 7 +++ .gitignore | 3 +- .jshintrc | 20 ------ Makefile.js | 148 +++++++++++++++++++++++++++++++++++++++++++++ gulpfile.js | 118 ------------------------------------ package.json | 27 +++------ test/.eslintrc.yml | 4 ++ 7 files changed, 170 insertions(+), 157 deletions(-) create mode 100644 .eslintrc.yml delete mode 100644 .jshintrc create mode 100644 Makefile.js delete mode 100644 gulpfile.js create mode 100644 test/.eslintrc.yml diff --git a/.eslintrc.yml b/.eslintrc.yml new file mode 100644 index 0000000..9001d6c --- /dev/null +++ b/.eslintrc.yml @@ -0,0 +1,7 @@ +env: + node: true + es6: true +parserOptions: + ecmaVersion: 6 + +extends: eslint diff --git a/.gitignore b/.gitignore index b448737..ab24919 100644 --- a/.gitignore +++ b/.gitignore @@ -8,8 +8,7 @@ node_modules/ npm-debug.log # Cover -.coverage_data/ -cover_html/ +/coverage/ npm-debug.log .vimrc.local diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index defbf02..0000000 --- a/.jshintrc +++ /dev/null @@ -1,20 +0,0 @@ -{ - "curly": true, - "eqeqeq": true, - "immed": true, - "indent": 4, - "eqnull": true, - "latedef": true, - "noarg": true, - "noempty": true, - "quotmark": "single", - "undef": true, - "unused": true, - "strict": true, - "trailing": true, - "validthis": true, - - "onevar": true, - - "node": true -} diff --git a/Makefile.js b/Makefile.js new file mode 100644 index 0000000..76ee68c --- /dev/null +++ b/Makefile.js @@ -0,0 +1,148 @@ +/** + * @fileoverview Build file + * @author nzakas + * @copyright jQuery Foundation and other contributors, https://jquery.org/ + * MIT License + */ +/* global echo, exec, exit, set, target */ + +"use strict"; + +/* eslint no-console: 0*/ +//------------------------------------------------------------------------------ +// Requirements +//------------------------------------------------------------------------------ + +require("shelljs/make"); +set("+e"); + +const checker = require("npm-license"); + +//------------------------------------------------------------------------------ +// Settings +//------------------------------------------------------------------------------ + +const OPEN_SOURCE_LICENSES = [ + /MIT/, /BSD/, /Apache/, /ISC/, /WTF/, /Public Domain/ +]; + +//------------------------------------------------------------------------------ +// Data +//------------------------------------------------------------------------------ + +const NODE = "node", + NODE_MODULES = "./node_modules/", + + // Utilities - intentional extra space at the end of each string + MOCHA = NODE_MODULES + "mocha/bin/_mocha ", + ESLINT = `${NODE} ${NODE_MODULES}/eslint/bin/eslint `, + ISTANBUL = `${NODE} ${NODE_MODULES}/istanbul/lib/cli.js `, + + // Files + MAKEFILE = "./Makefile.js", + JS_FILES = "src/**/*.js", + TEST_FILES = "test/**/*.js"; + +//------------------------------------------------------------------------------ +// Tasks +//------------------------------------------------------------------------------ + +target.all = function() { + target.test(); +}; + +target.lint = function() { + var errors = 0, + lastReturn; + + echo("Validating Makefile.js"); + lastReturn = exec(ESLINT + MAKEFILE); + if (lastReturn.code !== 0) { + errors++; + } + + echo("Validating JavaScript files"); + lastReturn = exec(ESLINT + JS_FILES); + if (lastReturn.code !== 0) { + errors++; + } + + echo("Validating JavaScript test files"); + lastReturn = exec(ESLINT + TEST_FILES); + if (lastReturn.code !== 0) { + errors++; + } + + if (errors) { + exit(1); + } +}; + +target.test = function() { + target.lint(); + + var errors = 0, + lastReturn; + + lastReturn = exec(`${ISTANBUL} cover ${MOCHA} -- -R progress -c ${TEST_FILES}`); + + if (lastReturn.code !== 0) { + errors++; + } + + if (errors) { + exit(1); + } + + target.checkLicenses(); +}; + +target.checkLicenses = function() { + + /** + * Returns true if the given dependency's licenses are all permissable for use in OSS + * @param {object} dependency object containing the name and licenses of the given dependency + * @returns {boolean} is permissable dependency + */ + function isPermissible(dependency) { + var licenses = dependency.licenses; + + if (Array.isArray(licenses)) { + return licenses.some(function(license) { + return isPermissible({ + name: dependency.name, + licenses: license + }); + }); + } + + return OPEN_SOURCE_LICENSES.some(function(license) { + return license.test(licenses); + }); + } + + echo("Validating licenses"); + + checker.init({ + start: __dirname + }, function(deps) { + var impermissible = Object.keys(deps).map(function(dependency) { + return { + name: dependency, + licenses: deps[dependency].licenses + }; + }).filter(function(dependency) { + return !isPermissible(dependency); + }); + + if (impermissible.length) { + impermissible.forEach(function(dependency) { + console.error("%s license for %s is impermissible.", + dependency.licenses, + dependency.name + ); + }); + exit(1); + } + }); +}; diff --git a/gulpfile.js b/gulpfile.js deleted file mode 100644 index 6cf9d2c..0000000 --- a/gulpfile.js +++ /dev/null @@ -1,118 +0,0 @@ -/* - Copyright (C) 2014 Yusuke Suzuki - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - 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'; - -var gulp = require('gulp'), - mocha = require('gulp-mocha'), - git = require('gulp-git'), - bump = require('gulp-bump'), - filter = require('gulp-filter'), - tagVersion = require('gulp-tag-version'), - sourcemaps = require('gulp-sourcemaps'), - plumber = require('gulp-plumber'), - source = require('vinyl-source-stream'), - lazypipe = require('lazypipe'), - eslint = require('gulp-eslint'), - fs = require('fs'); - -var TEST = [ 'test/*.js' ]; -var SOURCE = [ 'src/**/*.js' ]; - -var ESLINT_OPTION = { - rules: { - 'quotes': 0, - 'eqeqeq': 0, - 'no-use-before-define': 0, - 'no-shadow': 0, - 'no-new': 0, - 'no-underscore-dangle': 0, - 'no-multi-spaces': 0, - 'no-native-reassign': 0, - 'no-loop-func': 0, - 'no-lone-blocks': 0 - }, - ecmaFeatures: { - jsx: false, - modules: true - }, - env: { - node: true, - es6: true - } -}; - -gulp.task('test', function () { - return gulp.src(TEST) - .pipe(mocha({ - reporter: 'spec', - timeout: 100000 // 100s - })); -}); - -// Currently, not works for ES6. -gulp.task('lint', function () { - return gulp.src(SOURCE) - .pipe(eslint(ESLINT_OPTION)) - .pipe(eslint.formatEach('stylish', process.stderr)) - .pipe(eslint.failOnError()); -}); - -/** - * Bumping version number and tagging the repository with it. - * Please read http://semver.org/ - * - * You can use the commands - * - * gulp patch # makes v0.1.0 -> v0.1.1 - * gulp feature # makes v0.1.1 -> v0.2.0 - * gulp release # makes v0.2.1 -> v1.0.0 - * - * To bump the version numbers accordingly after you did a patch, - * introduced a feature or made a backwards-incompatible release. - */ - -function inc(importance) { - // get all the files to bump version in - return gulp.src(['./package.json']) - // bump the version number in those files - .pipe(bump({type: importance})) - // save it back to filesystem - .pipe(gulp.dest('./')) - // commit the changed version number - .pipe(git.commit('Bumps package version')) - // read only one file to get the version number - .pipe(filter('package.json')) - // **tag it in the repository** - .pipe(tagVersion({ - prefix: '' - })); -} - -gulp.task('patch', function () { return inc('patch'); }) -gulp.task('minor', function () { return inc('minor'); }) -gulp.task('major', function () { return inc('major'); }) - -gulp.task('travis', [ 'test' ]); -gulp.task('default', [ 'travis' ]); diff --git a/package.json b/package.json index 9135482..00be847 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "src/index.js", "version": "3.6.0", "engines": { - "node": ">=0.4.0" + "node": ">=4.0.0" }, "repository": "eslint/eslint-scope", "bugs": { @@ -13,9 +13,8 @@ }, "license": "BSD-2-Clause", "scripts": { - "test": "gulp travis", - "unit-test": "gulp test", - "lint": "gulp lint", + "test": "node Makefile.js test", + "lint": "node Makefile.js lint", "release": "eslint-release", "ci-release": "eslint-ci-release", "gh-release": "eslint-gh-release", @@ -28,22 +27,16 @@ }, "devDependencies": { "chai": "^3.4.1", + "eslint": "^3.15.0", + "eslint-config-eslint": "^3.0.0", "eslint-release": "^0.10.1", "espree": "^3.1.1", "esprima": "^2.7.1", - "gulp": "^3.9.0", - "gulp-bump": "^1.0.0", - "gulp-eslint": "^1.1.1", - "gulp-espower": "^1.0.2", - "gulp-filter": "^3.0.1", - "gulp-git": "^1.6.1", - "gulp-mocha": "^2.2.0", - "gulp-plumber": "^1.0.1", - "gulp-sourcemaps": "^1.6.0", - "gulp-tag-version": "^1.3.0", - "lazypipe": "^1.0.1", - "vinyl-source-stream": "^1.1.0", + "istanbul": "^0.4.5", + "mocha": "^3.2.0", + "npm-license": "^0.3.3", + "shelljs": "^0.7.6", "typescript": "~2.0.10", "typescript-eslint-parser": "^1.0.0" } -} \ No newline at end of file +} diff --git a/test/.eslintrc.yml b/test/.eslintrc.yml new file mode 100644 index 0000000..1278dab --- /dev/null +++ b/test/.eslintrc.yml @@ -0,0 +1,4 @@ +env: + mocha: true + +extends: '../.eslintrc.yml' From 7eaf45ed71a6869e622c8990cd6af322c54f2cff Mon Sep 17 00:00:00 2001 From: Reyad Attiyat Date: Sat, 11 Feb 2017 16:11:38 -0600 Subject: [PATCH 2/2] Chore: Fix linting rules --- src/definition.js | 2 +- src/index.js | 45 +- src/pattern-visitor.js | 17 +- src/reference.js | 14 +- src/referencer.js | 76 ++- src/scope-manager.js | 27 +- src/scope.js | 110 ++-- src/variable.js | 16 +- test/arguments.js | 18 +- test/catch-scope.js | 22 +- test/child-visitor-keys.js | 26 +- test/es6-arrow-function-expression.js | 44 +- test/es6-block-scope.js | 80 +-- test/es6-catch.js | 44 +- test/es6-class.js | 106 ++-- test/es6-default-parameters.js | 225 ++++--- test/es6-destructuring-assignments.js | 822 +++++++++++++------------- test/es6-export.js | 118 ++-- test/es6-import.js | 66 ++- test/es6-iteration-scope.js | 144 ++--- test/es6-new-target.js | 18 +- test/es6-object.js | 42 +- test/es6-rest-args.js | 40 +- test/es6-super.js | 26 +- test/es6-switch.js | 36 +- test/es6-template-literal.js | 42 +- test/fallback.js | 24 +- test/function-expression-name.js | 22 +- test/get-declared-variables.js | 159 ++--- test/global-increment.js | 16 +- test/implicit-global-reference.js | 60 +- test/implied-strict.js | 64 +- test/label.js | 30 +- test/nodejs-scope.js | 46 +- test/object-expression.js | 36 +- test/optimistic.js | 28 +- test/references.js | 234 ++++---- test/typescript.js | 28 +- test/with-scope.js | 20 +- 39 files changed, 1555 insertions(+), 1438 deletions(-) diff --git a/src/definition.js b/src/definition.js index 54ee3c6..20efbb9 100644 --- a/src/definition.js +++ b/src/definition.js @@ -23,7 +23,7 @@ */ "use strict"; -const Variable = require('./variable'); +const Variable = require("./variable"); /** * @class Definition diff --git a/src/index.js b/src/index.js index 320dfd8..6074848 100644 --- a/src/index.js +++ b/src/index.js @@ -47,35 +47,50 @@ */ "use strict"; -/*jslint bitwise:true */ +/* eslint no-underscore-dangle: ["error", { "allow": ["__currentScope"] }] */ -const assert = require('assert'); +const assert = require("assert"); -const ScopeManager = require('./scope-manager'); -const Referencer = require('./referencer'); -const Reference = require('./reference'); -const Variable = require('./variable'); -const Scope = require('./scope'); -const version = require('../package.json').version; +const ScopeManager = require("./scope-manager"); +const Referencer = require("./referencer"); +const Reference = require("./reference"); +const Variable = require("./variable"); +const Scope = require("./scope"); +const version = require("../package.json").version; +/** + * Set the default options + * @returns {Object} options + */ function defaultOptions() { return { optimistic: false, directive: false, nodejsScope: false, impliedStrict: false, - sourceType: 'script', // one of ['script', 'module'] + sourceType: "script", // one of ['script', 'module'] ecmaVersion: 5, childVisitorKeys: null, - fallback: 'iteration' + fallback: "iteration" }; } +/** + * Preform deep update on option object + * @param {Object} target - Options + * @param {Object} override - Updates + * @returns {Object} Updated options + */ function updateDeeply(target, override) { var key, val; - function isHashObject(target) { - return typeof target === 'object' && target instanceof Object && !(target instanceof Array) && !(target instanceof RegExp); + /** + * Is hash object + * @param {Object} value - Test value + * @returns {boolean} Result + */ + function isHashObject(value) { + return typeof value === "object" && value instanceof Object && !(value instanceof Array) && !(value instanceof RegExp); } for (key in override) { @@ -99,7 +114,7 @@ function updateDeeply(target, override) { * Main interface function. Takes an Esprima syntax tree and returns the * analyzed scopes. * @function analyze - * @param {esprima.Tree} tree + * @param {esprima.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 @@ -113,7 +128,7 @@ function updateDeeply(target, override) { * @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. - * @return {ScopeManager} + * @returns {ScopeManager} ScopeManager */ function analyze(tree, providedOptions) { var scopeManager, referencer, options; @@ -125,7 +140,7 @@ function analyze(tree, providedOptions) { referencer = new Referencer(options, scopeManager); referencer.visit(tree); - assert(scopeManager.__currentScope === null, 'currentScope should be null.'); + assert(scopeManager.__currentScope === null, "currentScope should be null."); return scopeManager; } diff --git a/src/pattern-visitor.js b/src/pattern-visitor.js index bd4bcb7..dbc793a 100644 --- a/src/pattern-visitor.js +++ b/src/pattern-visitor.js @@ -23,9 +23,16 @@ */ "use strict"; -const Syntax = require('estraverse').Syntax; -const esrecurse = require('esrecurse'); +/* eslint-disable no-undefined */ +const Syntax = require("estraverse").Syntax; +const esrecurse = require("esrecurse"); + +/** + * Get last array element + * @param {array} xs - array + * @returns {any} Last elment + */ function getLast(xs) { return xs[xs.length - 1] || null; } @@ -56,7 +63,7 @@ class PatternVisitor extends esrecurse.Visitor { const lastRestElement = getLast(this.restElements); this.callback(pattern, { topLevel: pattern === this.rootPattern, - rest: lastRestElement != null && lastRestElement.argument === pattern, + rest: lastRestElement !== null && lastRestElement !== undefined && lastRestElement.argument === pattern, assignments: this.assignments }); } @@ -127,7 +134,9 @@ class PatternVisitor extends esrecurse.Visitor { CallExpression(node) { // arguments are right hand nodes. - node.arguments.forEach(a => { this.rightHandNodes.push(a); }); + node.arguments.forEach(a => { + this.rightHandNodes.push(a); + }); this.visit(node.callee); } } diff --git a/src/reference.js b/src/reference.js index 8775440..9463a36 100644 --- a/src/reference.js +++ b/src/reference.js @@ -32,7 +32,7 @@ const RW = READ | WRITE; * @class Reference */ class Reference { - constructor(ident, scope, flag, writeExpr, maybeImplicitGlobal, partial, init) { + constructor(ident, scope, flag, writeExpr, maybeImplicitGlobal, partial, init) { /** * Identifier syntax node. * @member {esprima#Identifier} Reference#identifier @@ -84,7 +84,7 @@ class Reference { /** * Whether the reference is static. * @method Reference#isStatic - * @return {boolean} + * @returns {boolean} static */ isStatic() { return !this.tainted && this.resolved && this.resolved.scope.isStatic(); @@ -93,7 +93,7 @@ class Reference { /** * Whether the reference is writeable. * @method Reference#isWrite - * @return {boolean} + * @returns {boolean} write */ isWrite() { return !!(this.flag & Reference.WRITE); @@ -102,7 +102,7 @@ class Reference { /** * Whether the reference is readable. * @method Reference#isRead - * @return {boolean} + * @returns {boolean} read */ isRead() { return !!(this.flag & Reference.READ); @@ -111,7 +111,7 @@ class Reference { /** * Whether the reference is read-only. * @method Reference#isReadOnly - * @return {boolean} + * @returns {boolean} read only */ isReadOnly() { return this.flag === Reference.READ; @@ -120,7 +120,7 @@ class Reference { /** * Whether the reference is write-only. * @method Reference#isWriteOnly - * @return {boolean} + * @returns {boolean} write only */ isWriteOnly() { return this.flag === Reference.WRITE; @@ -129,7 +129,7 @@ class Reference { /** * Whether the reference is read-write. * @method Reference#isReadWrite - * @return {boolean} + * @returns {boolean} read write */ isReadWrite() { return this.flag === Reference.RW; diff --git a/src/referencer.js b/src/referencer.js index 9091fde..5ce74bb 100644 --- a/src/referencer.js +++ b/src/referencer.js @@ -23,24 +23,35 @@ */ "use strict"; -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'); +/* 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; +/** + * Traverse identifier in pattern + * @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) { // Call the callback at left hand identifier nodes, and Collect right hand nodes. var visitor = new PatternVisitor(options, rootPattern, callback); visitor.visit(rootPattern); // Process the right hand nodes recursively. - if (referencer != null) { + if (referencer !== null && referencer !== undefined) { visitor.rightHandNodes.forEach(referencer.visit, referencer); } } @@ -156,9 +167,9 @@ class Referencer extends esrecurse.Visitor { } visitPattern(node, options, callback) { - if (typeof options === 'function') { + if (typeof options === "function") { callback = options; - options = {processRightHandNodes: false} + options = {processRightHandNodes: false}; } traverseIdentifierInPattern( this.options, @@ -196,25 +207,34 @@ class Referencer extends esrecurse.Visitor { // Consider this function is in the MethodDefinition. this.scopeManager.__nestFunctionScope(node, this.isInnerMethodDefinition); + const that = this; + /** + * Visit pattern callback + * @param {pattern} pattern - pattern + * @param {Object} info - info + * @returns {void} + */ + function visitPatternCallback(pattern, info) { + that.currentScope().__define(pattern, + new ParameterDefinition( + pattern, + node, + i, + info.rest + )); + + that.referencingDefaultValue(pattern, info.assignments, null, true); + } + // Process parameter declarations. for (i = 0, iz = node.params.length; i < iz; ++i) { - this.visitPattern(node.params[i], {processRightHandNodes: true}, (pattern, info) => { - this.currentScope().__define(pattern, - new ParameterDefinition( - pattern, - node, - i, - info.rest - )); - - this.referencingDefaultValue(pattern, info.assignments, null, true); - }); + this.visitPattern(node.params[i], {processRightHandNodes: true}, visitPatternCallback); } // if there's a rest argument, add that if (node.rest) { this.visitPattern({ - type: 'RestElement', + type: "RestElement", argument: node.rest }, (pattern) => { this.currentScope().__define(pattern, @@ -289,7 +309,7 @@ class Referencer extends esrecurse.Visitor { } visitForIn(node) { - if (node.left.type === Syntax.VariableDeclaration && node.left.kind !== 'var') { + if (node.left.type === Syntax.VariableDeclaration && node.left.kind !== "var") { this.materializeTDZScope(node.right, node); this.visit(node.right); this.close(node.right); @@ -349,7 +369,7 @@ class Referencer extends esrecurse.Visitor { AssignmentExpression(node) { if (PatternVisitor.isPattern(node.left)) { - if (node.operator === '=') { + if (node.operator === "=") { this.visitPattern(node.left, {processRightHandNodes: true}, (pattern, info) => { var maybeImplicitGlobal = null; if (!this.currentScope().isStrict) { @@ -451,7 +471,7 @@ class Referencer extends esrecurse.Visitor { // NOTE: In ES6, ForStatement dynamically generates // per iteration environment. However, escope is // a static analyzer, we only generate one scope for ForStatement. - if (node.init && node.init.type === Syntax.VariableDeclaration && node.init.kind !== 'var') { + if (node.init && node.init.type === Syntax.VariableDeclaration && node.init.kind !== "var") { this.scopeManager.__nestForScope(node); } @@ -470,7 +490,7 @@ class Referencer extends esrecurse.Visitor { CallExpression(node) { // Check this is direct call to eval - if (!this.scopeManager.__ignoreEval() && node.callee.type === Syntax.Identifier && node.callee.name === 'eval') { + if (!this.scopeManager.__ignoreEval() && node.callee.type === Syntax.Identifier && node.callee.name === "eval") { // NOTE: This should be `variableScope`. Since direct eval call always creates Lexical environment and // let / const should be enclosed into it. Only VariableDeclaration affects on the caller's environment. this.currentScope().variableScope.__detectEval(); @@ -504,7 +524,7 @@ class Referencer extends esrecurse.Visitor { VariableDeclaration(node) { var variableTargetScope, i, iz, decl; - variableTargetScope = (node.kind === 'var') ? this.currentScope().variableScope : this.currentScope(); + variableTargetScope = (node.kind === "var") ? this.currentScope().variableScope : this.currentScope(); for (i = 0, iz = node.declarations.length; i < iz; ++i) { decl = node.declarations[i]; this.visitVariableDeclaration(variableTargetScope, Variable.Variable, node, i); @@ -554,7 +574,7 @@ class Referencer extends esrecurse.Visitor { ImportDeclaration(node) { var importer; - assert(this.scopeManager.__isES6() && this.scopeManager.isModule(), 'ImportDeclaration should appear when the mode is ES6 and in the module context.'); + assert(this.scopeManager.__isES6() && this.scopeManager.isModule(), "ImportDeclaration should appear when the mode is ES6 and in the module context."); importer = new Importer(node, this); importer.visit(node); diff --git a/src/scope-manager.js b/src/scope-manager.js index b29b9b1..9bd21bf 100644 --- a/src/scope-manager.js +++ b/src/scope-manager.js @@ -23,8 +23,10 @@ */ "use strict"; -const Scope = require('./scope'); -const assert = require('assert'); +/* eslint-disable no-underscore-dangle */ + +const Scope = require("./scope"); +const assert = require("assert"); const GlobalScope = Scope.GlobalScope; const CatchScope = Scope.CatchScope; @@ -68,7 +70,7 @@ class ScopeManager { } isModule() { - return this.__options.sourceType === 'module'; + return this.__options.sourceType === "module"; } isImpliedStrict() { @@ -103,16 +105,21 @@ class ScopeManager { * @method ScopeManager#acquire * @param {Esprima.Node} node - node for the acquired scope. * @param {boolean=} inner - look up the most inner scope, default value is false. - * @return {Scope?} + * @returns {Scope?} Scope from node */ acquire(node, inner) { var scopes, scope, i, iz; - function predicate(scope) { - if (scope.type === 'function' && scope.functionExpressionScope) { + /** + * predicate + * @param {Scope} testScope - scope to test + * @returns {boolean} predicate + */ + function predicate(testScope) { + if (testScope.type === "function" && testScope.functionExpressionScope) { return false; } - if (scope.type === 'TDZ') { + if (testScope.type === "TDZ") { return false; } return true; @@ -152,7 +159,7 @@ class ScopeManager { * acquire all scopes from node. * @method ScopeManager#acquireAll * @param {Esprima.Node} node - node for the acquired scope. - * @return {Scope[]?} + * @returns {Scopes?} Scope array */ acquireAll(node) { return this.__get(node); @@ -163,7 +170,7 @@ class ScopeManager { * @method ScopeManager#release * @param {Esprima.Node} node - releasing node. * @param {boolean=} inner - look up the most inner scope, default value is false. - * @return {Scope?} upper scope for the node. + * @returns {Scope?} upper scope for the node. */ release(node, inner) { var scopes, scope; @@ -195,7 +202,7 @@ class ScopeManager { return this.__nestScope(new GlobalScope(this, node)); } - __nestBlockScope(node, isMethodDefinition) { + __nestBlockScope(node) { return this.__nestScope(new BlockScope(this, this.__currentScope, node)); } diff --git a/src/scope.js b/src/scope.js index 13c8dcb..4bc0a1b 100644 --- a/src/scope.js +++ b/src/scope.js @@ -23,13 +23,24 @@ */ "use strict"; -const Syntax = require('estraverse').Syntax; +/* eslint-disable no-underscore-dangle */ +/* eslint-disable no-undefined */ -const Reference = require('./reference'); -const Variable = require('./variable'); -const Definition = require('./definition').Definition; -const assert = require('assert'); +const Syntax = require("estraverse").Syntax; +const Reference = require("./reference"); +const Variable = require("./variable"); +const Definition = require("./definition").Definition; +const assert = require("assert"); + +/** + * Test if scope is struct + * @param {Scope} scope - scope + * @param {Block} block - block + * @param {boolean} isMethodDefinition - is method definiton + * @param {boolean} useDirective - use directive + * @returns {boolean} is strict scope + */ function isStrictScope(scope, block, isMethodDefinition, useDirective) { var body, i, iz, stmt, expr; @@ -47,15 +58,15 @@ function isStrictScope(scope, block, isMethodDefinition, useDirective) { return true; } - if (scope.type === 'class' || scope.type === 'module') { + if (scope.type === "class" || scope.type === "module") { return true; } - if (scope.type === 'block' || scope.type === 'switch') { + if (scope.type === "block" || scope.type === "switch") { return false; } - if (scope.type === 'function') { + if (scope.type === "function") { if (block.type === Syntax.Program) { body = block; } else { @@ -65,7 +76,7 @@ function isStrictScope(scope, block, isMethodDefinition, useDirective) { if (!body) { return false; } - } else if (scope.type === 'global') { + } else if (scope.type === "global") { body = block; } else { return false; @@ -78,7 +89,7 @@ function isStrictScope(scope, block, isMethodDefinition, useDirective) { if (stmt.type !== Syntax.DirectiveStatement) { break; } - if (stmt.raw === '"use strict"' || stmt.raw === '\'use strict\'') { + if (stmt.raw === "\"use strict\"" || stmt.raw === "'use strict'") { return true; } } @@ -89,15 +100,15 @@ function isStrictScope(scope, block, isMethodDefinition, useDirective) { break; } expr = stmt.expression; - if (expr.type !== Syntax.Literal || typeof expr.value !== 'string') { + if (expr.type !== Syntax.Literal || typeof expr.value !== "string") { break; } - if (expr.raw != null) { - if (expr.raw === '"use strict"' || expr.raw === '\'use strict\'') { + if (expr.raw !== null && expr.raw !== undefined) { + if (expr.raw === "\"use strict\"" || expr.raw === "'use strict'") { return true; } } else { - if (expr.value === 'use strict') { + if (expr.value === "use strict") { return true; } } @@ -106,6 +117,12 @@ function isStrictScope(scope, block, isMethodDefinition, useDirective) { return false; } +/** + * Register scope + * @param {ScopeManager} scopeManager - scope manager + * @param {Scope} scope - scope + * @returns {void} + */ function registerScope(scopeManager, scope) { var scopes; @@ -119,10 +136,15 @@ function registerScope(scopeManager, scope) { } } +/** + * Should be statically + * @param {Object} def - def + * @returns {boolean} should be statically + */ function shouldBeStatically(def) { return ( (def.type === Variable.ClassName) || - (def.type === Variable.Variable && def.parent.kind !== 'var') + (def.type === Variable.Variable && def.parent.kind !== "var") ); } @@ -157,7 +179,7 @@ class Scope { * All those scopes are considered 'dynamic'. * @member {boolean} Scope#dynamic */ - this.dynamic = this.type === 'global' || this.type === 'with'; + this.dynamic = this.type === "global" || this.type === "with"; /** * A reference to the scope-defining syntax node. * @member {esprima.Node} Scope#block @@ -193,7 +215,7 @@ class Scope { * @member {Scope} Scope#variableScope */ this.variableScope = - (this.type === 'global' || this.type === 'function' || this.type === 'module') ? this : upperScope.variableScope; + (this.type === "global" || this.type === "function" || this.type === "module") ? this : upperScope.variableScope; /** * Whether this scope is created by a FunctionExpression. * @member {boolean} Scope#functionExpressionScope @@ -281,7 +303,7 @@ class Scope { var closeRef; if (this.__shouldStaticallyClose(scopeManager)) { closeRef = this.__staticCloseRef; - } else if (this.type !== 'global') { + } else if (this.type !== "global") { closeRef = this.__dynamicCloseRef; } else { closeRef = this.__globalCloseRef; @@ -322,12 +344,12 @@ class Scope { } __addDeclaredVariablesOfNode(variable, node) { - if (node == null) { + if (node === null || node === undefined) { return; } var variables = this.__declaredVariables.get(node); - if (variables == null) { + if (variables === null || variables === undefined) { variables = []; this.__declaredVariables.set(node, variables); } @@ -376,7 +398,7 @@ class Scope { } // Specially handle like `this`. - if (node.name === 'super') { + if (node.name === "super") { return; } @@ -407,12 +429,12 @@ class Scope { * returns resolved {Reference} * @method Scope#resolve * @param {Esprima.Identifier} ident - identifier to be resolved. - * @return {Reference} + * @returns {Reference} reference */ resolve(ident) { var ref, i, iz; - assert(this.__isClosed(), 'Scope should be closed.'); - assert(ident.type === Syntax.Identifier, 'Target should be identifier.'); + assert(this.__isClosed(), "Scope should be closed."); + assert(ident.type === Syntax.Identifier, "Target should be identifier."); for (i = 0, iz = this.references.length; i < iz; ++i) { ref = this.references[i]; if (ref.identifier === ident) { @@ -425,7 +447,7 @@ class Scope { /** * returns this scope is static * @method Scope#isStatic - * @return {boolean} + * @returns {boolean} static */ isStatic() { return !this.dynamic; @@ -434,7 +456,7 @@ class Scope { /** * returns this scope has materialized arguments * @method Scope#isArgumentsMaterialized - * @return {boolean} + * @returns {boolean} arguemnts materialized */ isArgumentsMaterialized() { return true; @@ -443,7 +465,7 @@ class Scope { /** * returns this scope has materialized `this` reference * @method Scope#isThisMaterialized - * @return {boolean} + * @returns {boolean} this materialized */ isThisMaterialized() { return true; @@ -464,7 +486,7 @@ class Scope { class GlobalScope extends Scope { constructor(scopeManager, block) { - super(scopeManager, 'global', null, block, false); + super(scopeManager, "global", null, block, false); this.implicit = { set: new Map(), variables: [], @@ -520,13 +542,13 @@ class GlobalScope extends Scope { class ModuleScope extends Scope { constructor(scopeManager, upperScope, block) { - super(scopeManager, 'module', upperScope, block, false); + super(scopeManager, "module", upperScope, block, false); } } class FunctionExpressionNameScope extends Scope { constructor(scopeManager, upperScope, block) { - super(scopeManager, 'function-expression-name', upperScope, block, false); + super(scopeManager, "function-expression-name", upperScope, block, false); this.__define(block.id, new Definition( Variable.FunctionName, @@ -542,13 +564,13 @@ class FunctionExpressionNameScope extends Scope { class CatchScope extends Scope { constructor(scopeManager, upperScope, block) { - super(scopeManager, 'catch', upperScope, block, false); + super(scopeManager, "catch", upperScope, block, false); } } class WithScope extends Scope { constructor(scopeManager, upperScope, block) { - super(scopeManager, 'with', upperScope, block, false); + super(scopeManager, "with", upperScope, block, false); } __close(scopeManager) { @@ -569,25 +591,25 @@ class WithScope extends Scope { class TDZScope extends Scope { constructor(scopeManager, upperScope, block) { - super(scopeManager, 'TDZ', upperScope, block, false); + super(scopeManager, "TDZ", upperScope, block, false); } } class BlockScope extends Scope { constructor(scopeManager, upperScope, block) { - super(scopeManager, 'block', upperScope, block, false); + super(scopeManager, "block", upperScope, block, false); } } class SwitchScope extends Scope { constructor(scopeManager, upperScope, block) { - super(scopeManager, 'switch', upperScope, block, false); + super(scopeManager, "switch", upperScope, block, false); } } class FunctionScope extends Scope { constructor(scopeManager, upperScope, block, isMethodDefinition) { - super(scopeManager, 'function', upperScope, block, isMethodDefinition); + super(scopeManager, "function", upperScope, block, isMethodDefinition); // section 9.2.13, FunctionDeclarationInstantiation. // NOTE Arrow functions never have an arguments objects. @@ -613,9 +635,9 @@ class FunctionScope extends Scope { return true; } - let variable = this.set.get('arguments'); - assert(variable, 'Always have arguments variable.'); - return variable.tainted || variable.references.length !== 0; + let variable = this.set.get("arguments"); + assert(variable, "Always have arguments variable."); + return variable.tainted || variable.references.length !== 0; } isThisMaterialized() { @@ -627,24 +649,24 @@ class FunctionScope extends Scope { __defineArguments() { this.__defineGeneric( - 'arguments', + "arguments", this.set, this.variables, null, null); - this.taints.set('arguments', true); + this.taints.set("arguments", true); } } - class ForScope extends Scope { +class ForScope extends Scope { constructor(scopeManager, upperScope, block) { - super(scopeManager, 'for', upperScope, block, false); + super(scopeManager, "for", upperScope, block, false); } } class ClassScope extends Scope { constructor(scopeManager, upperScope, block) { - super(scopeManager, 'class', upperScope, block, false); + super(scopeManager, "class", upperScope, block, false); } } diff --git a/src/variable.js b/src/variable.js index 40435e6..8d0ad62 100644 --- a/src/variable.js +++ b/src/variable.js @@ -70,14 +70,14 @@ class Variable { } } -Variable.CatchClause = 'CatchClause'; -Variable.Parameter = 'Parameter'; -Variable.FunctionName = 'FunctionName'; -Variable.ClassName = 'ClassName'; -Variable.Variable = 'Variable'; -Variable.ImportBinding = 'ImportBinding'; -Variable.TDZ = 'TDZ'; -Variable.ImplicitGlobalVariable = 'ImplicitGlobalVariable'; +Variable.CatchClause = "CatchClause"; +Variable.Parameter = "Parameter"; +Variable.FunctionName = "FunctionName"; +Variable.ClassName = "ClassName"; +Variable.Variable = "Variable"; +Variable.ImportBinding = "ImportBinding"; +Variable.TDZ = "TDZ"; +Variable.ImplicitGlobalVariable = "ImplicitGlobalVariable"; module.exports = Variable; diff --git a/test/arguments.js b/test/arguments.js index 73c2d95..9eb25cf 100644 --- a/test/arguments.js +++ b/test/arguments.js @@ -22,12 +22,14 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const esprima = require('esprima'); -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ -describe('arguments', function() { - it('arguments are correctly materialized', function() { +const expect = require("chai").expect; +const esprima = require("esprima"); +const analyze = require("..").analyze; + +describe("arguments", function() { + it("arguments are correctly materialized", function() { const ast = esprima.parse(` (function () { arguments; @@ -37,14 +39,14 @@ describe('arguments', function() { const scopeManager = analyze(ast); expect(scopeManager.scopes).to.have.length(2); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(0); expect(globalScope.references).to.have.length(0); const scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); expect(scope.isArgumentsMaterialized()).to.be.true; expect(scope.references).to.have.length(1); expect(scope.references[0].resolved).to.be.equal(scope.variables[0]); diff --git a/test/catch-scope.js b/test/catch-scope.js index cd4f58c..e2f0827 100644 --- a/test/catch-scope.js +++ b/test/catch-scope.js @@ -22,12 +22,14 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const esprima = require('esprima'); -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ -describe('catch', function() { - it('creates scope', function() { +const expect = require("chai").expect; +const esprima = require("esprima"); +const analyze = require("..").analyze; + +describe("catch", function() { + it("creates scope", function() { const ast = esprima.parse(` (function () { try { @@ -39,21 +41,21 @@ describe('catch', function() { const scopeManager = analyze(ast); expect(scopeManager.scopes).to.have.length(3); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(0); expect(globalScope.references).to.have.length(0); let scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); expect(scope.isArgumentsMaterialized()).to.be.false; expect(scope.references).to.have.length(0); scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('catch'); + expect(scope.type).to.be.equal("catch"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('e'); + expect(scope.variables[0].name).to.be.equal("e"); expect(scope.isArgumentsMaterialized()).to.be.true; expect(scope.references).to.have.length(0); }); diff --git a/test/child-visitor-keys.js b/test/child-visitor-keys.js index 49d39ad..0cba5ef 100644 --- a/test/child-visitor-keys.js +++ b/test/child-visitor-keys.js @@ -22,23 +22,23 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const esprima = require('esprima'); -const analyze = require('..').analyze; +const expect = require("chai").expect; +const esprima = require("esprima"); +const analyze = require("..").analyze; -describe('childVisitorKeys option', function() { - it('should handle as a known node if the childVisitorKeys option was given.', function() { +describe("childVisitorKeys option", function() { + it("should handle as a known node if the childVisitorKeys option was given.", function() { const ast = esprima.parse(` var foo = 0; `); - ast.body[0].declarations[0].init.type = 'NumericLiteral'; + ast.body[0].declarations[0].init.type = "NumericLiteral"; // should no error analyze( ast, { - fallback: 'none', + fallback: "none", childVisitorKeys: { NumericLiteral: [] } @@ -46,13 +46,13 @@ describe('childVisitorKeys option', function() { ); }); - it('should not visit to properties which are not given.', function() { + it("should not visit to properties which are not given.", function() { const ast = esprima.parse(` let foo = bar; `); ast.body[0].declarations[0].init = { - type: 'TestNode', + type: "TestNode", argument: ast.body[0].declarations[0].init }; @@ -72,13 +72,13 @@ describe('childVisitorKeys option', function() { expect(globalScope.through).to.have.length(0); }); - it('should visit to given properties.', function() { + it("should visit to given properties.", function() { const ast = esprima.parse(` let foo = bar; `); ast.body[0].declarations[0].init = { - type: 'TestNode', + type: "TestNode", argument: ast.body[0].declarations[0].init }; @@ -86,7 +86,7 @@ describe('childVisitorKeys option', function() { ast, { childVisitorKeys: { - TestNode: ['argument'] + TestNode: ["argument"] } } ); @@ -96,7 +96,7 @@ describe('childVisitorKeys option', function() { // `bar` in TestNode has been visited. expect(globalScope.through).to.have.length(1); - expect(globalScope.through[0].identifier.name).to.equal('bar'); + expect(globalScope.through[0].identifier.name).to.equal("bar"); }); }); diff --git a/test/es6-arrow-function-expression.js b/test/es6-arrow-function-expression.js index 5f99f89..5dae0b3 100644 --- a/test/es6-arrow-function-expression.js +++ b/test/es6-arrow-function-expression.js @@ -22,12 +22,14 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const parse = require('../third_party/esprima').parse; -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ -describe('ES6 arrow function expression', function() { - it('materialize scope for arrow function expression', function() { +const expect = require("chai").expect; +const parse = require("../third_party/esprima").parse; +const analyze = require("..").analyze; + +describe("ES6 arrow function expression", function() { + it("materialize scope for arrow function expression", function() { const ast = parse(` var arrow = () => { let i = 0; @@ -40,43 +42,43 @@ describe('ES6 arrow function expression', function() { 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.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(1); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); - expect(scope.block.type).to.be.equal('ArrowFunctionExpression'); + expect(scope.type).to.be.equal("function"); + expect(scope.block.type).to.be.equal("ArrowFunctionExpression"); expect(scope.isStrict).to.be.true; expect(scope.variables).to.have.length(2); // There's no "arguments" - expect(scope.variables[0].name).to.be.equal('i'); - expect(scope.variables[1].name).to.be.equal('j'); + expect(scope.variables[0].name).to.be.equal("i"); + expect(scope.variables[1].name).to.be.equal("j"); }); - it('generate bindings for parameters', function() { - const ast = parse(`var arrow = (a, b, c, d) => {}`); + it("generate bindings for parameters", function() { + const ast = parse("var arrow = (a, b, c, d) => {}"); const scopeManager = analyze(ast, {ecmaVersion: 6}); 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.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(1); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); - expect(scope.block.type).to.be.equal('ArrowFunctionExpression'); + expect(scope.type).to.be.equal("function"); + expect(scope.block.type).to.be.equal("ArrowFunctionExpression"); expect(scope.isStrict).to.be.true; expect(scope.variables).to.have.length(4); // There's no "arguments" - expect(scope.variables[0].name).to.be.equal('a'); - expect(scope.variables[1].name).to.be.equal('b'); - expect(scope.variables[2].name).to.be.equal('c'); - expect(scope.variables[3].name).to.be.equal('d'); + expect(scope.variables[0].name).to.be.equal("a"); + expect(scope.variables[1].name).to.be.equal("b"); + expect(scope.variables[2].name).to.be.equal("c"); + expect(scope.variables[3].name).to.be.equal("d"); }); }); diff --git a/test/es6-block-scope.js b/test/es6-block-scope.js index 79c4971..a81c306 100644 --- a/test/es6-block-scope.js +++ b/test/es6-block-scope.js @@ -22,12 +22,14 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const parse = require('../third_party/esprima').parse; -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ -describe('ES6 block scope', function() { - it('let is materialized in ES6 block scope#1', function() { +const expect = require("chai").expect; +const parse = require("../third_party/esprima").parse; +const analyze = require("..").analyze; + +describe("ES6 block scope", function() { + it("let is materialized in ES6 block scope#1", function() { const ast = parse(` { let i = 20; @@ -39,19 +41,19 @@ describe('ES6 block scope', function() { expect(scopeManager.scopes).to.have.length(2); // Program and BlcokStatement scope. let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); // No variable in Program scope. scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('block'); + expect(scope.type).to.be.equal("block"); expect(scope.variables).to.have.length(1); // `i` in block scope. - expect(scope.variables[0].name).to.be.equal('i'); + expect(scope.variables[0].name).to.be.equal("i"); expect(scope.references).to.have.length(2); - expect(scope.references[0].identifier.name).to.be.equal('i'); - expect(scope.references[1].identifier.name).to.be.equal('i'); + expect(scope.references[0].identifier.name).to.be.equal("i"); + expect(scope.references[1].identifier.name).to.be.equal("i"); }); - it('let is materialized in ES6 block scope#2', function() { + it("let is materialized in ES6 block scope#2", function() { const ast = parse(` { let i = 20; @@ -64,21 +66,21 @@ describe('ES6 block scope', function() { expect(scopeManager.scopes).to.have.length(2); // Program and BlcokStatement scope. let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(1); // No variable in Program scope. - expect(scope.variables[0].name).to.be.equal('i'); + expect(scope.variables[0].name).to.be.equal("i"); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('block'); + expect(scope.type).to.be.equal("block"); expect(scope.variables).to.have.length(1); // `i` in block scope. - expect(scope.variables[0].name).to.be.equal('i'); + expect(scope.variables[0].name).to.be.equal("i"); expect(scope.references).to.have.length(3); - expect(scope.references[0].identifier.name).to.be.equal('i'); - expect(scope.references[1].identifier.name).to.be.equal('i'); - expect(scope.references[2].identifier.name).to.be.equal('i'); + expect(scope.references[0].identifier.name).to.be.equal("i"); + expect(scope.references[1].identifier.name).to.be.equal("i"); + expect(scope.references[2].identifier.name).to.be.equal("i"); }); - it('function delaration is materialized in ES6 block scope', function() { + it("function delaration is materialized in ES6 block scope", function() { const ast = parse(` { function test() { @@ -91,24 +93,24 @@ describe('ES6 block scope', function() { expect(scopeManager.scopes).to.have.length(3); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('block'); + expect(scope.type).to.be.equal("block"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('test'); + expect(scope.variables[0].name).to.be.equal("test"); expect(scope.references).to.have.length(1); - expect(scope.references[0].identifier.name).to.be.equal('test'); + expect(scope.references[0].identifier.name).to.be.equal("test"); scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); expect(scope.references).to.have.length(0); }); - it('let is not hoistable#1', function() { + it("let is not hoistable#1", function() { const ast = parse(` var i = 42; (1) { @@ -122,22 +124,22 @@ describe('ES6 block scope', function() { expect(scopeManager.scopes).to.have.length(2); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(1); - expect(globalScope.variables[0].name).to.be.equal('i'); + expect(globalScope.variables[0].name).to.be.equal("i"); expect(globalScope.references).to.have.length(1); const scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('block'); + expect(scope.type).to.be.equal("block"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('i'); + expect(scope.variables[0].name).to.be.equal("i"); expect(scope.references).to.have.length(3); expect(scope.references[0].resolved).to.be.equal(scope.variables[0]); expect(scope.references[1].resolved).to.be.equal(scope.variables[0]); expect(scope.references[2].resolved).to.be.equal(scope.variables[0]); }); - it('let is not hoistable#2', function() { + it("let is not hoistable#2", function() { const ast = parse(` (function () { var i = 42; // (1) @@ -160,15 +162,15 @@ describe('ES6 block scope', function() { expect(scopeManager.scopes).to.have.length(4); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(0); expect(globalScope.references).to.have.length(0); let scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(2); - expect(scope.variables[0].name).to.be.equal('arguments'); - expect(scope.variables[1].name).to.be.equal('i'); + expect(scope.variables[0].name).to.be.equal("arguments"); + expect(scope.variables[1].name).to.be.equal("i"); const v1 = scope.variables[1]; expect(scope.references).to.have.length(3); expect(scope.references[0].resolved).to.be.equal(v1); @@ -176,9 +178,9 @@ describe('ES6 block scope', function() { expect(scope.references[2].resolved).to.be.equal(v1); scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('block'); + expect(scope.type).to.be.equal("block"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('i'); + expect(scope.variables[0].name).to.be.equal("i"); const v3 = scope.variables[0]; expect(scope.references).to.have.length(3); expect(scope.references[0].resolved).to.be.equal(v3); @@ -186,9 +188,9 @@ describe('ES6 block scope', function() { expect(scope.references[2].resolved).to.be.equal(v3); scope = scopeManager.scopes[3]; - expect(scope.type).to.be.equal('block'); + expect(scope.type).to.be.equal("block"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('i'); + expect(scope.variables[0].name).to.be.equal("i"); const v2 = scope.variables[0]; expect(scope.references).to.have.length(3); expect(scope.references[0].resolved).to.be.equal(v2); diff --git a/test/es6-catch.js b/test/es6-catch.js index c7bf5a5..6d1f20e 100644 --- a/test/es6-catch.js +++ b/test/es6-catch.js @@ -22,12 +22,14 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const parse = require('../third_party/esprima').parse; -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ -describe('ES6 catch', function() { - it('takes binding pattern', function() { +const expect = require("chai").expect; +const parse = require("../third_party/esprima").parse; +const analyze = require("..").analyze; + +describe("ES6 catch", function() { + it("takes binding pattern", function() { const ast = parse(` try { } catch ({ a, b, c, d }) { @@ -44,22 +46,22 @@ describe('ES6 catch', function() { expect(scopeManager.scopes).to.have.length(4); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); - expect(scope.block.type).to.be.equal('Program'); + 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); expect(scope.references).to.have.length(0); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('block'); - expect(scope.block.type).to.be.equal('BlockStatement'); + expect(scope.type).to.be.equal("block"); + expect(scope.block.type).to.be.equal("BlockStatement"); expect(scope.isStrict).to.be.false; expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('catch'); - expect(scope.block.type).to.be.equal('CatchClause'); + expect(scope.type).to.be.equal("catch"); + expect(scope.block.type).to.be.equal("CatchClause"); expect(scope.isStrict).to.be.false; // FIXME After Esprima's bug is fixed, I'll add tests #33 @@ -73,21 +75,21 @@ describe('ES6 catch', function() { // expect(scope.references).to.have.length(0); scope = scopeManager.scopes[3]; - expect(scope.type).to.be.equal('block'); - expect(scope.block.type).to.be.equal('BlockStatement'); + expect(scope.type).to.be.equal("block"); + expect(scope.block.type).to.be.equal("BlockStatement"); expect(scope.isStrict).to.be.false; expect(scope.variables).to.have.length(2); expect(scope.variables.map(variable => variable.name)).to.be.eql([ - 'e', - 'c' + "e", + "c" ]); expect(scope.references.map(ref => ref.identifier.name)).to.be.eql([ - 'e', - 'a', - 'b', - 'c', - 'c', - 'd' + "e", + "a", + "b", + "c", + "c", + "d" ]); }); }); diff --git a/test/es6-class.js b/test/es6-class.js index 78d5029..987aebd 100644 --- a/test/es6-class.js +++ b/test/es6-class.js @@ -22,12 +22,14 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const parse = require('../third_party/esprima').parse; -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ -describe('ES6 class', function() { - it('declaration name creates class scope', function() { +const expect = require("chai").expect; +const parse = require("../third_party/esprima").parse; +const analyze = require("..").analyze; + +describe("ES6 class", function() { + it("declaration name creates class scope", function() { const ast = parse(` class Derived extends Base { constructor() { @@ -40,33 +42,33 @@ describe('ES6 class', function() { expect(scopeManager.scopes).to.have.length(3); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); - expect(scope.block.type).to.be.equal('Program'); + 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(1); - expect(scope.variables[0].name).to.be.equal('Derived'); + expect(scope.variables[0].name).to.be.equal("Derived"); expect(scope.references).to.have.length(2); - expect(scope.references[0].identifier.name).to.be.equal('Base'); - expect(scope.references[1].identifier.name).to.be.equal('Derived'); + expect(scope.references[0].identifier.name).to.be.equal("Base"); + expect(scope.references[1].identifier.name).to.be.equal("Derived"); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('class'); - expect(scope.block.type).to.be.equal('ClassDeclaration'); + expect(scope.type).to.be.equal("class"); + expect(scope.block.type).to.be.equal("ClassDeclaration"); expect(scope.isStrict).to.be.true; expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('Derived'); + expect(scope.variables[0].name).to.be.equal("Derived"); expect(scope.references).to.have.length(0); scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('function'); - expect(scope.block.type).to.be.equal('FunctionExpression'); + expect(scope.type).to.be.equal("function"); + expect(scope.block.type).to.be.equal("FunctionExpression"); expect(scope.isStrict).to.be.true; expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); expect(scope.references).to.have.length(0); }); - it('expression name creates class scope#1', function() { + it("expression name creates class scope#1", function() { const ast = parse(` (class Derived extends Base { constructor() { @@ -78,27 +80,27 @@ describe('ES6 class', function() { expect(scopeManager.scopes).to.have.length(3); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); - expect(scope.block.type).to.be.equal('Program'); + 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); expect(scope.references).to.have.length(1); - expect(scope.references[0].identifier.name).to.be.equal('Base'); + expect(scope.references[0].identifier.name).to.be.equal("Base"); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('class'); - expect(scope.block.type).to.be.equal('ClassExpression'); + expect(scope.type).to.be.equal("class"); + expect(scope.block.type).to.be.equal("ClassExpression"); expect(scope.isStrict).to.be.true; expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('Derived'); + expect(scope.variables[0].name).to.be.equal("Derived"); expect(scope.references).to.have.length(0); scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('function'); - expect(scope.block.type).to.be.equal('FunctionExpression'); + expect(scope.type).to.be.equal("function"); + expect(scope.block.type).to.be.equal("FunctionExpression"); }); - it('expression name creates class scope#2', function() { + it("expression name creates class scope#2", function() { const ast = parse(` (class extends Base { constructor() { @@ -110,23 +112,23 @@ describe('ES6 class', function() { expect(scopeManager.scopes).to.have.length(3); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); - expect(scope.block.type).to.be.equal('Program'); + 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); expect(scope.references).to.have.length(1); - expect(scope.references[0].identifier.name).to.be.equal('Base'); + expect(scope.references[0].identifier.name).to.be.equal("Base"); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('class'); - expect(scope.block.type).to.be.equal('ClassExpression'); + expect(scope.type).to.be.equal("class"); + expect(scope.block.type).to.be.equal("ClassExpression"); scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('function'); - expect(scope.block.type).to.be.equal('FunctionExpression'); + expect(scope.type).to.be.equal("function"); + expect(scope.block.type).to.be.equal("FunctionExpression"); }); - it('computed property key may refer variables', function() { + it("computed property key may refer variables", function() { const ast = parse(` (function () { var yuyushiki = 42; @@ -144,31 +146,31 @@ describe('ES6 class', function() { expect(scopeManager.scopes).to.have.length(5); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); - expect(scope.block.type).to.be.equal('Program'); + expect(scope.type).to.be.equal("global"); + expect(scope.block.type).to.be.equal("Program"); expect(scope.isStrict).to.be.false; scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); - expect(scope.block.type).to.be.equal('FunctionExpression'); + expect(scope.type).to.be.equal("function"); + expect(scope.block.type).to.be.equal("FunctionExpression"); expect(scope.isStrict).to.be.false; expect(scope.variables).to.have.length(2); - expect(scope.variables[0].name).to.be.equal('arguments'); - expect(scope.variables[1].name).to.be.equal('yuyushiki'); + expect(scope.variables[0].name).to.be.equal("arguments"); + expect(scope.variables[1].name).to.be.equal("yuyushiki"); expect(scope.references).to.have.length(1); - expect(scope.references[0].identifier.name).to.be.equal('yuyushiki'); + expect(scope.references[0].identifier.name).to.be.equal("yuyushiki"); scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('class'); - expect(scope.block.type).to.be.equal('ClassExpression'); + expect(scope.type).to.be.equal("class"); + expect(scope.block.type).to.be.equal("ClassExpression"); expect(scope.isStrict).to.be.true; expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(2); - expect(scope.references[0].identifier.name).to.be.equal('yuyushiki'); - expect(scope.references[1].identifier.name).to.be.equal('yuyushiki'); + expect(scope.references[0].identifier.name).to.be.equal("yuyushiki"); + expect(scope.references[1].identifier.name).to.be.equal("yuyushiki"); }); - it('regression #49', function() { + it("regression #49", function() { const ast = parse(` class Shoe { constructor() { @@ -182,15 +184,15 @@ describe('ES6 class', function() { expect(scopeManager.scopes).to.have.length(3); const scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); - expect(scope.block.type).to.be.equal('Program'); + 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(2); - expect(scope.variables[0].name).to.be.equal('Shoe'); - expect(scope.variables[1].name).to.be.equal('shoe'); + expect(scope.variables[0].name).to.be.equal("Shoe"); + expect(scope.variables[1].name).to.be.equal("shoe"); expect(scope.references).to.have.length(2); - expect(scope.references[0].identifier.name).to.be.equal('shoe'); - expect(scope.references[1].identifier.name).to.be.equal('Shoe'); + expect(scope.references[0].identifier.name).to.be.equal("shoe"); + expect(scope.references[1].identifier.name).to.be.equal("Shoe"); }); }); diff --git a/test/es6-default-parameters.js b/test/es6-default-parameters.js index 18339a4..069c4df 100644 --- a/test/es6-default-parameters.js +++ b/test/es6-default-parameters.js @@ -22,45 +22,46 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const espree = require('../third_party/espree'); -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ +/* eslint-disable guard-for-in */ -describe('ES6 default parameters:', function() { - describe('a default parameter creates a writable reference for its initialization:', function() { +const expect = require("chai").expect; +const espree = require("../third_party/espree"); +const analyze = require("..").analyze; + +describe("ES6 default parameters:", function() { + describe("a default parameter creates a writable reference for its initialization:", function() { const patterns = { - FunctionDeclaration: `function foo(a, b = 0) {}`, - FunctionExpression: `let foo = function(a, b = 0) {};`, - ArrowExpression: `let foo = (a, b = 0) => {};` + FunctionDeclaration: "function foo(a, b = 0) {}", + FunctionExpression: "let foo = function(a, b = 0) {};", + ArrowExpression: "let foo = (a, b = 0) => {};" }; for (const name in patterns) { const code = patterns[name]; - (function(name, code) { - it(name, function() { - const numVars = name === 'ArrowExpression' ? 2 : 3; - const ast = espree(code); - - const scopeManager = analyze(ast, {ecmaVersion: 6}); - expect(scopeManager.scopes).to.have.length(2); // [global, foo] - - const scope = scopeManager.scopes[1]; - expect(scope.variables).to.have.length(numVars); // [arguments?, a, b] - expect(scope.references).to.have.length(1); - - const reference = scope.references[0]; - expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('b'); - expect(reference.resolved).to.equal(scope.variables[numVars - 1]); - expect(reference.writeExpr).to.not.be.undefined; - expect(reference.isWrite()).to.be.true; - expect(reference.isRead()).to.be.false; - }); - })(name, code); + it(name, function() { + const numVars = name === "ArrowExpression" ? 2 : 3; + const ast = espree(code); + + const scopeManager = analyze(ast, {ecmaVersion: 6}); + expect(scopeManager.scopes).to.have.length(2); // [global, foo] + + const scope = scopeManager.scopes[1]; + expect(scope.variables).to.have.length(numVars); // [arguments?, a, b] + expect(scope.references).to.have.length(1); + + const reference = scope.references[0]; + expect(reference.from).to.equal(scope); + expect(reference.identifier.name).to.equal("b"); + expect(reference.resolved).to.equal(scope.variables[numVars - 1]); + expect(reference.writeExpr).to.not.be.undefined; + expect(reference.isWrite()).to.be.true; + expect(reference.isRead()).to.be.false; + }); } }); - describe('a default parameter creates a readable reference for references in right:', function() { + describe("a default parameter creates a readable reference for references in right:", function() { const patterns = { FunctionDeclaration: ` let a; @@ -78,31 +79,29 @@ describe('ES6 default parameters:', function() { for (const name in patterns) { const code = patterns[name]; - (function(name, code) { - it(name, function() { - const numVars = name === 'ArrowExpression' ? 1 : 2; - const ast = espree(code); - - const scopeManager = analyze(ast, {ecmaVersion: 6}); - expect(scopeManager.scopes).to.have.length(2); // [global, foo] - - const scope = scopeManager.scopes[1]; - expect(scope.variables).to.have.length(numVars); // [arguments?, b] - expect(scope.references).to.have.length(2); // [b, a] - - const reference = scope.references[1]; - expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('a'); - expect(reference.resolved).to.equal(scopeManager.scopes[0].variables[0]); - expect(reference.writeExpr).to.be.undefined; - expect(reference.isWrite()).to.be.false; - expect(reference.isRead()).to.be.true; - }); - })(name, code); + it(name, function() { + const numVars = name === "ArrowExpression" ? 1 : 2; + const ast = espree(code); + + const scopeManager = analyze(ast, {ecmaVersion: 6}); + expect(scopeManager.scopes).to.have.length(2); // [global, foo] + + const scope = scopeManager.scopes[1]; + expect(scope.variables).to.have.length(numVars); // [arguments?, b] + expect(scope.references).to.have.length(2); // [b, a] + + const reference = scope.references[1]; + expect(reference.from).to.equal(scope); + expect(reference.identifier.name).to.equal("a"); + expect(reference.resolved).to.equal(scopeManager.scopes[0].variables[0]); + expect(reference.writeExpr).to.be.undefined; + expect(reference.isWrite()).to.be.false; + expect(reference.isRead()).to.be.true; + }); } }); - describe('a default parameter creates a readable reference for references in right (for const):', function() { + describe("a default parameter creates a readable reference for references in right (for const):", function() { const patterns = { FunctionDeclaration: ` const a = 0; @@ -120,31 +119,29 @@ describe('ES6 default parameters:', function() { for (const name in patterns) { const code = patterns[name]; - (function(name, code) { - it(name, function() { - const numVars = name === 'ArrowExpression' ? 1 : 2; - const ast = espree(code); - - const scopeManager = analyze(ast, {ecmaVersion: 6}); - expect(scopeManager.scopes).to.have.length(2); // [global, foo] - - const scope = scopeManager.scopes[1]; - expect(scope.variables).to.have.length(numVars); // [arguments?, b] - expect(scope.references).to.have.length(2); // [b, a] - - const reference = scope.references[1]; - expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('a'); - expect(reference.resolved).to.equal(scopeManager.scopes[0].variables[0]); - expect(reference.writeExpr).to.be.undefined; - expect(reference.isWrite()).to.be.false; - expect(reference.isRead()).to.be.true; - }); - })(name, code); + it(name, function() { + const numVars = name === "ArrowExpression" ? 1 : 2; + const ast = espree(code); + + const scopeManager = analyze(ast, {ecmaVersion: 6}); + expect(scopeManager.scopes).to.have.length(2); // [global, foo] + + const scope = scopeManager.scopes[1]; + expect(scope.variables).to.have.length(numVars); // [arguments?, b] + expect(scope.references).to.have.length(2); // [b, a] + + const reference = scope.references[1]; + expect(reference.from).to.equal(scope); + expect(reference.identifier.name).to.equal("a"); + expect(reference.resolved).to.equal(scopeManager.scopes[0].variables[0]); + expect(reference.writeExpr).to.be.undefined; + expect(reference.isWrite()).to.be.false; + expect(reference.isRead()).to.be.true; + }); } }); - describe('a default parameter creates a readable reference for references in right (partial):', function() { + describe("a default parameter creates a readable reference for references in right (partial):", function() { const patterns = { FunctionDeclaration: ` let a; @@ -162,31 +159,29 @@ describe('ES6 default parameters:', function() { for (const name in patterns) { const code = patterns[name]; - (function(name, code) { - it(name, function() { - const numVars = name === 'ArrowExpression' ? 1 : 2; - const ast = espree(code); - - const scopeManager = analyze(ast, {ecmaVersion: 6}); - expect(scopeManager.scopes).to.have.length(2); // [global, foo] - - const scope = scopeManager.scopes[1]; - expect(scope.variables).to.have.length(numVars); // [arguments?, b] - expect(scope.references).to.have.length(2); // [b, a] - - const reference = scope.references[1]; - expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('a'); - expect(reference.resolved).to.equal(scopeManager.scopes[0].variables[0]); - expect(reference.writeExpr).to.be.undefined; - expect(reference.isWrite()).to.be.false; - expect(reference.isRead()).to.be.true; - }); - })(name, code); + it(name, function() { + const numVars = name === "ArrowExpression" ? 1 : 2; + const ast = espree(code); + + const scopeManager = analyze(ast, {ecmaVersion: 6}); + expect(scopeManager.scopes).to.have.length(2); // [global, foo] + + const scope = scopeManager.scopes[1]; + expect(scope.variables).to.have.length(numVars); // [arguments?, b] + expect(scope.references).to.have.length(2); // [b, a] + + const reference = scope.references[1]; + expect(reference.from).to.equal(scope); + expect(reference.identifier.name).to.equal("a"); + expect(reference.resolved).to.equal(scopeManager.scopes[0].variables[0]); + expect(reference.writeExpr).to.be.undefined; + expect(reference.isWrite()).to.be.false; + expect(reference.isRead()).to.be.true; + }); } }); - describe('a default parameter creates a readable reference for references in right\'s nested scope:', function() { + describe("a default parameter creates a readable reference for references in right's nested scope:", function() { const patterns = { FunctionDeclaration: ` let a; @@ -204,26 +199,24 @@ describe('ES6 default parameters:', function() { for (const name in patterns) { const code = patterns[name]; - (function(name, code) { - it(name, function() { - const ast = espree(code); - - const scopeManager = analyze(ast, {ecmaVersion: 6}); - expect(scopeManager.scopes).to.have.length(3); // [global, foo, anonymous] - - const scope = scopeManager.scopes[2]; - expect(scope.variables).to.have.length(1); // [arguments] - expect(scope.references).to.have.length(1); // [a] - - const reference = scope.references[0]; - expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('a'); - expect(reference.resolved).to.equal(scopeManager.scopes[0].variables[0]); - expect(reference.writeExpr).to.be.undefined; - expect(reference.isWrite()).to.be.false; - expect(reference.isRead()).to.be.true; - }); - })(name, code); + it(name, function() { + const ast = espree(code); + + const scopeManager = analyze(ast, {ecmaVersion: 6}); + expect(scopeManager.scopes).to.have.length(3); // [global, foo, anonymous] + + const scope = scopeManager.scopes[2]; + expect(scope.variables).to.have.length(1); // [arguments] + expect(scope.references).to.have.length(1); // [a] + + const reference = scope.references[0]; + expect(reference.from).to.equal(scope); + expect(reference.identifier.name).to.equal("a"); + expect(reference.resolved).to.equal(scopeManager.scopes[0].variables[0]); + expect(reference.writeExpr).to.be.undefined; + expect(reference.isWrite()).to.be.false; + expect(reference.isRead()).to.be.true; + }); } }); }); diff --git a/test/es6-destructuring-assignments.js b/test/es6-destructuring-assignments.js index dcdbb92..1fd437b 100644 --- a/test/es6-destructuring-assignments.js +++ b/test/es6-destructuring-assignments.js @@ -22,13 +22,15 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const harmony = require('../third_party/esprima'); -const espree = require('../third_party/espree'); -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ -describe('ES6 destructuring assignments', function() { - it('Pattern in var in ForInStatement', function() { +const expect = require("chai").expect; +const harmony = require("../third_party/esprima"); +const espree = require("../third_party/espree"); +const analyze = require("..").analyze; + +describe("ES6 destructuring assignments", function() { + it("Pattern in var in ForInStatement", function() { const ast = harmony.parse(` (function () { for (var [a, b, c] in array); @@ -39,37 +41,37 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); expect(scope.implicit.left).to.have.length(1); - expect(scope.implicit.left[0].identifier.name).to.be.equal('array'); + expect(scope.implicit.left[0].identifier.name).to.be.equal("array"); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(4); - expect(scope.variables[0].name).to.be.equal('arguments'); - expect(scope.variables[1].name).to.be.equal('a'); - expect(scope.variables[2].name).to.be.equal('b'); - expect(scope.variables[3].name).to.be.equal('c'); + expect(scope.variables[0].name).to.be.equal("arguments"); + expect(scope.variables[1].name).to.be.equal("a"); + expect(scope.variables[2].name).to.be.equal("b"); + expect(scope.variables[3].name).to.be.equal("c"); expect(scope.references).to.have.length(4); - expect(scope.references[0].identifier.name).to.be.equal('a'); + expect(scope.references[0].identifier.name).to.be.equal("a"); expect(scope.references[0].isWrite()).to.be.true; expect(scope.references[0].partial).to.be.true; expect(scope.references[0].resolved).to.be.equal(scope.variables[1]); - expect(scope.references[1].identifier.name).to.be.equal('b'); + expect(scope.references[1].identifier.name).to.be.equal("b"); expect(scope.references[1].isWrite()).to.be.true; expect(scope.references[1].partial).to.be.true; expect(scope.references[1].resolved).to.be.equal(scope.variables[2]); - expect(scope.references[2].identifier.name).to.be.equal('c'); + expect(scope.references[2].identifier.name).to.be.equal("c"); expect(scope.references[2].isWrite()).to.be.true; expect(scope.references[2].partial).to.be.true; expect(scope.references[2].resolved).to.be.equal(scope.variables[3]); - expect(scope.references[3].identifier.name).to.be.equal('array'); + expect(scope.references[3].identifier.name).to.be.equal("array"); expect(scope.references[3].isWrite()).to.be.false; }); - it('Pattern in let in ForInStatement', function() { + it("Pattern in let in ForInStatement", function() { const ast = harmony.parse(` (function () { for (let [a, b, c] in array); @@ -80,44 +82,44 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(4); // [global, function, TDZ, for] let scope = scopeManager.scopes[0]; - expect(scope.type).to.equal('global'); + expect(scope.type).to.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); expect(scope.implicit.left).to.have.length(1); - expect(scope.implicit.left[0].identifier.name).to.equal('array'); + expect(scope.implicit.left[0].identifier.name).to.equal("array"); scope = scopeManager.scopes[2]; - expect(scope.type).to.equal('TDZ'); + expect(scope.type).to.equal("TDZ"); expect(scope.variables).to.have.length(3); - expect(scope.variables[0].name).to.equal('a'); - expect(scope.variables[1].name).to.equal('b'); - expect(scope.variables[2].name).to.equal('c'); + expect(scope.variables[0].name).to.equal("a"); + expect(scope.variables[1].name).to.equal("b"); + expect(scope.variables[2].name).to.equal("c"); expect(scope.references).to.have.length(1); - expect(scope.references[0].identifier.name).to.equal('array'); + expect(scope.references[0].identifier.name).to.equal("array"); expect(scope.references[0].isWrite()).to.be.false; scope = scopeManager.scopes[3]; - expect(scope.type).to.equal('for'); + expect(scope.type).to.equal("for"); expect(scope.variables).to.have.length(3); - expect(scope.variables[0].name).to.equal('a'); - expect(scope.variables[1].name).to.equal('b'); - expect(scope.variables[2].name).to.equal('c'); + expect(scope.variables[0].name).to.equal("a"); + expect(scope.variables[1].name).to.equal("b"); + expect(scope.variables[2].name).to.equal("c"); expect(scope.references).to.have.length(3); - expect(scope.references[0].identifier.name).to.equal('a'); + expect(scope.references[0].identifier.name).to.equal("a"); expect(scope.references[0].isWrite()).to.be.true; expect(scope.references[0].partial).to.be.true; expect(scope.references[0].resolved).to.equal(scope.variables[0]); - expect(scope.references[1].identifier.name).to.equal('b'); + expect(scope.references[1].identifier.name).to.equal("b"); expect(scope.references[1].isWrite()).to.be.true; expect(scope.references[1].partial).to.be.true; expect(scope.references[1].resolved).to.equal(scope.variables[1]); - expect(scope.references[2].identifier.name).to.equal('c'); + expect(scope.references[2].identifier.name).to.equal("c"); expect(scope.references[2].isWrite()).to.be.true; expect(scope.references[2].partial).to.be.true; expect(scope.references[2].resolved).to.equal(scope.variables[2]); }); - it('Pattern with default values in var in ForInStatement', function() { + it("Pattern with default values in var in ForInStatement", function() { const ast = espree(` (function () { for (var [a, b, c = d] in array); @@ -128,46 +130,46 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.equal('global'); + expect(scope.type).to.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); expect(scope.implicit.left).to.have.length(2); - expect(scope.implicit.left[0].identifier.name).to.equal('d'); - expect(scope.implicit.left[1].identifier.name).to.equal('array'); + expect(scope.implicit.left[0].identifier.name).to.equal("d"); + expect(scope.implicit.left[1].identifier.name).to.equal("array"); scope = scopeManager.scopes[1]; - expect(scope.type).to.equal('function'); + expect(scope.type).to.equal("function"); expect(scope.variables).to.have.length(4); - expect(scope.variables[0].name).to.equal('arguments'); - expect(scope.variables[1].name).to.equal('a'); - expect(scope.variables[2].name).to.equal('b'); - expect(scope.variables[3].name).to.equal('c'); + expect(scope.variables[0].name).to.equal("arguments"); + expect(scope.variables[1].name).to.equal("a"); + expect(scope.variables[2].name).to.equal("b"); + expect(scope.variables[3].name).to.equal("c"); expect(scope.references).to.have.length(6); - expect(scope.references[0].identifier.name).to.equal('c'); + expect(scope.references[0].identifier.name).to.equal("c"); expect(scope.references[0].isWrite()).to.be.true; - expect(scope.references[0].writeExpr.name).to.equal('d'); + expect(scope.references[0].writeExpr.name).to.equal("d"); expect(scope.references[0].partial).to.be.false; expect(scope.references[0].resolved).to.equal(scope.variables[3]); - expect(scope.references[1].identifier.name).to.equal('d'); + expect(scope.references[1].identifier.name).to.equal("d"); expect(scope.references[1].isWrite()).to.be.false; - expect(scope.references[2].identifier.name).to.equal('a'); + expect(scope.references[2].identifier.name).to.equal("a"); expect(scope.references[2].isWrite()).to.be.true; expect(scope.references[2].partial).to.be.true; expect(scope.references[2].resolved).to.equal(scope.variables[1]); - expect(scope.references[3].identifier.name).to.equal('b'); + expect(scope.references[3].identifier.name).to.equal("b"); expect(scope.references[3].isWrite()).to.be.true; expect(scope.references[3].partial).to.be.true; expect(scope.references[3].resolved).to.equal(scope.variables[2]); - expect(scope.references[4].identifier.name).to.equal('c'); + expect(scope.references[4].identifier.name).to.equal("c"); expect(scope.references[4].isWrite()).to.be.true; - expect(scope.references[4].writeExpr.name).to.equal('array'); + expect(scope.references[4].writeExpr.name).to.equal("array"); expect(scope.references[4].partial).to.be.true; expect(scope.references[4].resolved).to.equal(scope.variables[3]); - expect(scope.references[5].identifier.name).to.equal('array'); + expect(scope.references[5].identifier.name).to.equal("array"); expect(scope.references[5].isWrite()).to.be.false; }); - it('Pattern with default values in let in ForInStatement', function() { + it("Pattern with default values in let in ForInStatement", function() { const ast = espree(` (function () { for (let [a, b, c = d] in array); @@ -178,57 +180,57 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(4); // [global, function, TDZ, for] let scope = scopeManager.scopes[0]; - expect(scope.type).to.equal('global'); + expect(scope.type).to.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); expect(scope.implicit.left).to.have.length(2); - expect(scope.implicit.left[0].identifier.name).to.equal('array'); - expect(scope.implicit.left[0].from.type).to.equal('TDZ'); - expect(scope.implicit.left[1].identifier.name).to.equal('d'); - expect(scope.implicit.left[1].from.type).to.equal('for'); + expect(scope.implicit.left[0].identifier.name).to.equal("array"); + expect(scope.implicit.left[0].from.type).to.equal("TDZ"); + expect(scope.implicit.left[1].identifier.name).to.equal("d"); + expect(scope.implicit.left[1].from.type).to.equal("for"); scope = scopeManager.scopes[2]; - expect(scope.type).to.equal('TDZ'); + expect(scope.type).to.equal("TDZ"); expect(scope.variables).to.have.length(3); - expect(scope.variables[0].name).to.equal('a'); - expect(scope.variables[1].name).to.equal('b'); - expect(scope.variables[2].name).to.equal('c'); + expect(scope.variables[0].name).to.equal("a"); + expect(scope.variables[1].name).to.equal("b"); + expect(scope.variables[2].name).to.equal("c"); expect(scope.references).to.have.length(1); - expect(scope.references[0].identifier.name).to.equal('array'); + expect(scope.references[0].identifier.name).to.equal("array"); expect(scope.references[0].isWrite()).to.be.false; scope = scopeManager.scopes[3]; - expect(scope.type).to.equal('for'); + expect(scope.type).to.equal("for"); expect(scope.variables).to.have.length(3); - expect(scope.variables[0].name).to.equal('a'); - expect(scope.variables[1].name).to.equal('b'); - expect(scope.variables[2].name).to.equal('c'); + expect(scope.variables[0].name).to.equal("a"); + expect(scope.variables[1].name).to.equal("b"); + expect(scope.variables[2].name).to.equal("c"); expect(scope.references).to.have.length(5); - expect(scope.references[0].identifier.name).to.equal('c'); + expect(scope.references[0].identifier.name).to.equal("c"); expect(scope.references[0].isWrite()).to.be.true; - expect(scope.references[0].writeExpr.name).to.equal('d'); + expect(scope.references[0].writeExpr.name).to.equal("d"); expect(scope.references[0].partial).to.be.false; expect(scope.references[0].resolved).to.equal(scope.variables[2]); - expect(scope.references[1].identifier.name).to.equal('d'); + expect(scope.references[1].identifier.name).to.equal("d"); expect(scope.references[1].isWrite()).to.be.false; - expect(scope.references[2].identifier.name).to.equal('a'); + expect(scope.references[2].identifier.name).to.equal("a"); expect(scope.references[2].isWrite()).to.be.true; - expect(scope.references[2].writeExpr.name).to.equal('array'); + expect(scope.references[2].writeExpr.name).to.equal("array"); expect(scope.references[2].partial).to.be.true; expect(scope.references[2].resolved).to.equal(scope.variables[0]); - expect(scope.references[3].identifier.name).to.equal('b'); + expect(scope.references[3].identifier.name).to.equal("b"); expect(scope.references[3].isWrite()).to.be.true; - expect(scope.references[3].writeExpr.name).to.equal('array'); + expect(scope.references[3].writeExpr.name).to.equal("array"); expect(scope.references[3].partial).to.be.true; expect(scope.references[3].resolved).to.equal(scope.variables[1]); - expect(scope.references[4].identifier.name).to.equal('c'); + expect(scope.references[4].identifier.name).to.equal("c"); expect(scope.references[4].isWrite()).to.be.true; - expect(scope.references[4].writeExpr.name).to.equal('array'); + expect(scope.references[4].writeExpr.name).to.equal("array"); expect(scope.references[4].partial).to.be.true; expect(scope.references[4].resolved).to.equal(scope.variables[2]); }); - it('Pattern with nested default values in var in ForInStatement', function() { + it("Pattern with nested default values in var in ForInStatement", function() { const ast = espree(` (function () { for (var [a, [b, c = d] = e] in array); @@ -239,61 +241,61 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.equal('global'); + expect(scope.type).to.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); expect(scope.implicit.left).to.have.length(3); - expect(scope.implicit.left[0].identifier.name).to.equal('d'); - expect(scope.implicit.left[1].identifier.name).to.equal('e'); - expect(scope.implicit.left[2].identifier.name).to.equal('array'); + expect(scope.implicit.left[0].identifier.name).to.equal("d"); + expect(scope.implicit.left[1].identifier.name).to.equal("e"); + expect(scope.implicit.left[2].identifier.name).to.equal("array"); scope = scopeManager.scopes[1]; - expect(scope.type).to.equal('function'); + expect(scope.type).to.equal("function"); expect(scope.variables).to.have.length(4); - expect(scope.variables[0].name).to.equal('arguments'); - expect(scope.variables[1].name).to.equal('a'); - expect(scope.variables[2].name).to.equal('b'); - expect(scope.variables[3].name).to.equal('c'); + expect(scope.variables[0].name).to.equal("arguments"); + expect(scope.variables[1].name).to.equal("a"); + expect(scope.variables[2].name).to.equal("b"); + expect(scope.variables[3].name).to.equal("c"); expect(scope.references).to.have.length(9); - expect(scope.references[0].identifier.name).to.equal('b'); + expect(scope.references[0].identifier.name).to.equal("b"); expect(scope.references[0].isWrite()).to.be.true; - expect(scope.references[0].writeExpr.name).to.equal('e'); + expect(scope.references[0].writeExpr.name).to.equal("e"); expect(scope.references[0].partial).to.be.true; expect(scope.references[0].resolved).to.equal(scope.variables[2]); - expect(scope.references[1].identifier.name).to.equal('c'); + expect(scope.references[1].identifier.name).to.equal("c"); expect(scope.references[1].isWrite()).to.be.true; - expect(scope.references[1].writeExpr.name).to.equal('e'); + expect(scope.references[1].writeExpr.name).to.equal("e"); expect(scope.references[1].partial).to.be.true; expect(scope.references[1].resolved).to.equal(scope.variables[3]); - expect(scope.references[2].identifier.name).to.equal('c'); + expect(scope.references[2].identifier.name).to.equal("c"); expect(scope.references[2].isWrite()).to.be.true; - expect(scope.references[2].writeExpr.name).to.equal('d'); + expect(scope.references[2].writeExpr.name).to.equal("d"); expect(scope.references[2].partial).to.be.false; expect(scope.references[2].resolved).to.equal(scope.variables[3]); - expect(scope.references[3].identifier.name).to.equal('d'); + expect(scope.references[3].identifier.name).to.equal("d"); expect(scope.references[3].isWrite()).to.be.false; - expect(scope.references[4].identifier.name).to.equal('e'); + expect(scope.references[4].identifier.name).to.equal("e"); expect(scope.references[4].isWrite()).to.be.false; - expect(scope.references[5].identifier.name).to.equal('a'); + expect(scope.references[5].identifier.name).to.equal("a"); expect(scope.references[5].isWrite()).to.be.true; - expect(scope.references[5].writeExpr.name).to.equal('array'); + expect(scope.references[5].writeExpr.name).to.equal("array"); expect(scope.references[5].partial).to.be.true; expect(scope.references[5].resolved).to.equal(scope.variables[1]); - expect(scope.references[6].identifier.name).to.equal('b'); + expect(scope.references[6].identifier.name).to.equal("b"); expect(scope.references[6].isWrite()).to.be.true; - expect(scope.references[6].writeExpr.name).to.equal('array'); + expect(scope.references[6].writeExpr.name).to.equal("array"); expect(scope.references[6].partial).to.be.true; expect(scope.references[6].resolved).to.equal(scope.variables[2]); - expect(scope.references[7].identifier.name).to.equal('c'); + expect(scope.references[7].identifier.name).to.equal("c"); expect(scope.references[7].isWrite()).to.be.true; - expect(scope.references[7].writeExpr.name).to.equal('array'); + expect(scope.references[7].writeExpr.name).to.equal("array"); expect(scope.references[7].partial).to.be.true; expect(scope.references[7].resolved).to.equal(scope.variables[3]); - expect(scope.references[8].identifier.name).to.equal('array'); + expect(scope.references[8].identifier.name).to.equal("array"); expect(scope.references[8].isWrite()).to.be.false; }); - it('Pattern with nested default values in let in ForInStatement', function() { + it("Pattern with nested default values in let in ForInStatement", function() { const ast = espree(` (function () { for (let [a, [b, c = d] = e] in array); @@ -304,71 +306,71 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(4); // [global, function, TDZ, for] let scope = scopeManager.scopes[0]; - expect(scope.type).to.equal('global'); + expect(scope.type).to.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); expect(scope.implicit.left).to.have.length(3); - expect(scope.implicit.left[0].identifier.name).to.equal('array'); - expect(scope.implicit.left[0].from.type).to.equal('TDZ'); - expect(scope.implicit.left[1].identifier.name).to.equal('d'); - expect(scope.implicit.left[1].from.type).to.equal('for'); - expect(scope.implicit.left[2].identifier.name).to.equal('e'); - expect(scope.implicit.left[2].from.type).to.equal('for'); + expect(scope.implicit.left[0].identifier.name).to.equal("array"); + expect(scope.implicit.left[0].from.type).to.equal("TDZ"); + expect(scope.implicit.left[1].identifier.name).to.equal("d"); + expect(scope.implicit.left[1].from.type).to.equal("for"); + expect(scope.implicit.left[2].identifier.name).to.equal("e"); + expect(scope.implicit.left[2].from.type).to.equal("for"); scope = scopeManager.scopes[2]; - expect(scope.type).to.equal('TDZ'); + expect(scope.type).to.equal("TDZ"); expect(scope.variables).to.have.length(3); - expect(scope.variables[0].name).to.equal('a'); - expect(scope.variables[1].name).to.equal('b'); - expect(scope.variables[2].name).to.equal('c'); + expect(scope.variables[0].name).to.equal("a"); + expect(scope.variables[1].name).to.equal("b"); + expect(scope.variables[2].name).to.equal("c"); expect(scope.references).to.have.length(1); - expect(scope.references[0].identifier.name).to.equal('array'); + expect(scope.references[0].identifier.name).to.equal("array"); expect(scope.references[0].isWrite()).to.be.false; scope = scopeManager.scopes[3]; - expect(scope.type).to.equal('for'); + expect(scope.type).to.equal("for"); expect(scope.variables).to.have.length(3); - expect(scope.variables[0].name).to.equal('a'); - expect(scope.variables[1].name).to.equal('b'); - expect(scope.variables[2].name).to.equal('c'); + expect(scope.variables[0].name).to.equal("a"); + expect(scope.variables[1].name).to.equal("b"); + expect(scope.variables[2].name).to.equal("c"); expect(scope.references).to.have.length(8); - expect(scope.references[0].identifier.name).to.equal('b'); + expect(scope.references[0].identifier.name).to.equal("b"); expect(scope.references[0].isWrite()).to.be.true; - expect(scope.references[0].writeExpr.name).to.equal('e'); + expect(scope.references[0].writeExpr.name).to.equal("e"); expect(scope.references[0].partial).to.be.true; expect(scope.references[0].resolved).to.equal(scope.variables[1]); - expect(scope.references[1].identifier.name).to.equal('c'); + expect(scope.references[1].identifier.name).to.equal("c"); expect(scope.references[1].isWrite()).to.be.true; - expect(scope.references[1].writeExpr.name).to.equal('e'); + expect(scope.references[1].writeExpr.name).to.equal("e"); expect(scope.references[1].partial).to.be.true; expect(scope.references[1].resolved).to.equal(scope.variables[2]); - expect(scope.references[2].identifier.name).to.equal('c'); + expect(scope.references[2].identifier.name).to.equal("c"); expect(scope.references[2].isWrite()).to.be.true; - expect(scope.references[2].writeExpr.name).to.equal('d'); + expect(scope.references[2].writeExpr.name).to.equal("d"); expect(scope.references[2].partial).to.be.false; expect(scope.references[2].resolved).to.equal(scope.variables[2]); - expect(scope.references[3].identifier.name).to.equal('d'); + expect(scope.references[3].identifier.name).to.equal("d"); expect(scope.references[3].isWrite()).to.be.false; - expect(scope.references[4].identifier.name).to.equal('e'); + expect(scope.references[4].identifier.name).to.equal("e"); expect(scope.references[4].isWrite()).to.be.false; - expect(scope.references[5].identifier.name).to.equal('a'); + expect(scope.references[5].identifier.name).to.equal("a"); expect(scope.references[5].isWrite()).to.be.true; - expect(scope.references[5].writeExpr.name).to.equal('array'); + expect(scope.references[5].writeExpr.name).to.equal("array"); expect(scope.references[5].partial).to.be.true; expect(scope.references[5].resolved).to.equal(scope.variables[0]); - expect(scope.references[6].identifier.name).to.equal('b'); + expect(scope.references[6].identifier.name).to.equal("b"); expect(scope.references[6].isWrite()).to.be.true; - expect(scope.references[6].writeExpr.name).to.equal('array'); + expect(scope.references[6].writeExpr.name).to.equal("array"); expect(scope.references[6].partial).to.be.true; expect(scope.references[6].resolved).to.equal(scope.variables[1]); - expect(scope.references[7].identifier.name).to.equal('c'); + expect(scope.references[7].identifier.name).to.equal("c"); expect(scope.references[7].isWrite()).to.be.true; - expect(scope.references[7].writeExpr.name).to.equal('array'); + expect(scope.references[7].writeExpr.name).to.equal("array"); expect(scope.references[7].partial).to.be.true; expect(scope.references[7].resolved).to.equal(scope.variables[2]); }); - it('Pattern with default values in var in ForInStatement (separate declarations)', function() { + it("Pattern with default values in var in ForInStatement (separate declarations)", function() { const ast = espree(` (function () { var a, b, c; @@ -380,46 +382,46 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.equal('global'); + expect(scope.type).to.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); expect(scope.implicit.left).to.have.length(2); - expect(scope.implicit.left[0].identifier.name).to.equal('d'); - expect(scope.implicit.left[1].identifier.name).to.equal('array'); + expect(scope.implicit.left[0].identifier.name).to.equal("d"); + expect(scope.implicit.left[1].identifier.name).to.equal("array"); scope = scopeManager.scopes[1]; - expect(scope.type).to.equal('function'); + expect(scope.type).to.equal("function"); expect(scope.variables).to.have.length(4); - expect(scope.variables[0].name).to.equal('arguments'); - expect(scope.variables[1].name).to.equal('a'); - expect(scope.variables[2].name).to.equal('b'); - expect(scope.variables[3].name).to.equal('c'); + expect(scope.variables[0].name).to.equal("arguments"); + expect(scope.variables[1].name).to.equal("a"); + expect(scope.variables[2].name).to.equal("b"); + expect(scope.variables[3].name).to.equal("c"); expect(scope.references).to.have.length(6); - expect(scope.references[0].identifier.name).to.equal('a'); + expect(scope.references[0].identifier.name).to.equal("a"); expect(scope.references[0].isWrite()).to.be.true; expect(scope.references[0].partial).to.be.true; expect(scope.references[0].resolved).to.equal(scope.variables[1]); - expect(scope.references[1].identifier.name).to.equal('b'); + expect(scope.references[1].identifier.name).to.equal("b"); expect(scope.references[1].isWrite()).to.be.true; expect(scope.references[1].partial).to.be.true; expect(scope.references[1].resolved).to.equal(scope.variables[2]); - expect(scope.references[2].identifier.name).to.equal('c'); + expect(scope.references[2].identifier.name).to.equal("c"); expect(scope.references[2].isWrite()).to.be.true; - expect(scope.references[2].writeExpr.name).to.equal('d'); + expect(scope.references[2].writeExpr.name).to.equal("d"); expect(scope.references[2].partial).to.be.false; expect(scope.references[2].resolved).to.equal(scope.variables[3]); - expect(scope.references[3].identifier.name).to.equal('c'); + expect(scope.references[3].identifier.name).to.equal("c"); expect(scope.references[3].isWrite()).to.be.true; - expect(scope.references[3].writeExpr.name).to.equal('array'); + expect(scope.references[3].writeExpr.name).to.equal("array"); expect(scope.references[3].partial).to.be.true; expect(scope.references[3].resolved).to.equal(scope.variables[3]); - expect(scope.references[4].identifier.name).to.equal('d'); + expect(scope.references[4].identifier.name).to.equal("d"); expect(scope.references[4].isWrite()).to.be.false; - expect(scope.references[5].identifier.name).to.equal('array'); + expect(scope.references[5].identifier.name).to.equal("array"); expect(scope.references[5].isWrite()).to.be.false; }); - it('Pattern with default values in var in ForInStatement (separate declarations and with MemberExpression)', function() { + it("Pattern with default values in var in ForInStatement (separate declarations and with MemberExpression)", function() { const ast = espree(` (function () { var obj; @@ -431,40 +433,40 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.equal('global'); + expect(scope.type).to.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); expect(scope.implicit.left).to.have.length(2); - expect(scope.implicit.left[0].identifier.name).to.equal('d'); - expect(scope.implicit.left[1].identifier.name).to.equal('array'); + expect(scope.implicit.left[0].identifier.name).to.equal("d"); + expect(scope.implicit.left[1].identifier.name).to.equal("array"); scope = scopeManager.scopes[1]; - expect(scope.type).to.equal('function'); + expect(scope.type).to.equal("function"); expect(scope.variables).to.have.length(2); - expect(scope.variables[0].name).to.equal('arguments'); - expect(scope.variables[1].name).to.equal('obj'); + expect(scope.variables[0].name).to.equal("arguments"); + expect(scope.variables[1].name).to.equal("obj"); expect(scope.references).to.have.length(5); - expect(scope.references[0].identifier.name).to.equal('obj'); // obj.a + expect(scope.references[0].identifier.name).to.equal("obj"); // obj.a expect(scope.references[0].isWrite()).to.be.false; expect(scope.references[0].isRead()).to.be.true; expect(scope.references[0].resolved).to.equal(scope.variables[1]); - expect(scope.references[1].identifier.name).to.equal('obj'); // obj.b + expect(scope.references[1].identifier.name).to.equal("obj"); // obj.b expect(scope.references[1].isWrite()).to.be.false; expect(scope.references[1].isRead()).to.be.true; expect(scope.references[1].resolved).to.equal(scope.variables[1]); - expect(scope.references[2].identifier.name).to.equal('obj'); // obj.c + expect(scope.references[2].identifier.name).to.equal("obj"); // obj.c expect(scope.references[2].isWrite()).to.be.false; expect(scope.references[2].isRead()).to.be.true; expect(scope.references[2].resolved).to.equal(scope.variables[1]); - expect(scope.references[3].identifier.name).to.equal('d'); + expect(scope.references[3].identifier.name).to.equal("d"); expect(scope.references[3].isWrite()).to.be.false; expect(scope.references[3].isRead()).to.be.true; - expect(scope.references[4].identifier.name).to.equal('array'); + expect(scope.references[4].identifier.name).to.equal("array"); expect(scope.references[4].isWrite()).to.be.false; expect(scope.references[4].isRead()).to.be.true; }); - it('ArrayPattern in var', function() { + it("ArrayPattern in var", function() { const ast = harmony.parse(` (function () { var [a, b, c] = array; @@ -475,37 +477,37 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); expect(scope.implicit.left).to.have.length(1); - expect(scope.implicit.left[0].identifier.name).to.be.equal('array'); + expect(scope.implicit.left[0].identifier.name).to.be.equal("array"); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(4); - expect(scope.variables[0].name).to.be.equal('arguments'); - expect(scope.variables[1].name).to.be.equal('a'); - expect(scope.variables[2].name).to.be.equal('b'); - expect(scope.variables[3].name).to.be.equal('c'); + expect(scope.variables[0].name).to.be.equal("arguments"); + expect(scope.variables[1].name).to.be.equal("a"); + expect(scope.variables[2].name).to.be.equal("b"); + expect(scope.variables[3].name).to.be.equal("c"); expect(scope.references).to.have.length(4); - expect(scope.references[0].identifier.name).to.be.equal('a'); + expect(scope.references[0].identifier.name).to.be.equal("a"); expect(scope.references[0].isWrite()).to.be.true; expect(scope.references[0].partial).to.be.true; expect(scope.references[0].resolved).to.be.equal(scope.variables[1]); - expect(scope.references[1].identifier.name).to.be.equal('b'); + expect(scope.references[1].identifier.name).to.be.equal("b"); expect(scope.references[1].isWrite()).to.be.true; expect(scope.references[1].partial).to.be.true; expect(scope.references[1].resolved).to.be.equal(scope.variables[2]); - expect(scope.references[2].identifier.name).to.be.equal('c'); + expect(scope.references[2].identifier.name).to.be.equal("c"); expect(scope.references[2].isWrite()).to.be.true; expect(scope.references[2].partial).to.be.true; expect(scope.references[2].resolved).to.be.equal(scope.variables[3]); - expect(scope.references[3].identifier.name).to.be.equal('array'); + expect(scope.references[3].identifier.name).to.be.equal("array"); expect(scope.references[3].isWrite()).to.be.false; }); - it('SpreadElement in var', function() { + it("SpreadElement in var", function() { let ast = harmony.parse(` (function () { var [a, b, ...rest] = array; @@ -516,33 +518,33 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); expect(scope.implicit.left).to.have.length(1); - expect(scope.implicit.left[0].identifier.name).to.be.equal('array'); + expect(scope.implicit.left[0].identifier.name).to.be.equal("array"); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(4); - expect(scope.variables[0].name).to.be.equal('arguments'); - expect(scope.variables[1].name).to.be.equal('a'); - expect(scope.variables[2].name).to.be.equal('b'); - expect(scope.variables[3].name).to.be.equal('rest'); + expect(scope.variables[0].name).to.be.equal("arguments"); + expect(scope.variables[1].name).to.be.equal("a"); + expect(scope.variables[2].name).to.be.equal("b"); + expect(scope.variables[3].name).to.be.equal("rest"); expect(scope.references).to.have.length(4); - expect(scope.references[0].identifier.name).to.be.equal('a'); + expect(scope.references[0].identifier.name).to.be.equal("a"); expect(scope.references[0].isWrite()).to.be.true; expect(scope.references[0].partial).to.be.true; expect(scope.references[0].resolved).to.be.equal(scope.variables[1]); - expect(scope.references[1].identifier.name).to.be.equal('b'); + expect(scope.references[1].identifier.name).to.be.equal("b"); expect(scope.references[1].isWrite()).to.be.true; expect(scope.references[1].partial).to.be.true; expect(scope.references[1].resolved).to.be.equal(scope.variables[2]); - expect(scope.references[2].identifier.name).to.be.equal('rest'); + expect(scope.references[2].identifier.name).to.be.equal("rest"); expect(scope.references[2].isWrite()).to.be.true; expect(scope.references[2].partial).to.be.true; expect(scope.references[2].resolved).to.be.equal(scope.variables[3]); - expect(scope.references[3].identifier.name).to.be.equal('array'); + expect(scope.references[3].identifier.name).to.be.equal("array"); expect(scope.references[3].isWrite()).to.be.false; ast = harmony.parse(` @@ -555,23 +557,23 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); expect(scope.implicit.left).to.have.length(1); - expect(scope.implicit.left[0].identifier.name).to.be.equal('array'); + expect(scope.implicit.left[0].identifier.name).to.be.equal("array"); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(6); const expectedVariableNames = [ - 'arguments', - 'a', - 'b', - 'c', - 'd', - 'rest' + "arguments", + "a", + "b", + "c", + "d", + "rest" ]; for (let index = 0; index < expectedVariableNames.length; index++) { expect(scope.variables[index].name).to.be.equal(expectedVariableNames[index]); @@ -579,22 +581,22 @@ describe('ES6 destructuring assignments', function() { expect(scope.references).to.have.length(6); const expectedReferenceNames = [ - 'a', - 'b', - 'c', - 'd', - 'rest' + "a", + "b", + "c", + "d", + "rest" ]; for (let index = 0; index < expectedReferenceNames.length; index++) { expect(scope.references[index].identifier.name).to.be.equal(expectedReferenceNames[index]); expect(scope.references[index].isWrite()).to.be.true; expect(scope.references[index].partial).to.be.true; } - expect(scope.references[5].identifier.name).to.be.equal('array'); + expect(scope.references[5].identifier.name).to.be.equal("array"); expect(scope.references[5].isWrite()).to.be.false; }); - it('ObjectPattern in var', function() { + it("ObjectPattern in var", function() { const ast = harmony.parse(` (function () { var { @@ -611,37 +613,37 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); expect(scope.implicit.left).to.have.length(1); - expect(scope.implicit.left[0].identifier.name).to.be.equal('object'); + expect(scope.implicit.left[0].identifier.name).to.be.equal("object"); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(4); - expect(scope.variables[0].name).to.be.equal('arguments'); - expect(scope.variables[1].name).to.be.equal('shorthand'); - expect(scope.variables[2].name).to.be.equal('value'); - expect(scope.variables[3].name).to.be.equal('world'); + expect(scope.variables[0].name).to.be.equal("arguments"); + expect(scope.variables[1].name).to.be.equal("shorthand"); + expect(scope.variables[2].name).to.be.equal("value"); + expect(scope.variables[3].name).to.be.equal("world"); expect(scope.references).to.have.length(4); - expect(scope.references[0].identifier.name).to.be.equal('shorthand'); + expect(scope.references[0].identifier.name).to.be.equal("shorthand"); expect(scope.references[0].isWrite()).to.be.true; expect(scope.references[0].partial).to.be.true; expect(scope.references[0].resolved).to.be.equal(scope.variables[1]); - expect(scope.references[1].identifier.name).to.be.equal('value'); + expect(scope.references[1].identifier.name).to.be.equal("value"); expect(scope.references[1].isWrite()).to.be.true; expect(scope.references[1].partial).to.be.true; expect(scope.references[1].resolved).to.be.equal(scope.variables[2]); - expect(scope.references[2].identifier.name).to.be.equal('world'); + expect(scope.references[2].identifier.name).to.be.equal("world"); expect(scope.references[2].isWrite()).to.be.true; expect(scope.references[2].partial).to.be.true; expect(scope.references[2].resolved).to.be.equal(scope.variables[3]); - expect(scope.references[3].identifier.name).to.be.equal('object'); + expect(scope.references[3].identifier.name).to.be.equal("object"); expect(scope.references[3].isWrite()).to.be.false; }); - it('complex pattern in var', function() { + it("complex pattern in var", function() { const ast = harmony.parse(` (function () { var { @@ -658,48 +660,48 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); expect(scope.implicit.left).to.have.length(1); - expect(scope.implicit.left[0].identifier.name).to.be.equal('object'); + expect(scope.implicit.left[0].identifier.name).to.be.equal("object"); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(8); const expectedVariableNames = [ - 'arguments', - 'shorthand', - 'a', - 'b', - 'c', - 'd', - 'e', - 'world' + "arguments", + "shorthand", + "a", + "b", + "c", + "d", + "e", + "world" ]; for (let index = 0; index < expectedVariableNames.length; index++) { expect(scope.variables[index].name).to.be.equal(expectedVariableNames[index]); } expect(scope.references).to.have.length(8); const expectedReferenceNames = [ - 'shorthand', - 'a', - 'b', - 'c', - 'd', - 'e', - 'world' + "shorthand", + "a", + "b", + "c", + "d", + "e", + "world" ]; for (let index = 0; index < expectedReferenceNames.length; index++) { expect(scope.references[index].identifier.name).to.be.equal(expectedReferenceNames[index]); expect(scope.references[index].isWrite()).to.be.true; expect(scope.references[index].partial).to.be.true; } - expect(scope.references[7].identifier.name).to.be.equal('object'); + expect(scope.references[7].identifier.name).to.be.equal("object"); expect(scope.references[7].isWrite()).to.be.false; }); - it('ArrayPattern in AssignmentExpression', function() { + it("ArrayPattern in AssignmentExpression", function() { const ast = harmony.parse(` (function () { [a, b, c] = array; @@ -710,39 +712,39 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); expect(scope.implicit.left).to.have.length(4); expect(scope.implicit.left.map((left) => left.identifier.name)).to.deep.equal([ - 'a', - 'b', - 'c', - 'array' + "a", + "b", + "c", + "array" ]); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); expect(scope.references).to.have.length(4); - expect(scope.references[0].identifier.name).to.be.equal('a'); + expect(scope.references[0].identifier.name).to.be.equal("a"); expect(scope.references[0].isWrite()).to.be.true; expect(scope.references[0].partial).to.be.true; expect(scope.references[0].resolved).to.be.null; - expect(scope.references[1].identifier.name).to.be.equal('b'); + expect(scope.references[1].identifier.name).to.be.equal("b"); expect(scope.references[1].isWrite()).to.be.true; expect(scope.references[1].partial).to.be.true; expect(scope.references[1].resolved).to.be.null; - expect(scope.references[2].identifier.name).to.be.equal('c'); + expect(scope.references[2].identifier.name).to.be.equal("c"); expect(scope.references[2].isWrite()).to.be.true; expect(scope.references[2].partial).to.be.true; expect(scope.references[2].resolved).to.be.null; - expect(scope.references[3].identifier.name).to.be.equal('array'); + expect(scope.references[3].identifier.name).to.be.equal("array"); expect(scope.references[3].isWrite()).to.be.false; }); - it('ArrayPattern with MemberExpression in AssignmentExpression', function() { + it("ArrayPattern with MemberExpression in AssignmentExpression", function() { const ast = harmony.parse(` (function () { var obj; @@ -754,36 +756,36 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.equal('global'); + expect(scope.type).to.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); expect(scope.implicit.left).to.have.length(1); - expect(scope.implicit.left[0].identifier.name).to.equal('array'); + expect(scope.implicit.left[0].identifier.name).to.equal("array"); scope = scopeManager.scopes[1]; - expect(scope.type).to.equal('function'); + expect(scope.type).to.equal("function"); expect(scope.variables).to.have.length(2); - expect(scope.variables[0].name).to.equal('arguments'); - expect(scope.variables[1].name).to.equal('obj'); + expect(scope.variables[0].name).to.equal("arguments"); + expect(scope.variables[1].name).to.equal("obj"); expect(scope.references).to.have.length(4); - expect(scope.references[0].identifier.name).to.equal('obj'); + expect(scope.references[0].identifier.name).to.equal("obj"); expect(scope.references[0].isWrite()).to.be.false; expect(scope.references[0].isRead()).to.be.true; expect(scope.references[0].resolved).to.equal(scope.variables[1]); - expect(scope.references[1].identifier.name).to.equal('obj'); + expect(scope.references[1].identifier.name).to.equal("obj"); expect(scope.references[1].isWrite()).to.be.false; expect(scope.references[1].isRead()).to.be.true; expect(scope.references[1].resolved).to.equal(scope.variables[1]); - expect(scope.references[2].identifier.name).to.equal('obj'); + expect(scope.references[2].identifier.name).to.equal("obj"); expect(scope.references[2].isWrite()).to.be.false; expect(scope.references[2].isRead()).to.be.true; expect(scope.references[2].resolved).to.equal(scope.variables[1]); - expect(scope.references[3].identifier.name).to.equal('array'); + expect(scope.references[3].identifier.name).to.equal("array"); expect(scope.references[3].isWrite()).to.be.false; expect(scope.references[3].isRead()).to.be.true; }); - it('SpreadElement in AssignmentExpression', function() { + it("SpreadElement in AssignmentExpression", function() { let ast = harmony.parse(` (function () { [a, b, ...rest] = array; @@ -794,35 +796,35 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); expect(scope.implicit.left).to.have.length(4); expect(scope.implicit.left.map((left) => left.identifier.name)).to.deep.equal([ - 'a', - 'b', - 'rest', - 'array' + "a", + "b", + "rest", + "array" ]); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); expect(scope.references).to.have.length(4); - expect(scope.references[0].identifier.name).to.be.equal('a'); + expect(scope.references[0].identifier.name).to.be.equal("a"); expect(scope.references[0].isWrite()).to.be.true; expect(scope.references[0].partial).to.be.true; expect(scope.references[0].resolved).to.be.null; - expect(scope.references[1].identifier.name).to.be.equal('b'); + expect(scope.references[1].identifier.name).to.be.equal("b"); expect(scope.references[1].isWrite()).to.be.true; expect(scope.references[1].partial).to.be.true; expect(scope.references[1].resolved).to.be.null; - expect(scope.references[2].identifier.name).to.be.equal('rest'); + expect(scope.references[2].identifier.name).to.be.equal("rest"); expect(scope.references[2].isWrite()).to.be.true; expect(scope.references[2].partial).to.be.true; expect(scope.references[2].resolved).to.be.null; - expect(scope.references[3].identifier.name).to.be.equal('array'); + expect(scope.references[3].identifier.name).to.be.equal("array"); expect(scope.references[3].isWrite()).to.be.false; ast = harmony.parse(` @@ -835,32 +837,32 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); expect(scope.implicit.left).to.have.length(6); expect(scope.implicit.left.map((left) => left.identifier.name)).to.deep.equal([ - 'a', - 'b', - 'c', - 'd', - 'rest', - 'array' + "a", + "b", + "c", + "d", + "rest", + "array" ]); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); expect(scope.references).to.have.length(6); const expectedReferenceNames = [ - 'a', - 'b', - 'c', - 'd', - 'rest' + "a", + "b", + "c", + "d", + "rest" ]; for (let index = 0; index < expectedReferenceNames.length; index++) { expect(scope.references[index].identifier.name).to.be.equal(expectedReferenceNames[index]); @@ -868,11 +870,11 @@ describe('ES6 destructuring assignments', function() { expect(scope.references[index].partial).to.be.true; expect(scope.references[index].resolved).to.be.null; } - expect(scope.references[5].identifier.name).to.be.equal('array'); + expect(scope.references[5].identifier.name).to.be.equal("array"); expect(scope.references[5].isWrite()).to.be.false; }); - it('SpreadElement with MemberExpression in AssignmentExpression', function() { + it("SpreadElement with MemberExpression in AssignmentExpression", function() { const ast = harmony.parse(` (function () { [a, b, ...obj.rest] = array; @@ -883,37 +885,37 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.equal('global'); + expect(scope.type).to.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); expect(scope.implicit.left).to.have.length(4); expect(scope.implicit.left.map((left) => left.identifier.name)).to.deep.equal([ - 'a', - 'b', - 'obj', - 'array' + "a", + "b", + "obj", + "array" ]); scope = scopeManager.scopes[1]; - expect(scope.type).to.equal('function'); + expect(scope.type).to.equal("function"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.equal('arguments'); + expect(scope.variables[0].name).to.equal("arguments"); expect(scope.references).to.have.length(4); - expect(scope.references[0].identifier.name).to.equal('a'); + expect(scope.references[0].identifier.name).to.equal("a"); expect(scope.references[0].isWrite()).to.be.true; expect(scope.references[0].partial).to.be.true; expect(scope.references[0].resolved).to.be.null; - expect(scope.references[1].identifier.name).to.equal('b'); + expect(scope.references[1].identifier.name).to.equal("b"); expect(scope.references[1].isWrite()).to.be.true; expect(scope.references[1].partial).to.be.true; expect(scope.references[1].resolved).to.be.null; - expect(scope.references[2].identifier.name).to.equal('obj'); + expect(scope.references[2].identifier.name).to.equal("obj"); expect(scope.references[2].isWrite()).to.be.false; - expect(scope.references[3].identifier.name).to.equal('array'); + expect(scope.references[3].identifier.name).to.equal("array"); expect(scope.references[3].isWrite()).to.be.false; }); - it('ObjectPattern in AssignmentExpression', function() { + it("ObjectPattern in AssignmentExpression", function() { const ast = harmony.parse(` (function () { ({ @@ -930,39 +932,39 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); expect(scope.implicit.left).to.have.length(4); expect(scope.implicit.left.map((left) => left.identifier.name)).to.deep.equal([ - 'shorthand', - 'value', - 'world', - 'object' + "shorthand", + "value", + "world", + "object" ]); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); expect(scope.references).to.have.length(4); - expect(scope.references[0].identifier.name).to.be.equal('shorthand'); + expect(scope.references[0].identifier.name).to.be.equal("shorthand"); expect(scope.references[0].isWrite()).to.be.true; expect(scope.references[0].partial).to.be.true; expect(scope.references[0].resolved).to.null; - expect(scope.references[1].identifier.name).to.be.equal('value'); + expect(scope.references[1].identifier.name).to.be.equal("value"); expect(scope.references[1].isWrite()).to.be.true; expect(scope.references[1].partial).to.be.true; expect(scope.references[1].resolved).to.null; - expect(scope.references[2].identifier.name).to.be.equal('world'); + expect(scope.references[2].identifier.name).to.be.equal("world"); expect(scope.references[2].isWrite()).to.be.true; expect(scope.references[2].partial).to.be.true; expect(scope.references[2].resolved).to.null; - expect(scope.references[3].identifier.name).to.be.equal('object'); + expect(scope.references[3].identifier.name).to.be.equal("object"); expect(scope.references[3].isWrite()).to.be.false; }); - it('complex pattern in AssignmentExpression', function() { + it("complex pattern in AssignmentExpression", function() { const ast = harmony.parse(` (function () { ({ @@ -979,45 +981,45 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); expect(scope.implicit.left).to.have.length(8); expect(scope.implicit.left.map((left) => left.identifier.name)).to.deep.equal([ - 'shorthand', - 'a', - 'b', - 'c', - 'd', - 'e', - 'world', - 'object' + "shorthand", + "a", + "b", + "c", + "d", + "e", + "world", + "object" ]); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); expect(scope.references).to.have.length(8); const expectedReferenceNames = [ - 'shorthand', - 'a', - 'b', - 'c', - 'd', - 'e', - 'world' + "shorthand", + "a", + "b", + "c", + "d", + "e", + "world" ]; for (let index = 0; index < expectedReferenceNames.length; index++) { expect(scope.references[index].identifier.name).to.be.equal(expectedReferenceNames[index]); expect(scope.references[index].isWrite()).to.be.true; expect(scope.references[index].partial).to.be.true; } - expect(scope.references[7].identifier.name).to.be.equal('object'); + expect(scope.references[7].identifier.name).to.be.equal("object"); expect(scope.references[7].isWrite()).to.be.false; }); - it('ArrayPattern in parameters', function() { + it("ArrayPattern in parameters", function() { const ast = harmony.parse(` (function ([a, b, c]) { }(array)); @@ -1027,24 +1029,24 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(1); - expect(scope.references[0].identifier.name).to.be.equal('array'); + expect(scope.references[0].identifier.name).to.be.equal("array"); expect(scope.implicit.left).to.have.length(1); - expect(scope.implicit.left[0].identifier.name).to.be.equal('array'); + expect(scope.implicit.left[0].identifier.name).to.be.equal("array"); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(4); - expect(scope.variables[0].name).to.be.equal('arguments'); - expect(scope.variables[1].name).to.be.equal('a'); - expect(scope.variables[2].name).to.be.equal('b'); - expect(scope.variables[3].name).to.be.equal('c'); + expect(scope.variables[0].name).to.be.equal("arguments"); + expect(scope.variables[1].name).to.be.equal("a"); + expect(scope.variables[2].name).to.be.equal("b"); + expect(scope.variables[3].name).to.be.equal("c"); expect(scope.references).to.have.length(0); }); - it('SpreadElement in parameters', function() { + it("SpreadElement in parameters", function() { const ast = harmony.parse(` (function ([a, b, ...rest], ...rest2) { }(array)); @@ -1054,22 +1056,22 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(1); - expect(scope.references[0].identifier.name).to.be.equal('array'); + expect(scope.references[0].identifier.name).to.be.equal("array"); expect(scope.implicit.left).to.have.length(1); - expect(scope.implicit.left[0].identifier.name).to.be.equal('array'); + expect(scope.implicit.left[0].identifier.name).to.be.equal("array"); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(5); - expect(scope.variables[0].name).to.be.equal('arguments'); - expect(scope.variables[1].name).to.be.equal('a'); - expect(scope.variables[2].name).to.be.equal('b'); - expect(scope.variables[3].name).to.be.equal('rest'); + expect(scope.variables[0].name).to.be.equal("arguments"); + expect(scope.variables[1].name).to.be.equal("a"); + expect(scope.variables[2].name).to.be.equal("b"); + expect(scope.variables[3].name).to.be.equal("rest"); expect(scope.variables[3].defs[0].rest).to.be.false; - expect(scope.variables[4].name).to.be.equal('rest2'); + expect(scope.variables[4].name).to.be.equal("rest2"); expect(scope.variables[4].defs[0].rest).to.be.true; expect(scope.references).to.have.length(0); @@ -1121,7 +1123,7 @@ describe('ES6 destructuring assignments', function() { // expect(scope.references[5].isWrite()).to.be.false; }); - it('ObjectPattern in parameters', function() { + it("ObjectPattern in parameters", function() { const ast = harmony.parse(` (function ({ shorthand, @@ -1137,24 +1139,24 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(1); - expect(scope.references[0].identifier.name).to.be.equal('object'); + expect(scope.references[0].identifier.name).to.be.equal("object"); expect(scope.implicit.left).to.have.length(1); - expect(scope.implicit.left[0].identifier.name).to.be.equal('object'); + expect(scope.implicit.left[0].identifier.name).to.be.equal("object"); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(4); - expect(scope.variables[0].name).to.be.equal('arguments'); - expect(scope.variables[1].name).to.be.equal('shorthand'); - expect(scope.variables[2].name).to.be.equal('value'); - expect(scope.variables[3].name).to.be.equal('world'); + expect(scope.variables[0].name).to.be.equal("arguments"); + expect(scope.variables[1].name).to.be.equal("shorthand"); + expect(scope.variables[2].name).to.be.equal("value"); + expect(scope.variables[3].name).to.be.equal("world"); expect(scope.references).to.have.length(0); }); - it('complex pattern in parameters', function() { + it("complex pattern in parameters", function() { const ast = harmony.parse(` (function ({ shorthand, @@ -1170,25 +1172,25 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(1); - expect(scope.references[0].identifier.name).to.be.equal('object'); + expect(scope.references[0].identifier.name).to.be.equal("object"); expect(scope.implicit.left).to.have.length(1); - expect(scope.implicit.left[0].identifier.name).to.be.equal('object'); + expect(scope.implicit.left[0].identifier.name).to.be.equal("object"); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(8); const expectedVariableNames = [ - 'arguments', - 'shorthand', - 'a', - 'b', - 'c', - 'd', - 'e', - 'world' + "arguments", + "shorthand", + "a", + "b", + "c", + "d", + "e", + "world" ]; for (let index = 0; index < expectedVariableNames.length; index++) { expect(scope.variables[index].name).to.be.equal(expectedVariableNames[index]); @@ -1196,7 +1198,7 @@ describe('ES6 destructuring assignments', function() { expect(scope.references).to.have.length(0); }); - it('default values and patterns in var', function() { + it("default values and patterns in var", function() { const ast = espree(` (function () { var [a, b, c, d = 20 ] = array; @@ -1207,38 +1209,38 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(5); const expectedVariableNames = [ - 'arguments', - 'a', - 'b', - 'c', - 'd' + "arguments", + "a", + "b", + "c", + "d" ]; for (let index = 0; index < expectedVariableNames.length; index++) { expect(scope.variables[index].name).to.be.equal(expectedVariableNames[index]); } expect(scope.references).to.have.length(6); const expectedReferenceNames = [ - 'a', - 'b', - 'c', - 'd', // assign 20 - 'd', // assign array - 'array' + "a", + "b", + "c", + "d", // assign 20 + "d", // assign array + "array" ]; for (let index = 0; index < expectedReferenceNames.length; index++) { expect(scope.references[index].identifier.name).to.be.equal(expectedReferenceNames[index]); } }); - it('default values containing references and patterns in var', function() { + it("default values containing references and patterns in var", function() { const ast = espree(` (function () { var [a, b, c, d = e ] = array; @@ -1249,39 +1251,39 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(5); const expectedVariableNames = [ - 'arguments', - 'a', - 'b', - 'c', - 'd' + "arguments", + "a", + "b", + "c", + "d" ]; for (let index = 0; index < expectedVariableNames.length; index++) { expect(scope.variables[index].name).to.be.equal(expectedVariableNames[index]); } expect(scope.references).to.have.length(7); const expectedReferenceNames = [ - 'a', // assign array - 'b', // assign array - 'c', // assign array - 'd', // assign e - 'd', // assign array - 'e', - 'array' + "a", // assign array + "b", // assign array + "c", // assign array + "d", // assign e + "d", // assign array + "e", + "array" ]; for (let index = 0; index < expectedReferenceNames.length; index++) { expect(scope.references[index].identifier.name).to.be.equal(expectedReferenceNames[index]); } }); - it('nested default values containing references and patterns in var', function() { + it("nested default values containing references and patterns in var", function() { const ast = espree(` (function () { var [a, b, [c, d = e] = f ] = array; @@ -1292,35 +1294,35 @@ describe('ES6 destructuring assignments', function() { expect(scopeManager.scopes).to.have.length(2); let scope = scopeManager.scopes[0]; - expect(scope.type).to.equal('global'); + expect(scope.type).to.equal("global"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); scope = scopeManager.scopes[1]; - expect(scope.type).to.equal('function'); + expect(scope.type).to.equal("function"); expect(scope.variables).to.have.length(5); const expectedVariableNames = [ - 'arguments', - 'a', - 'b', - 'c', - 'd' + "arguments", + "a", + "b", + "c", + "d" ]; for (let index = 0; index < expectedVariableNames.length; index++) { expect(scope.variables[index].name).to.equal(expectedVariableNames[index]); } expect(scope.references).to.have.length(10); const expectedReferenceNames = [ - 'a', // assign array - 'b', // assign array - 'c', // assign f - 'c', // assign array - 'd', // assign f - 'd', // assign e - 'd', // assign array - 'e', - 'f', - 'array' + "a", // assign array + "b", // assign array + "c", // assign f + "c", // assign array + "d", // assign f + "d", // assign e + "d", // assign array + "e", + "f", + "array" ]; for (let index = 0; index < expectedReferenceNames.length; index++) { expect(scope.references[index].identifier.name).to.equal(expectedReferenceNames[index]); diff --git a/test/es6-export.js b/test/es6-export.js index 6e7edca..4f5ab32 100644 --- a/test/es6-export.js +++ b/test/es6-export.js @@ -22,171 +22,171 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const espree = require('../third_party/espree'); -const analyze = require('..').analyze; +const expect = require("chai").expect; +const espree = require("../third_party/espree"); +const analyze = require("..").analyze; -describe('export declaration', function() { +describe("export declaration", function() { // http://people.mozilla.org/~jorendorff/es6-draft.html#sec-static-and-runtme-semantics-module-records - it('should create vairable bindings', function() { - const ast = espree(`export var v;`, {sourceType: 'module'}); + it("should create vairable bindings", function() { + const ast = espree("export var v;", {sourceType: "module"}); - const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: 'module'}); + const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: "module"}); expect(scopeManager.scopes).to.have.length(2); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(0); expect(globalScope.references).to.have.length(0); const scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('module'); + expect(scope.type).to.be.equal("module"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('v'); - expect(scope.variables[0].defs[0].type).to.be.equal('Variable'); + expect(scope.variables[0].name).to.be.equal("v"); + expect(scope.variables[0].defs[0].type).to.be.equal("Variable"); expect(scope.references).to.have.length(0); }); - it('should create function declaration bindings', function() { - const ast = espree(`export default function f(){};`, {sourceType: 'module'}); + it("should create function declaration bindings", function() { + const ast = espree("export default function f(){};", {sourceType: "module"}); - const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: 'module'}); + const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: "module"}); expect(scopeManager.scopes).to.have.length(3); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(0); expect(globalScope.references).to.have.length(0); let scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('module'); + expect(scope.type).to.be.equal("module"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('f'); - expect(scope.variables[0].defs[0].type).to.be.equal('FunctionName'); + expect(scope.variables[0].name).to.be.equal("f"); + expect(scope.variables[0].defs[0].type).to.be.equal("FunctionName"); expect(scope.references).to.have.length(0); scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); expect(scope.references).to.have.length(0); }); - it('should export function expression', function() { - const ast = espree(`export default function(){};`, {sourceType: 'module'}); + it("should export function expression", function() { + const ast = espree("export default function(){};", {sourceType: "module"}); - const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: 'module'}); + const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: "module"}); expect(scopeManager.scopes).to.have.length(3); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(0); expect(globalScope.references).to.have.length(0); let scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('module'); + expect(scope.type).to.be.equal("module"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); expect(scope.references).to.have.length(0); }); - it('should export literal', function() { - const ast = espree(`export default 42;`, {sourceType: 'module'}); + it("should export literal", function() { + const ast = espree("export default 42;", {sourceType: "module"}); - const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: 'module'}); + const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: "module"}); expect(scopeManager.scopes).to.have.length(2); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(0); expect(globalScope.references).to.have.length(0); const scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('module'); + expect(scope.type).to.be.equal("module"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); }); - it('should refer exported references#1', function() { - const ast = espree(`export {x};`, {sourceType: 'module'}); + it("should refer exported references#1", function() { + const ast = espree("export {x};", {sourceType: "module"}); - const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: 'module'}); + const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: "module"}); expect(scopeManager.scopes).to.have.length(2); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(0); expect(globalScope.references).to.have.length(0); const scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('module'); + expect(scope.type).to.be.equal("module"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(1); - expect(scope.references[0].identifier.name).to.be.equal('x'); + expect(scope.references[0].identifier.name).to.be.equal("x"); }); - it('should refer exported references#2', function() { - const ast = espree(`export {v as x};`, {sourceType: 'module'}); + it("should refer exported references#2", function() { + const ast = espree("export {v as x};", {sourceType: "module"}); - const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: 'module'}); + const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: "module"}); expect(scopeManager.scopes).to.have.length(2); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(0); expect(globalScope.references).to.have.length(0); const scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('module'); + expect(scope.type).to.be.equal("module"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(1); - expect(scope.references[0].identifier.name).to.be.equal('v'); + expect(scope.references[0].identifier.name).to.be.equal("v"); }); - it('should not refer exported references from other source#1', function() { - const ast = espree(`export {x} from "mod";`, {sourceType: 'module'}); + it("should not refer exported references from other source#1", function() { + const ast = espree("export {x} from \"mod\";", {sourceType: "module"}); - const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: 'module'}); + const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: "module"}); expect(scopeManager.scopes).to.have.length(2); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(0); expect(globalScope.references).to.have.length(0); const scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('module'); + expect(scope.type).to.be.equal("module"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); }); - it('should not refer exported references from other source#2', function() { - const ast = espree(`export {v as x} from "mod";`, {sourceType: 'module'}); + it("should not refer exported references from other source#2", function() { + const ast = espree("export {v as x} from \"mod\";", {sourceType: "module"}); - const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: 'module'}); + const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: "module"}); expect(scopeManager.scopes).to.have.length(2); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(0); expect(globalScope.references).to.have.length(0); const scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('module'); + expect(scope.type).to.be.equal("module"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); }); - it('should not refer exported references from other source#3', function() { - const ast = espree(`export * from "mod";`, {sourceType: 'module'}); + it("should not refer exported references from other source#3", function() { + const ast = espree("export * from \"mod\";", {sourceType: "module"}); - const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: 'module'}); + const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: "module"}); expect(scopeManager.scopes).to.have.length(2); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(0); expect(globalScope.references).to.have.length(0); const scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('module'); + expect(scope.type).to.be.equal("module"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(0); }); diff --git a/test/es6-import.js b/test/es6-import.js index 9b15e02..2dab022 100644 --- a/test/es6-import.js +++ b/test/es6-import.js @@ -22,87 +22,89 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const espree = require('../third_party/espree'); -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ -describe('import declaration', function() { +const expect = require("chai").expect; +const espree = require("../third_party/espree"); +const analyze = require("..").analyze; + +describe("import declaration", function() { // http://people.mozilla.org/~jorendorff/es6-draft.html#sec-static-and-runtme-semantics-module-records - it('should import names from source', function() { - const ast = espree(`import v from "mod";`, {sourceType: 'module'}); + it("should import names from source", function() { + const ast = espree("import v from \"mod\";", {sourceType: "module"}); - const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: 'module'}); + const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: "module"}); expect(scopeManager.scopes).to.have.length(2); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(0); expect(globalScope.references).to.have.length(0); const scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('module'); + expect(scope.type).to.be.equal("module"); expect(scope.isStrict).to.be.true; expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('v'); - expect(scope.variables[0].defs[0].type).to.be.equal('ImportBinding'); + expect(scope.variables[0].name).to.be.equal("v"); + expect(scope.variables[0].defs[0].type).to.be.equal("ImportBinding"); expect(scope.references).to.have.length(0); }); - it('should import namespaces', function() { - const ast = espree( `import * as ns from "mod";`, {sourceType: 'module' + it("should import namespaces", function() { + const ast = espree( "import * as ns from \"mod\";", {sourceType: "module" }); - const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: 'module'}); + const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: "module"}); expect(scopeManager.scopes).to.have.length(2); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(0); expect(globalScope.references).to.have.length(0); const scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('module'); + expect(scope.type).to.be.equal("module"); expect(scope.isStrict).to.be.true; expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('ns'); - expect(scope.variables[0].defs[0].type).to.be.equal('ImportBinding'); + expect(scope.variables[0].name).to.be.equal("ns"); + expect(scope.variables[0].defs[0].type).to.be.equal("ImportBinding"); expect(scope.references).to.have.length(0); }); - it('should import insided names#1', function() { - const ast = espree(`import {x} from "mod";`, {sourceType: 'module' + it("should import insided names#1", function() { + const ast = espree("import {x} from \"mod\";", {sourceType: "module" }); - const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: 'module'}); + const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: "module"}); expect(scopeManager.scopes).to.have.length(2); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(0); expect(globalScope.references).to.have.length(0); const scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('module'); + expect(scope.type).to.be.equal("module"); expect(scope.isStrict).to.be.true; expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('x'); - expect(scope.variables[0].defs[0].type).to.be.equal('ImportBinding'); + expect(scope.variables[0].name).to.be.equal("x"); + expect(scope.variables[0].defs[0].type).to.be.equal("ImportBinding"); expect(scope.references).to.have.length(0); }); - it('should import insided names#2', function() { - const ast = espree(`import {x as v} from "mod";`, {sourceType: 'module'}); + it("should import insided names#2", function() { + const ast = espree("import {x as v} from \"mod\";", {sourceType: "module"}); - const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: 'module'}); + const scopeManager = analyze(ast, {ecmaVersion: 6, sourceType: "module"}); expect(scopeManager.scopes).to.have.length(2); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(0); expect(globalScope.references).to.have.length(0); const scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('module'); + expect(scope.type).to.be.equal("module"); expect(scope.isStrict).to.be.true; expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('v'); - expect(scope.variables[0].defs[0].type).to.be.equal('ImportBinding'); + expect(scope.variables[0].name).to.be.equal("v"); + expect(scope.variables[0].defs[0].type).to.be.equal("ImportBinding"); expect(scope.references).to.have.length(0); }); diff --git a/test/es6-iteration-scope.js b/test/es6-iteration-scope.js index 123eb7d..29f40a5 100644 --- a/test/es6-iteration-scope.js +++ b/test/es6-iteration-scope.js @@ -22,12 +22,14 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const parse = require('../third_party/esprima').parse; -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ -describe('ES6 iteration scope', function() { - it('let materialize iteration scope for ForInStatement#1', function() { +const expect = require("chai").expect; +const parse = require("../third_party/esprima").parse; +const analyze = require("..").analyze; + +describe("ES6 iteration scope", function() { + it("let materialize iteration scope for ForInStatement#1", function() { const ast = parse(` (function () { let i = 20; @@ -41,46 +43,46 @@ describe('ES6 iteration scope', function() { expect(scopeManager.scopes).to.have.length(5); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(2); - expect(scope.variables[0].name).to.be.equal('arguments'); - expect(scope.variables[1].name).to.be.equal('i'); + expect(scope.variables[0].name).to.be.equal("arguments"); + expect(scope.variables[1].name).to.be.equal("i"); expect(scope.references).to.have.length(1); - expect(scope.references[0].identifier.name).to.be.equal('i'); + expect(scope.references[0].identifier.name).to.be.equal("i"); expect(scope.references[0].resolved).to.be.equal(scope.variables[1]); let iterScope = scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('TDZ'); + expect(scope.type).to.be.equal("TDZ"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('i'); - expect(scope.variables[0].defs[0].type).to.be.equal('TDZ'); + expect(scope.variables[0].name).to.be.equal("i"); + expect(scope.variables[0].defs[0].type).to.be.equal("TDZ"); expect(scope.references).to.have.length(1); - expect(scope.references[0].identifier.name).to.be.equal('i'); + expect(scope.references[0].identifier.name).to.be.equal("i"); expect(scope.references[0].resolved).to.be.equal(scope.variables[0]); iterScope = scope = scopeManager.scopes[3]; - expect(scope.type).to.be.equal('for'); + expect(scope.type).to.be.equal("for"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('i'); + expect(scope.variables[0].name).to.be.equal("i"); expect(scope.references).to.have.length(1); - expect(scope.references[0].identifier.name).to.be.equal('i'); + expect(scope.references[0].identifier.name).to.be.equal("i"); expect(scope.references[0].resolved).to.be.equal(scope.variables[0]); scope = scopeManager.scopes[4]; - expect(scope.type).to.be.equal('block'); + expect(scope.type).to.be.equal("block"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(2); - expect(scope.references[0].identifier.name).to.be.equal('console'); + expect(scope.references[0].identifier.name).to.be.equal("console"); expect(scope.references[0].resolved).to.be.equal(null); - expect(scope.references[1].identifier.name).to.be.equal('i'); + expect(scope.references[1].identifier.name).to.be.equal("i"); expect(scope.references[1].resolved).to.be.equal(iterScope.variables[0]); }); - it('let materialize iteration scope for ForInStatement#2', function() { + it("let materialize iteration scope for ForInStatement#2", function() { const ast = parse(` (function () { let i = 20; @@ -94,56 +96,56 @@ describe('ES6 iteration scope', function() { expect(scopeManager.scopes).to.have.length(5); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(2); - expect(scope.variables[0].name).to.be.equal('arguments'); - expect(scope.variables[1].name).to.be.equal('i'); + expect(scope.variables[0].name).to.be.equal("arguments"); + expect(scope.variables[1].name).to.be.equal("i"); expect(scope.references).to.have.length(1); - expect(scope.references[0].identifier.name).to.be.equal('i'); + expect(scope.references[0].identifier.name).to.be.equal("i"); expect(scope.references[0].resolved).to.be.equal(scope.variables[1]); let iterScope = scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('TDZ'); + expect(scope.type).to.be.equal("TDZ"); expect(scope.variables).to.have.length(3); - expect(scope.variables[0].name).to.be.equal('i'); - expect(scope.variables[0].defs[0].type).to.be.equal('TDZ'); - expect(scope.variables[1].name).to.be.equal('j'); - expect(scope.variables[1].defs[0].type).to.be.equal('TDZ'); - expect(scope.variables[2].name).to.be.equal('k'); - expect(scope.variables[2].defs[0].type).to.be.equal('TDZ'); + expect(scope.variables[0].name).to.be.equal("i"); + expect(scope.variables[0].defs[0].type).to.be.equal("TDZ"); + expect(scope.variables[1].name).to.be.equal("j"); + expect(scope.variables[1].defs[0].type).to.be.equal("TDZ"); + expect(scope.variables[2].name).to.be.equal("k"); + expect(scope.variables[2].defs[0].type).to.be.equal("TDZ"); expect(scope.references).to.have.length(1); - expect(scope.references[0].identifier.name).to.be.equal('i'); + expect(scope.references[0].identifier.name).to.be.equal("i"); expect(scope.references[0].resolved).to.be.equal(scope.variables[0]); iterScope = scope = scopeManager.scopes[3]; - expect(scope.type).to.be.equal('for'); + expect(scope.type).to.be.equal("for"); expect(scope.variables).to.have.length(3); - expect(scope.variables[0].name).to.be.equal('i'); - expect(scope.variables[1].name).to.be.equal('j'); - expect(scope.variables[2].name).to.be.equal('k'); + expect(scope.variables[0].name).to.be.equal("i"); + expect(scope.variables[1].name).to.be.equal("j"); + expect(scope.variables[2].name).to.be.equal("k"); expect(scope.references).to.have.length(3); - expect(scope.references[0].identifier.name).to.be.equal('i'); + expect(scope.references[0].identifier.name).to.be.equal("i"); expect(scope.references[0].resolved).to.be.equal(scope.variables[0]); - expect(scope.references[1].identifier.name).to.be.equal('j'); + expect(scope.references[1].identifier.name).to.be.equal("j"); expect(scope.references[1].resolved).to.be.equal(scope.variables[1]); - expect(scope.references[2].identifier.name).to.be.equal('k'); + expect(scope.references[2].identifier.name).to.be.equal("k"); expect(scope.references[2].resolved).to.be.equal(scope.variables[2]); scope = scopeManager.scopes[4]; - expect(scope.type).to.be.equal('block'); + expect(scope.type).to.be.equal("block"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(2); - expect(scope.references[0].identifier.name).to.be.equal('console'); + expect(scope.references[0].identifier.name).to.be.equal("console"); expect(scope.references[0].resolved).to.be.equal(null); - expect(scope.references[1].identifier.name).to.be.equal('i'); + expect(scope.references[1].identifier.name).to.be.equal("i"); expect(scope.references[1].resolved).to.be.equal(iterScope.variables[0]); }); - it('let materialize iteration scope for ForStatement#2', function() { + it("let materialize iteration scope for ForStatement#2", function() { const ast = parse(` (function () { let i = 20; @@ -158,57 +160,57 @@ describe('ES6 iteration scope', function() { expect(scopeManager.scopes).to.have.length(4); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(0); const functionScope = scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(3); - expect(scope.variables[0].name).to.be.equal('arguments'); - expect(scope.variables[1].name).to.be.equal('i'); - expect(scope.variables[2].name).to.be.equal('obj'); + expect(scope.variables[0].name).to.be.equal("arguments"); + expect(scope.variables[1].name).to.be.equal("i"); + expect(scope.variables[2].name).to.be.equal("obj"); expect(scope.references).to.have.length(2); - expect(scope.references[0].identifier.name).to.be.equal('i'); + expect(scope.references[0].identifier.name).to.be.equal("i"); expect(scope.references[0].resolved).to.be.equal(scope.variables[1]); - expect(scope.references[1].identifier.name).to.be.equal('obj'); + expect(scope.references[1].identifier.name).to.be.equal("obj"); expect(scope.references[1].resolved).to.be.equal(scope.variables[2]); const iterScope = scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('for'); + expect(scope.type).to.be.equal("for"); expect(scope.variables).to.have.length(3); - expect(scope.variables[0].name).to.be.equal('i'); - expect(scope.variables[0].defs[0].type).to.be.equal('Variable'); - expect(scope.variables[1].name).to.be.equal('j'); - expect(scope.variables[1].defs[0].type).to.be.equal('Variable'); - expect(scope.variables[2].name).to.be.equal('k'); - expect(scope.variables[2].defs[0].type).to.be.equal('Variable'); + expect(scope.variables[0].name).to.be.equal("i"); + expect(scope.variables[0].defs[0].type).to.be.equal("Variable"); + expect(scope.variables[1].name).to.be.equal("j"); + expect(scope.variables[1].defs[0].type).to.be.equal("Variable"); + expect(scope.variables[2].name).to.be.equal("k"); + expect(scope.variables[2].defs[0].type).to.be.equal("Variable"); expect(scope.references).to.have.length(7); - expect(scope.references[0].identifier.name).to.be.equal('i'); + expect(scope.references[0].identifier.name).to.be.equal("i"); expect(scope.references[0].resolved).to.be.equal(scope.variables[0]); - expect(scope.references[1].identifier.name).to.be.equal('j'); + expect(scope.references[1].identifier.name).to.be.equal("j"); expect(scope.references[1].resolved).to.be.equal(scope.variables[1]); - expect(scope.references[2].identifier.name).to.be.equal('k'); + expect(scope.references[2].identifier.name).to.be.equal("k"); expect(scope.references[2].resolved).to.be.equal(scope.variables[2]); - expect(scope.references[3].identifier.name).to.be.equal('obj'); + expect(scope.references[3].identifier.name).to.be.equal("obj"); expect(scope.references[3].resolved).to.be.equal(functionScope.variables[2]); - expect(scope.references[4].identifier.name).to.be.equal('i'); + expect(scope.references[4].identifier.name).to.be.equal("i"); expect(scope.references[4].resolved).to.be.equal(scope.variables[0]); - expect(scope.references[5].identifier.name).to.be.equal('okok'); + expect(scope.references[5].identifier.name).to.be.equal("okok"); expect(scope.references[5].resolved).to.be.null; - expect(scope.references[6].identifier.name).to.be.equal('i'); + expect(scope.references[6].identifier.name).to.be.equal("i"); expect(scope.references[6].resolved).to.be.equal(scope.variables[0]); scope = scopeManager.scopes[3]; - expect(scope.type).to.be.equal('block'); + expect(scope.type).to.be.equal("block"); expect(scope.variables).to.have.length(0); expect(scope.references).to.have.length(4); - expect(scope.references[0].identifier.name).to.be.equal('console'); + expect(scope.references[0].identifier.name).to.be.equal("console"); expect(scope.references[0].resolved).to.be.null; - expect(scope.references[1].identifier.name).to.be.equal('i'); + expect(scope.references[1].identifier.name).to.be.equal("i"); expect(scope.references[1].resolved).to.be.equal(iterScope.variables[0]); - expect(scope.references[2].identifier.name).to.be.equal('j'); + expect(scope.references[2].identifier.name).to.be.equal("j"); expect(scope.references[2].resolved).to.be.equal(iterScope.variables[1]); - expect(scope.references[3].identifier.name).to.be.equal('k'); + expect(scope.references[3].identifier.name).to.be.equal("k"); expect(scope.references[3].resolved).to.be.equal(iterScope.variables[2]); }); }); diff --git a/test/es6-new-target.js b/test/es6-new-target.js index a31d5a1..f3004ff 100644 --- a/test/es6-new-target.js +++ b/test/es6-new-target.js @@ -22,13 +22,15 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ -const parse = require('../third_party/espree'); +const expect = require("chai").expect; +const analyze = require("..").analyze; -describe('ES6 new.target', function() { - it('should not make references of new.target', function() { +const parse = require("../third_party/espree"); + +describe("ES6 new.target", function() { + it("should not make references of new.target", function() { const ast = parse(` class A { constructor() { @@ -41,11 +43,11 @@ describe('ES6 new.target', function() { expect(scopeManager.scopes).to.have.length(3); const scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('function'); - expect(scope.block.type).to.be.equal('FunctionExpression'); + expect(scope.type).to.be.equal("function"); + expect(scope.block.type).to.be.equal("FunctionExpression"); expect(scope.isStrict).to.be.true; expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); expect(scope.references).to.have.length(0); }); }); diff --git a/test/es6-object.js b/test/es6-object.js index 12dfc68..d702e84 100644 --- a/test/es6-object.js +++ b/test/es6-object.js @@ -22,12 +22,14 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const parse = require('../third_party/esprima').parse; -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ -describe('ES6 object', function() { - it('method definition', function() { +const expect = require("chai").expect; +const parse = require("../third_party/esprima").parse; +const analyze = require("..").analyze; + +describe("ES6 object", function() { + it("method definition", function() { const ast = parse(` ({ constructor() { @@ -38,20 +40,20 @@ describe('ES6 object', function() { 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.type).to.be.equal("global"); + expect(scope.block.type).to.be.equal("Program"); expect(scope.isStrict).to.be.false; scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); - expect(scope.block.type).to.be.equal('FunctionExpression'); + expect(scope.type).to.be.equal("function"); + expect(scope.block.type).to.be.equal("FunctionExpression"); expect(scope.isStrict).to.be.false; expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); expect(scope.references).to.have.length(0); }); - it('computed property key may refer variables', function() { + it("computed property key may refer variables", function() { const ast = parse(` (function () { var yuyushiki = 42; @@ -69,21 +71,21 @@ describe('ES6 object', function() { expect(scopeManager.scopes).to.have.length(4); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); - expect(scope.block.type).to.be.equal('Program'); + expect(scope.type).to.be.equal("global"); + expect(scope.block.type).to.be.equal("Program"); expect(scope.isStrict).to.be.false; scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); - expect(scope.block.type).to.be.equal('FunctionExpression'); + expect(scope.type).to.be.equal("function"); + expect(scope.block.type).to.be.equal("FunctionExpression"); expect(scope.isStrict).to.be.false; expect(scope.variables).to.have.length(2); - expect(scope.variables[0].name).to.be.equal('arguments'); - expect(scope.variables[1].name).to.be.equal('yuyushiki'); + expect(scope.variables[0].name).to.be.equal("arguments"); + expect(scope.variables[1].name).to.be.equal("yuyushiki"); expect(scope.references).to.have.length(3); - expect(scope.references[0].identifier.name).to.be.equal('yuyushiki'); - expect(scope.references[1].identifier.name).to.be.equal('yuyushiki'); - expect(scope.references[2].identifier.name).to.be.equal('yuyushiki'); + expect(scope.references[0].identifier.name).to.be.equal("yuyushiki"); + expect(scope.references[1].identifier.name).to.be.equal("yuyushiki"); + expect(scope.references[2].identifier.name).to.be.equal("yuyushiki"); }); }); diff --git a/test/es6-rest-args.js b/test/es6-rest-args.js index 3a6165a..6f5fd2d 100644 --- a/test/es6-rest-args.js +++ b/test/es6-rest-args.js @@ -22,13 +22,15 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const esprima = require('../third_party/esprima').parse; -const espree = require('../third_party/espree'); -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ -describe('ES6 rest arguments', function() { - it('materialize rest argument in scope (esprima: rest property of FunctionDeclaration)', function() { +const expect = require("chai").expect; +const esprima = require("../third_party/esprima").parse; +const espree = require("../third_party/espree"); +const analyze = require("..").analyze; + +describe("ES6 rest arguments", function() { + it("materialize rest argument in scope (esprima: rest property of FunctionDeclaration)", function() { const ast = esprima(` function foo(...bar) { return bar; @@ -39,21 +41,21 @@ describe('ES6 rest arguments', function() { 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.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(1); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(2); - expect(scope.variables[0].name).to.be.equal('arguments'); - expect(scope.variables[1].name).to.be.equal('bar'); - expect(scope.variables[1].defs[0].name.name).to.be.equal('bar'); + expect(scope.variables[0].name).to.be.equal("arguments"); + expect(scope.variables[1].name).to.be.equal("bar"); + expect(scope.variables[1].defs[0].name.name).to.be.equal("bar"); expect(scope.variables[1].defs[0].rest).to.be.true; }); - it('materialize rest argument in scope (espree: RestElement)', function() { + it("materialize rest argument in scope (espree: RestElement)", function() { const ast = espree(` function foo(...bar) { return bar; @@ -64,17 +66,17 @@ describe('ES6 rest arguments', function() { 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.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(1); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(2); - expect(scope.variables[0].name).to.be.equal('arguments'); - expect(scope.variables[1].name).to.be.equal('bar'); - expect(scope.variables[1].defs[0].name.name).to.be.equal('bar'); + expect(scope.variables[0].name).to.be.equal("arguments"); + expect(scope.variables[1].name).to.be.equal("bar"); + expect(scope.variables[1].defs[0].name.name).to.be.equal("bar"); expect(scope.variables[1].defs[0].rest).to.be.true; }); }); diff --git a/test/es6-super.js b/test/es6-super.js index 0ff5221..a4a14f6 100644 --- a/test/es6-super.js +++ b/test/es6-super.js @@ -22,12 +22,12 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const parse = require('../third_party/esprima').parse; -const analyze = require('..').analyze; +const expect = require("chai").expect; +const parse = require("../third_party/esprima").parse; +const analyze = require("..").analyze; -describe('ES6 super', function() { - it('is not handled as reference', function() { +describe("ES6 super", function() { + it("is not handled as reference", function() { const ast = parse(` class Hello { constructor() { @@ -44,27 +44,27 @@ describe('ES6 super', function() { expect(scopeManager.scopes).to.have.length(4); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); + expect(scope.type).to.be.equal("global"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('Hello'); + expect(scope.variables[0].name).to.be.equal("Hello"); expect(scope.references).to.have.length(0); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('class'); + expect(scope.type).to.be.equal("class"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('Hello'); + expect(scope.variables[0].name).to.be.equal("Hello"); expect(scope.references).to.have.length(0); scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); expect(scope.references).to.have.length(0); // super is specially handled like `this`. scope = scopeManager.scopes[3]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); expect(scope.references).to.have.length(0); // super is specially handled like `this`. }); }); diff --git a/test/es6-switch.js b/test/es6-switch.js index 217d214..9d85efc 100644 --- a/test/es6-switch.js +++ b/test/es6-switch.js @@ -22,12 +22,14 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const parse = require('../third_party/esprima').parse; -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ -describe('ES6 switch', function() { - it('materialize scope', function() { +const expect = require("chai").expect; +const parse = require("../third_party/esprima").parse; +const analyze = require("..").analyze; + +describe("ES6 switch", function() { + it("materialize scope", function() { const ast = parse(` switch (ok) { case hello: @@ -45,26 +47,26 @@ describe('ES6 switch', function() { 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.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); expect(scope.references).to.have.length(1); - expect(scope.references[0].identifier.name).to.be.equal('ok'); + expect(scope.references[0].identifier.name).to.be.equal("ok"); scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('switch'); - expect(scope.block.type).to.be.equal('SwitchStatement'); + expect(scope.type).to.be.equal("switch"); + expect(scope.block.type).to.be.equal("SwitchStatement"); expect(scope.isStrict).to.be.false; expect(scope.variables).to.have.length(2); - expect(scope.variables[0].name).to.be.equal('i'); - expect(scope.variables[1].name).to.be.equal('test'); + expect(scope.variables[0].name).to.be.equal("i"); + expect(scope.variables[1].name).to.be.equal("test"); expect(scope.references).to.have.length(5); - expect(scope.references[0].identifier.name).to.be.equal('hello'); - expect(scope.references[1].identifier.name).to.be.equal('i'); - expect(scope.references[2].identifier.name).to.be.equal('i'); - expect(scope.references[3].identifier.name).to.be.equal('test'); - expect(scope.references[4].identifier.name).to.be.equal('test'); + expect(scope.references[0].identifier.name).to.be.equal("hello"); + expect(scope.references[1].identifier.name).to.be.equal("i"); + expect(scope.references[2].identifier.name).to.be.equal("i"); + expect(scope.references[3].identifier.name).to.be.equal("test"); + expect(scope.references[4].identifier.name).to.be.equal("test"); }); }); diff --git a/test/es6-template-literal.js b/test/es6-template-literal.js index a5127f4..405da77 100644 --- a/test/es6-template-literal.js +++ b/test/es6-template-literal.js @@ -22,12 +22,14 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const parse = require('../third_party/esprima').parse; -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ -describe('ES6 template literal', function() { - it('refer variables', function() { +const expect = require("chai").expect; +const parse = require("../third_party/esprima").parse; +const analyze = require("..").analyze; + +describe("ES6 template literal", function() { + it("refer variables", function() { const ast = parse(` (function () { let i, j, k; @@ -41,28 +43,28 @@ describe('ES6 template literal', function() { expect(scopeManager.scopes).to.have.length(3); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); - expect(scope.block.type).to.be.equal('Program'); + 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('FunctionExpression'); + expect(scope.type).to.be.equal("function"); + expect(scope.block.type).to.be.equal("FunctionExpression"); expect(scope.isStrict).to.be.false; expect(scope.variables).to.have.length(6); - expect(scope.variables[0].name).to.be.equal('arguments'); - expect(scope.variables[1].name).to.be.equal('i'); - expect(scope.variables[2].name).to.be.equal('j'); - expect(scope.variables[3].name).to.be.equal('k'); - expect(scope.variables[4].name).to.be.equal('testing'); - expect(scope.variables[5].name).to.be.equal('template'); + expect(scope.variables[0].name).to.be.equal("arguments"); + expect(scope.variables[1].name).to.be.equal("i"); + expect(scope.variables[2].name).to.be.equal("j"); + expect(scope.variables[3].name).to.be.equal("k"); + expect(scope.variables[4].name).to.be.equal("testing"); + expect(scope.variables[5].name).to.be.equal("template"); expect(scope.references).to.have.length(5); - expect(scope.references[0].identifier.name).to.be.equal('template'); - expect(scope.references[1].identifier.name).to.be.equal('testing'); - expect(scope.references[2].identifier.name).to.be.equal('i'); - expect(scope.references[3].identifier.name).to.be.equal('j'); - expect(scope.references[4].identifier.name).to.be.equal('template'); + expect(scope.references[0].identifier.name).to.be.equal("template"); + expect(scope.references[1].identifier.name).to.be.equal("testing"); + expect(scope.references[2].identifier.name).to.be.equal("i"); + expect(scope.references[3].identifier.name).to.be.equal("j"); + expect(scope.references[4].identifier.name).to.be.equal("template"); }); }); diff --git a/test/fallback.js b/test/fallback.js index 574c813..8ff7948 100644 --- a/test/fallback.js +++ b/test/fallback.js @@ -22,41 +22,41 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const esprima = require('../third_party/esprima'); -const analyze = require('..').analyze; +const expect = require("chai").expect; +const esprima = require("../third_party/esprima"); +const analyze = require("..").analyze; -describe('fallback option', function() { - it('should raise an error when it encountered an unknown node if no fallback.', function() { +describe("fallback option", function() { + it("should raise an error when it encountered an unknown node if no fallback.", function() { const ast = esprima.parse(` var foo = 0; `); - ast.body[0].declarations[0].init.type = 'NumericLiteral'; + ast.body[0].declarations[0].init.type = "NumericLiteral"; expect(function() { - analyze(ast, {fallback: 'none'}); + analyze(ast, {fallback: "none"}); }).to.throw("Unknown node type NumericLiteral"); }); - it('should not raise an error even if it encountered an unknown node when fallback is iteration.', function() { + it("should not raise an error even if it encountered an unknown node when fallback is iteration.", function() { const ast = esprima.parse(` var foo = 0; `); - ast.body[0].declarations[0].init.type = 'NumericLiteral'; + ast.body[0].declarations[0].init.type = "NumericLiteral"; analyze(ast); // default is `fallback: 'iteration'` - analyze(ast, {fallback: 'iteration'}); + analyze(ast, {fallback: "iteration"}); }); - it('should not raise an error even if it encountered an unknown node when fallback is a function.', function() { + it("should not raise an error even if it encountered an unknown node when fallback is a function.", function() { const ast = esprima.parse(` var foo = 0; `); - ast.body[0].declarations[0].init.type = 'NumericLiteral'; + ast.body[0].declarations[0].init.type = "NumericLiteral"; analyze(ast, {fallback: node => Object.keys(node)}); }); diff --git a/test/function-expression-name.js b/test/function-expression-name.js index e720c44..ba16e05 100644 --- a/test/function-expression-name.js +++ b/test/function-expression-name.js @@ -22,12 +22,14 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const parse = require('../third_party/esprima').parse; -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ -describe('function name', function() { - it('should create its special scope', function() { +const expect = require("chai").expect; +const parse = require("../third_party/esprima").parse; +const analyze = require("..").analyze; + +describe("function name", function() { + it("should create its special scope", function() { const ast = parse(` (function name() { }()); @@ -36,25 +38,25 @@ describe('function name', function() { const scopeManager = analyze(ast); expect(scopeManager.scopes).to.have.length(3); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(0); expect(globalScope.references).to.have.length(0); expect(globalScope.isArgumentsMaterialized()).to.be.true; // Function expression name scope let scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function-expression-name'); + expect(scope.type).to.be.equal("function-expression-name"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('name'); + expect(scope.variables[0].name).to.be.equal("name"); expect(scope.isArgumentsMaterialized()).to.be.true; expect(scope.references).to.have.length(0); expect(scope.upper === globalScope).to.be.true; // Function scope scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); expect(scope.isArgumentsMaterialized()).to.be.false; expect(scope.references).to.have.length(0); expect(scope.upper === scopeManager.scopes[1]).to.be.true; diff --git a/test/get-declared-variables.js b/test/get-declared-variables.js index 004b315..bb24e84 100644 --- a/test/get-declared-variables.js +++ b/test/get-declared-variables.js @@ -22,16 +22,23 @@ // 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('../third_party/espree'); -const analyze = require('..').analyze; - -describe('ScopeManager.prototype.getDeclaredVariables', function() { - const verify = (ast, type, expectedNamesList) => { +const expect = require("chai").expect; +const visit = require("esrecurse").visit; +const espree = require("../third_party/espree"); +const analyze = require("..").analyze; + +describe("ScopeManager.prototype.getDeclaredVariables", function() { + /** + * Verify + * @param {AST} ast - Abstract syntax tree + * @param {string} type - type + * @param {array} expectedNamesList - expected names + * @returns {void} + */ + function verify(ast, type, expectedNamesList) { const scopeManager = analyze(ast, { ecmaVersion: 6, - sourceType: 'module' + sourceType: "module" }); visit(ast, { @@ -41,7 +48,7 @@ describe('ScopeManager.prototype.getDeclaredVariables', function() { expect(actual).to.have.length(expected.length); if (actual.length > 0) { - const end = actual.length-1; + const end = actual.length - 1; for (let i = 0; i <= end; i++) { expect(actual[i].name).to.be.equal(expected[i]); } @@ -52,26 +59,26 @@ describe('ScopeManager.prototype.getDeclaredVariables', function() { }); expect(expectedNamesList).to.have.length(0); - }; + } - it('should get variables that declared on `VariableDeclaration`', function() { + it("should get variables that declared on `VariableDeclaration`", function() { const ast = espree(` var {a, x: [b], y: {c = 0}} = foo; let {d, x: [e], y: {f = 0}} = foo; const {g, x: [h], y: {i = 0}} = foo, {j, k = function() { let l; }} = bar; `); - verify(ast, 'VariableDeclaration', [ - ['a', 'b', 'c'], - ['d', 'e', 'f'], - ['g', 'h', 'i', 'j', 'k'], - ['l'] + verify(ast, "VariableDeclaration", [ + ["a", "b", "c"], + ["d", "e", "f"], + ["g", "h", "i", "j", "k"], + ["l"] ]); }); - it('should get variables that declared on `VariableDeclaration` in for-in/of', function() { + it("should get variables that declared on `VariableDeclaration` in for-in/of", function() { const ast = espree(` for (var {a, x: [b], y: {c = 0}} in foo) { let g; @@ -81,33 +88,33 @@ describe('ScopeManager.prototype.getDeclaredVariables', function() { } `); - verify(ast, 'VariableDeclaration', [ - ['a', 'b', 'c'], - ['g'], - ['d', 'e', 'f'], - ['h'] + verify(ast, "VariableDeclaration", [ + ["a", "b", "c"], + ["g"], + ["d", "e", "f"], + ["h"] ]); }); - it('should get variables that declared on `VariableDeclarator`', function() { + it("should get variables that declared on `VariableDeclarator`", function() { const ast = espree(` var {a, x: [b], y: {c = 0}} = foo; let {d, x: [e], y: {f = 0}} = foo; const {g, x: [h], y: {i = 0}} = foo, {j, k = function() { let l; }} = bar; `); - verify(ast, 'VariableDeclarator', [ - ['a', 'b', 'c'], - ['d', 'e', 'f'], - ['g', 'h', 'i'], - ['j', 'k'], - ['l'] + verify(ast, "VariableDeclarator", [ + ["a", "b", "c"], + ["d", "e", "f"], + ["g", "h", "i"], + ["j", "k"], + ["l"] ]); }); - it('should get variables that declared on `FunctionDeclaration`', function() { + it("should get variables that declared on `FunctionDeclaration`", function() { const ast = espree(` function foo({a, x: [b], y: {c = 0}}, [d, e]) { let z; @@ -117,14 +124,14 @@ describe('ScopeManager.prototype.getDeclaredVariables', function() { } `); - verify(ast, 'FunctionDeclaration', [ - ['foo', 'a', 'b', 'c', 'd', 'e'], - ['bar', 'f', 'g', 'h', 'i', 'j'] + verify(ast, "FunctionDeclaration", [ + ["foo", "a", "b", "c", "d", "e"], + ["bar", "f", "g", "h", "i", "j"] ]); }); - it('should get variables that declared on `FunctionExpression`', function() { + it("should get variables that declared on `FunctionExpression`", function() { const ast = espree(` (function foo({a, x: [b], y: {c = 0}}, [d, e]) { let z; @@ -134,15 +141,15 @@ describe('ScopeManager.prototype.getDeclaredVariables', function() { }); `); - verify(ast, 'FunctionExpression', [ - ['foo', 'a', 'b', 'c', 'd', 'e'], - ['bar', 'f', 'g', 'h', 'i', 'j'], - ['q'] + verify(ast, "FunctionExpression", [ + ["foo", "a", "b", "c", "d", "e"], + ["bar", "f", "g", "h", "i", "j"], + ["q"] ]); }); - it('should get variables that declared on `ArrowFunctionExpression`', function() { + it("should get variables that declared on `ArrowFunctionExpression`", function() { const ast = espree(` (({a, x: [b], y: {c = 0}}, [d, e]) => { let z; @@ -152,40 +159,40 @@ describe('ScopeManager.prototype.getDeclaredVariables', function() { }); `); - verify(ast, 'ArrowFunctionExpression', [ - ['a', 'b', 'c', 'd', 'e'], - ['f', 'g', 'h', 'i', 'j'] + verify(ast, "ArrowFunctionExpression", [ + ["a", "b", "c", "d", "e"], + ["f", "g", "h", "i", "j"] ]); }); - it('should get variables that declared on `ClassDeclaration`', function() { + it("should get variables that declared on `ClassDeclaration`", function() { const ast = espree(` class A { foo(x) { let y; } } class B { foo(x) { let y; } } `); - verify(ast, 'ClassDeclaration', [ - ['A', 'A'], // outer scope's and inner scope's. - ['B', 'B'] + verify(ast, "ClassDeclaration", [ + ["A", "A"], // outer scope's and inner scope's. + ["B", "B"] ]); }); - it('should get variables that declared on `ClassExpression`', function() { + it("should get variables that declared on `ClassExpression`", function() { const ast = espree(` (class A { foo(x) { let y; } }); (class B { foo(x) { let y; } }); `); - verify(ast, 'ClassExpression', [ - ['A'], - ['B'] + verify(ast, "ClassExpression", [ + ["A"], + ["B"] ]); }); - it('should get variables that declared on `CatchClause`', function() { + it("should get variables that declared on `CatchClause`", function() { const ast = espree(` try {} catch ({a, b}) { let x; @@ -195,77 +202,77 @@ describe('ScopeManager.prototype.getDeclaredVariables', function() { } `); - verify(ast, 'CatchClause', [ - ['a', 'b'], - ['c', 'd'] + verify(ast, "CatchClause", [ + ["a", "b"], + ["c", "d"] ]); }); - it('should get variables that declared on `ImportDeclaration`', function() { + it("should get variables that declared on `ImportDeclaration`", function() { const ast = espree(` import "aaa"; import * as a from "bbb"; import b, {c, x as d} from "ccc";`, - {sourceType: 'module'} + {sourceType: "module"} ); - verify(ast, 'ImportDeclaration', [ + verify(ast, "ImportDeclaration", [ [], - ['a'], - ['b', 'c', 'd'] + ["a"], + ["b", "c", "d"] ]); }); - it('should get variables that declared on `ImportSpecifier`', function() { + it("should get variables that declared on `ImportSpecifier`", function() { const ast = espree(` import "aaa"; import * as a from "bbb"; import b, {c, x as d} from "ccc";`, - {sourceType: 'module'} + {sourceType: "module"} ); - verify(ast, 'ImportSpecifier', [ - ['c'], - ['d'] + verify(ast, "ImportSpecifier", [ + ["c"], + ["d"] ]); }); - it('should get variables that declared on `ImportDefaultSpecifier`', function() { + it("should get variables that declared on `ImportDefaultSpecifier`", function() { const ast = espree(` import "aaa"; import * as a from "bbb"; import b, {c, x as d} from "ccc";`, - {sourceType: 'module'} + {sourceType: "module"} ); - verify(ast, 'ImportDefaultSpecifier', [ - ['b'] + verify(ast, "ImportDefaultSpecifier", [ + ["b"] ]); }); - it('should get variables that declared on `ImportNamespaceSpecifier`', function() { + it("should get variables that declared on `ImportNamespaceSpecifier`", function() { const ast = espree(` import "aaa"; import * as a from "bbb"; import b, {c, x as d} from "ccc";`, - {sourceType: 'module'} + {sourceType: "module"} ); - verify(ast, 'ImportNamespaceSpecifier', [ - ['a'] + verify(ast, "ImportNamespaceSpecifier", [ + ["a"] ]); }); - it('should not get duplicate even if it\'s declared twice', function() { - const ast = espree(`var a = 0, a = 1;`); + it("should not get duplicate even if it's declared twice", function() { + const ast = espree("var a = 0, a = 1;"); - verify(ast, 'VariableDeclaration', [ - ['a'] + verify(ast, "VariableDeclaration", [ + ["a"] ]); }); }); diff --git a/test/global-increment.js b/test/global-increment.js index 83f77b3..ee9b9fa 100644 --- a/test/global-increment.js +++ b/test/global-increment.js @@ -22,18 +22,20 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const parse = require('../third_party/esprima').parse; -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ -describe('global increment', function() { - it('becomes read/write', function() { - const ast = parse(`b++;`); +const expect = require("chai").expect; +const parse = require("../third_party/esprima").parse; +const analyze = require("..").analyze; + +describe("global increment", function() { + it("becomes read/write", function() { + const ast = parse("b++;"); const scopeManager = analyze(ast); expect(scopeManager.scopes).to.have.length(1); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(0); expect(globalScope.references).to.have.length(1); expect(globalScope.references[0].isReadWrite()).to.be.true; diff --git a/test/implicit-global-reference.js b/test/implicit-global-reference.js index a9e04bf..73efc56 100644 --- a/test/implicit-global-reference.js +++ b/test/implicit-global-reference.js @@ -21,12 +21,12 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const parse = require('../third_party/esprima').parse; -const analyze = require('..').analyze; +const expect = require("chai").expect; +const parse = require("../third_party/esprima").parse; +const analyze = require("..").analyze; -describe('implicit global reference', function() { - it('assignments global scope', function() { +describe("implicit global reference", function() { + it("assignments global scope", function() { const ast = parse(` var x = 20; x = 300; @@ -38,7 +38,7 @@ describe('implicit global reference', function() { [ [ [ - 'Variable' + "Variable" ] ] ] @@ -47,7 +47,7 @@ describe('implicit global reference', function() { expect(scopes[0].implicit.variables.map(variable => variable.name)).to.be.eql([]); }); - it('assignments global scope without definition', function() { + it("assignments global scope without definition", function() { const ast = parse(` x = 300; x = 300; @@ -64,12 +64,12 @@ describe('implicit global reference', function() { expect(scopes[0].implicit.variables.map(variable => variable.name)).to.be.eql( [ - 'x' + "x" ] ); }); - it('assignments global scope without definition eval', function() { + it("assignments global scope without definition eval", function() { const ast = parse(` function inner() { eval(str); @@ -83,7 +83,7 @@ describe('implicit global reference', function() { [ [ [ - 'FunctionName' + "FunctionName" ] ], [ @@ -96,7 +96,7 @@ describe('implicit global reference', function() { expect(scopes[0].implicit.variables.map(variable => variable.name)).to.be.eql([]); }); - it('assignment leaks', function() { + it("assignment leaks", function() { const ast = parse(` function outer() { x = 20; @@ -108,22 +108,22 @@ describe('implicit global reference', function() { expect(scopes.map(scope => scope.variables.map(variable => variable.name))).to.be.eql( [ [ - 'outer' + "outer" ], [ - 'arguments' + "arguments" ] ] ); expect(scopes[0].implicit.variables.map(variable => variable.name)).to.be.eql( [ - 'x' + "x" ] ); }); - it('assignment doesn\'t leak', function() { + it("assignment doesn't leak", function() { const ast = parse(` function outer() { function inner() { @@ -138,15 +138,15 @@ describe('implicit global reference', function() { expect(scopes.map(scope => scope.variables.map(variable => variable.name))).to.be.eql( [ [ - 'outer' + "outer" ], [ - 'arguments', - 'inner', - 'x' + "arguments", + "inner", + "x" ], [ - 'arguments' + "arguments" ] ] ); @@ -155,7 +155,7 @@ describe('implicit global reference', function() { }); - it('for-in-statement leaks', function() { + it("for-in-statement leaks", function() { const ast = parse(` function outer() { for (x in y) { } @@ -166,22 +166,22 @@ describe('implicit global reference', function() { expect(scopes.map(scope => scope.variables.map(variable => variable.name))).to.be.eql( [ [ - 'outer' + "outer" ], [ - 'arguments' + "arguments" ] ] ); expect(scopes[0].implicit.variables.map(variable => variable.name)).to.be.eql( [ - 'x' + "x" ] ); }); - it('for-in-statement doesn\'t leaks', function() { + it("for-in-statement doesn't leaks", function() { const ast = parse(` function outer() { function inner() { @@ -196,15 +196,15 @@ describe('implicit global reference', function() { expect(scopes.map(scope => scope.variables.map(variable => variable.name))).to.be.eql( [ [ - 'outer' + "outer" ], [ - 'arguments', - 'inner', - 'x' + "arguments", + "inner", + "x" ], [ - 'arguments' + "arguments" ] ] ); diff --git a/test/implied-strict.js b/test/implied-strict.js index 148241b..9882960 100644 --- a/test/implied-strict.js +++ b/test/implied-strict.js @@ -22,12 +22,14 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const parse = require('../third_party/esprima').parse; -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ -describe('impliedStrict option', function() { - it('ensures all user scopes are strict if ecmaVersion >= 5', function() { +const expect = require("chai").expect; +const parse = require("../third_party/esprima").parse; +const analyze = require("..").analyze; + +describe("impliedStrict option", function() { + it("ensures all user scopes are strict if ecmaVersion >= 5", function() { const ast = parse(` function foo() { function bar() { @@ -40,22 +42,22 @@ describe('impliedStrict option', function() { expect(scopeManager.scopes).to.have.length(3); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); - expect(scope.block.type).to.be.equal('Program'); + expect(scope.type).to.be.equal("global"); + expect(scope.block.type).to.be.equal("Program"); expect(scope.isStrict).to.be.true; scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); - expect(scope.block.type).to.be.equal('FunctionDeclaration'); + expect(scope.type).to.be.equal("function"); + expect(scope.block.type).to.be.equal("FunctionDeclaration"); expect(scope.isStrict).to.be.true; scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('function'); - expect(scope.block.type).to.be.equal('FunctionDeclaration'); + expect(scope.type).to.be.equal("function"); + expect(scope.block.type).to.be.equal("FunctionDeclaration"); expect(scope.isStrict).to.be.true; }); - it('ensures impliedStrict option is only effective when ecmaVersion option >= 5', function() { + it("ensures impliedStrict option is only effective when ecmaVersion option >= 5", function() { const ast = parse(` function foo() {} `); @@ -64,17 +66,17 @@ describe('impliedStrict option', function() { 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.type).to.be.equal("global"); + expect(scope.block.type).to.be.equal("Program"); expect(scope.isStrict).to.be.false; scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); - expect(scope.block.type).to.be.equal('FunctionDeclaration'); + expect(scope.type).to.be.equal("function"); + expect(scope.block.type).to.be.equal("FunctionDeclaration"); expect(scope.isStrict).to.be.false; }); - it('omits a nodejs global scope when ensuring all user scopes are strict', function() { + it("omits a nodejs global scope when ensuring all user scopes are strict", function() { const ast = parse(` function foo() {} `); @@ -83,42 +85,42 @@ describe('impliedStrict option', function() { expect(scopeManager.scopes).to.have.length(3); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); - expect(scope.block.type).to.be.equal('Program'); + expect(scope.type).to.be.equal("global"); + expect(scope.block.type).to.be.equal("Program"); expect(scope.isStrict).to.be.false; scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); - expect(scope.block.type).to.be.equal('Program'); + expect(scope.type).to.be.equal("function"); + expect(scope.block.type).to.be.equal("Program"); expect(scope.isStrict).to.be.true; scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('function'); - expect(scope.block.type).to.be.equal('FunctionDeclaration'); + expect(scope.type).to.be.equal("function"); + expect(scope.block.type).to.be.equal("FunctionDeclaration"); expect(scope.isStrict).to.be.true; }); - it('omits a module global scope when ensuring all user scopes are strict', function() { + it("omits a module global scope when ensuring all user scopes are strict", function() { const ast = parse(` function foo() {}`, - {sourceType: 'module'} + {sourceType: "module"} ); - let scopeManager = analyze(ast, {ecmaVersion: 6, impliedStrict: true, sourceType: 'module'}); + let scopeManager = analyze(ast, {ecmaVersion: 6, impliedStrict: true, sourceType: "module"}); expect(scopeManager.scopes).to.have.length(3); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); - expect(scope.block.type).to.be.equal('Program'); + expect(scope.type).to.be.equal("global"); + expect(scope.block.type).to.be.equal("Program"); expect(scope.isStrict).to.be.false; scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('module'); + expect(scope.type).to.be.equal("module"); expect(scope.isStrict).to.be.true; scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('function'); - expect(scope.block.type).to.be.equal('FunctionDeclaration'); + expect(scope.type).to.be.equal("function"); + expect(scope.block.type).to.be.equal("FunctionDeclaration"); expect(scope.isStrict).to.be.true; }); }); diff --git a/test/label.js b/test/label.js index 95e6b30..d5c32f8 100644 --- a/test/label.js +++ b/test/label.js @@ -22,31 +22,33 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const parse = require('../third_party/esprima').parse; -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ -describe('label', function() { - it('should not create variables', function() { - const ast = parse(`function bar() { q: for(;;) { break q; } }`); +const expect = require("chai").expect; +const parse = require("../third_party/esprima").parse; +const analyze = require("..").analyze; + +describe("label", function() { + it("should not create variables", function() { + const ast = parse("function bar() { q: for(;;) { break q; } }"); const scopeManager = analyze(ast); expect(scopeManager.scopes).to.have.length(2); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(1); - expect(globalScope.variables[0].name).to.be.equal('bar'); + expect(globalScope.variables[0].name).to.be.equal("bar"); expect(globalScope.references).to.have.length(0); const scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); expect(scope.isArgumentsMaterialized()).to.be.false; expect(scope.references).to.have.length(0); }); - it('should count child node references', function() { + it("should count child node references", function() { const ast = parse(` var foo = 5; @@ -59,11 +61,11 @@ describe('label', function() { const scopeManager = analyze(ast); expect(scopeManager.scopes).to.have.length(1); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(1); - expect(globalScope.variables[0].name).to.be.equal('foo'); + expect(globalScope.variables[0].name).to.be.equal("foo"); expect(globalScope.through.length).to.be.equal(3); - expect(globalScope.through[2].identifier.name).to.be.equal('foo'); + expect(globalScope.through[2].identifier.name).to.be.equal("foo"); expect(globalScope.through[2].isRead()).to.be.true; }); }); diff --git a/test/nodejs-scope.js b/test/nodejs-scope.js index dff2c11..4f7ab7a 100644 --- a/test/nodejs-scope.js +++ b/test/nodejs-scope.js @@ -22,12 +22,14 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const parse = require('../third_party/esprima').parse; -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ -describe('nodejsScope option', function() { - it('creates a function scope following the global scope immediately', function() { +const expect = require("chai").expect; +const parse = require("../third_party/esprima").parse; +const analyze = require("..").analyze; + +describe("nodejsScope option", function() { + it("creates a function scope following the global scope immediately", function() { const ast = parse(` 'use strict'; var hello = 20; @@ -37,47 +39,47 @@ describe('nodejsScope option', function() { 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.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.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'); + 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', function() { + it("creates a function scope following the global scope immediately and creates module scope", function() { const ast = parse(` import {x as v} from "mod";`, - {sourceType: 'module' } + {sourceType: "module" } ); - const scopeManager = analyze(ast, {ecmaVersion: 6, nodejsScope: true, sourceType: 'module'}); + const scopeManager = analyze(ast, {ecmaVersion: 6, nodejsScope: true, sourceType: "module"}); expect(scopeManager.scopes).to.have.length(3); let scope = scopeManager.scopes[0]; - expect(scope.type).to.be.equal('global'); - expect(scope.block.type).to.be.equal('Program'); + 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.type).to.be.equal("function"); + expect(scope.block.type).to.be.equal("Program"); expect(scope.isStrict).to.be.false; expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('module'); + expect(scope.type).to.be.equal("module"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('v'); - expect(scope.variables[0].defs[0].type).to.be.equal('ImportBinding'); + expect(scope.variables[0].name).to.be.equal("v"); + expect(scope.variables[0].defs[0].type).to.be.equal("ImportBinding"); expect(scope.references).to.have.length(0); }); }); diff --git a/test/object-expression.js b/test/object-expression.js index d8ad451..27d1144 100644 --- a/test/object-expression.js +++ b/test/object-expression.js @@ -1,34 +1,34 @@ "use strict"; -const expect = require('chai').expect; -const analyze = require('..').analyze; +const expect = require("chai").expect; +const analyze = require("..").analyze; -describe('object expression', function() { - it('doesn\'t require property type', function() { +describe("object expression", function() { + it("doesn't require property type", function() { // Hardcoded AST. Esprima adds an extra 'Property' // key/value to ObjectExpressions, so we're not using // it parse a program string. const ast = { - type: 'Program', + type: "Program", body: [{ - type: 'VariableDeclaration', + type: "VariableDeclaration", declarations: [{ - type: 'VariableDeclarator', + type: "VariableDeclarator", id: { - type: 'Identifier', - name: 'a' + type: "Identifier", + name: "a" }, init: { - type: 'ObjectExpression', + type: "ObjectExpression", properties: [{ - kind: 'init', + kind: "init", key: { - type: 'Identifier', - name: 'foo' + type: "Identifier", + name: "foo" }, value: { - type: 'Identifier', - name: 'a' + type: "Identifier", + name: "a" } }] } @@ -39,8 +39,8 @@ describe('object expression', function() { const scope = analyze(ast).scopes[0]; expect(scope.variables).to.have.length(1); expect(scope.references).to.have.length(2); - expect(scope.variables[0].name).to.be.equal('a'); - expect(scope.references[0].identifier.name).to.be.equal('a'); - expect(scope.references[1].identifier.name).to.be.equal('a'); + expect(scope.variables[0].name).to.be.equal("a"); + expect(scope.references[0].identifier.name).to.be.equal("a"); + expect(scope.references[1].identifier.name).to.be.equal("a"); }); }); diff --git a/test/optimistic.js b/test/optimistic.js index 450dabb..2dc59b0 100644 --- a/test/optimistic.js +++ b/test/optimistic.js @@ -21,12 +21,12 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const parse = require('../third_party/esprima').parse; -const analyze = require('..').analyze; +const expect = require("chai").expect; +const parse = require("../third_party/esprima").parse; +const analyze = require("..").analyze; -describe('optimistic', function() { - it('direct call to eval', function() { +describe("optimistic", function() { + it("direct call to eval", function() { const ast = parse(` function outer() { eval(str); @@ -42,21 +42,21 @@ describe('optimistic', function() { expect(scopes.map(scope => scope.variables.map(variable => variable.name))).to.be.eql( [ [ - 'outer' + "outer" ], [ - 'arguments', - 'i', - 'inner' + "arguments", + "i", + "inner" ], [ - 'arguments' + "arguments" ] ] ); }); - it('with statement', function() { + it("with statement", function() { const ast = parse(` function outer() { eval(str); @@ -72,11 +72,11 @@ describe('optimistic', function() { expect(scopes.map(scope => scope.variables.map(variable => variable.name))).to.be.eql( [ [ - 'outer' + "outer" ], [ - 'arguments', - 'i' + "arguments", + "i" ], [ ] diff --git a/test/references.js b/test/references.js index 2972d9a..ff4bed5 100644 --- a/test/references.js +++ b/test/references.js @@ -22,14 +22,16 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const espree = require('../third_party/espree'); -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ -describe('References:', function() { - describe('When there is a `let` declaration on global,', function() { - it('the reference on global should be resolved.', function() { - const ast = espree(`let a = 0;`); +const expect = require("chai").expect; +const espree = require("../third_party/espree"); +const analyze = require("..").analyze; + +describe("References:", function() { + describe("When there is a `let` declaration on global,", function() { + it("the reference on global should be resolved.", function() { + const ast = espree("let a = 0;"); const scopeManager = analyze(ast, {ecmaVersion: 6}); expect(scopeManager.scopes).to.have.length(1); @@ -40,14 +42,14 @@ describe('References:', function() { const reference = scope.references[0]; expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('a'); + expect(reference.identifier.name).to.equal("a"); expect(reference.resolved).to.equal(scope.variables[0]); expect(reference.writeExpr).to.not.be.undefined; expect(reference.isWrite()).to.be.true; expect(reference.isRead()).to.be.false; }); - it('the reference in functions should be resolved.', function() { + it("the reference in functions should be resolved.", function() { const ast = espree(` let a = 0; function foo() { @@ -64,14 +66,14 @@ describe('References:', function() { const reference = scope.references[1]; expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('a'); + expect(reference.identifier.name).to.equal("a"); expect(reference.resolved).to.equal(scopeManager.scopes[0].variables[0]); expect(reference.writeExpr).to.be.undefined; expect(reference.isWrite()).to.be.false; expect(reference.isRead()).to.be.true; }); - it('the reference in default parameters should be resolved.', function() { + it("the reference in default parameters should be resolved.", function() { const ast = espree(` let a = 0; function foo(b = a) { @@ -87,7 +89,7 @@ describe('References:', function() { const reference = scope.references[1]; expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('a'); + expect(reference.identifier.name).to.equal("a"); expect(reference.resolved).to.equal(scopeManager.scopes[0].variables[0]); expect(reference.writeExpr).to.be.undefined; expect(reference.isWrite()).to.be.false; @@ -95,9 +97,9 @@ describe('References:', function() { }); }); - describe('When there is a `const` declaration on global,', function() { - it('the reference on global should be resolved.', function() { - const ast = espree(`const a = 0;`); + describe("When there is a `const` declaration on global,", function() { + it("the reference on global should be resolved.", function() { + const ast = espree("const a = 0;"); const scopeManager = analyze(ast, {ecmaVersion: 6}); expect(scopeManager.scopes).to.have.length(1); @@ -108,14 +110,14 @@ describe('References:', function() { const reference = scope.references[0]; expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('a'); + expect(reference.identifier.name).to.equal("a"); expect(reference.resolved).to.equal(scope.variables[0]); expect(reference.writeExpr).to.not.be.undefined; expect(reference.isWrite()).to.be.true; expect(reference.isRead()).to.be.false; }); - it('the reference in functions should be resolved.', function() { + it("the reference in functions should be resolved.", function() { const ast = espree(` const a = 0; function foo() { @@ -132,7 +134,7 @@ describe('References:', function() { const reference = scope.references[1]; expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('a'); + expect(reference.identifier.name).to.equal("a"); expect(reference.resolved).to.equal(scopeManager.scopes[0].variables[0]); expect(reference.writeExpr).to.be.undefined; expect(reference.isWrite()).to.be.false; @@ -140,9 +142,9 @@ describe('References:', function() { }); }); - describe('When there is a `var` declaration on global,', function() { - it('the reference on global should NOT be resolved.', function() { - const ast = espree(`var a = 0;`); + describe("When there is a `var` declaration on global,", function() { + it("the reference on global should NOT be resolved.", function() { + const ast = espree("var a = 0;"); const scopeManager = analyze(ast, {ecmaVersion: 6}); expect(scopeManager.scopes).to.have.length(1); @@ -153,14 +155,14 @@ describe('References:', function() { const reference = scope.references[0]; expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('a'); + expect(reference.identifier.name).to.equal("a"); expect(reference.resolved).to.be.null; expect(reference.writeExpr).to.not.be.undefined; expect(reference.isWrite()).to.be.true; expect(reference.isRead()).to.be.false; }); - it('the reference in functions should NOT be resolved.', function() { + it("the reference in functions should NOT be resolved.", function() { const ast = espree(` var a = 0; function foo() { @@ -177,7 +179,7 @@ describe('References:', function() { const reference = scope.references[1]; expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('a'); + expect(reference.identifier.name).to.equal("a"); expect(reference.resolved).to.be.null; expect(reference.writeExpr).to.be.undefined; expect(reference.isWrite()).to.be.false; @@ -185,8 +187,8 @@ describe('References:', function() { }); }); - describe('When there is a `function` declaration on global,', function() { - it('the reference on global should NOT be resolved.', function() { + describe("When there is a `function` declaration on global,", function() { + it("the reference on global should NOT be resolved.", function() { const ast = espree(` function a() {} a(); @@ -201,14 +203,14 @@ describe('References:', function() { const reference = scope.references[0]; expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('a'); + expect(reference.identifier.name).to.equal("a"); expect(reference.resolved).to.be.null; expect(reference.writeExpr).to.be.undefined; expect(reference.isWrite()).to.be.false; expect(reference.isRead()).to.be.true; }); - it('the reference in functions should NOT be resolved.', function() { + it("the reference in functions should NOT be resolved.", function() { const ast = espree(` function a() {} function foo() { @@ -225,7 +227,7 @@ describe('References:', function() { const reference = scope.references[1]; expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('a'); + expect(reference.identifier.name).to.equal("a"); expect(reference.resolved).to.be.null; expect(reference.writeExpr).to.be.undefined; expect(reference.isWrite()).to.be.false; @@ -233,8 +235,8 @@ describe('References:', function() { }); }); - describe('When there is a `class` declaration on global,', function() { - it('the reference on global should be resolved.', function() { + describe("When there is a `class` declaration on global,", function() { + it("the reference on global should be resolved.", function() { const ast = espree(` class A {} let b = new A(); @@ -249,14 +251,14 @@ describe('References:', function() { const reference = scope.references[1]; expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('A'); + expect(reference.identifier.name).to.equal("A"); expect(reference.resolved).to.equal(scope.variables[0]); expect(reference.writeExpr).to.be.undefined; expect(reference.isWrite()).to.be.false; expect(reference.isRead()).to.be.true; }); - it('the reference in functions should be resolved.', function() { + it("the reference in functions should be resolved.", function() { const ast = espree(` class A {} function foo() { @@ -273,7 +275,7 @@ describe('References:', function() { const reference = scope.references[1]; expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('A'); + expect(reference.identifier.name).to.equal("A"); expect(reference.resolved).to.equal(scopeManager.scopes[0].variables[0]); expect(reference.writeExpr).to.be.undefined; expect(reference.isWrite()).to.be.false; @@ -281,8 +283,8 @@ describe('References:', function() { }); }); - describe('When there is a `let` declaration in functions,', function() { - it('the reference on the function should be resolved.', function() { + describe("When there is a `let` declaration in functions,", function() { + it("the reference on the function should be resolved.", function() { const ast = espree(` function foo() { let a = 0; @@ -298,14 +300,14 @@ describe('References:', function() { const reference = scope.references[0]; expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('a'); + expect(reference.identifier.name).to.equal("a"); expect(reference.resolved).to.equal(scope.variables[1]); expect(reference.writeExpr).to.not.be.undefined; expect(reference.isWrite()).to.be.true; expect(reference.isRead()).to.be.false; }); - it('the reference in nested functions should be resolved.', function() { + it("the reference in nested functions should be resolved.", function() { const ast = espree(` function foo() { let a = 0; @@ -324,7 +326,7 @@ describe('References:', function() { const reference = scope.references[1]; expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('a'); + expect(reference.identifier.name).to.equal("a"); expect(reference.resolved).to.equal(scopeManager.scopes[1].variables[1]); expect(reference.writeExpr).to.be.undefined; expect(reference.isWrite()).to.be.false; @@ -332,8 +334,8 @@ describe('References:', function() { }); }); - describe('When there is a `var` declaration in functions,', function() { - it('the reference on the function should be resolved.', function() { + describe("When there is a `var` declaration in functions,", function() { + it("the reference on the function should be resolved.", function() { const ast = espree(` function foo() { var a = 0; @@ -349,14 +351,14 @@ describe('References:', function() { const reference = scope.references[0]; expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('a'); + expect(reference.identifier.name).to.equal("a"); expect(reference.resolved).to.equal(scope.variables[1]); expect(reference.writeExpr).to.not.be.undefined; expect(reference.isWrite()).to.be.true; expect(reference.isRead()).to.be.false; }); - it('the reference in nested functions should be resolved.', function() { + it("the reference in nested functions should be resolved.", function() { const ast = espree(` function foo() { var a = 0; @@ -375,7 +377,7 @@ describe('References:', function() { const reference = scope.references[1]; expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('a'); + expect(reference.identifier.name).to.equal("a"); expect(reference.resolved).to.equal(scopeManager.scopes[1].variables[1]); expect(reference.writeExpr).to.be.undefined; expect(reference.isWrite()).to.be.false; @@ -383,9 +385,9 @@ describe('References:', function() { }); }); - describe('When there is a `let` declaration with destructuring assignment', function() { - it('"let [a] = [1];", the reference should be resolved.', function() { - const ast = espree(`let [a] = [1];`); + describe("When there is a `let` declaration with destructuring assignment", function() { + it("\"let [a] = [1];\", the reference should be resolved.", function() { + const ast = espree("let [a] = [1];"); const scopeManager = analyze(ast, {ecmaVersion: 6}); expect(scopeManager.scopes).to.have.length(1); @@ -396,15 +398,15 @@ describe('References:', function() { const reference = scope.references[0]; expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('a'); + expect(reference.identifier.name).to.equal("a"); expect(reference.resolved).to.equal(scope.variables[0]); expect(reference.writeExpr).to.not.be.undefined; expect(reference.isWrite()).to.be.true; expect(reference.isRead()).to.be.false; }); - it('"let {a} = {a: 1};", the reference should be resolved.', function() { - const ast = espree(`let {a} = {a: 1};`); + it("\"let {a} = {a: 1};\", the reference should be resolved.", function() { + const ast = espree("let {a} = {a: 1};"); const scopeManager = analyze(ast, {ecmaVersion: 6}); expect(scopeManager.scopes).to.have.length(1); @@ -415,15 +417,15 @@ describe('References:', function() { const reference = scope.references[0]; expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('a'); + expect(reference.identifier.name).to.equal("a"); expect(reference.resolved).to.equal(scope.variables[0]); expect(reference.writeExpr).to.not.be.undefined; expect(reference.isWrite()).to.be.true; expect(reference.isRead()).to.be.false; }); - it('"let {a: {a}} = {a: {a: 1}};", the reference should be resolved.', function() { - const ast = espree(`let {a: {a}} = {a: {a: 1}};`); + it("\"let {a: {a}} = {a: {a: 1}};\", the reference should be resolved.", function() { + const ast = espree("let {a: {a}} = {a: {a: 1}};"); const scopeManager = analyze(ast, {ecmaVersion: 6}); expect(scopeManager.scopes).to.have.length(1); @@ -434,7 +436,7 @@ describe('References:', function() { const reference = scope.references[0]; expect(reference.from).to.equal(scope); - expect(reference.identifier.name).to.equal('a'); + expect(reference.identifier.name).to.equal("a"); expect(reference.resolved).to.equal(scope.variables[0]); expect(reference.writeExpr).to.not.be.undefined; expect(reference.isWrite()).to.be.true; @@ -442,39 +444,39 @@ describe('References:', function() { }); }); - describe('Reference.init should be a boolean value of whether it is one to initialize or not.', function() { + describe("Reference.init should be a boolean value of whether it is one to initialize or not.", function() { const trueCodes = [ - 'var a = 0;', - 'let a = 0;', - 'const a = 0;', - 'var [a] = [];', - 'let [a] = [];', - 'const [a] = [];', - 'var [a = 1] = [];', - 'let [a = 1] = [];', - 'const [a = 1] = [];', - 'var {a} = {};', - 'let {a} = {};', - 'const {a} = {};', - 'var {b: a} = {};', - 'let {b: a} = {};', - 'const {b: a} = {};', - 'var {b: a = 0} = {};', - 'let {b: a = 0} = {};', - 'const {b: a = 0} = {};', - 'for (var a in []);', - 'for (let a in []);', - 'for (var [a] in []);', - 'for (let [a] in []);', - 'for (var [a = 0] in []);', - 'for (let [a = 0] in []);', - 'for (var {a} in []);', - 'for (let {a} in []);', - 'for (var {a = 0} in []);', - 'for (let {a = 0} in []);', - 'new function(a = 0) {}', - 'new function([a = 0] = []) {}', - 'new function({b: a = 0} = {}) {}' + "var a = 0;", + "let a = 0;", + "const a = 0;", + "var [a] = [];", + "let [a] = [];", + "const [a] = [];", + "var [a = 1] = [];", + "let [a = 1] = [];", + "const [a = 1] = [];", + "var {a} = {};", + "let {a} = {};", + "const {a} = {};", + "var {b: a} = {};", + "let {b: a} = {};", + "const {b: a} = {};", + "var {b: a = 0} = {};", + "let {b: a = 0} = {};", + "const {b: a = 0} = {};", + "for (var a in []);", + "for (let a in []);", + "for (var [a] in []);", + "for (let [a] in []);", + "for (var [a = 0] in []);", + "for (let [a = 0] in []);", + "for (var {a} in []);", + "for (let {a} in []);", + "for (var {a = 0} in []);", + "for (let {a = 0} in []);", + "new function(a = 0) {}", + "new function([a = 0] = []) {}", + "new function({b: a = 0} = {}) {}" ]; trueCodes.forEach(code => @@ -489,7 +491,7 @@ describe('References:', function() { expect(scope.references).to.have.length.of.at.least(1); scope.references.forEach(reference => { - expect(reference.identifier.name).to.equal('a'); + expect(reference.identifier.name).to.equal("a"); expect(reference.isWrite()).to.be.true; expect(reference.init).to.be.true; }); @@ -497,17 +499,17 @@ describe('References:', function() { ); let falseCodes = [ - 'let a; a = 0;', - 'let a; [a] = [];', - 'let a; [a = 1] = [];', - 'let a; ({a}) = {};', - 'let a; ({b: a}) = {};', - 'let a; ({b: a = 0}) = {};', - 'let a; for (a in []);', - 'let a; for ([a] in []);', - 'let a; for ([a = 0] in []);', - 'let a; for ({a} in []);', - 'let a; for ({a = 0} in []);' + "let a; a = 0;", + "let a; [a] = [];", + "let a; [a = 1] = [];", + "let a; ({a}) = {};", + "let a; ({b: a}) = {};", + "let a; ({b: a = 0}) = {};", + "let a; for (a in []);", + "let a; for ([a] in []);", + "let a; for ([a = 0] in []);", + "let a; for ({a} in []);", + "let a; for ({a = 0} in []);" ]; falseCodes.forEach(code => it(`"${code}", all references should be false.`, function() { @@ -521,28 +523,28 @@ describe('References:', function() { expect(scope.references).to.have.length.of.at.least(1); scope.references.forEach(reference => { - expect(reference.identifier.name).to.equal('a'); + expect(reference.identifier.name).to.equal("a"); expect(reference.isWrite()).to.be.true; expect(reference.init).to.be.false; - }) + }); }) ); falseCodes = [ - 'let a; let b = a;', - 'let a; let [b] = a;', - 'let a; let [b = a] = [];', - 'let a; for (var b in a);', - 'let a; for (var [b = a] in []);', - 'let a; for (let b in a);', - 'let a; for (let [b = a] in []);', - 'let a,b; b = a;', - 'let a,b; [b] = a;', - 'let a,b; [b = a] = [];', - 'let a,b; for (b in a);', - 'let a,b; for ([b = a] in []);', - 'let a; a.foo = 0;', - 'let a,b; b = a.foo;' + "let a; let b = a;", + "let a; let [b] = a;", + "let a; let [b = a] = [];", + "let a; for (var b in a);", + "let a; for (var [b = a] in []);", + "let a; for (let b in a);", + "let a; for (let [b = a] in []);", + "let a,b; b = a;", + "let a,b; [b] = a;", + "let a,b; [b = a] = [];", + "let a,b; for (b in a);", + "let a,b; for ([b = a] in []);", + "let a; a.foo = 0;", + "let a,b; b = a.foo;" ]; falseCodes.forEach(code => it(`"${code}", readonly references of "a" should be undefined.`, function() { @@ -553,7 +555,7 @@ describe('References:', function() { const scope = scopeManager.scopes[0]; expect(scope.variables).to.have.length.of.at.least(1); - expect(scope.variables[0].name).to.equal('a'); + expect(scope.variables[0].name).to.equal("a"); const references = scope.variables[0].references; expect(references).to.have.length.of.at.least(1); @@ -563,7 +565,7 @@ describe('References:', function() { expect(reference.init).to.be.undefined; }); }) - ) + ); }); }); diff --git a/test/typescript.js b/test/typescript.js index 4859b86..2f1cec0 100644 --- a/test/typescript.js +++ b/test/typescript.js @@ -4,21 +4,23 @@ */ "use strict"; +/* eslint-disable no-unused-expressions */ + //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ -const expect = require('chai').expect, - parse = require('typescript-eslint-parser').parse, - analyze = require('../src').analyze; +const expect = require("chai").expect, + parse = require("typescript-eslint-parser").parse, + analyze = require("../src").analyze; //------------------------------------------------------------------------------ // Tests //------------------------------------------------------------------------------ -describe('typescript', () => { - describe('multiple call signatures', () => { - it('should create a function scope', () => { +describe("typescript", () => { + describe("multiple call signatures", () => { + it("should create a function scope", () => { const ast = parse(` function foo(bar: number): number; function foo(bar: string): string; @@ -32,30 +34,30 @@ describe('typescript', () => { expect(scopeManager.scopes).to.have.length(4); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(1); expect(globalScope.references).to.have.length(0); expect(globalScope.isArgumentsMaterialized()).to.be.true; // Function scopes let scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(2); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); expect(scope.isArgumentsMaterialized()).to.be.false; expect(scope.references).to.have.length(0); scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(2); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); expect(scope.isArgumentsMaterialized()).to.be.false; expect(scope.references).to.have.length(0); scope = scopeManager.scopes[3]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(2); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); expect(scope.isArgumentsMaterialized()).to.be.false; expect(scope.references).to.have.length(1); diff --git a/test/with-scope.js b/test/with-scope.js index d526c01..96450ba 100644 --- a/test/with-scope.js +++ b/test/with-scope.js @@ -22,12 +22,14 @@ // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. "use strict"; -const expect = require('chai').expect; -const parse = require('../third_party/esprima').parse; -const analyze = require('..').analyze; +/* eslint-disable no-unused-expressions */ -describe('with', function() { - it('creates scope', function() { +const expect = require("chai").expect; +const parse = require("../third_party/esprima").parse; +const analyze = require("..").analyze; + +describe("with", function() { + it("creates scope", function() { const ast = parse(` (function () { with (obj) { @@ -39,20 +41,20 @@ describe('with', function() { const scopeManager = analyze(ast); expect(scopeManager.scopes).to.have.length(3); const globalScope = scopeManager.scopes[0]; - expect(globalScope.type).to.be.equal('global'); + expect(globalScope.type).to.be.equal("global"); expect(globalScope.variables).to.have.length(0); expect(globalScope.references).to.have.length(0); let scope = scopeManager.scopes[1]; - expect(scope.type).to.be.equal('function'); + expect(scope.type).to.be.equal("function"); expect(scope.variables).to.have.length(1); - expect(scope.variables[0].name).to.be.equal('arguments'); + expect(scope.variables[0].name).to.be.equal("arguments"); expect(scope.isArgumentsMaterialized()).to.be.false; expect(scope.references).to.have.length(1); expect(scope.references[0].resolved).to.be.null; scope = scopeManager.scopes[2]; - expect(scope.type).to.be.equal('with'); + expect(scope.type).to.be.equal("with"); expect(scope.variables).to.have.length(0); expect(scope.isArgumentsMaterialized()).to.be.true; expect(scope.references).to.have.length(1);