EzDevInfo.com

eslint

A fully pluggable tool for identifying and reporting on patterns in JavaScript. ESLint - Pluggable JavaScript linter a pluggable and configurable linter tool for identifying and reporting on patterns in javascript. maintain your code quality with ease.

Running Eslint on Rhino

I need to run Eslint on Java. The only solution I thought about is to "browserify" the node-js module, and try to run it on Rhino. Can anyone suggest a better way?


Source: (StackOverflow)

ESlint: validate files by glob pattern

Can I split the ESLint rules by glob pattern?

I mean case like this:

{ 
  "*.server.js": { /* rules set for the server JS */ },
  "*.client.js": { /* rules set for the client JS */ },
  "*.special.js": { /* some very special JS */ },
  "*.js": { /* all other JS with its own rules not intersecting with others */ }
}

There is the jshint-groups wrapper for JSHint. Is there something like this for ESLint?


Source: (StackOverflow)

Advertisements

vim with syntastic and eslint: show warnings

I'm using Vim with the syntastic plugin and eslint.

When I save a JavaScript file, I can see errors come up just fine, but I can't get the warnings to show.

Here's what I have in my .vimrc:

let g:syntastic_javascript_checkers = ['eslint']

I installed eslint with:

npm install eslint -g

I'm running Linux Mint 17

How do I get warnings to appear?


Source: (StackOverflow)

Export unused function for linting

I have the following code in a Javascript file:

/* exported something */

function something() {}

The something function is not used in the file because it is called from a Html form submission.

When the linter runs, es-lint in my case, I receive a no-unused-vars message.

I expected that exported comment would fix this, but this is not the case.

Am I doing something wrong? Am I missing something?


Source: (StackOverflow)

ESLint: Disable all default rules

How to disable all default rules in ESLint? It shows on this page http://eslint.org/docs/user-guide/command-line-interface that you can use --reset but when I do so:

$ eslint --global globalVar --reset --rule 'no-undef: 2' main.js
Invalid option '--reset' - perhaps you meant '--quiet'?

Source: (StackOverflow)

How to disable ESLint react/prop-types rule in a fil?

I'm using React and Eslint with eslint-plugin-react. I want to disable the prop-types rule in one file.

var React = require('react'); 
var Model = require('./ComponentModel');

var Component = React.createClass({
/* eslint-disable react/prop-types */
    propTypes: Model.propTypes,
/* eslint-enable react/prop-types */
    render: function () {
        return (
            <div className="component">
                {this.props.title}
            </div>
        );
    }
});

Source: (StackOverflow)

Using npm as a task runner/build tool - having problems with some cli modules

I'm trying to use npm as a task runner/build tool after reading this article:

How to use npm as a build tool

and while I'm having some success, I'm stuck on one thing. When running a command-line global tool like JSLINT, JSHINT, or ESLINT, npm will always show the Exit 1 code in the console window:

enter image description here

As you can see, the command works fine, but npm sees it as an error and displays the error log info. Is this normal and/or is there a way to turn it off for specific commands?

Additional info: this is script block in my package.json config:

"scripts": { "start": "node ./src/server/index.js", "test": "", "lint": "eslint index.js" }

then from npm cli I type:

npm run lint

This will execute the script found in the package.json file with the label: 'lint'


Source: (StackOverflow)

Run eslint before watchify runs

I'm adding watchify to our build process but I want to put a precondition to watchify running, and that is that the file(s) that changed pass our linting step (which is using ESLint).

I was thinking of doing this:

function runBrowserify(watch){
  var babel = babelify.configure({
      optional: ['es7.objectRestSpread']
  });
  var b = browserify({
    entries: './app/main.js',
    debug: true,
    extensions: ['.jsx', '.js'],
    cache: {},
    packageCache: {},
    fullPaths: true
  })
  .transform(babel);

  if(watch) {
    // if watch is enable, wrap this bundle inside watchify
    b = watchify(b);
    b.on('update', function(ids) {
      //run the linting step
      lint(ids);

      //run the watchify bundle step
      gutil.log(gutil.colors.blue('watchify'), 'Started');
      bundleShare(b);
    });
    b.on('time', function (time) {
      gutil.log(gutil.colors.blue('watchify'), 'Finished', 'after', gutil.colors.magenta(time), gutil.colors.magenta('ms'));
    });
  }

  bundleShare(b);
}

function bundleShare(b) {
  b.bundle()
    .pipe(source('main.min.js'))
    .pipe(gulp.dest('./dist'));
}

function lint(glob) {
  return gulp.src(glob)
    .pipe(eslint())
    .pipe(eslint.format())
    .pipe(eslint.failOnError());
}

The problem is that the linting step is async so it doesn't finish before the bundling would be done (it also throws so I probably need to use plumber to stop it from terminating the watch step).

So how would I make a precondition before I call bundleShared?


Source: (StackOverflow)

Enforce deferring controller logic

We are trying to apply the guidelines listed in John Papa's AngularJS Style Guide.

One of the rules which we started to follow is Defer Controller Logic:

Defer logic in a controller by delegating to services and factories.

Why?: Logic may be reused by multiple controllers when placed within a service and exposed via a function.

Why?: Logic in a service can more easily be isolated in a unit test, while the calling logic in the controller can be easily mocked.

Why?: Removes dependencies and hides implementation details from the controller.

This is something we've violated in the past by putting data retrieval logic into controllers instead of isolating it in a service.

Now I'd like to make the rule as strict as possible. Ideally, I would like angular to throw an error if one of the configured services is passed as a dependency to a controller. Is it something that can be solved on the angular level, or I should try solving it separately - for example, statically with a custom ESlint rule?

Would appreciate any insights or hints.


In particular, the following controller violates the rule, because it uses $http service directly:

function OrderController($http, $q, config, userInfo) {
    var vm = this;
    vm.checkCredit = checkCredit;
    vm.isCreditOk;

    function checkCredit() {
        var settings = {};

        return $http.get(settings)
            .then(function(data) {
               vm.isCreditOk = vm.total <= maxRemainingAmount;
            })
            .catch(function(error) {

            });
    };
}

Also, let me know if I'm getting overly concerned/crazy about the code quality :)


Source: (StackOverflow)

How to tell eslint that you prefer single quotes around your strings

I'm new to eslint and it's spewing out a ton of errors telling me to use doublequotes:

error  Strings must use doublequote

That's not my preference. I've got an .eslintrc file set up with the basics:

{
  "env": {
    "node": 1
  }
}

I'd like to configure it for single quotes.


Source: (StackOverflow)

How to to run eslint on *.js.erb files?

I'd like to lint the files in my rails project (ideally, in my editor while making edits) via eslint, but I am currently unable to lint files that are pre-processed with ERB.

How can I include *.js.erb files in the linting process?


Source: (StackOverflow)

How to config ESLint for React on Atom Editor

In Atom Editor I installed the following plugins

  1. linter
  2. linter-eslint

It seems they don't recognize the JSX syntaxis.

I have it working on the command line but had to use other plugins like esprima-fb and eslint-plugin-react. Looks like there are no such plugins for Atom Editor and would like to know if anyone of you knows a way to hack around this.


Source: (StackOverflow)

How can I make Syntastic load a different checker based on existance of files in root?

At work we use a different syntax checker than I do when working on open source. Is there a way to have Syntastic specify a default checker, and change checkers if an rc file is found at the project root?

Example: if .eslintrc is found use eslint. If no .eslintrc is found, use standard.

Thanks!

edit: also opened an issue on scrooloose/syntastic.


Source: (StackOverflow)

Alternative to nested ternary operator in JS

I personally love ternary operators, and in my humble opinion, they make complicated expressions very easy to digest. Take this one:

  word = (res.distance === 0) ? 'a'
    : (res.distance === 1 && res.difference > 3) ? 'b'
    : (res.distance === 2 && res.difference > 5 && String(res.key).length > 5) ? 'c'
    : 'd';

However in our project's ESLINT rules nested ternary operators are forbidden, so I have to get rid of the above.

I'm trying to find out alternatives to this approach. I really don't want to turn it into a huge if / else statement, but don't know if there's any other options.


Source: (StackOverflow)

How to get local eslint plugins to work with locally installed eslint

According to the docs plugins should work if they are npm modules named "eslint-plugin-"

Here's a plugin that follows that pattern. Source is here.

So, we make a new project

md foo
cd foo
npm init
... answer questions ..
npm install --save-dev eslint
npm install --save-dev eslint-plugin-require
echo "define(function(){});" > test.js
echo "{\"rules\":{\"require\": 2}}" > conf.json
node node_modules/eslint/bin/eslint.js -c conf.json --plugin eslint-plugin-require test.js

produces

~/node_modules/eslint/lib/eslint.js:569
                throw new Error("Definition for rule '" + key + "' was not
                      ^
Error: Definition for rule 'require' was not found. 

change the config to

echo "{\"rules\":{\"eslint-plugin-require\": 2}}" > conf.json

nor

echo "{\"rules\":{\"require-define\": 2}}" > conf.json

nor

echo "{\"rules\":{\"require-require-define\": 2}}" > conf.json

nor

echo "{\"rules\":{\"eslint-plugin-require-define\": 2}}" > conf.json

nor

echo "{\"rules\":{\"eslint-plugin-require-require-define\": 2}}" > conf.json

does not fix it

How do I use locally installed eslint plugins?


Source: (StackOverflow)