jtmthf / eslint-plugin-ejs

eslint processor that parsers and strips pjs templates
Apache License 2.0
0 stars 1 forks source link

Cannot read property 'forEach' of undefined #1

Open Jarrydcrawford opened 8 years ago

Jarrydcrawford commented 8 years ago

I was having some errors blow up in my Atom text editor specifically whenever I had syntax error on one of my JS files. Tried running eslint from the command line and the same issue surfaced:

node_modules/.bin/eslint path/to/file.js

Cannot read property 'forEach' of undefined
TypeError: Cannot read property 'forEach' of undefined
    at ~/path/to/project/node_modules/eslint-plugin-ejs/lib/processors/ejs.js:92:26
    at Array.map (native)
    at Object.module.exports.postprocess (~/path/to/project/node_modules/eslint-plugin-ejs/lib/processors/ejs.js:84:21)
    at processText (~/path/to/project/node_modules/eslint/lib/cli-engine.js:251:30)
    at processFile (~/path/to/project/node_modules/eslint/lib/cli-engine.js:297:18)
    at executeOnFile (~/path/to/project/node_modules/eslint/lib/cli-engine.js:667:25)
    at ~/path/to/project/node_modules/eslint/lib/cli-engine.js:705:13
    at Array.forEach (native)
    at CLIEngine.executeOnFiles (~/path/to/project/node_modules/eslint/lib/cli-engine.js:704:18)
    at Object.execute (~/path/to/project/node_modules/eslint/lib/cli.js:174:107)

Had a look at the offending portion of code in node_modules/eslint-plugin-ejs/lib/processors/ejs.js and reduced the index being applied to markers by one:

markers[line - 1].forEach(function(removed) {

Fixed my issue but untested against other scenarios. Ran the tests from within the package but failed against my amend.

Mac OSX 10.11.6 running node v6.4.0 with the following packages involved:

eslint@3.3.1
eslint-plugin-ejs@0.0.2
eslint-plugin-react@6.1.2

.eslintrc is as follows:

{
    "extends": "airbnb",
    "env": {
        "browser": true,
        "mocha": true,
        "node": true,
    },
    "plugins": ["eslint-plugin-ejs", "mocha", "chai-expect", "eslint-plugin-react"],
    "rules": {
        "global-require": [0],
        "import/extensions": [1, {"js": "never", "jsx": "never", "json": "always"}],
        "indent": ["error", 4],
        "no-cond-assign": [1, "except-parens"],
        "semi": ["error", "never"],
        "react/jsx-indent": [1, 4],
        "react/jsx-indent-props": [1, 4],
        "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
        "react/require-extension": [0],
    },
    "globals": {
        "__DEV__": true,
    }
}
jtmthf commented 8 years ago

I'm wondering if this has to do with the way eslint 3.x handles line numbers. Can you try this on eslint 2.x, and see if the error persists? If so, I'll cut an updated version that targets 3.x.

Jarrydcrawford commented 8 years ago

Tried creating a very isolated test case and it worked fine on both v3.x and v2.x so must be a combination of eslint plugins. I'll make a more specific test case and then let you know the results!

JohnRiv commented 7 years ago

I just encountered this error. Using ESLint 3.19.0. It happened when we had an eol-last error that was in our code (Newline required at end of file but not found). I figured that out by using @Jarrydcrawford's code sample that changes markers[line] to markers[line - 1].

When I fixed the lint error in my code and reverted the change to eslint-plugin-ejs, ESLint ran properly.

Would an appropriate fix be to use markers[line - 1] if markers[line] is undefined?

JohnRiv commented 7 years ago

@jtmthf would you be open to a PR with my suggested fix?

gee4vee commented 4 years ago

I know this is old but we're encountering this as well.

bombillazo commented 1 year ago

happening to me as well