I have set up the basic Vue.js 2 Quickstart app. One of the things I wanted was eslint. It's nice, but as a Java developer not having a semicolin at the end of the line is blasphemy. So I add this rule like this to my .eslintrc.js file:
// add your custom rules here
'rules': {
'semi': "always",
// allow paren-less arrow functions
'arrow-parens': 0,
// allow async-await
'generator-star-spacing': 0,
// allow debugger during development
'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0
}
However, when I save it, I am getting this error:
Error: /asdf/code/vue/.eslintrc.js: Configuration for rule "semi" is invalid: Severity should be one of the following: 0 = off, 1 = warn, 2 = error (you passed '"always"').
which does not match the documentation here: ESLint Rules
I am using Atom if that makes a difference.
I did try an use a "0" but that did not actually fix atom telling me there is a semi-colon everywhere.
Related
I am using eslint to set up my node js project but I keep getting the following warnings:
Now in my .eslint.json I am unable to find a rule/technique to help ignore these new tags and types. I have looked in the following documentation:
https://eslint.org/docs/rules/
I assume that you are also using https://github.com/gajus/eslint-plugin-jsdoc
If so then you have to add this rule to your eslint config file:
"jsdoc/check-tag-names": ["error", { "definedTags": ["consumes", "produces", "route"] }],
and whatever custom tags you also have in your docs...
I recently started using the eslint module to help clean-up some JavaScript files. The following error is being reported when I lint one of my files:
127:17 error Unexpected alias 'me' for 'this' consistent-this
After checking the documentation, I understand the error is being reported because my code is assigning the value of this to a variable named me instead of that.
What is the proper way to configure my project's .eslintrc.json to make it so the following line of code is not reported as an error: var me = this;?
The rule should be like this in your .eslintrc
{
"rules" : {
"consistent-this": ["error", "me"]
}
}
I am trying to integrate the stackdriver-error-js library into my Vue project as a module.
The code and the setup:
in package.json
"stackdriver-errors-js": "^0.2.0"
in bootstrap.js
import errorHandler from './error/error-reporting';
in error-reporting.js
import { StackdriverErrorReporter } from 'stackdriver-errors-js';
let errorHandler;
errorHandler = new StackdriverErrorReporter();
errorHandler.start({
key: "{{.Config.StackDriverApiKey}}",
projectId: "{{.Config.StackDriverProject}}",
service: "{{.Config.GoogleCloudProjectID}}",
version: "{{.Copacknfig.GaeEnv}}",
disabled: false
});
export default errorHandler;
The actual error
The error I got now is (console output and test)
[vue-devtools] Ready. Detected Vue v2.4.2
(function testErrorReporting() {window.onerror(null, null, null, null, new Error('Test: Something broke!'));})();
stackdriver-errors.js:109 Uncaught ReferenceError: StackTrace is not defined
at StackdriverErrorReporter.webpackJsonp.556.StackdriverErrorReporter.report (stackdriver-errors.js:109)
at window.onerror (stackdriver-errors.js:67)
at testErrorReporting (<anonymous>:1:40)
at <anonymous>:1:111
and line (stackdriver-errors.js:109)
...
StackTrace.fromError(err).then(function(stack){
...
If you do not load the stackdriver-errors-concat.min.js file, you also manually need to also the stacktrace-js module.
stackdriver-errors expects a StackTrace object to be present.
Since the library you want to use is experimental, and therefore cannot be used in a production environment, it would be better to use a different library which has been tested and validated for production use.
I suggest using this other library instead, which includes features related to Stackdriver error reporting for Node.js and JavaScript.
First of all, install the dependency by running this command:
npm install --save #google-cloud/error-reporting
This will add the dependency automatically to package.json.
In error-reporting.js, you can add the dependencyby adding this to your code (All the parameters are optional):
var errors = require('#google-cloud/error-reporting')({
projectId: 'my-project-id',
keyFilename: '/path/to/keyfile.json',
credentials: require('./path/to/keyfile.json'),
// if true library will attempt to report errors to the service regardless
// of the value of NODE_ENV
// defaults to false
ignoreEnvironmentCheck: false,
// determines the logging level internal to the library; levels range 0-5
// where 0 indicates no logs should be reported and 5 indicates all logs
// should be reported
// defaults to 2 (warnings)
logLevel: 2,
// determines whether or not unhandled rejections are reported to the
// error-reporting console
reportUnhandledRejections: true,
serviceContext: {
service: 'my-service',
version: 'my-service-version'
}
});
After that, use this code to test if the error is properly reported by Stackdriver:
errors.report(new Error('Something broke!'));
Please be aware that this library is currently on a beta stage, so there might be some changes to it in the future.
I am using Grunt as my Build Tool and ESLint as my linting tool for an app I am working on. I am also using the Underscore Node package, and have made use of it in my app. Unfortunately, when I run ESLint on my code, it thinks that _ is an undefined variable in the following line:
return _.pluck(objects, nameColumn);
This is the error it is giving me:
78:21 error "_" is not defined no-undef
I would prefer not to disable the no-undef rule for ESLint, and I have tried installing the Underscore plugin, but I am still receiving this error. If anyone else has any ideas for what to try with this, I would be very appreciative!
If there is any further information I can give that would help anyone with helping me get this figured out, just let me know!
The official documentation should give you an idea on how to fix this.
Any reference to an undeclared variable causes a warning, unless the variable is explicitly mentioned in a /*global ...*/ comment, or specified in the globals key in the configuration file.
The easiest fix would be to add
/* global _ */
at the top of your file.
Or better, explicitly specify that the variable is read-only, to disallow overwriting the variable:
/* global _:readonly */
But since you'll have to do that for each new js file, it can get annoying. If you are using underscore often, I'd suggest to add globals to your .eslintrc file, for example:
{
"globals": {
"_": "readonly"
}
}
And save this as .eslintrc in your project root, or optionally in your user home directory. Although some say the latter not recommended, it can sometimes be convenient, but you have to remember that you have it there :)
Explanation of the above rule: "_": "readonly" (used to be "_": false, now deprecated) means that a variable named _ tells eslint that this variable is defined globally and it will not emit any no-undef errors for this variable. As #sebastian pointed out, "readonly" (or false - deprecated) means that the variable can't be overwritten, so the code _ = 'something else' would yield an error no-global-assign. If you were to instead use "_": "writable" (or "_": true - deprecated), this means that the value can be re-assigned and the previously mentioned error will not occur.
But keep in mind that this will only happen if you assign directly to the global variable as I have shown in the example. You can still shadow it and eslint won't say anything. For example, these snippets wouldn't yield the no-global-assign:
const _ = 'haha I broke your _'
or as function argument name, e.g.
function (_) {
console.log(_, 'might not be the _ you were looking for')
}
If you are using jest for testing - in your environment - in eslintrc.json
"env":{
"jest":true
}
My .jscsrc file looks like below
{
"preset": "wikimedia",
"requireSpacesInsideArrayBrackets": null,
"validateIndentation": 4,
"disallowMultipleVarDecl": true,
"disallowSpaceAfterObjectKeys": "ignoreMultiLine",
"disallowSpacesInsideParentheses": { "only": [ "{", "}" ] }
}
But on running jscs over my code, it throws the following error in console
Missing space after opening round bracket at js/app.js :
29 | windowScrollTimeout = null;
30 | if (currentTopOffet < prevTopOffset) {
31 | $('header').removeClass('mobile-hide');
----------------------------------------------------------------^
I also tried setting the value of disallowSpacesInsideParentheses to true but still no change in results. Any idea what I am doing wrong? Or is it that I am trying to solve my problem using wrong rule? Can somebody point me to the right rule set?
Thanks
Checking the source for disallowSpacesInsideParentheses, that rule won't throw the error you're seeing. It seems that your rule is conflicting with the wikimedia presets rule:
"requireSpacesInsideParentheses": "all"
requireSpacesInsideParentheses seems to be the culprit, not any rule you've set, as it's the only rule which will throw that error. To overwrite the preset rule, according to the docs you'll need to set the rule to null in your .jscsrc file.
"requireSpacesInsideParentheses": "null"