Code Monkey home page Code Monkey logo

rubocop-sketchup's Introduction

RuboCop SketchUp

Gem Version Build status Documentation Status

Code analysis for SketchUp extensions using the SketchUp Ruby API.

Tool intended to assist with development of SketchUp extension by providing static analysis for common issues.

Installation

TL;DR (Global Installation)

gem install rubocop
gem install rubocop-sketchup

The Long Version

For more information about the installation process and how to manage your gem dependencies locally for a project refer to the dedicated section in the manual:

Usage

TL;DR

Add .rubocop.yml in the root of your project:

require: rubocop-sketchup

AllCops:
  DisabledByDefault: true
  DisplayStyleGuide: true
  SketchUp:
    SourcePath: src # Path to extension sources in project directory.
    EncryptedExtension: false # Enable if you plan to encrypt your extension.
    TargetSketchUpVersion: 2016 M1

SketchupDeprecations:
  Enabled: true

SketchupPerformance:
  Enabled: true

SketchupRequirements:
  Enabled: true

SketchupSuggestions:
  Enabled: true

SketchupBugs:
  Enabled: true

The Long Version

Formatters

TL;DR

JSON

rubocop --format json --out results.json

Extension Review

rubocop -f extension_review -o report.html

The Long Version

The Cops

All cops are located under lib/rubocop/sketchup, and contain examples/documentation.

Manual: Cops

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

rubocop-sketchup is MIT licensed.

rubocop-sketchup's People

Contributors

eneroth3 avatar koic avatar macumber avatar thomthom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rubocop-sketchup's Issues

Reformat tabs in output as spaces?

This is more of a RuboCop improvement, consider offering PR upstream.

The issue is that some files analysed use tabs - which then makes the error markers not properly line up. (Using the default reporter to console.)

Unclear instructions...

I don't understand what this sentence is trying to say:

Running only cops for Extension Warehouse Technical requirements

If you want to focus on only the technical requirements for having your extension hosted on Extension Warehouse then you can limit the cop to only the SketchupRequirements department. This is the most important department which you should ignore.

Cop: require + sketchup.rb, extensions.rb, langhandler.rb

Due to how require and Sketchup.require checks whether a file has been loaded, the following files should always be required in the exact given manner:

require 'sketchup.rb'
require 'extensions.rb'
require 'langhandler.rb'

Any other variants, such as omitting the file extension or capitalization might cause the file to double load.

SketchupRequirements/SketchupExtension fails with error

rubocop/rubocop#5656 (comment)

I keep running into errors with node matcher.

The latest case is a snippet like this:

module Example
  msg = "Hello World"
  msg += "Foo Bar"
end

A matcher like this:

        def_node_search :sketchup_extension_new, <<-PATTERN
          ({:lvasgn :ivasgn :cvasgn :gvasgn :casgn} ...
            (:send
              (:const nil? :SketchupExtension) :new
              _
              _))
        PATTERN

Inspection like this:

def investigate(processed_source)
  source_node = processed_source.ast
  extension_nodes = sketchup_extension_new(source_node).to_a
  # ...
end

Which leads to error like this:

  1) RuboCop::Cop::SketchupRequirements::SketchupExtension Default source path does not throw an error when inspecting source
     Failure/Error: extension_nodes = sketchup_extension_new(source_node).to_a

     NoMethodError:
       undefined method `type' for :msg:Symbol
     # C:3:in `block in sketchup_extension_new'
     # C:2:in `sketchup_extension_new'
     # ./lib/rubocop/sketchup/requirements/sketchup_extension.rb:44:in `each'
     # ./lib/rubocop/sketchup/requirements/sketchup_extension.rb:44:in `to_a'
     # ./lib/rubocop/sketchup/requirements/sketchup_extension.rb:44:in `investigate'
     # ./spec/rubocop/sketchup/requirements/sketchup_extension_spec.rb:93:in `block (3 levels) in <top (required)>'

My spec to reproduce:

    it 'does not throw an error when inspecting source' do
      inspect_source(['module Example',
                      '  msg = "Hello World"',
                      '  msg += "Foo Bar"',
                      'end'],
                      './src/hello.rb')
      expect(cop.offenses.size).to eq(1)
    end

Any ideas? (I get lost when I try to debug into the matchers.)

Run in SU?

ReadMe states:
'This is not intended to be used inside of SketchUp'

Do you think it would be helpful if it could? Just wondering...

Cop: Don't hard code file extensions in `Sketchup.require`

SketchUp will automatically resolve a file's extension to .rb, .rbs or .rbe.

Omitting file extensions is required when encrypting extensions. By always omitting the extension one avoid accidentally running into this issue.

Except 'sketchup.rb' - the load guard is case sensitive, if required with
different capitalization the file will load multiple times and add multiple
menus.

Cop: SketchUp compatibility

We can use YARD to provide lists of new features in each SketchUp version.
Developers can then declare minimum compatibilty and have RuboCop validate
against it. Similar to how RuboCop already can check Ruby compatibilty.

yard --use-cache --list --query "object.tag(:version).text =~ /2018/"

yard --use-cache --list --query "object.tag(:version).text == 'SketchUp 6'"
yard --use-cache --list --query "object.tag(:version).text == 'SketchUp 7'"
yard --use-cache --list --query "object.tag(:version).text == 'SketchUp 8'"
yardoc -t versions -f text

LayOut 2018
LayOut 2019
SketchUp 2013
SketchUp 2014
SketchUp 2015
SketchUp 2016
SketchUp 2016 M1
SketchUp 2017
SketchUp 2018
SketchUp 2019
SketchUp 6.0
SketchUp 7.0
SketchUp 7.0 M1
SketchUp 7.1
SketchUp 7.1 M1
SketchUp 8.0
SketchUp 8.0 M1
SketchUp 8.0 M2

SketchUp Global Variables

Identify global variables defined by the SketchUp API and add them to the list of variables to ignore in GlobalVariables.

tt_cleanup/vendor/error-handler/error_reporter.rb:225:27: E: SketchupRequirements/GlobalVariables: Do not introduce global variables.
      $LOADED_FEATURES || $loaded_files
                          ^^^^^^^^^^^^^

4 files inspected, 1 offense detected

Make OperationName smarter

Only transform first letter. Leave the rest alone.

This will allow strings like: "HTML" or "SketchUp" from being flagged.

Look into default config yml

See how rubocop-cask have done it. They appear to have a default yml configured with cop descriptions etc. Though they had to monkey patch rubocop by the looks of it.

Checking for methods defined in shipped extensions

It could be helpful to developers to get a warning/error if their extensions rely on methods defined in shipped extensions. I have myself accidentally used some of these methods without realizing they weren't a part of the API and later got errors when running the extension on a computer where these extensions were disabled.

Cop: Detect model change in observers

This cop cannot catch everything, but obvious calls to model changing methods can be checked that they are wrapped in transparent operations.

yard --use-cache --list --query "object.path =~ /^Sketchup::Entities#add_/"

Fill out default.yml

Look if build tool from rubocop-rspec can be re-purposed to automatically generate.

Add more deprecation cops

Use YARD to identify deprecated features. (Might be worth doing a plain text search as well.)
Add a cop for each item.

 yard --use-cache --list --query 'object.has_tag?(:deprecated)'

This also works:

 yard --use-cache --list --query '@deprecated'

Cop: Look for securerandom and openssl

There are performance issue with the OpenSSL library that Ruby ship. In a clean SU session, default model there is a small delay observed in the Windows version of SU.

But with a larger model loaded, or session that have had larger files loaded the lag will be minutes.

SecureRandom is affected by this, as it uses OpenSSL to seed.

This issue seem to have been addressed in OpenSSL 1.1 which was updated in Ruby 2.4.

openssl/openssl#1079

Custom reporter

Create a reporter that compose an easy to digest overview of rubocop-sketchup violations.
HTML format.

Cop: LanguageHandler variables

It's often seen that extensions use the global variable for their own LanguageHandler. We should catch and warn about that.

Ignore reading from DC globals?

Since there are so many extensions tapping into DCs it might be useful to reduce noise by omitting it from the SketchupRequirement department checking for global usage. At least reading, but trying to assign anything to it should still flag an offense.

There is still a cop that warns about using DC internals (not part of Requirement)

Encoding issues might prevent Rubocop parsing

Not sure if this is a bug - but need to figure out a way to catch and handle this scenario when using the gem for EW automated moderation.

rubocop/rubocop#1289

Analysing lss_archlite_1_0_5 (1800/3772) ...
invalid codepoint 0xD83D in UTF-8
C:/Ruby23/lib/ruby/gems/2.3.0/gems/parser-2.4.0.0/lib/parser/lexer.rb:16909:in `chr'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/parser-2.4.0.0/lib/parser/lexer.rb:16909:in `advance'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/parser-2.4.0.0/lib/parser/base.rb:231:in `next_token'
C:/Ruby23/lib/ruby/2.3.0/racc/parser.rb:259:in `_racc_do_parse_c'
C:/Ruby23/lib/ruby/2.3.0/racc/parser.rb:259:in `do_parse'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/parser-2.4.0.0/lib/parser/base.rb:162:in `parse'
C:/Ruby23/lib/ruby/gems/2.3.0/gems/parser-2.4.0.0/lib/parser/base.rb:209:in `tokenize'

Cop: Global Include

Detect use of include in global namespace.

require 'sketchup.rb'
include Math
module Example
  # ...
end

Cop: Don't use exit to break from methods

Some extensions tries to use exit to break from methods. This is actually a call to terminate the interpreter. Currently we catch and prevent this, but not with exit!.

We should have a cop that detect usage of both exit and exit!.

Deprecation warnings with Ruby 2.4

when running bundle exec rake:

C:/Users/tthomas2/SourceTree/rubocop-sketchup/spec/rubocop/sketchup/requirements/ruby_core_namespace_spec.rb:18: warning: constant ::Bignum is deprecated
C:/Users/tthomas2/SourceTree/rubocop-sketchup/spec/rubocop/sketchup/requirements/ruby_core_namespace_spec.rb:18: warning: constant ::Fixnum is deprecated

Can be ignored for now - need to check for Fixnum and Bignum due to older Ruby versions used in SketchUp.

But in future Ruby versions this might break - which could cause problems when running RuboCop using a newer Ruby version.

Dynamic Component global variables

DC defines a set of globals which several extensions are tapping into for various reasons. Some are to extend features, some are to work around bugs.

The policy on doing is is that it's entirely unsupported and on their own risk. When encountered we will review on a case by case basis.

There should be cops that check for usage of DC global variables and warn about using them.

Might want to add a cop to check for modifications of DC variables - if they are added to the GlobalVariable Cop's exclude list. There have been observations of people using the DC variables for their own extensions. (Copy+Paste)

Cop: Root rb file should not load extension files.

See if we can craft a cop that checks the root rb file for includes that relate to the extension subfolder.

An extension should not load logic prior to being loaded by the SketchupExtension handler - this to ensure the user can disable the extension.

SketchupSuggestions/SketchupRequire and Tools folder Ruby files

Sketchup::require 'sketchup.rb'

SketchupSuggestions/SketchupRequire: Don't hard code file extensions with Sketchup.require

Either recommend require is used to load the files from the Tools folder, or ignore these require for the SketchupSuggestions/SketchupRequire cop.

Cop: Avoid `Sketchup.find_support_file`

Some users might manage extensions manually or via third party extension
managers. In these cases using Sketchup.find_support_file will fail to
resolve the path to the files. Prefer using __FILE__ or __dir__ to resolve
files relative to the current file.

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.