Looking at Juniors <Code>!

let them fix it!
give them tools
perfect code write we
let variable = 'value'
if (variable = 'should not match') {
console.log('Oops! ๐ฉ')
}
console.log(variable)
Oops! ๐ฉ should not match
let variable = 'value'
if ('should not match' = variable) {
console.log('this will never happen! ๐')
}
if ('should not match' = variable) {
^^^^^^^^^^^^^^^^^^
SyntaxError: Invalid left-hand side in assignment
at internalCompileFunction (node:internal/vm:74:18)
at wrapSafe (node:internal/modules/cjs/loader:1128:20)
at Module._compile (node:internal/modules/cjs/loader:1169:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1259:10)
at Module.load (node:internal/modules/cjs/loader:1068:32)
at Module._load (node:internal/modules/cjs/loader:909:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:82:12)
at node:internal/main/run_main_module:23:47
Node.js v19.1.0
catch human error easily
version 1
var object = {
trailing : 'comma',
unchanged : 'line'
}
version 2
var object = {
trailing : 'comma',
clean : 'only insert this line',
unchanged : 'line',
dirty : 'add comma above',
}
Oops, I had to change it
diff -u version-1.js version-2.js
--- version-1.js 2022-11-30 09:06:25.640608070 +0545
+++ version-2.js 2022-11-30 09:06:25.650608070 +0545
@@ -1,4 +1,6 @@
var object = {
trailing : 'comma',
- unchanged : 'line'
+ clean : 'only insert this line',
+ unchanged : 'line',
+ dirty : 'add comma above',
}
trailing commas will give you
โจ cleaner diffs
your ๐ฅท ninja girl
every rules should be specified
module.exports = {
rules : {
'yoda' : ['error', 'always'],
'comma-dangle' : ['error', 'always-multiline'],
}
}
๐ก eslint to rescue
module.exports = {
extends : [
'eslint:recommended',
],
rules : {
'yoda' : ['error', 'always'],
'comma-dangle' : ['error', 'always-multiline'],
}
}
๐งป๐งป๐งป๐งป๐งป๐งป
WARNING: highly opinionated section
tell me when I go crazy! ๐ง
rules : {
'max-len' : [ 'error', {
code : 120, // its hard I know ๐ค
ignoreStrings: true,
ignoreTrailingComments: true,
}],
// stop it โ, its time to split into files
'max-lines' : [ 'error', {
max : 500,
skipBlankLines : true,
skipComments : true,
}],
}
๐ง readability matters !
rules : {
'indent' : [
'error',
4, // 2 it less, 4 is good, 8 is excessive
{ SwitchCase : 1 },
],
'max-depth' : [ 'error', 5 ],
'brace-style' : [
'error',
'1tbs', // one true brace style ๐
{ allowSingleLine: true },
],
}
minify can write short <code> than you!
where
rules : {
'no-unused-vars' : [ 'error', {
args : 'none', // for callback
}],
}
tree-shaking is for compiler!
linter will bring consistency in <code>
rest still lies on you
/* eslint no-fallthrough: error */
/* eslint no-console: off */
switch ('match') {
case 'match':
console.log('no break here ๐')
// break
case 'not a match':
console.log('should not be here')
}
eslint no-fallthrough.js
/path/to/file/no-fallthrough.js 7:5 error Expected a 'break' statement before 'case' no-fallthrough โ 1 problem (1 error, 0 warnings)
console.log('why this is in production')
// eslint-disable-next-line no-console
console.log('this is allowed')
console.log('trialing noqa') // eslint-disable-line no-console
eslint no-console.js
/path/to/file/no-console.js 1:1 error Unexpected console statement no-console 6:1 error Unexpected console statement no-console โ 2 problems (2 errors, 0 warnings)
๐ your have stuff
function foo {
return 'module is expected to be'
}
/* global boo */
function boo {
return 'setting global will stop pesky linter'
}
module.exports = {
env : { // just showing options
browser : true,
node : true,
commonjs : true,
es6 : true,
}
}
One who didn't try JAVASCRIPT because didn't like
JAVA, 10 years later I'm giving this talk!
Do you have Questions ?
| github/rhoit | gitlab/rhoit | twitter/rhoitman |