From 6154a69c1546041eb63f96e05d2c571a0af0fe19 Mon Sep 17 00:00:00 2001 From: Andres Rodriguez Date: Thu, 13 Jul 2023 17:18:10 -0400 Subject: [PATCH] Add basic ruby linter (#15168) Adds rubocop as a linter for ruby.: - Adds .rubocop.yml which defines specific config on top of default - Adds Gradle task 'lint' task - Adds 'lint:report' rake task to report Lint Cops. - Adds 'lint:format' rake task to automatically format the code. --- .rubocop.yml | 220 ++++++++++++++++++++++++++++++++++++++++++++++ Gemfile.template | 1 + Rakefile | 2 + build.gradle | 8 ++ rakelib/lint.rake | 38 ++++++++ 5 files changed, 269 insertions(+) create mode 100644 .rubocop.yml create mode 100644 rakelib/lint.rake diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 00000000000..d6ed3a22d16 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,220 @@ +AllCops: + RubyInterpreters: + - ruby + - rake + - jruby + Include: + - '**/*.rb' + - '**/*.gemfile' + - '**/*.gemspec' + - '**/*.rake' + - '**/Gemfile' + - '**/Rakefile' + Exclude: + - 'node_modules/**/*' + - 'tmp/**/*' + - 'vendor/**/*' + - '.git/**/*' + - 'build/**/*' + - 'config/**/*' + DisplayCopNames: true + UseCache: false +#################### Layout ########################### +Layout/AccessModifierIndentation: + Enabled: false +Layout/ArgumentAlignment: + Enabled: false +Layout/ArrayAlignment: + Enabled: false +Layout/AssignmentIndentation: + Enabled: false +Layout/BeginEndAlignment: + Enabled: false +Layout/BlockAlignment: + Enabled: false +Layout/BlockEndNewline: + Enabled: false +Layout/CaseIndentation: + Enabled: false +Layout/ClassStructure: + Enabled: false +Layout/ClosingHeredocIndentation: + Enabled: false +Layout/ClosingParenthesisIndentation: + Enabled: false +Layout/CommentIndentation: + Enabled: false +Layout/ConditionPosition: + Enabled: false +Layout/DefEndAlignment: + Enabled: false +Layout/DotPosition: + Enabled: false +Layout/ElseAlignment: + Enabled: false +Layout/EmptyComment: + Enabled: false +Layout/EmptyLineAfterGuardClause: + Enabled: false +Layout/EmptyLineAfterMagicComment: + Enabled: false +Layout/EmptyLineAfterMultilineCondition: + Enabled: false +Layout/EmptyLineBetweenDefs: + Enabled: false +Layout/EmptyLines: + Enabled: false +Layout/EmptyLinesAroundAccessModifier: + Enabled: false +Layout/EmptyLinesAroundArguments: + Enabled: false +Layout/EmptyLinesAroundAttributeAccessor: + Enabled: false +Layout/EmptyLinesAroundBeginBody: + Enabled: false +Layout/EmptyLinesAroundBlockBody: + Enabled: false +Layout/EmptyLinesAroundClassBody: + Enabled: false +Layout/EmptyLinesAroundExceptionHandlingKeywords: + Enabled: false +Layout/EmptyLinesAroundMethodBody: + Enabled: false +Layout/EmptyLinesAroundModuleBody: + Enabled: false +Layout/EndAlignment: + Enabled: false +Layout/EndOfLine: + Enabled: false +Layout/ExtraSpacing: + Enabled: false +Layout/FirstArgumentIndentation: + Enabled: false +Layout/FirstArrayElementIndentation: + Enabled: false +Layout/FirstArrayElementLineBreak: + Enabled: false +Layout/FirstHashElementIndentation: + Enabled: false +Layout/FirstHashElementLineBreak: + Enabled: false +Layout/FirstMethodArgumentLineBreak: + Enabled: false +Layout/FirstMethodParameterLineBreak: + Enabled: false +Layout/FirstParameterIndentation: + Enabled: false +Layout/HashAlignment: + Enabled: false +Layout/HeredocArgumentClosingParenthesis: + Enabled: false +Layout/HeredocIndentation: + Enabled: false +Layout/IndentationConsistency: + Enabled: false +Layout/IndentationStyle: + Enabled: false +Layout/IndentationWidth: + Enabled: false +Layout/InitialIndentation: + Enabled: false +Layout/LeadingCommentSpace: + Enabled: false +Layout/LeadingEmptyLines: + Enabled: false +Layout/LineContinuationLeadingSpace: + Enabled: false +Layout/LineContinuationSpacing: + Enabled: false +Layout/LineEndStringConcatenationIndentation: + Enabled: false +Layout/LineLength: + Enabled: false +Layout/MultilineArrayBraceLayout: + Enabled: false +Layout/MultilineArrayLineBreaks: + Enabled: false +Layout/MultilineAssignmentLayout: + Enabled: false +Layout/MultilineBlockLayout: + Enabled: false +Layout/MultilineHashBraceLayout: + Enabled: false +Layout/MultilineHashKeyLineBreaks: + Enabled: false +Layout/MultilineMethodArgumentLineBreaks: + Enabled: false +Layout/MultilineMethodCallBraceLayout: + Enabled: false +Layout/MultilineMethodCallIndentation: + Enabled: false +Layout/MultilineMethodDefinitionBraceLayout: + Enabled: false +Layout/MultilineMethodParameterLineBreaks: + Enabled: false +Layout/MultilineOperationIndentation: + Enabled: false +Layout/ParameterAlignment: + Enabled: false +Layout/RedundantLineBreak: + Enabled: false +Layout/RescueEnsureAlignment: + Enabled: false +Layout/SingleLineBlockChain: + Enabled: false +Layout/SpaceAfterColon: + Enabled: false +Layout/SpaceAfterComma: + Enabled: false +Layout/SpaceAfterMethodName: + Enabled: false +Layout/SpaceAfterNot: + Enabled: false +Layout/SpaceAfterSemicolon: + Enabled: false +Layout/SpaceAroundBlockParameters: + Enabled: false +Layout/SpaceAroundEqualsInParameterDefault: + Enabled: false +Layout/SpaceAroundKeyword: + Enabled: false +Layout/SpaceAroundMethodCallOperator: + Enabled: false +Layout/SpaceAroundOperators: + Enabled: false +Layout/SpaceBeforeBlockBraces: + Enabled: false +Layout/SpaceBeforeBrackets: + Enabled: false +Layout/SpaceBeforeComma: + Enabled: false +Layout/SpaceBeforeComment: + Enabled: false +Layout/SpaceBeforeFirstArg: + Enabled: false +Layout/SpaceBeforeSemicolon: + Enabled: false +Layout/SpaceInLambdaLiteral: + Enabled: false +Layout/SpaceInsideArrayLiteralBrackets: + Enabled: false +Layout/SpaceInsideArrayPercentLiteral: + Enabled: false +Layout/SpaceInsideBlockBraces: + Enabled: false +Layout/SpaceInsideHashLiteralBraces: + Enabled: false +Layout/SpaceInsideParens: + Enabled: false +Layout/SpaceInsidePercentLiteralDelimiters: + Enabled: false +Layout/SpaceInsideRangeLiteral: + Enabled: false +Layout/SpaceInsideReferenceBrackets: + Enabled: false +Layout/SpaceInsideStringInterpolation: + Enabled: false +Layout/TrailingEmptyLines: + Enabled: false +Layout/TrailingWhitespace: + Enabled: false diff --git a/Gemfile.template b/Gemfile.template index 31db3131274..81b40c1181b 100644 --- a/Gemfile.template +++ b/Gemfile.template @@ -20,6 +20,7 @@ gem "octokit", "~> 4.25", :group => :build gem "rubyzip", "~> 1", :group => :build gem "stud", "~> 0.0.22", :group => :build +gem "rubocop", :group => :development gem "belzebuth", :group => :development gem "benchmark-ips", :group => :development gem "ci_reporter_rspec", "~> 1", :group => :development diff --git a/Rakefile b/Rakefile index c5001c17774..d6734b280e7 100644 --- a/Rakefile +++ b/Rakefile @@ -34,5 +34,7 @@ Developing? `rake test:install-core` installs any dependencies for testing Logstash core `rake test:core` to run Logstash core tests `rake vendor:clean` clean vendored dependencies used for Logstash development + `rake lint:report` to run the Rubocop linter + `rake lint:format` to automatically format the code HELP end diff --git a/build.gradle b/build.gradle index 7faff0849d2..6df2890e392 100644 --- a/build.gradle +++ b/build.gradle @@ -739,6 +739,14 @@ class JDKDetails { } } +tasks.register("lint") { + // Calls rake's 'lint' task + dependsOn installDevelopmentGems + doLast { + rake(projectDir, buildDir, 'lint:report') + } +} + tasks.register("downloadJdk", Download) { // CLI project properties: -Pjdk_bundle_os=[windows|linux|darwin] -Pjdk_arch=[arm64|x86_64] diff --git a/rakelib/lint.rake b/rakelib/lint.rake new file mode 100644 index 00000000000..0841d521778 --- /dev/null +++ b/rakelib/lint.rake @@ -0,0 +1,38 @@ +# Licensed to Elasticsearch B.V. under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Elasticsearch B.V. licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +namespace "lint" do + + module RuboCLI + def self.run!(*args) + require "rubocop" + cli = RuboCop::CLI.new + result = cli.run(["--force-exclusion", *args]) + raise "Linting failed." if result.nonzero? + end + end + + # task that runs lint report + task "report" do + RuboCLI.run!("--lint") + end + + # task that automatically fixes code formatting + task "format" do + RuboCLI.run!("--fix-layout") + end +end