Back to Standard

JavaScript Standard Style

docs/RULES-iteu.md

17.1.233.9 KB
Original Source

JavaScript Standard Style

<p align="center"> <a href="/docs/RULES-en.md">English</a> • <a href="/docs/RULES-esla.md">Español (Latinoamérica)</a> • <a href="/docs/RULES-fr.md">Français</a> • <a href="/docs/RULES-id.md">Bahasa Indonesia</a> • <a href="/docs/RULES-iteu.md">Italiano (Italian)</a> • <a href="/docs/RULES-ja.md">日本語 (Japanese)</a> • <a href="/docs/RULES-kokr.md">한국어 (Korean)</a> • <a href="/docs/RULES-ptbr.md">Português (Brasil)</a> • <a href="/docs/RULES-zhcn.md">简体中文 (Simplified Chinese)</a> • <a href="/docs/RULES-zhtw.md">繁體中文 (Taiwanese Mandarin)</a> </p>

Questo è un TL;DR (troppo lungo, non ho letto) delle regole JavaScript di standard.

Il modo migliore per imparare standard è quello di installarlo e provarlo sul tuo codice.

Regole

  • Usa due spazi per indentare.

    eslint: indent

    js
    function hello (name) {
      console.log('hi', name)
    }
    
  • Usa il singolo apostrofo per le stringhe tranne per evitare l'escaping.

    eslint: quotes

    js
    console.log('hello there')
    $("<div class='box'>")
    
  • Niente variabili inutilizzate.

    eslint: no-unused-vars

    js
    function myFunction () {
      var result = something()   // ✗ evita
    }
    
  • Aggiungi uno spazio dopo le parole chiave.

    eslint: keyword-spacing

    js
    if (condition) { ... }   // ✓ ok
    if(condition) { ... }    // ✗ evita
    
  • Aggiungi uno spazio prima delle parenti di una dichiarazione di una funzione.

    eslint: space-before-function-paren

    js
    function name (arg) { ... }   // ✓ ok
    function name(arg) { ... }    // ✗ evita
    
    run(function () { ... })      // ✓ ok
    run(function() { ... })       // ✗ evita
    
  • Usa sempree === invece di ==.

    Eccezione: obj == null è consentito per controllare null || undefined.

    eslint: eqeqeq

    js
    if (name === 'John')   // ✓ ok
    if (name == 'John')    // ✗ evita
    
    js
    if (name !== 'John')   // ✓ ok
    if (name != 'John')    // ✗ evita
    
  • Operatori infisso necessitano sempre di uno spazio.

    eslint: space-infix-ops

    js
    // ✓ ok
    var x = 2
    var message = 'hello, ' + name + '!'
    
    js
    // ✗ evita
    var x=2
    var message = 'hello, '+name+'!'
    
  • Le virgole devono avere uno spazio dopo di loro.

    eslint: comma-spacing

    js
    // ✓ ok
    var list = [1, 2, 3, 4]
    function greet (name, options) { ... }
    
    js
    // ✗ evita
    var list = [1,2,3,4]
    function greet (name,options) { ... }
    
  • Tieni gli else sulla stessa linea della parentesi graffa.

    eslint: brace-style

    js
    // ✓ ok
    if (condition) {
      // ...
    } else {
      // ...
    }
    
    js
    // ✗ evita
    if (condition) {
      // ...
    }
    else {
      // ...
    }
    
  • Per dichiarazioni if su più righe, utilizza le parentesi graffe.

    eslint: curly

    js
    // ✓ ok
    if (options.quiet !== true) console.log('done')
    
    js
    // ✓ ok
    if (options.quiet !== true) {
      console.log('done')
    }
    
    js
    // ✗ evita
    if (options.quiet !== true)
      console.log('done')
    
  • Gestisci sempre l'oggetto err come parametro di funzione.

    eslint: handle-callback-err

    js
    // ✓ ok
    run(function (err) {
      if (err) throw err
      window.alert('done')
    })
    
    js
    // ✗ evita
    run(function (err) {
      window.alert('done')
    })
    
  • Usa il prefisso per le variabili gloabli con window..

    Eccezioni sono: document, console e navigator.

    eslint: no-undef

    js
    window.alert('hi')   // ✓ ok
    
  • Non sono consentite più di una riga vuota.

    eslint: no-multiple-empty-lines

    js
    // ✓ ok
    var value = 'hello world'
    console.log(value)
    
    js
    // ✗ evita
    var value = 'hello world'
    
    
    console.log(value)
    
  • Per l'operatore ternario su più di una riga, piazza ? e : nelle righe che gli appartengono.

    eslint: operator-linebreak

    js
    // ✓ ok
    var location = env.development ? 'localhost' : 'www.api.com'
    
    // ✓ ok
    var location = env.development
      ? 'localhost'
      : 'www.api.com'
    
    // ✗ evita
    var location = env.development ?
      'localhost' :
      'www.api.com'
    
  • Per l'utilizzo del var, ogni assegnazione deve avere la sua definizione

    eslint: one-var

    js
    // ✓ ok
    var silent = true
    var verbose = true
    
    // ✗ evita
    var silent = true, verbose = true
    
    // ✗ evita
    var silent = true,
        verbose = true
    
  • Circonda gli assegnatori condizionali con parentesi aggiuntive. Questo assicura che ci sia l'espressione è intenzionalmente un assegnamento (=) invece di un errore di scrittura (===).

    eslint: no-cond-assign

    js
    // ✓ ok
    while ((m = text.match(expr))) {
      // ...
    }
    
    // ✗ evita
    while (m = text.match(expr)) {
      // ...
    }
    
  • Aggiungi degli spazie all'interno di blocchi su una singola riga.

    eslint: block-spacing

    js
      function foo () {return true}    // ✗ evita
      function foo () { return true }  // ✓ ok
    
  • Utilizza il camelcase per i nomi delle variabili e funzioni.

    eslint: camelcase

    js
      function my_function () { }    // ✗ evita
      function myFunction () { }     // ✓ ok
    
      var my_var = 'hello'           // ✗ evita
      var myVar = 'hello'            // ✓ ok
    
  • I trailing comma non sono ammessi.

    eslint: comma-dangle

    js
      var obj = {
        message: 'hello',   // ✗ evita
      }
    
  • The virgole devono essere messe alla fine della riga corrente

    eslint: comma-style

    js
      var obj = {
        foo: 'foo'
        ,bar: 'bar'   // ✗ avoid
      }
    
      var obj = {
        foo: 'foo',
        bar: 'bar'   // ✓ ok
      }
    
  • Il punto deve essere sulla stessa linea della proprietà.

    eslint: dot-location

    js
      console.
        log('hello')  // ✗ avoid
    
      console
        .log('hello') // ✓ ok
    
  • I file devono terminare con una nuova linea.

    eslint: eol-last

  • No spazi tra la definizione di una funzione e la sua invocazione

    eslint: func-call-spacing

    js
    console.log ('hello') // ✗ avoid
    console.log('hello')  // ✓ ok
    
  • Aggiungi uno spazio tra i due punti e il valore della chiave all'interno di un oggetto.

    eslint: key-spacing

    js
    var obj = { 'key' : 'value' }    // ✗ avoid
    var obj = { 'key' :'value' }     // ✗ avoid
    var obj = { 'key':'value' }      // ✗ avoid
    var obj = { 'key': 'value' }     // ✓ ok
    
  • I nomi dei costruttori devono iniziare con la lettera maiuscola

    eslint: new-cap

    js
    function animal () {}
    var dog = new animal()    // ✗ avoid
    
    function Animal () {}
    var dog = new Animal()    // ✓ ok
    
  • I costruttori senza argomenti devono essere invocati con le parentesi.

    eslint: new-parens

    js
    function Animal () {}
    var dog = new Animal    // ✗ avoid
    var dog = new Animal()  // ✓ ok
    
  • Gli oggetti devono un getter quando un settere viene definito.

    eslint: accessor-pairs

    js
    var person = {
      set name (value) {    // ✗ avoid
        this._name = value
      }
    }
    
    var person = {
      set name (value) {
        this._name = value
      },
      get name () {         // ✓ ok
        return this._name
      }
    }
    
  • I costruttori di classi derivate devono chiamare super.

    eslint: constructor-super

    js
    class Dog {
      constructor () {
        super()   // ✗ avoid
      }
    }
    
    class Dog extends Mammal {
      constructor () {
        super()   // ✓ ok
      }
    }
    
  • Usa gli array literals invece dei costruttori.

    eslint: no-array-constructor

    js
    var nums = new Array(1, 2, 3)   // ✗ avoid
    var nums = [1, 2, 3]            // ✓ ok
    
  • Evita l'utilizzo di arguments.callee e arguments.caller.

    eslint: no-caller

    js
    function foo (n) {
      if (n <= 0) return
    
      arguments.callee(n - 1)   // ✗ avoid
    }
    
    function foo (n) {
      if (n <= 0) return
    
      foo(n - 1)
    }
    
  • Non è possibile cambiare il nome delle classi.

    eslint: no-class-assign

    js
    class Dog {}
    Dog = 'Fido'    // ✗ avoid
    
  • Evita di modificare il varole di una variabile dichiarata usando const.

    eslint: no-const-assign

    js
    const score = 100
    score = 125       // ✗ avoid
    
  • Evita l'uso delle espressioni costanti all'interno delle condizioni (eccezione fatta per i cicli).

    eslint: no-constant-condition

    js
    if (false) {    // ✗ avoid
      // ...
    }
    
    if (x === 0) {  // ✓ ok
      // ...
    }
    
    while (true) {  // ✓ ok
      // ...
    }
    
  • No controllo dei caratteri nelle espressioni regolari.

    eslint: no-control-regex

    js
    var pattern = /\x1f/    // ✗ avoid
    var pattern = /\x20/    // ✓ ok
    
  • No debugger nel codice.

    eslint: no-debugger

    js
    function sum (a, b) {
      debugger      // ✗ avoid
      return a + b
    }
    
  • Non usare delete sulle variabili.

    eslint: no-delete-var

    js
    var name
    delete name     // ✗ avoid
    
  • No argomenti doppi all'interno della definizione di una funzione.

    eslint: no-dupe-args

    js
    function sum (a, b, a) {  // ✗ avoid
      // ...
    }
    
    function sum (a, b, c) {  // ✓ ok
      // ...
    }
    
  • No nomi doppi all'interno di una classe.

    eslint: no-dupe-class-members

    js
    class Dog {
      bark () {}
      bark () {}    // ✗ avoid
    }
    
  • Non è possibile dichiarare chiavi doppie degli oggetti literals.

    eslint: no-dupe-keys

    js
    var user = {
      name: 'Jane Doe',
      name: 'John Doe'    // ✗ avoid
    }
    
  • Non è possibile dichiarare due case che controllano lo stesso valore all'interno di uno switch

    eslint: no-duplicate-case

    js
    switch (id) {
      case 1:
        // ...
      case 1:     // ✗ avoid
    }
    
  • Utilizza un solo import per module.

    eslint: no-duplicate-imports

    js
    import { myFunc1 } from 'module'
    import { myFunc2 } from 'module'          // ✗ avoid
    
    import { myFunc1, myFunc2 } from 'module' // ✓ ok
    
  • No caratteri vuoti all'iterno delle espressioni regolari.

    eslint: no-empty-character-class

    js
    const myRegex = /^abc[]/      // ✗ avoid
    const myRegex = /^abc[a-z]/   // ✓ ok
    
  • Non è possibile usare il destructuring vuoto

    eslint: no-empty-pattern

    js
    const { a: {} } = foo         // ✗ avoid
    const { a: { b } } = foo      // ✓ ok
    
  • No all'uso di eval().

    eslint: no-eval

    js
    eval( "var result = user." + propName ) // ✗ avoid
    var result = user[propName]             // ✓ ok
    
  • No all reassegnamento delle eccezioni all'interno di blocchi catch.

    eslint: no-ex-assign

    js
    try {
      // ...
    } catch (e) {
      e = 'new value'             // ✗ avoid
    }
    
    try {
      // ...
    } catch (e) {
      const newVal = 'new value'  // ✓ ok
    }
    
  • Non è possibile estendere oggetti nativi.

    eslint: no-extend-native

    js
    Object.prototype.age = 21     // ✗ avoid
    
  • Evita il binding non necessari.

    eslint: no-extra-bind

    js
    const name = function () {
      getName()
    }.bind(user)    // ✗ avoid
    
    const name = function () {
      this.getName()
    }.bind(user)    // ✓ ok
    
  • Evita di effettuare il cast di booleani che non sono necessari.

    eslint: no-extra-boolean-cast

    js
    const result = true
    if (!!result) {   // ✗ avoid
      // ...
    }
    
    const result = true
    if (result) {     // ✓ ok
      // ...
    }
    
  • No parentesi non necessarie intorno alle funzioni.

    eslint: no-extra-parens

    js
    const myFunc = (function () { })   // ✗ avoid
    const myFunc = function () { }     // ✓ ok
    
  • Utilizza il break per evitare l'esecuzioni dei successivi case all'interno di un blocco switch.

    eslint: no-fallthrough

    js
    switch (filter) {
      case 1:
        doSomething()    // ✗ avoid
      case 2:
        doSomethingElse()
    }
    
    switch (filter) {
      case 1:
        doSomething()
        break           // ✓ ok
      case 2:
        doSomethingElse()
    }
    
    switch (filter) {
      case 1:
        doSomething()
        // fallthrough  // ✓ ok
      case 2:
        doSomethingElse()
    }
    
  • No ai decimali con la virgola mobile.

    eslint: no-floating-decimal

    js
    const discount = .5      // ✗ avoid
    const discount = 0.5     // ✓ ok
    
  • Evita la riassegnazione di una funzione dichiarata.

    eslint: no-func-assign

    js
    function myFunc () { }
    myFunc = myOtherFunc    // ✗ avoid
    
  • Evita la riassegnazione di oggetti che possono essere solo letti.

    eslint: no-global-assign

    js
    window = {}     // ✗ avoid
    
  • No agli eval() sottointesi.

    eslint: no-implied-eval

    js
    setTimeout("alert('Hello world')")                   // ✗ avoid
    setTimeout(function () { alert('Hello world') })     // ✓ ok
    
  • No alla dichiarazione di funzioni all'interno di blocchi innestati.

    eslint: no-inner-declarations

    js
    if (authenticated) {
      function setAuthUser () {}    // ✗ avoid
    }
    
  • No a stringhe di expressioni regolari invalide all'interno dei costruttori RegExp.

    eslint: no-invalid-regexp

    js
    RegExp('[a-z')    // ✗ avoid
    RegExp('[a-z]')   // ✓ ok
    
  • No a spazi irregolari.

    eslint: no-irregular-whitespace

    js
    function myFunc () /*<NBSP>*/{}   // ✗ avoid
    
  • No all'uso di __iterator__.

    eslint: no-iterator

    js
    Foo.prototype.__iterator__ = function () {}   // ✗ avoid
    
  • No a etichette che condividono lo stesso nome di una variabile all'interno dello stesso scope.

    eslint: no-label-var

    js
    var score = 100
    function game () {
      score: while (true) {      // ✗ avoid
        score -= 10
        if (score > 0) continue score
        break
      }
    }
    
  • No al blocco label.

    eslint: no-labels

    js
    label:
      while (true) {
        break label     // ✗ avoid
      }
    
  • No a blocchi non necessari.

    eslint: no-lone-blocks

    js
    function myFunc () {
      {                   // ✗ avoid
        myOtherFunc()
      }
    }
    
    function myFunc () {
      myOtherFunc()       // ✓ ok
    }
    
  • Evita di mixare spazi e tab per indentare.

    eslint: no-mixed-spaces-and-tabs

  • Non usare spazi multipli per indentare.

    eslint: no-multi-spaces

    js
    const id =    1234    // ✗ avoid
    const id = 1234       // ✓ ok
    
  • No a stringhe su più linee.

    eslint: no-multi-str

    js
    const message = 'Hello \
                     world'     // ✗ avoid
    
  • No all'uso del new senza l'assegnazione ad una variabile.

    eslint: no-new

    js
    new Character()                     // ✗ avoid
    const character = new Character()   // ✓ ok
    
  • No all'uso del costruttore Function.

    eslint: no-new-func

    js
    var sum = new Function('a', 'b', 'return a + b')    // ✗ avoid
    
  • No all'uso del costruttore Object.

    eslint: no-new-object

    js
    let config = new Object()   // ✗ avoid
    
  • No all'uso di new require.

    eslint: no-new-require

    js
    const myModule = new require('my-module')    // ✗ avoid
    
  • No all'uso del costruttore Symbol.

    eslint: no-new-symbol

    js
    const foo = new Symbol('foo')   // ✗ avoid
    
  • No all'uso di istanze di primitive.

    eslint: no-new-wrappers

    js
    const message = new String('hello')   // ✗ avoid
    
  • Non chiamare le proprietà di oggetti globali come funzioni.

    eslint: no-obj-calls

    js
    const math = Math()   // ✗ avoid
    
  • No agli ottali literals.

    eslint: no-octal

    js
    const octal = 042         // ✗ avoid
    const decimal = 34        // ✓ ok
    const octalString = '042' // ✓ ok
    
  • No all'escape di sequenze di ottali nelle stringhe letterali.

    eslint: no-octal-escape

    js
    const copyright = 'Copyright \251'  // ✗ avoid
    
  • Evita la concatenazione di strighe quando si usano __dirname e __filename.

    eslint: no-path-concat

    js
    const pathToFile = __dirname + '/app.js'            // ✗ avoid
    const pathToFile = path.join(__dirname, 'app.js')   // ✓ ok
    
  • Evita l'uso di __proto__. Usa invece getPrototypeOf.

    eslint: no-proto

    js
    const foo = obj.__proto__               // ✗ avoid
    const foo = Object.getPrototypeOf(obj)  // ✓ ok
    
  • No alla ridichiarazione di variabili.

    eslint: no-redeclare

    js
    let name = 'John'
    let name = 'Jane'     // ✗ avoid
    
    let name = 'John'
    name = 'Jane'         // ✓ ok
    
  • Evita spazi multipli all'interno di espressioni regolari literals.

    eslint: no-regex-spaces

    js
    const regexp = /test   value/   // ✗ avoid
    
    const regexp = /test {3}value/  // ✓ ok
    const regexp = /test value/     // ✓ ok
    
  • Gli assegnamenti nei blocchi return devono essere circondati da parentesi .

    eslint: no-return-assign

    js
    function sum (a, b) {
      return result = a + b     // ✗ avoid
    }
    
    function sum (a, b) {
      return (result = a + b)   // ✓ ok
    }
    
  • Evita di assegnare una variabile a se stessa

    eslint: no-self-assign

    js
    name = name   // ✗ avoid
    
  • Evita di confrontare una variabile con se stessa.

    eslint: no-self-compare

    js
    if (score === score) {}   // ✗ avoid
    
  • Evita l'uso della virgola come operatore.

    eslint: no-sequences

    js
    if (doSomething(), !!test) {}   // ✗ avoid
    
  • Nomi protetti non possono essere reassegnati (shadowing).

    eslint: no-shadow-restricted-names

    js
    let undefined = 'value'     // ✗ avoid
    
  • Array sparsi non sono consentiti.

    eslint: no-sparse-arrays

    js
    let fruits = ['apple',, 'orange']       // ✗ avoid
    
  • I tab non dovrebbero essere utilizzati

    eslint: no-tabs

  • Strighe regolari non devono contenere i placeholders propri dei template literal.

    eslint: no-template-curly-in-string

    js
    const message = 'Hello ${name}'   // ✗ avoid
    const message = `Hello ${name}`   // ✓ ok
    
  • super() deve essere chiamato prima di this.

    eslint: no-this-before-super

    js
    class Dog extends Animal {
      constructor () {
        this.legs = 4     // ✗ avoid
        super()
      }
    }
    
  • Puoi usare throw solo su un oggetto Error.

    eslint: no-throw-literal

    js
    throw 'error'               // ✗ avoid
    throw new Error('error')    // ✓ ok
    
  • Spazi bianchi banditi alla fine di una linea.

    eslint: no-trailing-spaces

  • Inizializzare con undefined non è consentito.

    eslint: no-undef-init

    js
    let name = undefined    // ✗ avoid
    
    let name
    name = 'value'          // ✓ ok
    
  • No alla non-modifica delle condizioni all'interno dei cicli.

    eslint: no-unmodified-loop-condition

    js
    for (let i = 0; i < items.length; j++) {...}    // ✗ avoid
    for (let i = 0; i < items.length; i++) {...}    // ✓ ok
    
  • No all'uso dell'operatore ternario quando esiste una alternativa migliore.

    eslint: no-unneeded-ternary

    js
    let score = val ? val : 0     // ✗ avoid
    let score = val || 0          // ✓ ok
    
  • No a codice non eseguibile dopo l'uso di return, throw, continue, e break.

    eslint: no-unreachable

    js
    function doSomething () {
      return true
      console.log('never called')     // ✗ avoid
    }
    
  • No a controlli di flusso all'interno di blocchi finally.

    eslint: no-unsafe-finally

    js
    try {
      // ...
    } catch (e) {
      // ...
    } finally {
      return 42     // ✗ avoid
    }
    
  • L'operando sinistro di una operazione relazionale non può essere negato.

    eslint: no-unsafe-negation

    js
    if (!key in obj) {}       // ✗ avoid
    
  • Evita l'uso non necessario di .call() e .apply().

    eslint: no-useless-call

    js
    sum.call(null, 1, 2, 3)   // ✗ avoid
    
  • Evita l'uso di una non necessario computed property all'interno di una chiave di un oggetto

    eslint: no-useless-computed-key

    js
    const user = { ['name']: 'John Doe' }   // ✗ avoid
    const user = { name: 'John Doe' }       // ✓ ok
    
  • No all'uso non necessario di costruttori.

    eslint: no-useless-constructor

    js
    class Car {
      constructor () {      // ✗ avoid
      }
    }
    
  • No all'uso non necessario degli escape.

    eslint: no-useless-escape

    js
    let message = 'Hell\o'  // ✗ avoid
    
  • Rinominare l'assegnamento di import, export, e destrutturazione (destructuring) usano lo stesso nome non è consentito.

    eslint: no-useless-rename

    js
    import { config as config } from './config'     // ✗ avoid
    import { config } from './config'               // ✓ ok
    
  • No spazi prima di una proprietà

    eslint: no-whitespace-before-property

    js
    user .name      // ✗ avoid
    user.name       // ✓ ok
    
  • No all'uso di with.

    eslint: no-with

    js
    with (val) {...}    // ✗ avoid
    
  • Mantere consistenza di nuove righe all'interno delle proprietà di un oggetto.

    eslint: object-property-newline

    js
    const user = {
      name: 'Jane Doe', age: 30,
      username: 'jdoe86'            // ✗ avoid
    }
    
    const user = { name: 'Jane Doe', age: 30, username: 'jdoe86' }    // ✓ ok
    
    const user = {
      name: 'Jane Doe',
      age: 30,
      username: 'jdoe86'
    }                                                                 // ✓ ok
    
  • No padding all'interno dei blocchi.

    eslint: padded-blocks

    js
    if (user) {
                                // ✗ avoid
      const name = getName()
    
    }
    
    if (user) {
      const name = getName()    // ✓ ok
    }
    
  • No spazi tra l'operatore spread e la loro espressione.

    eslint: rest-spread-spacing

    js
    fn(... args)    // ✗ avoid
    fn(...args)     // ✓ ok
    
  • Le virgole devono avere uno spazio dopo e non prima.

    eslint: semi-spacing

    js
    for (let i = 0 ;i < items.length ;i++) {...}    // ✗ avoid
    for (let i = 0; i < items.length; i++) {...}    // ✓ ok
    
  • Avere uno spazio prima dei blocchi.

    eslint: space-before-blocks

    js
    if (admin){...}     // ✗ avoid
    if (admin) {...}    // ✓ ok
    
  • No spazi all'interno delle parentesi

    eslint: space-in-parens

    js
    getName( name )     // ✗ avoid
    getName(name)       // ✓ ok
    
  • Uso dello spazio dopo l'uso di un operatore unario.

    eslint: space-unary-ops

    js
    typeof!admin        // ✗ avoid
    typeof !admin        // ✓ ok
    
  • Usa gli spazi dentro i commenti.

    eslint: spaced-comment

    js
    //comment           // ✗ avoid
    // comment          // ✓ ok
    
    /*comment*/         // ✗ avoid
    /* comment */       // ✓ ok
    
  • No spazi dentro le strighe template.

    eslint: template-curly-spacing

    js
    const message = `Hello, ${ name }`    // ✗ avoid
    const message = `Hello, ${name}`      // ✓ ok
    
  • Usa IsNaN() quando controlli NaN.

    eslint: use-isnan

    js
    if (price === NaN) { }      // ✗ avoid
    if (isNaN(price)) { }       // ✓ ok
    
  • typeof deve essere comparato con una stringa valida.

    eslint: valid-typeof

    js
    typeof name === 'undefimed'     // ✗ avoid
    typeof name === 'undefined'     // ✓ ok
    
  • Espressioni di funzioni immediatamente invocate (IIFEs - Immediately Invoked Function Expressions) devono essere circondate da parentesi.

    eslint: wrap-iife

    js
    const getName = function () { }()     // ✗ avoid
    
    const getName = (function () { }())   // ✓ ok
    const getName = (function () { })()   // ✓ ok
    
  • Mettere uno spazio prima e dopo * dentro le espressioni yield* .

    eslint: yield-star-spacing

    js
    yield* increment()    // ✗ avoid
    yield * increment()   // ✓ ok
    
  • Evitare condizioni Yoda.

    eslint: yoda

    js
    if (42 === age) { }    // ✗ avoid
    if (age === 42) { }    // ✓ ok
    

Semicolons

  • No punti e virgola (semicolons). (see: 1, 2, 3)

    eslint: semi

    js
    window.alert('hi')   // ✓ ok
    window.alert('hi');  // ✗ avoid
    
  • Mai iniziare una linea con (, [, o `. Questo è l'unico trucchetto che permette di omettere i punti e virgola. E standard ti protegge da potenziali errori.

    eslint: no-unexpected-multiline

    js
    // ✓ ok
    ;(function () {
      window.alert('ok')
    }())
    
    // ✗ avoid
    (function () {
      window.alert('ok')
    }())
    
    js
    // ✓ ok
    ;[1, 2, 3].forEach(bar)
    
    // ✗ avoid
    [1, 2, 3].forEach(bar)
    
    js
    // ✓ ok
    ;`hello`.indexOf('o')
    
    // ✗ avoid
    `hello`.indexOf('o')
    

    Nota: Se tendi a scrivere codice in questo modo, probabilmente stai cercando di essere troppo furbo.

    È scoraggiato l'uso di scorciatoie, in favore di espressioni chiare e leggibili quando possibile.

    Invece di:

    js
    ;[1, 2, 3].forEach(bar)
    

    È preferito questo approccio:

    js
    var nums = [1, 2, 3]
    nums.forEach(bar)
    

Letture utili

E un video utile:

Oggigiorno, tutti i più popolari minificatori di codice si basano su AST, quindi gestiscono il JavaScript senza punti e virgola (semicolons-less) senza problemi (visto che i punti e virgola non sono obbligatori in JavaScript).

[Relying on automatic semicolon insertion] is quite safe, and perfectly valid JS that every browser understands. Closure compiler, yuicompressor, packer, and jsmin all can properly minify it. There is no performance impact anywhere.

I am sorry that, instead of educating you, the leaders in this language community have given you lies and fear. That was shameful. I recommend learning how statements in JS are actually terminated (and in which cases they are not terminated), so that you can write code that you find beautiful.

In general, \n ends a statement unless:

  1. The statement has an unclosed paren, array literal, or object literal or ends in some other way that is not a valid way to end a statement. (For instance, ending with . or ,.)
  2. The line is -- or ++ (in which case it will decrement/increment the next token.)
  3. It is a for(), while(), do, if(), or else, and there is no {
  4. The next line starts with [, (, +, *, /, -, ,, ., or some other binary operator that can only be found between two tokens in a single expression.

The first is pretty obvious. Even JSLint is ok with \n chars in JSON and parenthesized constructs, and with var statements that span multiple lines ending in ,.

The second is super weird. I’ve never seen a case (outside of these sorts of conversations) where you’d want to do write i\n++\nj, but, point of fact, that’s parsed as i; ++j, not i++; j.

The third is well understood, if generally despised. if (x)\ny() is equivalent to if (x) { y() }. The construct doesn’t end until it reaches either a block, or a statement.

; is a valid JavaScript statement, so if(x); is equivalent to if(x){} or, “If x, do nothing.” This is more commonly applied to loops where the loop check also is the update function. Unusual, but not unheard of.

The fourth is generally the fud-inducing “oh noes, you need semicolons!” case. But, as it turns out, it’s quite easy to prefix those lines with semicolons if you don’t mean them to be continuations of the previous line. For example, instead of this:

js
foo();
[1,2,3].forEach(bar);

you could do this:

js
foo()
;[1,2,3].forEach(bar)

The advantage is that the prefixes are easier to notice, once you are accustomed to never seeing lines starting with ( or [ without semis.