Adding the 'node' environment to my .eslintrc has unexpected behavior - javascript

I'm running eslint v1.8.0 against this test.js file:
require('fs');
var a = 1;
At first, my .eslintrc file is blank:
{
}
Running eslint test.js returns:
1:1 error "require" is not defined no-undef
1:9 error Strings must use doublequote quotes
2:5 error "a" is defined but never used no-unused-vars
This is a node app, though, so I need to tweak it a bit. Running eslint --env node test.js returns:
1:9 error Strings must use doublequote quotes
2:5 error "a" is defined but never used no-unused-vars
Perfect, that's exactly what I want. So I modify my .eslintrc file to be:
{
"env": {
"node": true
}
}
When I run estlint test.js file now, it returns nothing at all. Why does adding this to my .eslintrc remove the quotes and no-unused-vars warnings?

After eslint 1.0.0, all the rules by default are off. So if you run eslint with no rules on you should get 0 results. This tells me that you might have .eslintrc file some where in the folder chain up or down which is getting picked up. Run eslint with --debug flag to understand where are the settings getting picked from.

Related

process.env throws Uncaught ReferenceError but process.env.NODE_ENV does not

In my client-side javascript file if I run the following I get no error:
console.log(process.env.NODE_ENV); // outputs development or production
But if I run the following then I get an error:
console.log(process.env) // Uncaught ReferenceError: process is not defined
Why is this happening? Using Webpack 5 for bundling.
Because you're using a bundler that replaces process.env.NODE_ENV with another value.
For Webpack, it's DefinePlugin doing the dirty work (and setting the Webpack --mode option automatically adds the plugin); for e.g. esbuild it's define.
Your resulting bundled JavaScript will not have process.env.NODE_ENV at all, just a static string "production" or "development". However, just process.env will not be replaced (unless configured so), so you're getting a reference error.

Setting file extensions in ESLint configuration using overrides

I'm trying to specify the file extensions that ESLint should check by setting overrides in the configuration rather than needing to use the --ext command line flag. The documentation for that says:
By default, ESLint lints *.js files and the files that match the overrides entries of your configuration.
The documentation for overrides says that glob patterns for files can be along the lines of "src/*.js" or "**/*.js" and that these are relative to the base directory of your ESLint configuration file. Neither of these options are working for me to make ESLint process files beyond the default of *.js (in my case *.jsx).
Here's the output when I run ESLint with --ext:
scott#dev /home/scott/project (main)
$ npx eslint --ext .js,.jsx ./src
/home/scott/project/src/file.jsx
1:1 warning Unexpected console statement no-console
✖ 1 problem (0 errors, 1 warning)
As expected, it produces a warning. But without --ext, ESLint doesn't produce any output.
The directory structure:
/home/scott/project/
.eslintrc.js
src/
file.jsx
.eslintrc.js:
const rules = {
// ...
};
module.exports = {
// ...
overrides: [
{
files: [ "*.jsx" ], // also doesn't work with "src/**/*.jsx", "**/*.jsx"
rules: rules
}
],
rules: rules
};
What do I have to do?

Definition for rule '#typescript-eslint/no-implicit-any' was not found with Storybook js

I am getting this error when I make changes to the generated code from running Storybook.js. These are the instructions I am following
https://gist.github.com/shilman/bc9cbedb2a7efb5ec6710337cbd20c0c
But because I am adding StorybookJS to an existing project, the only commands I've ran are:
$ npx -p #storybook/cli#next sb init --story-format=csf-ts
$ yarn add #storybook/addon-docs#next --dev
Running yarn storybook produces this error.
ERROR in ./src/stories/0-Welcome.stories.tsx
Module Error (from ./node_modules/eslint-loader/dist/cjs.js):
Line 5:1: Definition for rule '#typescript-eslint/no-implicit-any' was not found #typescript-eslint/no-implicit-any
Line 24:1: Definition for rule '#typescript-eslint/no-implicit-any' was not found #typescript-eslint/no-implicit-any
The storybook server runs fine but as soon as I make changes to some text and Storybook tries to reload the changes, I get the error seen above.
There is a lot of magic that happens behind the scenes of create-react-app so I am unaware of how to pinpoint and fix this error. My guess is that a eslint typescript rule is missing so upon Storybook reloading any type of change, the missing rule error occurs
It sounds like you're missing the .eslintrc.json file which describes the rules. You can read more about the file format and options here: https://eslint.org/docs/user-guide/configuring
Example:
{
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"rules": {
"semi": "error"
}
}
If the error still occurs, try configuring the no-implicit-any rule.

ESLINT errors ( Error '$' is not defined)

I have Brackets installed, and i getting ESLINT error, as far as i know i dont have ESLINT installed
That is the first time i get this error, my question is, how do i install ESLINT and configure it that i don't receive the errors. ( The JavaScript code is copied from CODEPEN so it should be ok)
The main problem is ESLINt, i tried with other JS plugins and get the same error, as i said its the first time im getting this error.
You need to add the '$' to the globals object inside your .eslintrc file.
Like so:
"eslintConfig": {
"globals": {
"$": true
}
}
You need to configure it to allow globals:
https://eslint.org/docs/user-guide/configuring#specifying-globals
Either comment on top of the file
/* global $ */
or in the config

Implied globals are not caught as linter errors

When I create a new file in a directory, jshint doesn't catch "implied globals" error. There's no such issue with the other files in the directory.
My file has just one line
console.log(hohoho);
and here's the linter output
$ jshint --verbose --show-non-errors foo.js
foo.js :
Implied globals:
hohoho: 1
For the other files I see a problem:
$ jshint --verbose asset.js
asset.js: line 6, col 13, 'hohoho' is not defined. (W117)
2 errors
My .jshintrc doesn't mention any explicit files. Any ideas?
Just needed to add a 'use strict'; line at the top of the file to catch the globals issue.

Categories

Resources