babel: Template string failing with Cannot read property 'range' of null
It throws error when dynamic import, template literal inline and rule template-curly-spacing
as follow:
Tested on:
babel-eslint@10.0.3
(with @babel/parser@7.7.7
)
eslint@6.7.2
Sample code:
import(`${'package.json'}`)
.eslintrc.json
{
"parser": "babel-eslint",
"rules": {
"template-curly-spacing": ["error", "never"]
}
}
The problem is coming from @babel/parser@7.7.7
, which added ImportExpression
.
_Originally posted by @yitomok in https://github.com/babel/babel-eslint/issues/799#issuecomment-567598343_
Edits by @JLHwung
If you come across this issue from search engine, see below for the solution:
- uninstall
babel-eslint
- install
@babel/eslint-parser
- replace
babel-eslint
by@babel/eslint-parser
in your ESLint config - if you use
eslint-plugin-babel
, do 4.a - 4.c, otherwise skip it 4.a uninstalleslint-plugin-babel
4.b install@babel/eslint-plugin
4.c replace"babel"
or"eslint-plugin-babel"
to"@babel"
- The working ESLint config should look like https://github.com/babel/babel/issues/10904#issuecomment-691827355
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 60
- Comments: 28 (7 by maintainers)
Commits related to this issue
- Fixed issue with dependency latest version breaking build [ref](https://github.com/babel/babel/issues/10904) — committed to msamprz/deriv-app by msamprz 4 years ago
- Downgrade babel-eslint For reasons that are not entirely clear to me, recent versions of eslint break when trying to parse inline template literals. The issue can be tracked https://github.com/babel... — committed to tvkitchen/countertop by slifty 4 years ago
- Downgrade babel-eslint For reasons that are not entirely clear to me, recent versions of eslint break when trying to parse inline template literals. The issue can be tracked https://github.com/babel... — committed to tvkitchen/countertop by slifty 4 years ago
- Replace babel-eslint with @babel/eslint-parser as recommended in https://github.com/babel/babel/issues/10904 — committed to brophdawg11/vue-ssr-build by urbnjamesmi1 4 years ago
- chore(deps): babel-eslint ➡ @babel/eslint-parser To fix problems with linting. See https://github.com/babel/babel/issues/10904\#issuecomment-767275502 and https://app.circleci.com/pipelines/github/... — committed to cypress-io/cypress by flotwig 3 years ago
- chore: upgrade babel eslint-parser Upgrade eslint parser for having this bug fixed: https://github.com/babel/babel/issues/10904 — committed to tocco/tocco-client by izzappel 3 years ago
- chore: upgrade babel eslint-parser Upgrade eslint parser for having this bug fixed: https://github.com/babel/babel/issues/10904 — committed to tocco/tocco-client by izzappel 3 years ago
FYI; This is still broken.
package versions eslint@7.13.0 @babel/eslint-parser@7.12.1
Have to selectively disable ESLint rules to skirt the problem, like so:
or ESLint still blows up with:
Here, inside
template-curly-spacing
thesourceCode.getFirstToken
call ends up returningnull
:[EDIT]
Looks like this is STILL the problem with a wrong version of
@babel/types
being used. After a thorough wipe and reinstall of all things@babel
(asnpm dedup
was deduping to the WRONG version) it does work.I though the whole idea of having the eslint parser go into the mono repo was to maintain it together with Babel and prevent exactly that shitty issue?
Fixed in
@babel/eslint-parser@7.11.3
.This should be fixed in the latest v11 prerelease (and will be included in the next major release, which will be under a new package name).
How should fix, upgrading packages?? or Should wait some new release? I upgrade to @babel/eslint-parser@7.11.3 but I still have these problem. Thanks !
use @babel/eslint-parser, because
I found this minimum configuration helpful (with unrelated or too specific pieces removed):
package.json
.babelrc (required)
.eslintrc
Thanks @LexSwed, It seems that was the problem in my case as well. I needed to run
yarn-deduplicate yarn.lock
after installing@babel/eslint-parser@7.11.3
to get it to work.“babel-eslint”: “10.1.0” OR “@babel/eslint-parser”: “^7.11.0”, “eslint”: “7.6.0”,
The
babel-eslint
parser can be used byeslint
to allow it to parse JS code using babel plugins which are in proposal stage.I ran in to this particular issue with
babel-eslint
when it tried to parse the following code with optional chaining:The eslint doesn’t throw the error if the optional chaining is removed from ☝️.
A log at https://github.com/babel/babel-eslint/blob/10.x/lib/babylon-to-espree/toAST.js#L101 or https://github.com/babel/babel/blob/master/eslint/babel-eslint-parser/src/convert/convertAST.js#L64 to log the node object shows that the following node is missing when optional chaining is present.
I believe that these nodes are generated by the “@babel/parser” library. Is it possible that the issue is in the parser lib?
when will this release? use 8.x babel-eslint will result in other bugs…
Try switching to
@babel/eslint-parser
.https://babeljs.io/blog/2020/07/13/the-state-of-babel-eslint
@EarthyOrange Thanks for digging into this! I’ll look into this with this new information (though if you want to continue investigating, feel free). This could be a bug in
@babel/parser
’s logic (most likely in theestree
plugin) or it could be a bug in@babel/eslint-parser
, which serves as the compatibility layer between ESLint and Babel (and does perform some mutations of the AST).