Implied globals are not caught as linter errors - javascript

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.

Related

How do I specify the path for ES6 import in Google Closure Compiler?

Using the latest version of Closure Compiler, I can't seem to get ES6 modules to load no matter how I specify the module's path in the import statement.
To test this, I created two very simple files in the same directory.
defaults.js:
const defaults = {color: "#F44336"}
export {defaults}
test.js:
import {defaults} from 'defaults.js'
console.log(defaults.color)
When I just run test.js without attempting to process it with Closure Compiler, it works as expected and imports the module. But trying to compile it produces the error:
test.js:1:0: ERROR - [JSC_JS_MODULE_LOAD_WARNING] Failed to load module "defaults"
This is the command line I'm using:
compiler \
--module_resolution NODE \
--compilation_level ADVANCED \
--language_in ECMASCRIPT_2020 \
--language_out ECMASCRIPT_2020 \
--js test.js
I've tried specifying the path to the module file in test.js as a fully qualified absolute path, and various forms of relative path, and nothing has worked. I also tried adding the module file as a --js option for the compiler, but no dice.
It seems like such a simple thing but I have not found a solution by extensive googling, either. Can anyone tell me what I'm missing?
I recreated your files (note the difference):
defaults.js
const defaults = {color: "#F44336"}
export {defaults}
test.js
import {defaults} from './defaults.js' // relative path!
console.log(defaults.color)
Compilation
You MUST specify all files involved in the compilation with --js.
$ cc --module_resolution NODE --compilation_level ADVANCED --js defaults.js --js test.js
console.log("#F44336");
cc is just an alias to my copy of the Closure compiler
Addendum
I'm pretty sure that your biggest mistake has to be this:
import {defaults} from 'defaults.js'
This was never going to load the file on disk but instead was looking for a NPM package called defaults.js in your dependencies.

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?

I need to fail a build in Visual Studio Online (VSTS) from my gulp linting process

I use JShint and output errors and warnings. I want to fail a build on this result. How can I accomplish this using the VSO build process?
You can add a command line task and build by your private agent on a local machine which jshint is installed.
For the Command Line task, you can use below settings:
Tool: jshint
Arguments: the .js file in your repo, such as gulpfile.js
And you will get the same error message as you used JShint directly, such as the error message in VSO build as:
jshint gulpfile.js
gulpfile.js: line 9, col 11, Duplicate key 'string'.
gulpfile.js: line 11, col 2, Missing semicolon.
gulpfile.js: line 22, col 36, Missing semicolon.
3 errors
Process completed with exit code 2.

ReferenceError: regeneratorRuntime is not defined (but working inside a scope)

I' ve come across this strange occurrence of:
ReferenceError: regeneratorRuntime is not defined
... which I've managed to reproduce in a very minimal setting (compared to similar SO questions on the same issue), and also noticed some weird behavior depending on whether scopes are used.
The following code works:
'use strict';
require('babel-polyfill');
{ // scope A (if you remove it you observe different behavior when .babelrc is present)
function *simplestIterator() {
yield 42;
}
for (let v of simplestIterator()) {
console.log(v);
}
}
Packages are:
$ npm ls --depth 0
simple-babel-serverside-node-only-archetype#1.0.0 /home/mperdikeas/regeneratorRuntimeNotDefined
├── babel-cli#6.7.5
├── babel-core#6.7.6
├── babel-polyfill#6.7.4
├── babel-preset-es2016#6.0.11
└── babel-runtime#6.6.1
Contents of .babelrc are:
$ cat .babelrc
{
"presets": ["es2016"]
}
However, when the scope is removed and the simplestIterator is placed on the global scope it fails with:
ReferenceError: regeneratorRuntime is not defined
Even more strangely, if the .babelrc file is removed/renamed the code succeeds whether the scope is present or not. BTW whether it is scope or an IIFE that encapsulates the generator makes no difference.
Minimal github repo demonstrating this behavior here.
To observe the behavior:
git clone https://github.com/mperdikeas/regeneratorRuntimeNotDefined.git
cd regeneratorRuntimeNotDefined/
npm install
npm run build && npm run start
The above will output 42 on the console. Now remove the scope and see what happens. Then rename .babelrc to see it working again (with or without scope).
My questions are:
why does the es2016 Babel preset trigger this error
why does putting the generator in a scope solves the problem?
update
Based on the accepted answer, and since this was code for a module I was writing I ended up doing:
require('babel-polyfill');
module.exports = require('./app.js');
Babel assumes that the polyfill will be loaded before anything else in your application, but you're using a function declaration, which is hoisted, meaning that it exists and is usable before require has been called.
In the case of generators, then need regeneratorRuntime which is provided by the polyfill, but the polyfill hasn't loaded when the regenerator is initialized.
The Babel team's recommendation is to make two files:
index.js
require('babel-polyfill');
require('./app');
Also you could do the following with es2015 preset and transform-regenerator plugin:
.babelrc
{
"presets": ["es2015"],
'plugins': [
'transform-regenerator'
]
}
Code
let regeneratorRuntime = require("regenerator-runtime");
// You code with ES6 generators
P.S.
Of course you should install babel-plugin-transform-regenerator npm package.
I know this has been answered but, unfortunately, they didn't fix the problem for me.
what solved it was to import babel babel-polyfills inside the file
import "core-js/stable";
import "regenerator-runtime/runtime";
you can find it on the official babeljs documetation orat this article

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

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.

Categories

Resources