Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'range' of null from template-curly-spacing #530

Closed
jasonblanchard opened this issue Oct 16, 2017 · 120 comments
Closed

Comments

@jasonblanchard
Copy link

@jasonblanchard jasonblanchard commented Oct 16, 2017

With this .eslintrc config:

{
  "root": true,
  "parser": "babel-eslint",
  "extends": [
    "eslint:recommended",
    "semistandard"
  ],
  "env": {
    "browser": true,
    "commonjs": true,
    "es6": true,
    "mocha": true,
    "node": true
  },
  "plugins": [
    "babel"
  ],
  "rules": {
    "arrow-parens": 0,
    "no-class-assign": 0,
    "space-before-function-paren": [2, "never"]
  }
}

And these dependencies:

  "dependencies": {
    "babel-runtime": "6.26.0",
  },
  "devDependencies": {
    "babel-cli": "6.26.0",
    "babel-core": "6.26.0",
    "babel-eslint": "8.0.1",
    "babel-plugin-transform-runtime": "6.23.0",
    "babel-preset-latest": "6.24.1",
    "babel-preset-stage-0": "6.24.1",
    "chai": "4.1.2",
    "dotenv": "4.0.0",
    "eslint": "4.9.0",
    "eslint-config-semistandard": "11.0.0",
    "eslint-config-standard": "10.2.1",
    "eslint-plugin-babel": "4.1.2",
    "eslint-plugin-import": "2.7.0",
    "eslint-plugin-node": "5.2.0",
    "eslint-plugin-promise": "3.6.0",
    "eslint-plugin-standard": "3.0.1",
    "mocha": "4.0.1"
  }

this code snippet:

export default class SomeClass {
  someMethod(params) {
    const test = 'test';
    return `${test}`; // Seems to have to do with this template. Returning `test` directly is fine.
  }
}

results in this error when running eslint:

Cannot read property 'range' of null
TypeError: Cannot read property 'range' of null
    at SourceCode.getTokenBefore (/Users/jason/my-project/node_modules/eslint/lib/token-store/index.js:303:17)
    at checkSpacingBefore (/Users/jason/my-project/node_modules/eslint/lib/rules/template-curly-spacing.js:51:42)
    at TemplateElement (/Users/jason/my-project/node_modules/eslint/lib/rules/template-curly-spacing.js:116:17)
    at listeners.(anonymous function).forEach.listener (/Users/jason/my-project/node_modules/eslint/lib/util/safe-emitter.js:47:58)
    at Array.forEach (native)
    at Object.emit (/Users/jason/my-project/node_modules/eslint/lib/util/safe-emitter.js:47:38)
    at NodeEventGenerator.applySelector (/Users/jason/my-project/node_modules/eslint/lib/util/node-event-generator.js:251:26)
    at NodeEventGenerator.applySelectors (/Users/jason/my-project/node_modules/eslint/lib/util/node-event-generator.js:280:22)
    at NodeEventGenerator.enterNode (/Users/jason/my-project/node_modules/eslint/lib/util/node-event-generator.js:294:14)
    at CodePathAnalyzer.enterNode (/Users/jason/my-project/node_modules/eslint/lib/code-path-analysis/code-path-analyzer.js:606:23)

If I remove "parser": "babel-eslint" the problem goes away, so I think it has to do with babel-eslint. The error also goes away if I pin babel-eslint to 7.2.3.

Happy to provide any other information if necessary!

@hzoo

This comment has been minimized.

Copy link
Member

@hzoo hzoo commented Nov 6, 2017

Weird.. 🤔 it works fine with just

  "dependencies": {
    "babel-eslint": "^8.0.1",
    "eslint": "^4.10.0"
  },

Also just FYI, babel-preset-latest has been deprecated?

Can you recreate a repo that produces this error (and with the minimal dependencies/config). I don't think the other eslint plugins/configs should matter.

Also can you try v8.0.2? Seems like npm/yarn aren't resolving beta versions correctly

@afc163

This comment has been minimized.

Copy link

@afc163 afc163 commented Dec 11, 2017

ant-design/ant-design@d1568c9

Locking to 8.0.1 seems to be ok.

@afc163

This comment has been minimized.

Copy link

@afc163 afc163 commented Dec 11, 2017

8.0.2 works fine too.

@afc163

This comment has been minimized.

@yesmeck

This comment has been minimized.

Copy link

@yesmeck yesmeck commented Dec 28, 2017

After a few hours digging, I found the root problem. There are multiple copies of @babel/types in our node_modules.

[1] % npm list @babel/types
├─┬ babel-eslint@8.1.2
│ ├─┬ @babel/traverse@7.0.0-beta.31
│ │ ├─┬ @babel/helper-function-name@7.0.0-beta.31
│ │ │ ├─┬ @babel/helper-get-function-arity@7.0.0-beta.31
│ │ │ │ └── @babel/types@7.0.0-beta.31
│ │ │ ├─┬ @babel/template@7.0.0-beta.31
│ │ │ │ └── @babel/types@7.0.0-beta.31
│ │ │ └── @babel/types@7.0.0-beta.31
│ │ └── @babel/types@7.0.0-beta.31
│ └── @babel/types@7.0.0-beta.31
└─┬ babel-plugin-import@1.6.3
  └─┬ @babel/helper-module-imports@7.0.0-beta.36
    └── @babel/types@7.0.0-beta.36

@babel/types@7.0.0-beta.36 is lifted to the root, so that babel-eslint and @babel/traverse both have a copy of @babel/types in their own node_modules. The monkey patch for MethodDefinition not effect the version of @babel/traverse's @babel/types

@yesmeck

This comment has been minimized.

Copy link

@yesmeck yesmeck commented Dec 28, 2017

babel-eslint should upgrade @babel-traverse to 7.0.0-beta.36 #550.

@hzoo Why not move babel-eslint to mono-repo?

yesmeck added a commit to ant-design/ant-design that referenced this issue Dec 28, 2017
Temporary fix for babel/babel-eslint#530
lixiaoyang1992 added a commit to lixiaoyang1992/ant-design that referenced this issue Jan 3, 2018
lixiaoyang1992 added a commit to lixiaoyang1992/ant-design that referenced this issue Jan 3, 2018
yesmeck added a commit to ant-design/ant-design that referenced this issue Jan 9, 2018
@delch

This comment has been minimized.

Copy link

@delch delch commented Feb 6, 2018

how to fix this ? Anybody know ?

@yesmeck

This comment has been minimized.

Copy link

@yesmeck yesmeck commented Feb 6, 2018

@delch Install @babel/types@7.0.0-beta.36 as your project's dependent.

@JustFly1984

This comment has been minimized.

Copy link

@JustFly1984 JustFly1984 commented Feb 22, 2018

I have same issue. The most annoying thing is that it doesn't bubbles for old peaces of code with template literals, but only for fresh once. Can't figure out why. It does creeps me out a lot.

ERROR in ./src/components/common/input-combobox.js
Module build failed: TypeError: Cannot read property 'range' of null
    at SourceCode.getTokenBefore (/media/projects/sites/taximow-static/node_modules/eslint/lib/token-store/index.js:303:18)
    at checkSpacingBefore (/media/projects/sites/taximow-static/node_modules/eslint/lib/rules/template-curly-spacing.js:52:42)
    at TemplateElement (/media/projects/sites/taximow-static/node_modules/eslint/lib/rules/template-curly-spacing.js:117:17)
    at listeners.(anonymous function).forEach.listener (/media/projects/sites/taximow-static/node_modules/eslint/lib/util/safe-emitter.js:47:58)
    at Array.forEach (<anonymous>)
    at Object.emit (/media/projects/sites/taximow-static/node_modules/eslint/lib/util/safe-emitter.js:47:38)
    at NodeEventGenerator.applySelector (/media/projects/sites/taximow-static/node_modules/eslint/lib/util/node-event-generator.js:251:26)
    at NodeEventGenerator.applySelectors (/media/projects/sites/taximow-static/node_modules/eslint/lib/util/node-event-generator.js:280:22)
    at NodeEventGenerator.enterNode (/media/projects/sites/taximow-static/node_modules/eslint/lib/util/node-event-generator.js:294:14)
    at CodePathAnalyzer.enterNode (/media/projects/sites/taximow-static/node_modules/eslint/lib/code-path-analysis/code-path-analyzer.js:608:23)
    at Traverser.enter [as _enter] (/media/projects/sites/taximow-static/node_modules/eslint/lib/linter.js:1006:32)
    at Traverser._traverse (/media/projects/sites/taximow-static/node_modules/eslint/lib/util/traverser.js:132:14)
    at Traverser._traverse (/media/projects/sites/taximow-static/node_modules/eslint/lib/util/traverser.js:144:34)
    at Traverser._traverse (/media/projects/sites/taximow-static/node_modules/eslint/lib/util/traverser.js:147:30)
    at Traverser._traverse (/media/projects/sites/taximow-static/node_modules/eslint/lib/util/traverser.js:147:30)
    at Traverser._traverse (/media/projects/sites/taximow-static/node_modules/es
@JustFly1984

This comment has been minimized.

Copy link

@JustFly1984 JustFly1984 commented Feb 22, 2018

ps I have "@babel/types": "7.0.0-beta.39" in my devDependencies

@darrenklein

This comment has been minimized.

Copy link

@darrenklein darrenklein commented Mar 5, 2018

In case anyone is in a similar situation - this problem occurred for me when I bumped my Node version from 6.11.1 to 8.9.4. I took two steps to resolve this -

  • As recommended by @jasonblanchard, I set my version of babel-eslint to 7.2.3
  • I also manually included @babel/types": "7.0.0-beta.40 in my deps as recommended by @yesmeck, though I'm not certain that this had any effect as it was not enough to resolve the problem on its own.
@arty-name

This comment has been minimized.

Copy link

@arty-name arty-name commented Mar 19, 2018

In my case upgrading to babel-eslint@8.2.2 has solved the problem.

@adamchenwei

This comment has been minimized.

Copy link

@adamchenwei adamchenwei commented Mar 24, 2018

@jasonblanchard confirmed pin pin babel-eslint to 7.2.3. solved the issue!

its good to resolve the issue since that means newer version of this plugin really is nobody using it.. lol

@JustFly1984

This comment has been minimized.

Copy link

@JustFly1984 JustFly1984 commented Mar 29, 2018

I've missed this error for a while, but today I had edited like 20 files using template literals, and I had this error again. Weirdest thing that error appeared in 10 of edited files, and there was no errors for other part of the files. All 20 files are React components, and all 20 fixes had similar pattern. I looked at babel-eslint version, and I had 8.2.2

I've downgraded it to 7.2.3 and errors disappeared.

@kkarkos

This comment has been minimized.

Copy link

@kkarkos kkarkos commented Apr 5, 2018

I tried all suggested fixes from above but I still get the error. The error came out of nowhere after switching branches and coming back to my initial branch. Nothing in package.json had changed. I just removed node_modules and re-installed all with yarn install. I also tried eslint 5.0.0.alpha and different babel-eslint versions. Frustrating, the error is:
[Error - 17:33:23] TypeError: Cannot read property 'range' of null at SourceCode.getTokenBefore (/Users/user/Documents/_Projects/project/node_modules/eslint/lib/token-store/index.js:303:17) at checkSpacingBefore (/Users/user/Documents/_Projects/project/node_modules/eslint/lib/rules/template-curly-spacing.js:52:42) at TemplateElement (/Users/user/Documents/_Projects/project/node_modules/eslint/lib/rules/template-curly-spacing.js:117:17) at listeners.(anonymous function).forEach.listener (/Users/user/Documents/_Projects/project/node_modules/eslint/lib/util/safe-emitter.js:47:58) at Array.forEach (native) at Object.emit (/Users/user/Documents/_Projects/project/node_modules/eslint/lib/util/safe-emitter.js:47:38) at NodeEventGenerator.applySelector (/Users/user/Documents/_Projects/project/node_modules/eslint/lib/util/node-event-generator.js:251:26) at NodeEventGenerator.applySelectors (/Users/user/Documents/_Projects/project/node_modules/eslint/lib/util/node-event-generator.js:280:22) at NodeEventGenerator.enterNode (/Users/user/Documents/_Projects/project/node_modules/eslint/lib/util/node-event-generator.js:294:14) at CodePathAnalyzer.enterNode (/Users/user/Documents/_Projects/project/node_modules/eslint/lib/code-path-analysis/code-path-analyzer.js:608:23) at Traverser.enter [as _enter] (/Users/user/Documents/_Projects/project/node_modules/eslint/lib/linter.js:865:28)

I'm on node 9.5.0

@chhuang

This comment has been minimized.

Copy link

@chhuang chhuang commented Apr 5, 2018

Same error, if I remove the intent rule it's fine.

"rules": {
  "indent": [2, 2]
}
@matthew-dean

This comment has been minimized.

Copy link

@matthew-dean matthew-dean commented Apr 23, 2018

Why is babel-eslint 8.x picking a beta build of @babel/types in the first place? And a very specific, non-bug-fixable version?? https://github.com/babel/babel-eslint/blob/master/package.json#L16 Seems like downgrading to 7.2.3 is definitely a safer option, and downgrading fixed our Jenkins build.

@ryaninvents

This comment has been minimized.

Copy link

@ryaninvents ryaninvents commented May 1, 2018

I managed to fix this problem for myself; our company's internal ESLint config defined "parser": "babel-eslint", but that line also existed in each project's .eslintrc.

Removing the line "parser": "babel-eslint" from each project caused ESLint to rely on the version installed from our config, and fixed the issue. I know npm flattens the dependency tree, so I can't explain why this works; I'm just happy it does work.

@music2code

This comment has been minimized.

Copy link

@music2code music2code commented May 19, 2018

@ryaninvents Worked for me too. Thank you!

@Thomazella

This comment has been minimized.

Copy link

@Thomazella Thomazella commented Oct 2, 2019

@abritinthebay

This comment has been minimized.

Copy link

@abritinthebay abritinthebay commented Oct 3, 2019

Blown away and reinstalled - on latest versions - still throws this error unless I a) turn off template-curly-spacing and b) exclude TemplateLiteral from indent :/

@monsieurnebo

This comment has been minimized.

Copy link

@monsieurnebo monsieurnebo commented Oct 9, 2019

Removing node_modules & package-lock.json before reinstalling all dependencies did the job.

Thanks!

jameshadfield added a commit to nextstrain/auspice that referenced this issue Oct 14, 2019
Upgraded to fix errors. See babel/babel-eslint#530
raduachim added a commit to egendata/example-cv that referenced this issue Oct 23, 2019
raduachim added a commit to egendata/example-cv that referenced this issue Oct 23, 2019
* refactor: 💡 Moved apm adapter code

* fix: 🐛 Updated core-sc dependency

* fix: 🐛 Updated dependencies with major versions

Also fixed linting errors bcs increased eslint OCD

* feat: 🎸 Hooked up instrumentation

Performance of individual crypto actions can now be seen in APM (in
Kibana):x

* chore: 🤖 regenerate package-lock to fix babel-eslint issue

babel/babel-eslint#530 (comment)
egendata-dev pushed a commit to egendata/example-cv that referenced this issue Oct 23, 2019
# [1.1.0](v1.0.0...v1.1.0) (2019-10-23)

### Features

* 🎸 Hooked up instrumentation ([#6](#6)) ([f6448dd](f6448dd)), closes [/github.com/babel/babel-eslint/issues/530#issuecomment-446934009](https://github.com//github.com/babel/babel-eslint/issues/530/issues/issuecomment-446934009)
@EsrefDurna

This comment has been minimized.

Copy link

@EsrefDurna EsrefDurna commented Nov 8, 2019

i tried everything about this is still an issue, can we re open this ?

@kvz

This comment has been minimized.

Copy link

@kvz kvz commented Dec 9, 2019

In my case this fixed it:

rm -f yarn.lock
rm -rf node_modules
yarn add $(npm outdated |awk '/eslint/ {print $1"@"$4}')
yarn

I guess it's bringing a big sledgehammer and could invite more consequences to your code-base than are welcome though (upgraded linting, subtle or not so subtle dependency changes).

@Excalibaard

This comment has been minimized.

Copy link

@Excalibaard Excalibaard commented Dec 9, 2019

Some suggestions described in #681 worked for me in the end.

I initially tried the suggested method with removing yarn.lock and completely re-installing the modules, but the error persisted.

Defining a resolution for @babel/types, so that every package uses the same version, should work to resolve the issue.

package.json

  "devDependencies": {
    "babel-eslint": "10.0.3"
  }
  "resolutions": {
    "@babel/types": "7.7.4"
  }

If that still doesn't work, disable the rule that causes the crash in your eslintrc.json or whichever config file you use:

#681 (comment)

... In my case at least the error is generated during handling of template literals within the indent rule. I can live with not checking indentation for template literals, so I've fixed it for now using the ignoredNodes option for the indent rule, roughly as follows:

"indent": ["error", 2, {
  "ignoredNodes": ["TemplateLiteral"]
}]

Not sure how else I can be of help, except maybe reiterate @ljqx's suggestion of trying to normalize the @babel/types dep across babel and babel-eslint. I can't tell if npm is doing something wrong here or if yarn's more deterministic algorithm is hiding a flaw in the code.

Interestingly, I removed the specific indent rule since, to see if the issue was fixed (resolution still in place). babel-eslint kept working without the rule, even though template literals were present.

@oggo

This comment has been minimized.

Copy link

@oggo oggo commented Dec 9, 2019

Justo to report, i had the same problem with "babel-eslint": "^10.0.3" and "eslint": "^5.16.0" on node 10.17.0 and npm 6.11.3. For me rm -rf node_modules && npm install solved the problem

@dclowd9901

This comment has been minimized.

Copy link

@dclowd9901 dclowd9901 commented Dec 10, 2019

Still having this problem myself. On babel-eslint@10.0.3 and eslint@6.7.2. removing babel-eslint from the parser setting worked to quell the error, but then created a new error around assigning functions to class properties (a la foo = () => {...} in classes)

@yitomok

This comment has been minimized.

Copy link

@yitomok yitomok commented Dec 19, 2019

I think the cause is as what I said here: #799 (comment)

@KempfCreative

This comment has been minimized.

Copy link

@KempfCreative KempfCreative commented Dec 20, 2019

This just started failing for me yesterday.

@romandragan

This comment has been minimized.

Copy link

@romandragan romandragan commented Dec 26, 2019

Same for me, was resolved by:

"@babel/generator": "7.7.4"
@ivan-kleshnin

This comment has been minimized.

Copy link

@ivan-kleshnin ivan-kleshnin commented Dec 31, 2019

None of the above workarounds helped. The bug persists with the latest ESLINT, etc.
Should be reopened IMO.

@Khadosh

This comment has been minimized.

Copy link

@Khadosh Khadosh commented Dec 31, 2019

@ivan-kleshnin do yarn cache clean

@invainn

This comment has been minimized.

Copy link

@invainn invainn commented Jan 2, 2020

+1

@hawkeye64

This comment has been minimized.

Copy link

@hawkeye64 hawkeye64 commented Jan 4, 2020

This works: #799 (comment)

@qiqingfu

This comment has been minimized.

Copy link

@qiqingfu qiqingfu commented Jan 8, 2020

@afc163 解决了, 谢谢

@vemmkof

This comment has been minimized.

Copy link

@vemmkof vemmkof commented Jan 20, 2020

The way that I fix this was running yarn audit to know which dependencies upgrade (babel), upgrade them, delete node_modules folder and file yarn.lock (package-lock.json for npm), then run yarn for install all the project dependencies.

@ksweetie

This comment has been minimized.

Copy link

@ksweetie ksweetie commented Jan 28, 2020

Adding a resolutions as outlined in #799 (comment) worked for me. However, I didn't want to add this to my package.json.

But what also worked for me was to yarn upgrade babel-eslint @babel/types.

@Artur93gev

This comment has been minimized.

Copy link

@Artur93gev Artur93gev commented Feb 4, 2020

Most likely the issue is causing of wrong parsing.

import(`some_path/${variable}`) // issue exists

Fix

import("some_path" + variable) // issue fixed
@AlexWayfer

This comment has been minimized.

Copy link

@AlexWayfer AlexWayfer commented Feb 5, 2020

I confirm, it's broken for such code:

import(`../layouts/${this.name}.vue`)

(the code is from nice article)

Please, reopen the issue. yarn cache clean, rm yarn.lock, rm -rf node_modules, yarn audit don't help.

> yarn audit 
yarn audit v1.21.1
0 vulnerabilities found - Packages audited: 24351
Done in 1.29s.

> yarn outdated
yarn outdated v1.21.1
Done in 1.09s.
AlexWayfer added a commit to AlexWayfer/stream_timer that referenced this issue Feb 5, 2020
@ljharb

This comment has been minimized.

Copy link
Member

@ljharb ljharb commented Feb 5, 2020

This is an ancient issue; please file a new one.

@AlexWayfer

This comment has been minimized.

Copy link

@AlexWayfer AlexWayfer commented Feb 5, 2020

This is an ancient issue; please file a new one.

OK, but I see open #608, #741, #799 and #815, which are pretty similar, so I'll just watch for them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

You can’t perform that action at this time.