Giter Site home page Giter Site logo

doc-js-eslint-with-marcio-s-code's Introduction

Study notes - Passing a junk of code throught ESLint

The following is a piece of junk code I usually write. Then I end up using ESLint with the Airbnb style set of rules on it. And punished myself to fix all the inconsistencies.

Eslint result

My initial code


class _Promise {
  constructor(user_exec_function) {
    user_exec_function( (data) => { this.resolveMethod(data) } );
    this.endUserThenFunction = null;
  }

  resolveMethod(data) {
    console.log(this.endUserThenFunction);
    this.endUserThenFunction(data);
  }

  then(endPassedThenFunction) {
    this.endUserThenFunction = endPassedThenFunction;
  }
}

function myTest() {
  return new _Promise(
     callBackForCallingThen => {
        setTimeout( ()=> {
            callBackForCallingThen("data")
        },3000);
     }
  );
}

myTest().then(data => {
  console.log('Result is ' + data);
});

The initial eslint test output:


   1:16  error    Block must not be padded by blank lines                                              padded-blocks
   3:14  error    Unexpected space before function parentheses                                         space-before-function-paren
   3:16  error    Identifier 'user_exec_function' is not in camel case                                 camelcase
   4:23  error    There should be no spaces inside this paren                                          space-in-parens
   4:61  error    Missing semicolon                                                                    semi
   4:64  error    There should be no spaces inside this paren                                          space-in-parens
   9:5   warning  Unexpected console statement                                                         no-console
  20:1   error    Expected indentation of 4 spaces but found 5                                         indent
  20:6   error    Expected parentheses around arrow function argument having a body with curly braces  arrow-parens
  21:1   error    Expected indentation of 6 spaces but found 8                                         indent
  21:19  error    There should be no spaces inside this paren                                          space-in-parens
  21:22  error    Missing space before =>                                                              arrow-spacing
  22:1   error    Expected indentation of 8 spaces but found 12                                        indent
  22:36  error    Strings must use singlequote                                                         quotes
  22:43  error    Missing semicolon                                                                    semi
  23:1   error    Expected indentation of 6 spaces but found 8                                         indent
  23:10  error    A space is required after ','                                                        comma-spacing
  24:1   error    Expected indentation of 4 spaces but found 5                                         indent
  24:7   error    Missing trailing comma                                                               comma-dangle
  28:15  error    Expected parentheses around arrow function argument having a body with curly braces  arrow-parens
  29:3   warning  Unexpected console statement                                                         no-console
  29:15  error    Unexpected string concatenation                                                      prefer-template

✖ 22 problems (20 errors, 2 warnings)
  19 errors and 0 warnings potentially fixable with the `--fix` option.

Block must not be padded by blank lines

Unexpected space before function parentheses

Identifier 'user_exec_function' is not in camel case

There should be no spaces inside this paren

Missing semicolon

Expected indentation of 4 spaces but found 5

Expected parentheses around arrow function argument having a body with curly braces

space-in-parens again

There should be space before =>

Strings must use single quote

Adding semi colon

Space after comma

Fixing function to become condensed

  • Removing the necessity to the trailing comma but this is interesting one because, really, if a function could have more arguments, better is to keep a trailing cooma so that its better visualization in the git, always a new line

Parenthesis to the argument of the arrow function

template

Code after

class _Promise {
  constructor(userExecutor) {
    userExecutor((data) => { this.resolveMethod(data); });
    this.endUserThenFunction = null;
  }

  resolveMethod(data) {
    console.log(this.endUserThenFunction);
    this.endUserThenFunction(data);
  }

  then(endPassedThenFunction) {
    this.endUserThenFunction = endPassedThenFunction;
  }
}

function myTest() {
  return new _Promise((callBackForCallingThen) => {
    setTimeout(() => {
      callBackForCallingThen('data');
    }, 3000);
  });
}

myTest().then((data = 'default') => {
  console.log(`Result is ${data}`);
});

myPromise instead of _Promise

Also fixed _Promise since the Airbnb rule didn´t catch it.

class myPromise {
  constructor(userExecutor) {
    userExecutor((data) => { this.resolveMethod(data); });
    this.endUserThenFunction = null;
  }

  resolveMethod(data) {
    console.log(this.endUserThenFunction);
    this.endUserThenFunction(data);
  }

  then(endPassedThenFunction) {
    this.endUserThenFunction = endPassedThenFunction;
  }
}

function myTest() {
  return new myPromise((callBackForCallingThen) => {
    setTimeout(() => {
      callBackForCallingThen('data');
    }, 3000);
  });
}

myTest().then((data = 'default') => {
  console.log(`Result is ${data}`);
});

doc-js-eslint-with-marcio-s-code's People

Contributors

taboca avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.