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

TypeError: Cannot read property '0' of undefined #66

Closed
gaborantal opened this issue Oct 9, 2020 · 4 comments · Fixed by #67 · May be fixed by OKEAMAH/remix-project#10
Closed

TypeError: Cannot read property '0' of undefined #66

gaborantal opened this issue Oct 9, 2020 · 4 comments · Fixed by #67 · May be fixed by OKEAMAH/remix-project#10

Comments

@gaborantal
Copy link

I just tried to run the provided code from README. However there are several problems: the first is that eslintScope.analyze requires a second parameter called providedOptions.

The second problem is a TypeError:

D:\path-to-project\node_modules\eslint-scope\lib\scope.js:711
        const bodyStart = this.block.body.range[0];
                                               ^

TypeError: Cannot read property '0' of undefined
    at FunctionScope.__isValidResolution (D:\path-to-project\node_modules\eslint-scope\lib\scope.js:711:48)
    at FunctionScope.__resolve (D:\path-to-project\node_modules\eslint-scope\lib\scope.js:357:19)
    at FunctionScope.__staticCloseRef (D:\path-to-project\node_modules\eslint-scope\lib\scope.js:294:19)
    at FunctionScope.__close (D:\path-to-project\node_modules\eslint-scope\lib\scope.js:336:22)
    at Referencer.close (D:\path-to-project\node_modules\eslint-scope\lib\referencer.js:129:68)
    at Referencer.visitFunction (D:\path-to-project\node_modules\eslint-scope\lib\referencer.js:261:14)
    at Referencer.ArrowFunctionExpression (D:\path-to-project\node_modules\eslint-scope\lib\referencer.js:573:14)
    at Referencer.Visitor.visit (D:\path-to-project\node_modules\esrecurse\esrecurse.js:104:34)
    at Referencer.Visitor.visitChildren (D:\path-to-project\node_modules\esrecurse\esrecurse.js:83:38)
    at Referencer.CallExpression (D:\path-to-project\node_modules\eslint-scope\lib\referencer.js:500:14)

Process finished with exit code 1

My used dependencies are (from package.json):

  "dependencies": {
    "eslint-scope": "^5.1.1",
    "espree": "^7.3.0",
    "estraverse": "^5.2.0"
  }

Versions:
node --version
v12.19.0
npm --version
6.14.8

My full code (I was just fiddling around to test something):

const eslintScope = require('eslint-scope');
const espree = require('espree');
const estraverse = require('estraverse');

code = `
function alma() {
    console.log("sh");
}
const materials = [
  'Hydrogen',
  'Helium',
  'Lithium',
  'Beryllium'
];

console.log(materials.map(material => material.length));
class Car {
  constructor(brand) {
    this._carname = brand;
  }
  get carname() {
    return this._carname;
  }
  set carname(x) {
    this._carname = x;
  }
}

let mycar = new Car("Ford");
mycar.carname = "Volvo";
`;


const ast = espree.parse(code, { ecmaVersion: 2021, loc: true, });
console.log(ast);
var scopeManager = eslintScope.analyze(ast, {});

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

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

        if (/Function/.test(node.type)) {
            currentScope = scopeManager.acquire(node);  // get current function scope
        }
    },
    leave: function(node, parent) {
        if (/Function/.test(node.type)) {
            currentScope = currentScope.upper;  // set to parent scope
        }

        // do stuff
    }
});
console.log(ast);
@mdjermanovic
Copy link
Member

Hi @gaborantal, thanks for the issue!

the first is that eslintScope.analyze requires a second parameter called providedOptions

var scopeManager = eslintScope.analyze(ast, {});

It isn't technically required, eslintScope.analyze(ast) works without the second argument (I've just tried) and uses default options.

Though, it's true that you should probably always provide options, like ecmaVersion etc. See for example how ESLint calls eslintScope.analyze here.

The second problem is a TypeError

D:\path-to-project\node_modules\eslint-scope\lib\scope.js:711
       const bodyStart = this.block.body.range[0];
                                              ^

TypeError: Cannot read property '0' of undefined
const ast = espree.parse(code, { ecmaVersion: 2021, loc: true, });

This should be range: true, eslint-scope doesn't use loc.

I think it would be good to add { range: true } to the README example.

@gaborantal
Copy link
Author

@mdjermanovic Yup, thanks, that solved my problem, it works like a charm now. I am also on your opinion, these should've been in the sample in README. I'd like to notice that it would've been better to use let/const/etc in the sample codes. If you don't mind, I'll send a PR with this change.

(the loc is intented to used by my future function that will be implemented.)

@nzakas
Copy link
Member

nzakas commented Oct 9, 2020

@gaborantal feel free to submit a PR.

@mdmackint
Copy link

I'm not very good with npm/node.js, but I would guess that it wasn't defined, or wasn't defined as an array

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants