ESlint
Set up ESlint and try to make it pass linting
Used 118 times
T
TobiasBales
Usage
Run this command in your Rails app directory in the terminal:
rails app:template LOCATION="https://railsbytes.com/script/V2Gsqd"
Template Source
Review the code before running this template on your machine.
if File.exists? ".eslintrc.js"
puts ".eslintrc.js already exists, aborting eslint setup"
return
end
def yarn(*packages)
run("yarn add --dev #{packages.join(" ")}")
end
def file_contains(file, string)
File.foreach(file).detect { |line| line.include?(string) }
end
yarn '[email protected]', 'eslint-config-airbnb-base', 'eslint-plugin-import', 'babel-eslint'
create_file '.eslintrc.js' do <<~EOF
module.exports = {
parser: 'babel-eslint',
env: {
browser: true,
es2020: true,
},
extends: [
'airbnb-base',
],
parserOptions: {
ecmaVersion: 11,
sourceType: 'module',
},
rules: {
},
};
EOF
end
inject_into_file 'babel.config.js', before: "module.exports" do <<~EOF
/* eslint-disable func-names */
EOF
end
inject_into_file 'postcss.config.js', before: "module.exports" do <<~EOF
/* eslint-disable global-require */
/* eslint-disable import/no-extraneous-dependencies */
EOF
end
gsub_file "app/javascript/channels/consumer.js", "using the `rails generate channel` command." do |match|
"\n// #{match}"
end
if file_contains "app/javascript/packs/application.js", 'require("channels")'
inject_into_file "app/javascript/packs/application.js", before: 'require("channels")' do <<~EOF
// eslint-disable-next-line import/no-unresolved
EOF
end
end
if file_contains "app/javascript/packs/application.js", 'import "controllers"'
gsub_file "app/javascript/packs/application.js", 'import "controllers"' do |match|
<<~EOF
// eslint-disable-next-line import/no-unresolved
#{match}
EOF
end
end
gsub_file 'app/javascript/packs/application.js', 'bootstrap/dist/css/bootstrap', 'bootstrap/dist/css/bootstrap.css'
system('npx eslint --fix app')