eslint error cannot read property 'range' of null - node-modules

I have had the same eslint issue for days now.
Everyone on the team has the same eslintrc & installed version of eslint. Their eslint works fine, mine does not.
I have tried restarting my computer, deleting node_modules, deleting anything under my user (in home directory). Nothing works.
Issue:
./node_modules/.bin/eslint *.js*
 1 ↵  11504  10:19:47
Cannot read property 'range' of null
TypeError: Cannot read property 'range' of null
at SourceCode.getTokenBefore (/Users/jhill/gitRepo/sponsoroo/node_modules/eslint/lib/token-store/index.js:303:18)
at checkSpacingBefore (/Users/jhill/gitRepo/sponsoroo/node_modules/eslint/lib/rules/template-curly-spacing.js:52:42)
at TemplateElement (/Users/jhill/gitRepo/sponsoroo/node_modules/eslint/lib/rules/template-curly-spacing.js:117:17)
at listeners.(anonymous function).forEach.listener (/Users/jhill/gitRepo/sponsoroo/node_modules/eslint/lib/util/safe-emitter.js:47:58)
at Array.forEach (<anonymous>)
at Object.emit (/Users/jhill/gitRepo/sponsoroo/node_modules/eslint/lib/util/safe-emitter.js:47:38)
at NodeEventGenerator.applySelector (/Users/jhill/gitRepo/sponsoroo/node_modules/eslint/lib/util/node-event-generator.js:251:26)
at NodeEventGenerator.applySelectors (/Users/jhill/gitRepo/sponsoroo/node_modules/eslint/lib/util/node-event-generator.js:280:22)
at NodeEventGenerator.enterNode (/Users/jhill/gitRepo/sponsoroo/node_modules/eslint/lib/util/node-event-generator.js:294:14)
at CodePathAnalyzer.enterNode (/Users/jhill/gitRepo/sponsoroo/node_modules/eslint/lib/code-path-analysis/code-path-analyzer.js:608:23)
Details
Version:
./node_modules/.bin/eslint --version
v4.16.0
.eslintrc
{
"extends": ["airbnb-base", "plugin:security/recommended"],
"rules": {
"import/prefer-default-export": "off",
"no-console": "off",
"class-methods-use-this": "off",
"global-require": "off",
"consistent-return": "off",
"import/no-extraneous-dependencies": ["error", {"devDependencies": true}],
/* JSX */
"jsx-quotes": [2, "prefer-double"],
/* React */
"react/display-name": 1,
"react/jsx-boolean-value": 1,
"react/jsx-no-duplicate-props": 1,
"react/jsx-no-undef": 1,
"react/jsx-quotes": 0,
"react/jsx-sort-props": 0,
"react/jsx-uses-react": 1,
"react/jsx-uses-vars": 1,
"react/no-danger": 1,
"react/no-did-mount-set-state": 1,
"react/no-did-update-set-state": 1,
"react/no-multi-comp": 1,
"react/no-unknown-property": 1,
"react/prop-types": 1,
"react/react-in-jsx-scope": 1,
"react/self-closing-comp": 1,
"react/sort-comp": 1,
"react/wrap-multilines": 0,
/* Security */
"security/detect-non-literal-fs-filename": 2,
"security/detect-non-literal-regexp": 2,
"security/detect-unsafe-regex": 2,
"security/detect-buffer-noassert": 2,
"security/detect-child-process": 2,
"security/detect-disable-mustache-escape": 2,
"security/detect-eval-with-expression": 2,
"security/detect-no-csrf-before-method-override": 2,
"security/detect-non-literal-require": 2,
"security/detect-object-injection": 2,
"security/detect-possible-timing-attacks": 1,
"security/detect-pseudoRandomBytes": 2,
"no-unsafe-innerhtml/no-unsafe-innerhtml": 2
},
"parser": "babel-eslint",
"plugins": [
"babel",
"react",
"security",
"no-unsafe-innerhtml"
],
"env": {
"jest": true,
"browser": true,
"es6": true,
"node": true
},
"settings": {
"import/extensions": {
"webpack": {
"config": "./webpack.config.react.js"
}
}
}
}
Also, if I delete "parser": "babel-eslint" from my .eslinrc, the problem/erorr goes away, but my eslint doesn't work. I am using ES6 syntax.
UPDATE:
My solution was to use yarn instead of npm. Has solved the problem for the time being.

These .eslintrc rules fixed the problem for me:
rules : {
"template-curly-spacing" : "off",
"indent": ["error", 2, {
"ignoredNodes": ["TemplateLiteral"]
}]
}

babel-eslint has been deprecated in favor of #babel/eslint-parser.
read here
Here are steps to upgrade from babel-eslint to #babel/eslint-parser:
replace babel-eslint with #babel/eslint-parser:
npm
npm uninstall babel-eslint babel-eslint-plugin
npm install --save-dev #babel/eslint-parser #babel/eslint-plugin
Yarn
yarn remove babel-eslint babel-eslint-plugin
yarn add --dev #babel/eslint-parser #babel/eslint-plugin
Upgrade .eslintrc.js:
module.exports = {
--- parser: "babel-eslint",
+++ parser: "#babel/eslint-parser"
plugins: [
--- "babel"
+++ "#babel
]
};

As #Vasan mentioned, specifically, on issue 530, this was the comment that helped me to solve the issue:
Most likely the issue is causing of wrong parsing.
import(`some_path/${variable}`) // issue exists
Fix
import("some_path" + variable) // issue fixed

Fix the version of babel-eslint to 7.2.3 or 8.0.1 by running:
npm install --save-dev babel-eslint#7.2.3
Or:
npm install --save-dev babel-eslint#8.0.1

I added only two rules in .eslintrc.js file and it resolved for me.
rules : {
"template-curly-spacing" : "off",
indent : "off"
}

Expanding off #SergeyP's answer above.
It's worth noting that you might need to set the requireConfigFile flag to false in your parserOptions if, like me, you get a Parsing error: No Babel config file detected error after following his instructions.
module.exports = {
parser: '#babel/eslint',
parserOptions: {
requireConfigFile: false,
},
// ...snip...
}
There is further information here in regards to this issue expanding on this.
https://github.com/babel/ember-cli-babel/issues/366

This is caused most probably due to indentation in Template Literals ,so to fix it just add below in your .eslintrc
"indent": ["error", 2, {
"ignoredNodes": ["TemplateLiteral"]
}]

For me a warning was showing for each string literal (e.g some text ${someVariable}) and installing these two packages solved my problem:
npm i #babel/types #babel/traverse --save-dev

Related

How to auto-correct Ruby linting errors in VS Code (in 2023)

I'm using VS Code and writing Ruby code on MacOS 12.6. I have installed several extensions for linting. I can see the errors, but I cannot get any of them to provide automatic correction, even for trivial mistakes like single vs double quotes. It always says "no quick fixes available".
If I use the command palette to run "Format document" it will correct these kinds of mistakes. So somehow VS Code knows how to fix these problems. It just won't do it in a convenient way.
You can see in the screenshot which extensions I have installed / active:
Ruby by Peng Lv
VSCode Ruby by Stafford Bunk
Ruby Solargraph by Castwide
ruby-rubocop by misogi
I don't really care which extensions are used. I would be happy with any working configuration that provides this basic functionality.
Here is my entire settings.json
{
"workbench.tree.indent": 16,
"editor.formatOnSaveMode": "modifications",
"editor.formatOnSaveTimeout": 5000,
// "ruby.rubocop.onSave": true,
"ruby.useBundler": true, //run non-lint commands with bundle exec
"ruby.useLanguageServer": true, // use the internal language server (see below)
"ruby.lint": {
"rubocop": {
"useBundler": true // enable rubocop via bundler
},
"reek": {
"useBundler": true // enable reek via bundler
}
},
"ruby.format": "rubocop", // use rubocop for formatting
"eslint.format.enable": true,
"eslint.options": {
"extensions": [ ".html", ".js", ".vue", ".jsx" ]
},
"eslint.validate": [
"html",
"javascript",
"vue"
],
"[vue]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
// "[ruby]": {
// "editor.defaultFormatter": "misogi.ruby-rubocop",
// "editor.formatOnSave": true
// },
"[json]": {},
"ruby.codeCompletion": "rcodetools",
"ruby.intellisense": "rubyLocate",
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.tabSize": 2,
"[ruby]": {
"editor.defaultFormatter": "misogi.ruby-rubocop"
},
"editor.formatOnSave": true,
"ruby.rubocop.useBundler": true,
}
Note
This question sounds similar to: vscode( vscode-ruby + rubocop ) how to auto correct on save?
But that question is old and the answers reference config settings that aren’t recognized anymore.
There is a revived Rubocop for Visual Studio Code extension by Loran Kloeze available on the Visual Studio Marketplace.
This extension is a fork of the original Rubocop extension and adds a few features, such as Rubocop server support for improved performance and specifically, a feature to Implement quick fixes for Rubocop.
Even with the original extension by m1sogi, you should have been able to use the global auto correct function. The new quick fix feature is probably more useful though...

Gatsby Develop Failing using gatsby-plugin-sass

After installing the gatsby-plugin-sass module:
When I try to run gatsby build, I get the following error:
ERROR
Unknown error from PostCSS plugin. Your current PostCSS version is 6.0.23, but autoprefixer uses 7.0.26. Perhaps this is the source of the error below.
ERROR #98123 WEBPACK
Generating development JavaScript bundle failed
Browser queries must be an array or string. Got object.
File: src/indexs.sass
failed Building development bundle - 9.200s
I have been working on a resolution to this for hours. I have tried:
custom webpack rules in gatsby-node.js for sass files
reading, re-reading, and re-re-reading the instruction on gatsby's site
updating PostCSS using npm in every way I know how
So far, nothing has worked.
Why is it so complicated to get sass working with gatsby? When the documentation on gatsby's site makes it seem so easy?
Any suggestions what I can do to get this working?
in gatsby-node.js:
exports.onCreateWebpackConfig = ({
stage,
rules,
loaders,
plugins,
actions,
}) => {
// console.log('rules',rules)
// console.log('rules.css',rules.css())
// console.log('rules',rules.postcss())
actions.setWebpackConfig({
module: {
rules: [
{
test: /\.s[ac]ss$/i,
use: [
// Creates `style` nodes from JS strings
'style-loader',
// Translates CSS into CommonJS
'css-loader',
// Compiles Sass to CSS
'sass-loader',
],
},
],
},
plugins: [
plugins.define({
__DEVELOPMENT__: stage === `develop` || stage === `develop-html`,
}),
],
})
}
In gatsby-config.js:
{
resolve: `gatsby-plugin-postcss`,
options: {
postCssPlugins: [require(`postcss-preset-env`)({ stage: 0 })],
},
},
`gatsby-plugin-sass`,
the sass import line in gatsby-browser.js:
import "./src/indexs.sass"
Using sass instead of node-sass saved my day.
remove node-sass
npm uninstall node-sass
or
yarn remove node-sass
and add sass aka dart-sass
npm install --save-dev sass
or
yarn add sass
then edit gatsby-config.js
plugins: [
{
resolve: `gatsby-plugin-sass`,
options: {
implementation: require("sass"),
},
},
]
now run gatsby develop
:)
I'm a bit late to the party but hopefully this might help someone.
I have Sass setup and working with Gatsby without to much extra config required.
Install both node-sass and gatsby-plugin-sass via npm.
npm install --save node-sass gatsby-plugin-sass
Include gatsby-plugin-sass in your gatsby-config.js file in plugins: [] as below with any other Gatsby plugins you maybe using.
module.exports = {
siteMetadata: {
title: `#`,
description: `#`,
author: `#`,
},
plugins: [
`gatsby-plugin-sass`,
],
}
Write your styles as .sass or .scss files and import your main styles.scss (or whatever you prefer to name it) either in your main Layout.js file or gatsby-browser.js file as below using the path to the location of your styles.scss file.
import "./src/styles/styles.scss"
I hope this works for you but if you have any other trouble add a comment and I'll try to reply back.
I hope someone chimes in on this to show how exactly to set up gatsbys sass plugin. I could not get it to work at all.
But I did find a workaround in my case:
I removed gatsby-plugin-sass from the plugins array in gatsby-config.js, turning it off (but I did not uninstall it using npm)
I installed postcss-node-sass and postcss
I added this info to the plugins array in gatsby-config.js:
{
resolve: `gatsby-plugin-postcss`,
options: {
postCssPlugins: [
require(`postcss-preset-env`)({ stage: 0 }),
require(`postcss-node-sass`)(),
],
},
},
I added a custom rule for webpack in gatsby-node.js:
exports.onCreateWebpackConfig = ({
stage,
rules,
loaders,
plugins,
actions,
}) => {
// console.log('rules',rules)
// console.log('rules.css',rules.css())
// console.log('rules',rules.postcss())
actions.setWebpackConfig({
module: {
rules: [
{
test: /\.s[ac]ss$/i,
use: [
// Creates `style` nodes from JS strings
'style-loader',
// Translates CSS into CommonJS
'css-loader',
// Compiles Sass to CSS
'sass-loader',
],
},
],
},
plugins: [
plugins.define({
__DEVELOPMENT__: stage === `develop` || stage === `develop-html`,
}),
],
})
}

karma-typescript: import JS file with Async keyword

I'm using karma-typescript, with this karma config file :
karmaTypescriptConfig: {
compilerOptions: {
target: "es5",
lib: ["dom", "es2015", "es2017"]
},
bundlerOptions: {
transforms: [require("karma-typescript-es6-transform")()]
}
},
In my spec files, I have this code :
import {} from './local/lib.js'
In my lib.js, I have this code :
async function() {}
When executing my tests with npm test, I have this error :
ERROR [source-reader.karma-typescript] Error parsing code: Unexpected token (X:Y) in /local/lib.js
If I remove the async keyword, everything is alright.
How can I edit my karma config file to fix the error ?
According to an issue in the Github of the karma-typescript package (https://github.com/monounity/karma-typescript/issues/344), there is an undocumented flag which may help you test code which contains ES2017 code:
karmaTypescriptConfig: {
compilerOptions: {
target: "es5",
lib: ["dom", "es2015", "es2017"]
},
bundlerOptions: {
acornOptions: {
ecmaVersion: 8,
},
transforms: [require("karma-typescript-es6-transform")()]
}
},
This flag made appear our issues with the async keyword. However, there is still an issue with the spread syntax (...array) in our code, even using this flag. If anyone knows an answer how to also fix that, I will happily extend my answer.

Angular2-Seed + Typings + D3: Import error, 'Cannot find module 'd3''

I'm trying to import D3 so that I can use it in an Angular2 module.
Some background info:
I'm writing Angular2 in TypeScript.
I am using Angular2-seed
What I did:
I installed the NPM D3 package:
npm install d3 --save
I installed the D3 type descriptions using Typings, as that is what Angular2-Seed uses for the libraries it already has installed.
typings install d3 --save
Then, in my Angular2 module file, I added the import statement
import * as d3 from 'd3';
The result is that TSC is giving me the error message "Cannot find module 'd3'". What am I missing or doing wrong?
So if in package.json you already have a dependency for like:
"dependencies": {
...
"d3": "^3.5.17",
...
}
you then can go in /tools/config/seed.config.ts and add
'd3': '${this.NPM_BASE}d3/d3.min.js' in SYSTEM_CONFIG_DEV object, like:
protected SYSTEM_CONFIG_DEV: any = {
defaultJSExtensions: true,
packageConfigPaths: [
`${this.NPM_BASE}*/package.json`,
`${this.NPM_BASE}**/package.json`,
`${this.NPM_BASE}#angular/*/package.json`
],
paths: {
[this.BOOTSTRAP_MODULE]: `${this.APP_BASE}${this.BOOTSTRAP_MODULE}`,
'#angular/core': `${this.NPM_BASE}#angular/core/bundles/core.umd.js`,
'#angular/common': `${this.NPM_BASE}#angular/common/bundles/common.umd.js`,
'#angular/compiler': `${this.NPM_BASE}#angular/compiler/bundles/compiler.umd.js`,
'#angular/forms': `${this.NPM_BASE}#angular/forms/bundles/forms.umd.js`,
'#angular/http': `${this.NPM_BASE}#angular/http/bundles/http.umd.js`,
'#angular/router': `${this.NPM_BASE}#angular/router/index.js`,
'#angular/platform-browser': `${this.NPM_BASE}#angular/platform-browser/bundles/platform-browser.umd.js`,
'#angular/platform-browser-dynamic': `${this.NPM_BASE}#angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js`,
'rxjs/*': `${this.NPM_BASE}rxjs/*`,
'd3': '${this.NPM_BASE}d3/d3.min.js',
'app/*': `/app/*`,
'*': `${this.NPM_BASE}*`
},
packages: {
rxjs: { defaultExtension: false }
}
Let me know if it help. Thanks!
I had the same issue, and the above answer helped for debugging my soltution - in that it identified it was a config issue, however using angular2#2.1.0 i had to update {root}/e2e/tscconfig.json (by adding:
"types": [
"d3"
]
as follows:
{
"compileOnSave": false,
"compilerOptions": {
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"module": "commonjs",
"moduleResolution": "node",
"outDir": "../dist/out-tsc-e2e",
"sourceMap": true,
"target": "es5",
"typeRoots": [
"../node_modules/#types"
],
"types": [
"d3"
]
}
}
Keep in mind that there is a tscconfig.json in {root}/src/ as well. I updated in this and I still had a dependency issue with:
import * as D3 from 'd3';
in my component that through the error. Hope this helps at least one person!
For Angular 5+
you can just do:
npm i -g typings
npm i d3#3.5.5 --save
typings install d3=github:DefinitelyTyped/DefinitelyTyped/d3/d3.d.ts#6e2f2280ef16ef277049d0ce8583af167d586c59 --global --save

Compass not compiling in sublime text 2

Getting the following error when I'm trying to save my scss files:
File to import not found or unreadable: compass. (Sass::SyntaxError)
My Sass builder config:
{
"output_path": "../css",
"options": {
"cache": true,
"debug": true,
"line-comments": true,
"line-numbers": true,
"style": "nested"
}
}
Installed the following packages with my Sublime Text 2:
https://github.com/bnlucas/SassBuilder
https://github.com/WhatWeDo/Sublime-Text-2-Compass-Build-System
So the question is, what am I doing wrong here? Do I need to add something in my build config?
Thanks in advance!

Resources